实验 6
任务 1
1 #include <stdio.h> 2 #include <string.h> 3 #define N 3 4 5 typedef struct student { 6 int id; 7 char name[20]; 8 char subject[20]; 9 double perf; 10 double mid; 11 double final; 12 double total; 13 char level[10]; 14 } STU; 15 16 void input(STU [], int); 17 void output(STU [], int); 18 void calc(STU [], int); 19 int fail(STU [], STU [], int); 20 void sort(STU [], int); 21 22 int main() { 23 STU st[N], fst[N]; 24 int k; 25 26 printf("录入学生成绩信息:\n"); 27 input(st, N); 28 29 printf("\n loading ...\n"); 30 calc(st, N); 31 32 k = fail(st, fst, N); 33 sort(st, N); 34 printf("\n学生成绩排名情况:\n"); 35 output(st, N); 36 37 printf("\n不及格学生信息:\n"); 38 output(fst, k); 39 40 return 0; 41 } 42 43 void input(STU s[], int n) { 44 int i; 45 46 for(i = 0; i < n; i++) 47 scanf("%d %s %s %lf %lf %lf", &s[i].id, s[i].name, s[i].subject, 48 &s[i].perf, &s[i].mid, &s[i].final); 49 } 50 51 void output(STU s[], int n) { 52 int i; 53 54 printf("---------我是分割线--------\n"); 55 printf("学号 姓名 科目 平时 期中 期末 总评 等级\n"); 56 for(i = 0; i<n; i++) 57 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); 58 } 59 60 61 void calc(STU s[],int n) { 62 int i; 63 64 for(i = 0; i < n; i++) { 65 s[i].total = s[i].perf * 0.2 + 66 s[i].mid * 0.2 + 67 s[i].final * 0.6; 68 69 if(s[i].total >= 90) 70 strcpy(s[i].level, "优"); 71 else if(s[i].total >= 80) 72 strcpy(s[i].level, "良"); 73 else if(s[i].total >= 70) 74 strcpy(s[i].level, "中"); 75 else if(s[i].total >= 60) 76 strcpy(s[i].level, "及格"); 77 else 78 strcpy(s[i].level, "不及格"); 79 } 80 } 81 82 int fail(STU s[], STU t[], int n) { 83 int i, cnt = 0; 84 85 for(i = 0; i < n; i++) 86 if(s[i].total < 60) 87 t[cnt++] = s[i]; 88 89 return cnt; 90 } 91 92 void sort(STU s[], int n) { 93 int i, j; 94 STU t; 95 96 for(i = 0; i < n-1; i++) 97 for(j = 0; j < n-1-i; j++) 98 if(s[j].total < s[j+1].total) { 99 t = s[j]; 100 s[j] = s[j+1]; 101 s[j+1] = t; 102 } 103 }
任务 2
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 {"《撒野》", "巫哲"}, 19 {"《悲惨世界》", "雨果"}, 20 {"《鼠疫》", "加缪"}, 21 {"《高等数学》", "王顺凤"} }; 22 Book *ptr; 23 int i; 24 char author[M]; 25 int found; 26 27 // 使用指针遍历结构体数组 28 printf("-------------------所有图书信息-------------------\n"); 29 for(ptr = x; ptr < x + N; ++ptr) 30 printf("%-30s%-30s\n", ptr->name, ptr->author); 31 32 // 查找指定作者的图书 33 printf("\n-------------------按作者查询图书-------------------\n"); 34 printf("输入作者名: "); 35 gets(author); 36 found = 0; 37 for(ptr = x; ptr < x + N; ++ptr) 38 if(strcmp(ptr->author, author) == 0) { 39 found = 1; 40 printf("%-30s%-30s\n", ptr->name, ptr->author); 41 } 42 43 if(!found) 44 printf("暂未收录该作者书籍!\n"); 45 46 return 0; 47 }
任务 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); 16 17 18 int main() { 19 int n; 20 Film *head; 21 22 head = NULL; 23 printf("输入影片数目: "); 24 scanf("%d", &n); 25 26 27 head = insert(head, n); 28 29 30 printf("\n所有影片信息如下: \n"); 31 output(head); 32 33 return 0; 34 } 35 36 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 }
任务 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 salse_amount(Book x[], int n); 15 16 int main(){ 17 Book x[N] = {{"978-7-5327-6082-4", "门将之死", "罗纳德.伦", 42, 51}, 18 {"978-7-308-17047-5", "自由与爱之地:入以色列记", "云也退", 49 , 30}, 19 {"978-7-5404-9344-8", "伦敦人", "克莱格泰勒", 68, 27}, 20 {"978-7-5447-5246-6", "软件体的生命周期", "特德姜", 35, 90}, 21 {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49}, 22 {"978-7-5133-5750-0", "主机战争", "布莱克.J.哈里斯", 128, 42}, 23 {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44}, 24 {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42}, 25 {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55}, 26 {"978-7-229-14156-1", "源泉", "安.兰德", 84, 59} 27 }; 28 29 30 printf("图书馆销量排名(按销售册数):\n"); 31 sort(x,N); 32 output(x,N); 33 34 printf("\n图书销售总额:%.2f\n",salse_amount(x,N)); 35 36 return 0; 37 38 } 39 40 41 void output(Book x[], int n){ 42 printf("ISBN号 书名 作者 售价 销售册数\n"); 43 int i; 44 for(i=0;i<n;i++){ 45 printf("%-15s %-25s %-15s %-5g %-5d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count); 46 } 47 48 49 } 50 51 52 53 void sort(Book x[], int n){ 54 int i,j; 55 Book t;; 56 for(i=0;i<n;i++){ 57 for(j=0;j<n-1-i;j++){ 58 if(x[j+1].sales_count>x[j].sales_count){ 59 t=x[j]; 60 x[j]=x[j+1]; 61 x[j+1]=t; 62 } 63 } 64 } 65 66 67 } 68 69 70 71 72 double salse_amount(Book x[], int n){ 73 int i; 74 double sum=0; 75 for(i=0;i<n;i++){ 76 sum=sum+(x[i].sales_count*x[i].sales_price); 77 } 78 79 return sum; 80 }
任务 5
1 #include<stdio.h> 2 3 typedef struct { 4 int year; 5 int month; 6 int day; 7 } Date; 8 9 void input(Date *pd); 10 int day_of_year(Date d); 11 int compare_dates(Date d1,Date d2); 12 13 14 15 16 17 void test1(){ 18 Date d; 19 int i; 20 21 printf("输入日期:\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 29 30 31 void test2(){ 32 Date Alice_birth,Bob_birth; 33 int i; 34 int ans; 35 36 printf("输入出生日期:\n"); 37 for(i=0;i<3;++i){ 38 input(&Alice_birth); 39 input(&Bob_birth); 40 ans=compare_dates(Alice_birth,Bob_birth); 41 42 if(ans==0) printf("Alice和Bob一样大\n\n"); 43 else if(ans==-1) printf("Alice比Bob大\n\n"); 44 else printf("Alice比Bob小\n\n"); 45 } 46 } 47 48 49 50 int main(){ 51 printf("测试1:输入日期,打印输出这是一年中第多少天\n"); 52 test1(); 53 54 printf("\n测试2:两个人年龄大小关系\n"); 55 test2(); 56 57 } 58 59 60 void input(Date *pd) { 61 scanf("%d-%d-%d", &(pd->year), &(pd->month), &(pd->day)); 62 } 63 64 65 int day_of_year(Date d){ 66 int i; 67 int sum=0; 68 if(d.year%4==0&&d.year%100!=0||d.year%400==0){ 69 for(i=1;i<d.month;i++){ 70 if(i==1||i==3||i==5||i==7||i==8||i==10) sum=sum+31; 71 if(i==2) sum=sum+29; 72 if(i==4||i==6||i==9||i==11) sum=sum+30; 73 74 } 75 sum=sum+d.day; 76 } 77 else{ 78 for(i=1;i<d.month;i++){ 79 if(i==1||i==3||i==5||i==7||i==8||i==10) sum=sum+31; 80 if(i==2) sum=sum+28; 81 if(i==4||i==6||i==9||i==11) sum=sum+30; 82 83 } 84 sum=sum+d.day; 85 } 86 87 return sum; 88 } 89 90 91 92 int compare_dates(Date d1,Date d2){ 93 if(d1.year==d2.year&&d1.month==d2.month&&d1.day==d2.day) return 0; 94 if(d1.year<d2.year||(d1.year==d2.year&&d1.month<d2.month)||(d1.year==d2.year&&d1.month==d2.month&&d1.day<d2.day)) 95 return -1; 96 97 98 }
任务 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 13 14 void output(Account x[], int n); 15 16 int main() { 17 Account x[] = {{"A1001", "123456", student}, 18 {"A1002", "123abcdef", student}, 19 {"A1009", "xyz12121", student}, 20 {"X1009", "9213071x", admin}, 21 {"C11553", "129dfg32k", teacher}, 22 {"X3005", "921kfmg917", student}}; 23 int n; 24 n = sizeof(x)/sizeof(Account); 25 output(x, n); 26 27 return 0; 28 } 29 30 31 void output(Account x[], int n){ 32 int i,j; 33 for(i=0;i<n;i++){ 34 printf("%s\t",x[i].username) ; 35 for(j=0;j<strlen(x[i].password);j++) printf("*"); 36 if(i==0) printf(" "); 37 printf("\t"); 38 switch (x[i].type) { 39 case admin: 40 printf("admin\n"); 41 break; 42 case student: 43 printf("student\n"); 44 break; 45 case teacher: 46 printf("teacher\n"); 47 break; 48 } 49 } 50 51 }
任务 7
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 6 typedef struct { 7 char name[20]; 8 char phone[12]; 9 int vip; 10 } Contact; 11 12 // 函数声明 13 void set_vip_contact(Contact x[], int n, char name[]); 14 void output(Contact x[], int n); 15 void display(Contact x[], int n); 16 17 #define N 10 18 int main() { 19 Contact list[N] = {{"刘一", "15510846604", 0}, 20 {"陈二", "18038747351", 0}, 21 {"张三", "18853253914", 0}, 22 {"李四", "13230584477", 0}, 23 {"王五", "15547571923", 0}, 24 {"赵六", "18856659351", 0}, 25 {"周七", "17705843215", 0}, 26 {"孙八", "15552933732", 0}, 27 {"吴九", "18077702405", 0}, 28 {"郑十", "18820725036", 0}}; 29 int vip_cnt, i; 30 char name[20]; 31 32 printf("显示原始通讯录信息: \n"); 33 output(list, N); 34 35 printf("\n输入要设置的紧急联系人个数: "); 36 scanf("%d", &vip_cnt); 37 38 printf("输入%d个紧急联系人姓名:\n", vip_cnt); 39 for(i = 0; i < vip_cnt; ++i) { 40 scanf("%s", name); 41 set_vip_contact(list, N, name); 42 } 43 44 printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n"); 45 display(list, N); 46 47 return 0; 48 } 49 50 // 补足函数set_vip_contact实现 51 // 功能:将联系人数组x中,联系人姓名与name一样的人,设置为紧急联系人(即成员vip值设为1) 52 void set_vip_contact(Contact x[], int n, char name[]) { 53 int i; 54 for(i = 0; i < n; i++) { 55 if (!strcmp(name, x[i].name)) { 56 x[i].vip = 1; 57 58 } 59 } 60 } 61 62 // 补足函数display实现 63 // 功能: 显示联系人数组x中的联系人信息 64 // 按姓名字典序升序显示, 紧急联系人显示在最前面 65 void display(Contact x[], int n) { 66 Contact temp ; 67 int i, j; 68 69 for (i = 0; i < n - 1; i++) { 70 for (j = 0; j < n - i - 1; j++) { 71 if (x[j].vip < x[j + 1].vip) { 72 73 74 temp = x[j]; 75 x[j] = x[j + 1]; 76 x[j + 1] = temp; 77 } else if (x[j].vip == x[j + 1].vip && strcmp(x[j].name, x[j + 1].name) > 0) { 78 79 80 temp = x[j]; 81 x[j] = x[j + 1]; 82 x[j + 1] = temp; 83 } 84 } 85 } 86 87 88 for (i = 0; i < n; i++) { 89 printf("%-10s%-15s", x[i].name, x[i].phone); 90 if (x[i].vip) 91 printf("%5s", "*"); 92 printf("\n"); 93 } 94 95 } 96 97 98 void output(Contact x[], int n) { 99 int i; 100 101 for (i = 0; i < n; ++i) { 102 printf("%-10s%-15s", x[i].name, x[i].phone); 103 if (x[i].vip) 104 printf("%5s", "*"); 105 printf("\n"); 106 } 107 }