实验6 C语言结构体、枚举应用编程

发布时间 2023-12-17 23:38:49作者: mdlmdl

task1

  1 // P286例8.17
  2 // 对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块
  3 // 打印不及格学生信息和所有学生信息程分别调用 
  4  
  5 #include <stdio.h>
  6 #include <string.h> 
  7 #define N 3        // 运行程序输入测试时,可以把这个数组改小一些输入测试 
  8 
  9 typedef struct student {
 10     int id;             // 学号 
 11     char name[20];         // 姓名 
 12     char subject[20];     // 考试科目
 13     double perf;         // 平时成绩 
 14     double mid;         // 期中成绩 
 15     double final;         // 期末成绩
 16     double total;         // 总评成绩 
 17     char level[10];     // 成绩等级
 18 } STU;
 19 
 20 void input(STU [], int);            // 录入学生信息
 21 void output(STU [], int);            // 输出学生信息
 22 void calc(STU [], int);                // 计算总评和等级 
 23 int fail(STU [], STU [], int);        // 统计不及格学生信息
 24 void sort(STU [], int);                // 排序 
 25 
 26 int main() {
 27     STU st[N], fst[N];   // 数组st记录学生信息,fst记录不及格学生信息 
 28     int k;  // 用于记录不及格学生个数 
 29     
 30     printf("录入学生成绩信息:\n");
 31     input(st, N);
 32     
 33     printf("\n成绩处理...\n");
 34     calc(st, N);
 35     
 36     k = fail(st, fst, N);
 37     sort(st, N);
 38     printf("\n学生成绩排名情况:\n");
 39     output(st, N);
 40     
 41     printf("\n不及格学生信息:\n");
 42     output(fst, k);
 43     
 44     return 0;
 45 } 
 46 
 47 void input(STU s[], int n) {
 48     int i;
 49     
 50     for(i = 0; i < n; i++)
 51         scanf("%d %s %s %lf %lf %lf", &s[i].id, s[i].name, s[i].subject,
 52                                       &s[i].perf, &s[i].mid, &s[i].final);
 53 }
 54 
 55 void output(STU s[], int n) {
 56        int i;
 57    
 58       printf("-----------------\n");
 59       printf("学号   姓名     科目   平时   期中   期末   总评   等级\n");
 60        for(i = 0; i<n; i++)
 61           printf("%d   %-6s   %-4s   %-4.0f   %-4.0f   %-4.0f   %-4.1f   %s\n",s[i].id,s[i].name,s[i].subject,s[i].perf,s[i].mid,s[i].final,s[i].total,s[i].level);
 62 }
 63 
 64 
 65 void calc(STU s[],int n) {
 66     int i;
 67 
 68     for(i = 0; i < n; i++) {    
 69         s[i].total = s[i].perf * 0.2 + 
 70                      s[i].mid * 0.2 +
 71                      s[i].final * 0.6;
 72         
 73         if(s[i].total >= 90)
 74           strcpy(s[i].level, "");
 75         else if(s[i].total >= 80 && s[i].total < 90)
 76           strcpy(s[i].level, "");
 77         else if(s[i].total >= 70 && s[i].total < 80)
 78           strcpy(s[i].level, ""); 
 79         else if(s[i].total >= 60 && s[i].total < 70)
 80           strcpy(s[i].level, "及格");
 81         else
 82           strcpy(s[i].level, "不及格");         
 83     }
 84 }
 85 
 86 int fail(STU s[], STU t[], int n) {
 87       int i, cnt = 0;
 88       
 89       for(i = 0; i < n; i++)
 90           if(s[i].total < 60)
 91             t[cnt++] = s[i];
 92             
 93     return cnt;
 94 }
 95 
 96 void sort(STU s[], int n) {
 97     int i, j;
 98     STU t;
 99     
100     for(i = 0; i < n-1; i++)
101       for(j = 0; j < n-1-i; j++)
102         if(s[j].total < s[j+1].total) {
103             t = s[j];
104             s[j] = s[j+1];
105             s[j+1] = t;
106         }
107 }

task2

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 10
 4 #define M 80
 5 
 6 typedef struct {
 7     char name[M];       // 书名
 8     char author[M];     // 作者
 9 } Book;
