实验6

任务1

源代码

  1 #include <stdio.h>
  2 #include <string.h>
  3 #define N 3  // 测试用3个学生,可自行修改
  4 
  5 // 定义学生结构体
  6 typedef struct student {
  7     int id;         // 学号
  8     char name[20];  // 姓名
  9     char subject[20];//考试科目
 10     double perf;    // 平时成绩
 11     double mid;     // 期中成绩
 12     double final;   // 期末成绩
 13     double total;   // 总评成绩
 14     char level[10];  // 成绩等级
 15 } STU;
 16 
 17 // 函数声明
 18 void input(STU s[], int n);    // 录入学生信息
 19 void output(STU s[], int n);   // 输出n个学生信息
 20 void calc(STU s[], int n);     // 计算总评和等级
 21 int fail(STU s[], STU t[], int n); // 统计不及格,返回人数
 22 void sort(STU s[], int n);     // 按总评降序排序
 23 
 24 int main() {
 25     STU st[N], fst[N]; // st存全部学生,fst存不及格学生
 26     int k;             // 不及格人数
 27 
 28     printf("录入学生成绩信息:\n");
 29     input(st, N);
 30 
 31     printf("\n成绩处理...\n");
 32     calc(st, N);
 33 
 34     k = fail(st, fst, N);
 35     sort(st, N);
 36 
 37     printf("\n学生成绩排名情况:\n");
 38     output(st, N);
 39 
 40     printf("\n不及格学生信息:\n");
 41     output(fst, k);
 42 
 43     return 0;
 44 }
 45 
 46 // 录入学生数据
 47 void input(STU s[], int n) {
 48     int i;
 49     for(i = 0; i < n; i++) {
 50         scanf("%d %s %s %lf %lf %lf", &s[i].id, s[i].name, s[i].subject,
 51               &s[i].perf, &s[i].mid, &s[i].final);
 52     }
 53 }
 54 
 55 // 输出学生信息表格
 56 void output(STU s[], int n) {
 57     int i;
 58     printf("----------------------------------------\n");
 59     printf("学号   姓名   科目   平时   期中   期末   总评   等级\n");
 60     for(i = 0; i < n; i++) {
 61         printf("%-4d %-6s %-4s %4.1f %4.1f %4.1f %4.1f %s\n",
 62                s[i].id, s[i].name, s[i].subject,
 63                s[i].perf, s[i].mid, s[i].final, s[i].total, s[i].level);
 64     }
 65 }
 66 
 67 // 计算总评成绩+划分等级
 68 void calc(STU s[], int n) {
 69     int i;
 70     for(i = 0; i < n; i++) {
 71         // 平时20% + 期中20% + 期末60%
 72         s[i].total = s[i].perf * 0.2 + s[i].mid * 0.2 + s[i].final * 0.6;
 73         if(s[i].total >= 90)
 74             strcpy(s[i].level, "");
 75         else if(s[i].total >= 80)
 76             strcpy(s[i].level, "");
 77         else if(s[i].total >= 70)
 78             strcpy(s[i].level, "");
 79         else if(s[i].total >= 60)
 80             strcpy(s[i].level, "及格");
 81         else
 82             strcpy(s[i].level, "不及格");
 83     }
 84 }
 85 
 86 // 筛选不及格学生存入t数组,返回不及格人数
 87 int fail(STU s[], STU t[], int n) {
 88     int i, cnt = 0;
 89     for(i = 0; i < n; i++) {
 90         if(s[i].total < 60) {
 91             t[cnt++] = s[i];
 92 
 93         }
 94     }
 95     return cnt;
 96 }
 97 
 98 // 冒泡排序:总评降序
 99 void sort(STU s[], int n) {
100     int i, j;
101     STU t;
102     for(i = 0; i < n - 1; i++) {
103         for(j = 0; j < n - 1 - i; j++) {
104             if(s[j].total < s[j+1].total) {
105                 t = s[j];
106                 s[j] = s[j+1];
107                 s[j+1] = t;
108             }
109         }
110     }
111 }

截图

image

 任务2

