实验6
TASK 1
1 #include<stdio.h> 2 #include<string.h> 3 #define N 3 4 5 typedef struct student 6 { 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 void input(STU[],int); 18 void output(STU[],int); 19 void calc(STU[],int); 20 int fail(STU[],STU[],int); 21 void sort(STU[],int); 22 23 int main() 24 { 25 STU st[N],fst[N]; 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 printf("\n学生成绩排名情况:\n"); 37 output(st,N); 38 39 printf("\n不及格学生信息:\n"); 40 output(fst,k); 41 42 return 0; 43 } 44 45 void input(STU s[],int n) 46 { 47 int i; 48 for(i=0;i<n;i++) 49 { 50 scanf("%d %s %s %lf %lf %lf",&s[i].id,&s[i].name,&s[i].subject,&s[i].perf,&s[i].mid,&s[i].final); 51 } 52 } 53 54 void output(STU s[],int n) 55 { 56 int i; 57 58 printf("-------------------\n"); 59 printf("学号 姓名 科目 平时 期中 期末 总评 等级\n"); 60 61 for(i=0;i<n;i++) 62 { 63 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); 64 } 65 } 66 67 void calc(STU s[],int n) 68 { 69 int i; 70 71 for(i=0;i<n;i++) 72 { 73 s[i].total=s[i].perf*0.2+s[i].mid*0.2+s[i].final*0.6; 74 75 if(s[i].total>=90) 76 strcpy(s[i].level,"优"); 77 else if(s[i].total>=80) 78 strcpy(s[i].level,"良"); 79 else if(s[i].total>=70) 80 strcpy(s[i].level,"中"); 81 else if(s[i].total>=60) 82 strcpy(s[i].level,"及格"); 83 else 84 strcpy(s[i].level,"不及格"); 85 } 86 } 87 88 int fail(STU s[],STU t[],int n) 89 { 90 int i,cnt=0; 91 92 for(i=0;i<n;i++) 93 { 94 if(s[i].total<60) 95 t[cnt++]=s[i]; 96 } 97 return cnt; 98 } 99 100 void sort(STU s[],int n) 101 { 102 int i,j; 103 STU t; 104 105 for(i=0;i<n;i++) 106 { 107 for(j=0;j<n;j++) 108 { 109 if(s[j].total<s[j+1].total) 110 { 111 t=s[j]; 112 s[j]=s[j+1]; 113 s[j+1]=t; 114 } 115 } 116 } 117 }
TASK 2
1 #include<stdio.h> 2 #include<string.h> 3 #define N 10 4 #define M 80 5 6 typedef struct 7 { 8 char name[M]; 9 char author[M]; 10 }book; 11 12 int main() 13 { 14 book x[N]={{"《一九八四》","乔治·奥威尔"},{"《美丽新世界》","赫胥黎"},{"《昨日的世界》","斯蒂芬·茨威格"},{"《万历十五年》","黄仁宇"},{"《一只特立独行的猪》","王小波"},{"《百年孤独》","马尔克斯"},{"《查令十字街84号》","海莲·汉芙"},{"《只是孩子》","帕蒂·史密斯"},{"《刀锋》","毛姆"},{"《沉默的大多数》","王小波"}}; 15 book *ptr; 16 int i; 17 char author[M]; 18 int found; 19 20 printf("-------------------所有图书信息-------------------\n"); 21 for(ptr=x;ptr<x+N;++ptr) 22 printf("%-30s%-30s\n",ptr->name,ptr->author); 23 24 printf("\n-------------------按作者查询图书-------------------\n"); 25 printf("输入作者名:"); 26 gets(author); 27 found=0; 28 for(ptr=x;ptr<x+N;++ptr) 29 if(strcmp(ptr->author,author)==0) 30 { 31 found=1; 32 printf("%-30s%-30s\n", ptr->name, ptr->author); 33 } 34 if(!found) 35 printf("暂未收录该作者书籍!\n"); 36 37 return 0; 38 }
TASK 3
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 80 4 5 typedef struct filminfo 6 { 7 char name[N]; 8 char director[N]; 9 char region[N]; 10 int year; 11 struct filminfo *next; 12 }film; 13 14 void output(film *head); 15 film *insert(film *head,int n); 16 17 int main() 18 { 19 int n; 20 film *head; 21 22 head=NULL; 23 printf("输入影片数目:"); 24 scanf("%d",&n); 25 26 head=insert(head,n); 27 printf("\n所有影片信息如下:\n"); 28 29 output(head); 30 return 0; 31 } 32 33 film *insert(film *head,int n) 34 { 35 int i; 36 film *p; 37 38 for(i=1;i<=n;++i) 39 { 40 p=(film *)malloc(sizeof(film)); 41 printf("请输入第%d部影片信息:",i); 42 scanf("%s %s %s %d",p->name,p->director,p->region,&p->year); 43 44 p->next=head; 45 head=p; 46 } 47 return head; 48 } 49 50 void output(film *head) 51 { 52 film *p; 53 p=head; 54 while(p!=NULL) 55 { 56 printf("%-20s %-20s %-20s %d\n",p->name,p->director,p->region,p->year); 57 p=p->next; 58 } 59 }
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 80 4 5 typedef struct filminfo 6 { 7 char name[N]; 8 char director[N]; 9 char region[N]; 10 int year; 11 struct filminfo *next; 12 }film; 13 14 void output(film *head); 15 film *insert(film *head,int n); 16 17 int main() 18 { 19 int n; 20 film *head; 21 film *p; 22 23 p=(film *)malloc(sizeof(film)); 24 p->next=NULL; 25 head=p; 26 27 printf("输入影片数目:"); 28 scanf("%d",&n); 29 30 head=insert(head,n); 31 printf("\n所有影片信息如下:\n"); 32 33 output(head); 34 return 0; 35 } 36 37 film *insert(film *head,int n) 38 { 39 int i; 40 film *p; 41 42 for(i=1;i<=n;++i) 43 { 44 p=(film *)malloc(sizeof(film)); 45 printf("请输入第%d部影片信息:",i); 46 scanf("%s %s %s %d",p->name,p->director,p->region,&p->year); 47 48 p->next=head; 49 head=p; 50 } 51 return head; 52 } 53 54 void output(film *head) 55 { 56 film *p; 57 p=head; 58 while(p!=NULL) 59 { 60 printf("%-20s %-20s %-20s %d\n",p->name,p->director,p->region,p->year); 61 p=p->next; 62 } 63 }
修改后改变任意节点都不会影响头指针变量,不需要分类讨论改变节点的情况了
TASK 4
1 #include<stdio.h> 2 #define N 10 3 4 typedef struct 5 { 6 char isbn[20]; 7 char name[80]; 8 char author[80]; 9 double sales_price; 10 int sales_count; 11 }book; 12 13 void output(book x[],int n); 14 void sort(book x[],int n); 15 double sales_amount(book x[],int n); 16 17 int main() 18 { 19 book x[N]={{"978-7-5327-6082-4", "门将之死", "罗纳德.伦", 42, 51}, 20 {"978-7-308-17047-5", "自由与爱之地:入以色列记", "云也退", 49 21 , 30}, 22 {"978-7-5404-9344-8", "伦敦人", "克莱格泰勒", 68, 27}, 23 {"978-7-5447-5246-6", "软件体的生命周期", "特德姜", 35, 90}, 24 {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49}, 25 {"978-7-5133-5750-0", "主机战争", "布莱克.J.哈里斯", 128, 42}, 26 {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 27 22.5, 44}, 28 {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 29 42}, 30 {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55}, 31 {"978-7-229-14156-1", "源泉", "安.兰德", 84, 59} 32 }; 33 printf("图书销量排名(按销售册数):\n"); 34 sort(x,N); 35 output(x,N); 36 37 printf("\n图书销售总额:%.2f\n",sales_amount(x,N)); 38 39 return 0; 40 } 41 42 void output(book x[],int n) 43 { 44 int i; 45 printf(" ISBN号 书名 作者 售价 销售册数 \n"); 46 for (i = 0; i < n; i++) 47 { 48 printf(" %s %s %s %.2f %d \n", x[i].isbn, x[i].name, x[i].author, x[i].sales_price, x[i].sales_count); 49 } 50 } 51 52 void sort(book x[],int n) 53 { 54 int i,j; 55 book t; 56 for(i=0;i<n-1;++i) 57 { 58 for(j=0;j<n-i-1;++j) 59 { 60 if(x[j].sales_count<x[j+1].sales_count) 61 { 62 t=x[j]; 63 x[j]=x[j+1]; 64 x[j+1]=t; 65 } 66 } 67 68 } 69 } 70 71 double sales_amount(book x[],int n) 72 { 73 double totsals=0; 74 int i; 75 for(i=0;i<n;i++) 76 { 77 totsals+=x[i].sales_price*x[i].sales_count; 78 } 79 return totsals; 80 }
TASK 5
1 #include<stdio.h> 2 3 typedef struct 4 { 5 int year; 6 int month; 7 int day; 8 }date; 9 10 void input(date *pd); 11 int day_of_year(date d); 12 int compare_dates(date d1,date d2); 13 14 void test1() 15 { 16 date d; 17 int i; 18 19 printf("输入日期:(以形如2024-12-16这样的形式输入)\n"); 20 for(i=0;i<3;++i) 21 { 22 input(&d); 23 printf("%d-%02d-%02d是这一年中第%d天\n\n",d.year,d.month,d.day,day_of_year(d)); 24 } 25 } 26 27 void test2() 28 { 29 date Alice_birth,Bob_birth; 30 int i; 31 int ans; 32 33 printf("输入Alice和Bob出生日期:(以形如2024-12-16这样的形式输入)\n"); 34 for(i=0;i<3;++i) 35 { 36 input(&Alice_birth); 37 input(&Bob_birth); 38 ans=compare_dates(Alice_birth,Bob_birth); 39 40 if(ans==0) 41 printf("Alice和Bob一样大\n\n"); 42 else if(ans==-1) 43 printf("Alice比Bob大\n\n"); 44 else 45 printf("Alice比Bob小\n\n"); 46 } 47 } 48 49 int main() 50 { 51 printf("测试1:输入日期,打印输出这是一年中第多少天\n"); 52 test1(); 53 54 printf("测试2:两个人年龄大小关系\n"); 55 test2(); 56 } 57 58 void input(date *pd) 59 { 60 scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day); 61 } 62 63 int day_of_year(date d) 64 { 65 int daynum[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 66 int day_count=0; 67 int i; 68 69 if(d.year%4==0&&d.year%100!=0||(d.year%400==0)) 70 { 71 daynum[1]=29; 72 } 73 74 for(i=0;i<d.month-1;++i) 75 { 76 day_count+=daynum[i]; 77 } 78 day_count+=d.day; 79 return day_count; 80 } 81 82 int compare_dates(date d1,date d2) 83 { 84 if(d1.year!=d2.year) 85 return d1.year-d2.year; 86 else if(d1.month!=d2.month) 87 return d1.month-d2.month; 88 else 89 return d1.day-d2.day; 90 }
TASK 6
1 #include<stdio.h> 2 #include<string.h> 3 4 enum role{admin,student,teacher}; 5 6 typedef struct 7 { 8 char username[20]; 9 char password[20]; 10 enum role type; 11 }account; 12 13 void output(account x[],int n); 14 15 int main() 16 { 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 }; 24 int n; 25 n=sizeof(x)/sizeof(account); 26 output(x,n); 27 28 return 0; 29 } 30 31 void output(account x[], int n) 32 { 33 int i,j; 34 for (i = 0; i < n; i++) { 35 printf("%s\t\t", x[i].username); 36 for (j = 1; j < strlen(x[i].password); j++) 37 { 38 printf("*"); 39 } 40 printf("\t\t"); 41 switch (x[i].type) 42 { 43 case admin: 44 printf("admin\n"); 45 break; 46 case student: 47 printf("student\n"); 48 break; 49 case teacher: 50 printf("teacher\n"); 51 break; 52 } 53 } 54 }
TASK 7
1 #include <stdio.h> 2 #include <string.h> 3 4 typedef struct 5 { 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 { 13 int i; 14 for (i = 0; i < n; i++) 15 { 16 if (strcmp(x[i].name, name) == 0) 17 { 18 x[i].vip = 1; 19 break; 20 } 21 } 22 } 23 24 void output(contact x[], int n) 25 { 26 int i; 27 for (i = 0; i < n; i++) 28 { 29 printf("%-19s%-15s", x[i].name, x[i].phone); 30 if (x[i].vip) 31 printf("%5s", "*"); 32 printf("\n"); 33 } 34 } 35 36 void display(contact x[], int n) 37 { 38 int i, j; 39 contact temp; 40 for (i = 0; i < n - 1; i++) 41 { 42 for (j = 0; j < n - 1 - i; j++) 43 { 44 if (x[j + 1].vip && !x[j].vip) 45 { 46 temp = x[j]; 47 x[j] = x[j + 1]; 48 x[j + 1] = temp; 49 } 50 else if (x[j].vip == x[j + 1].vip && strcmp(x[j].name, x[j + 1].name) > 0) 51 { 52 temp = x[j]; 53 x[j] = x[j + 1]; 54 x[j + 1] = temp; 55 } 56 } 57 } 58 output(x, n); 59 } 60 61 #define N 10 62 int main() 63 { 64 contact list[N] = 65 { 66 {"刘一", "15510846604", 0}, 67 {"陈二", "18038747351", 0}, 68 {"张三", "18853253914", 0}, 69 {"李四", "13230584477", 0}, 70 {"王五", "15547571923", 0}, 71 {"赵六", "18856659351", 0}, 72 {"周七", "17705843215", 0}, 73 {"孙八", "15552933732", 0}, 74 {"吴九", "18077702405", 0}, 75 {"郑十", "18820725036", 0} 76 }; 77 int vip_cnt, i; 78 char name[20]; 79 80 printf("显示原始通讯录信息\n"); 81 output(list, N); 82 83 printf("\n输入要设置的紧急联系人个数:"); 84 scanf("%d", &vip_cnt); 85 printf("输入%d个紧急联系人姓名:\n", vip_cnt); 86 for (i = 0; i < vip_cnt; ++i) 87 { 88 scanf("%s", name); 89 set_vip_contact(list, N, name); 90 } 91 92 printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n"); 93 display(list, N); 94 95 return 0; 96 }