10 
11 int main() {
12     Book x[N] = { {"《一九八四》", "乔治.奥威尔"},
13                   {"《美丽新世界》", "赫胥黎"},
14                   {"《昨日的世界》", "斯蒂芬.茨威格"}, 
15                   {"《万历十五年》", "黄仁宇"},
16                   {"《一只特立独行的猪》", "王小波"},
17                   {"《百年孤独》", "马尔克斯"},
18                   {"《查令十字街84号》", "海莲.汉芙"},
19                   {"《只是孩子》", "帕蒂.史密斯"}, 
20                   {"《刀锋》", "毛姆"},
21                   {"《沉默的大多数》", "王小波"} };
22     Book *ptr;
23     int i;
24     char author[M];
25 
26     // 使用指针遍历结构体数组
27     printf("所有图书信息: \n");
28     for(ptr = x; ptr < x + N; ++ptr)
29         printf("%-30s%-30s\n", ptr->name, ptr->author);
30 
31     // 查找指定作者的图书
32     printf("\n输入作者名: ");
33     gets(author);
34     for(ptr = x; ptr < x + N; ++ptr)
35         if(strcmp(ptr->author, author) == 0) {
36             printf("%-30s%-30s\n", ptr->name, ptr->author);
37         }
38 
39     return 0;
40 }

task3.1

 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         
46         // 把结点从表头插入到链表中
47         p->next = head;
48         head = p;   // 更新头指针变量
49     }
50 
51     return head;
52 }
53 
54 // 遍历输出链表信息
55 void output(Film *head) {
56     Film *p;
57 
58     p = head;
59     while(p != NULL) {
60         printf("%-20s %-20s %-20s %d\n", p->name, p->director, p->region, p->year);
61         p = p -> next;
62     }
63 }

task3.2

 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     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 }