源代码

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 10
 4 #define M 80
 5 
 6 // 图书结构体
 7 typedef struct {
 8     char name[M];   // 书名(原图char name[M];漏写数组长度,语法错误)
 9     char author[M];  // 作者
10 } Book;
11 
12 int main() {
13     // 初始化图书数组
14     Book x[N] = {
15         {"一九八四", "乔治.奥威尔"},
16         {"美丽新世界", "赫胥黎"},
17         {"昨日的世界", "斯蒂芬.茨威格"},
18         {"万历十五年", "黄仁宇"},
19         {"一只特立独行的猪", "王小波"},
20         {"百年孤独", "马尔克斯"},
21         {"查令十字街84号", "海莲.汉芙"},
22         {"只是孩子", "帕蒂.史密斯"},
23         {"刀锋", "毛姆"},
24         {"沉默的大多数", "王小波"}
25     };
26 
27     Book *ptr;
28     char author[M];
29     int found;
30 
31     // 1. 使用指针遍历结构体数组,输出全部图书
32     printf("----------------所有图书信息----------------\n");
33     for(ptr = x; ptr < x + N; ++ptr)
34         printf("%-30s%-30s\n", ptr->name, ptr->author);
35 
36     // 2. 输入作者名,查找对应图书
37     printf("\n----------------按作者查询图书----------------\n");
38     printf("输入作者名:");
39     fgets(author, M, stdin);
40     // 去除fgets读取的换行符
41     author[strcspn(author, "\n")] = '\0';
42     
43     found = 0;
44     for(ptr = x; ptr < x + N; ++ptr) {
45         if(strcmp(ptr->author, author) == 0) {
46             found = 1;
47             printf("%-30s%-30s\n", ptr->name, ptr->author);
48         }
49     }
50     if(!found)
51         printf("暂未收录该作者书籍!\n");
52 
53     return 0;
54 }

截图

image

 任务3

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define N 80
 4 
 5 typedef struct FilmInfo {
 6     char name[N];
 7     char director[N];
 8     char region[N];
 9     int year;
10     struct FilmInfo *next;
11 } Film;
12 
13 
14 void output(Film *head);   // 遍历输出链表信息
15 Film *insert(Film *head, int n);   // 向链表中插入n个结点,返回头指针
16 
17 
18 int main() {
19     int n;          // 结点数
20     Film *head;     // 头指针变量,存放链表中第一个节点的地址
21 
22     head = NULL;
23     printf("输入影片数目: ");
24     scanf("%d", &n);
25 
26     // 向链表中插入n部影片信息
27     head = insert(head, n);
28 
29     // 遍历输出链表中所有影片信息
30     printf("\n所有影片信息如下: \n");
31     output(head);
32 
33     return 0;
34 }
35 
36 // 向链表中插入n个结点,从表头插入,返回头指针变量
37 Film *insert(Film *head, int n) {
38     int i;
39     Film *p;
40 
41     for(i = 1; i <= n; ++i) {
42         p = (Film *)malloc(sizeof(Film));
43         printf("请输入第%d部影片信息: ", i);
44         scanf("%s %s %s %d", p->name, p->director, p->region, &p->year);
45         head = p;   // 更新头指针变量
46     }
47 
48     return head;
49 }
50 
51 // 遍历输出链表信息
52 void output(Film *head) {
53     Film *p;
54 
55     p = head;
56     while(p != NULL) {
57         printf("%-20s %-20s %-20s %d\n", p->name, p->director, p->region, p->year);
58         p = p -> next;
59     }
60 }

截图

image

 源代码

 1 #define N 80#include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 
 5 typedef struct FilmInfo {
 6     char name[N];
 7     char director[N];
 8     char region[N];
 9     int year;
10     struct FilmInfo *next;
11 } Film;
12 
13 
14 void output(Film *head);   // 遍历输出链表信息
15 Film *insert(Film *head, int n);   // 向链表中插入n个结点,返回头指针
16 
17 
18 int main() {
19     int n;          // 结点数
20     Film *head;     // 头指针变量,存放链表中第一个节点的地址
21     Film *p;        // 存放新申请的Film节点内存空间地址
22 
23     // 创建头结点
24     p = (Film *)malloc(sizeof(Film));
25     p->next = NULL;
26     head = p;       // 头指针变量存放头节点的地址
27 
28     printf("输入影片数目: ");
29     scanf("%d", &n);
30 
31     // 向链表中插入n部影片信息
32     head = insert(head, n);
33 
34     // 遍历输出链表中所有影片信息
35     printf("\n所有影片信息如下: \n");
36     output(head);
37 
38     return 0;
39 }
40 
41 // 向链表中插入n个结点,从表头插入,返回头指针变量
42 Film *insert(Film *head, int n) {
43     int i;
44     Film *p;
45 
46     for(i = 1; i <= n; ++i) {
47         p = (Film *)malloc(sizeof(Film));
48         printf("请输入第%d部影片信息: ", i);
49         scanf("%s %s %s %d", p->name, p->director, p->region, &p->year);
50         
51         // 把结点从表头插入到链表中
52         p->next = head->next;
53         head->next = p;
54     }
55 
56     return head;
57 }
58 
59 // 遍历输出链表信息
60 void output(Film *head) {
61     Film *p;
62 
63     p = head->next;
64     while(p != NULL) {
65         printf("%-20s %-20s %-20s %d\n", p->name, p->director, p->region, p->year);
66         p = p -> next;
67     }
68 }

任务4

源代码

 1 #include <stdio.h>
 2 #define N 10
 3 
 4 typedef struct {
 5     char isbn[20];
 6     char name[80];
 7     char author[80];
 8     double sales_price;
 9     int sales_count;
10 } Book;
11 
12 void output(Book x[], int n);
13 void sort(Book x[], int n);
14 double sales_amount(Book x[], int n);
15 
16 int main() {
17     Book x[N] = {
18         {"978-7-5327-6082-4", "门将之死", "罗纳德.伦", 42, 51},
19         {"978-7-308-17047-5", "自由与爱之地:入以色列记", "云也退", 49, 30},
20         {"978-7-5404-9344-8", "伦敦人", "克莱格泰勒", 68, 27},
21         {"978-7-5447-5246-6", "软件体的生命周期", "特德姜", 35, 90},
22         {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49},
23         {"978-7-5133-5750-1", "主机战争", "布莱克.J.哈里斯", 128, 42},
24         {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰.史崔勒基", 22.5, 44},
25         {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42},
26         {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维.多伊奇", 37.5, 55},
27         {"978-7-229-14156-1", "源泉", "安.兰德", 84, 59}
28     };
29 
30     printf("图书销量排名(按销售册数):\n");
31     sort(x, N);
32     output(x, N);
33 
34     printf("\n图书销售总额:%.2f\n", sales_amount(x, N));
35 
36     return 0;
37 }
38 
39 void output(Book x[], int n) {
40     int i;
41     printf("%-20s %-30s %-15s %-8s %s\n", "ISBN号", "书名", "作者", "售价", "销售册数");
42     for (i = 0; i < n; i++) {
43         printf("%-20s %-30s %-15s %-8.1f %d\n",
44                x[i].isbn, x[i].name, x[i].author, x[i].sales_price, x[i].sales_count);
45     }
46 }
47 
48 void sort(Book x[], int n) {
49     int i, j;
50     Book temp;
51     for (i = 0; i < n - 1; i++) {
52         for (j = 0; j < n - 1 - i; j++) {
53             if (x[j].sales_count < x[j+1].sales_count) {
54                 temp = x[j];
55                 x[j] = x[j+1];
56                 x[j+1] = temp;
57             }
58         }
59     }
60 }
61 
62 double sales_amount(Book x[], int n) {
63     int i;
64     double total = 0.0;
65     for (i = 0; i < n; i++) {
66         total += x[i].sales_price * x[i].sales_count;
67     }
68     return total;
69 }

截图

image

 任务5

源代码

 1 #include <stdio.h>
 2 typedef struct {
 3     int year;
 4     int month;
 5     int day;
 6 } Date;
 7 
 8 void input(Date *pd);
 9 int day_of_year(Date d);
10 int compare_dates(Date d1, Date d2);
11 
12 void test1() {
13     Date d;
14     int i;
15     printf("输入日期:(以形如2025-12-19这样的形式输入)\n");
16     for(i = 0; i < 3; i++) {
17         input(&d);
18         printf("%d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d));
19     }
20 }
21 
22 void test2() {
23     Date Alice_birth, Bob_birth;
24     int i;
25     int ans;
26     printf("输入Alice和Bob出生日期:(以形如2025-12-19这样的形式输入)\n");
27     for(i = 0; i < 3; i++) {
28         input(&Alice_birth);
29         input(&Bob_birth);
30         ans = compare_dates(Alice_birth, Bob_birth);
31         if(ans == 0)
32             printf("Alice和Bob一样大\n\n");
33         else if(ans == -1)
34             printf("Alice比Bob大\n\n");
35         else
36             printf("Alice比Bob小\n\n");
37     }
38 }
39 
40 int main() {
41     printf("测试1: 输入日期,打印输出这是一年中第多少天\n");
42     test1();
43     printf("测试2: 比较两个日期先后\n");
44     test2();
45     return 0;
46 }
47 
48 void input(Date *pd) {
49     scanf("%d-%d-%d", &pd->year, &pd->month, &pd->day);
50 }
51 
52 int day_of_year(Date d) {
53     int mon[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
54     int sum = d.day;
55     for(int i = 0; i < d.month - 1; i++)
56         sum += mon[i];
57     if(d.month > 2) {
58         if((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0)
59             sum++;
60     }
61     return sum;
62 }
63 
64 int compare_dates(Date d1, Date d2) {
65     if(d1.year > d2.year)
66         return 1;
67     if(d1.year < d2.year)
68         return -1;
69     if(d1.month > d2.month)
70         return 1;
71     if(d1.month < d2.month)
72         return -1;
73     if(d1.day > d2.day)
74         return 1;
75     if(d1.day < d2.day)
76         return -1;
77     return 0;
78 }

截图

image

 任务6

源代码

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 enum Role {admin, student, teacher};
 5 
 6 typedef struct {
 7     char username[20];
 8     char password[20];
 9     enum Role type;
10 } Account;
11 
12 void output(Account x[], int n);
13 
14 int main() {
15     Account x[] = {
16         {"A1001", "123456", student},
17         {"A1002", "123abcdef", student},
18         {"A1009", "xyz12121", student},
19         {"x1009", "9213071x", admin},
20         {"C11553", "129dfg32k", teacher},
21         {"X3005", "921kfmg917", student}
22     };
23     int n;
24     n = sizeof(x)/sizeof(Account);
25     output(x, n);
26     return 0;
27 }
28 
29 void output(Account x[], int n) {
30     int i,j;
31     char mask[20];
32     char roleStr[20];
33     for(i=0;i<n;i++){
34         int len = strlen(x[i].password);
35         for(j=0;j<len;j++){
36             mask[j] = '*';
37         }
38         mask[len] = '\0';
39         if(x[i].type == admin)
40             strcpy(roleStr,"管理员");
41         else if(x[i].type == student)
42             strcpy(roleStr,"学生");
43         else
44             strcpy(roleStr,"教师");
45         printf("用户名:%s  密码:%s  角色:%s\n",x[i].username,mask,roleStr);
46     }
47 }

截图

image

 任务7

源代码

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 10
 4 
 5 typedef struct {
 6     char name[20];
 7     char phone[12];
 8     int vip;
 9 } Contact;
10 
11 void set_vip_contact(Contact x[], int n, char name[]);
12 void output(Contact x[], int n);
13 void display(Contact x[], int n);
14 
15 int main() {
16     Contact list[N] = {
17         {"刘一", "15510846604", 0},
18         {"陈二", "18038747351", 0},
19         {"张三", "18853253914", 0},
20         {"李四", "13230584477", 0},
21         {"王五", "15547571923", 0},
22         {"赵六", "18856659351", 0},
23         {"周七", "17705843215", 0},
24         {"孙八", "15552933732", 0},
25         {"吴九", "18077702405", 0},
26         {"郑十", "18820725036", 0}
27     };
28     int vip_cnt, i;
29     char name[20];
30 
31     printf("显示原始通讯录信息:\n");
32     output(list, N);
33 
34     printf("\n输入要设置的紧急联系人个数:");
35     scanf("%d", &vip_cnt);
36     printf("输入%d个紧急联系人姓名:\n", vip_cnt);
37     for(i = 0; i < vip_cnt; ++i) {
38         scanf("%s", name);
39         set_vip_contact(list, N, name);
40     }
41 
42     printf("\n显示通讯录列表:(按姓名字典序排列,紧急联系人最先显示)\n");
43     display(list, N);
44 
45     return 0;
46 }
47 
48 void set_vip_contact(Contact x[], int n, char name[]) {
49     int i;
50     for(i = 0; i < n; i++) {
51         if(strcmp(x[i].name, name) == 0) {
52             x[i].vip = 1;
53         }
54     }
55 }
56 
57 void output(Contact x[], int n) {
58     int i;
59     for(i = 0; i < n; ++i) {
60         printf("%-10s%-15s", x[i].name, x[i].phone);
61         if(x[i].vip)
62             printf("%5s", "*");
63         printf("\n");
64     }
65 }
66 
67 void display(Contact x[], int n) {
68     Contact temp[N];
69     Contact t;
70     int i, j, pos = 0;
71     for(i = 0; i < n; i++) {
72         if(x[i].vip == 1) {
73             temp[pos++] = x[i];
74         }
75     }
76     for(i = 0; i < n; i++) {
77         if(x[i].vip == 0) {
78             temp[pos++] = x[i];
79         }
80     }
81     for(i = 0; i < pos - 1; i++) {
82         for(j = 0; j < pos - 1 - i; j++) {
83             if(strcmp(temp[j].name, temp[j+1].name) > 0) {
84                 t = temp[j];
85                 temp[j] = temp[j+1];
86                 temp[j+1] = t;
87             }
88         }
89     }
90     output(temp, pos);
91 }

截图

image

 

image

 

image

 

posted @ 2026-06-16 21:48  龚祉天  阅读(9)  评论(0)    收藏  举报