task4

 1
 1 #include <stdio.h>
 2 #define N 10
 3 
 4 typedef struct {
 5     char isbn[20];          // isbn号
 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] = {{"978-7-229-14156-1", "源泉", "安.兰德", 84, 59},
18                  {"978-7-5133-5261-1", "李白来到旧金山", "谭夏阳", 48, 16},
19                  {"978-7-5617-4347-8", "陌生人日记", "周怡芳", 72.6, 27},
20                  {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49},
21                  {"978-7-5046-9568-0", "数据化决策", "道格拉斯·W·哈伯德", 49, 42},
22                  {"978-7-5133-4388-6", "美好时代的背后", "凯瑟琳.布", 34.5, 39},
23                  {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55},
24                  {"978-7-5321-5691-7", "何为良好生活", "陈嘉映", 29.5 , 31},
25                  {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42},
26                  {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44}};
27     
28     printf("图书销量排名: \n");
29     sort(x, N);
30     output(x, N);
31 
32     printf("\n图书销售总额: %.2f\n", sales_amount(x, N));
33     
34     return 0;
35 }
36 
37 
38  
39  void output(Book x[], int n)
40  {int i;
41  printf("ISBN号             书名                        作者                   售价     销售册数\n");
42  for(i = 0; i<n; i++)
43            printf("%-18s %-25s   %-20s   %-6.1f   %-4d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
44  } 
45  void sort(Book x[], int n)
46  {int i,j;Book t;
47  for(i=0;i<n-1;i++)
48  {for(j=0;j<n-1-i;j++)
49  {
50  if(x[j].sales_count<x[j+1].sales_count)
51   {
52   t=x[j];
53   x[j]=x[j+1];
54   x[j+1]=t;
55   }
56   } 
57  }
58 } 
59 
60  
61  
62  double sales_amount(Book x[], int n)
63  {int i;double sales=0,s;
64  for(i=0;i<n;i++)
65  {s=x[i].sales_count*x[i].sales_price;
66  sales=sales+s;
67  }
68  return sales;
69  }

 

 

task5

  1 #include <stdio.h>
  2 
  3 typedef struct {
  4     int year;
  5     int month;
  6     int day;
  7 } Date;
  8 
  9 // 函数声明
 10 void input(Date *pd);                   // 输入日期给pd指向的Date变量
 11 int day_of_year(Date d);                // 返回日期d是这一年的第多少天
 12 int compare_dates(Date d1, Date d2);    // 比较两个日期: 
 13                                         // 如果d1在d2之前,返回-1;
 14                                         // 如果d1在d2之后,返回1
 15                                         // 如果d1和d2相同,返回0
 16 
 17 void test1() {
 18     Date d;
 19     int i;
 20 
 21     printf("输入日期:(以形如2023-12-11这样的形式输入)\n");
 22     for(i = 0; i < 3; ++i) {
 23         input(&d);
 24         printf("%d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d));
 25     }
 26 }
 27 
 28 void test2() {
 29     Date Alice_birth, Bob_birth;
 30     int i;
 31     int ans;
 32 
 33     printf("输入Alice和Bob出生日期:(以形如2023-12-11这样的形式输入)\n");
 34     for(i = 0; i < 3; ++i) {
 35         input(&Alice_birth);
 36         input(&Bob_birth);
 37         ans = compare_dates(Alice_birth, Bob_birth);
 38         
 39         if(ans == 0)
 40             printf("Alice和Bob一样大\n\n");
 41         else if(ans == -1)
 42             printf("Alice比Bob大\n\n");
 43         else
 44             printf("Alice比Bob小\n\n");
 45     }
 46 }
 47 
 48 int main() {
 49     printf("测试1: 输入日期, 打印输出这是一年中第多少天\n");
 50     test1();
 51 
 52     printf("\n测试2: 两个人年龄大小关系\n");
 53     test2();
 54 }
 55 
 56 // 补足函数input实现
 57 // 功能: 输入日期给pd指向的Date变量
 58 void input(Date *pd) {
 59     scanf("%d-%d-%d",&(*pd).year,&(*pd).month,&(*pd).day);
 60 }
 61 
 62 // 补足函数day_of_year实现
 63 // 功能:返回日期d是这一年的第多少天
 64 int day_of_year(Date d) {
 65     int i;int s,sum;
 66     if((d.year%4==0&&d.year%100!=0)||d.year%400==0)
 67     {sum=0;
 68          int x[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
 69          for(i=1;i<=d.month;i++)
 70          {s=x[i-1];
 71          sum=sum+s;
 72          }
 73          } 
 74     else
 75     {sum=0;
 76          int x[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 77          for(i=1;i<=d.month;i++)
 78          {s=x[i-1];
 79          sum=sum+s;
 80          }
 81     }
 82         return sum+d.day;
 83 }
 84 
 85 // 补足函数compare_dates实现
 86 // 功能:比较两个日期: 
 87 // 如果d1在d2之前,返回-1;
 88 // 如果d1在d2之后,返回1
 89 // 如果d1和d2相同,返回0
 90 int compare_dates(Date d1, Date d2) {
 91     int ans;
 92     if (d1.year < d2.year) {
 93         ans = -1;
 94         return ans;
 95     }
 96     else if (d1.year > d2.year) {
 97         ans = 1;
 98         return ans;
 99     }
100     else {
101         if (d1.month < d2.month) {
102             ans = -1;
103             return ans;
104         }
105         else if (d1.month > d2.month) {
106             ans = 1;
107             return ans;
108         }
109         else {
110             if (d1.day < d2.day) {
111                 ans = -1;
112                 return ans;
113             }
114             else if (d1.day > d2.day) {
115                 ans = 1;
116                 return ans;
117             }
118             else {
119                 ans = 0;
120                 return ans;
121             }
122         }
123     }
124     
125     // 待补足
126     // ×××
127 }

task6

 1 #include <stdio.h>
 2 #include <string.h>
 3 enum Role { admin, student, teacher };
 4 typedef struct {
 5     char username[20];
 6     char password[20];
 7     enum Role type;
 8 } Account;
 9 
10 void output(Account x[], int n);
11 int main() {
12     Account x[] = { {"A1001", "123456", student},
13     {"A1002", "123abcdef", student},
14     {"A1009", "xyz12121", student},
15     {"X1009", "9213071x", admin},
16     {"C11553", "129dfg32k", teacher},
17     {"X3005", "921kfmg917", student} };
18     int n;
19     n = sizeof(x) / sizeof(Account);
20     output(x, n);
21     return 0;
22 }
23 
24 void output(Account x[], int n) {
25      int i,j;
26      for(i=0;i<n;i++){
27          printf("%s\t\t",x[i].username);
28          
29          for(j=0;j<strlen(x[i].password );j++)
30              printf("*");
31          if(i==0)
32              printf("\t");
33          printf("\t");
34          switch(x[i].type ){
35          case(0):printf("admin");break;
36          case(1):printf("student");break;
37          case(2):printf("teacher");break;}
38      
39  
40          printf("\n");
41      }
42  }