实验6
实验任务1
源代码
#include <stdio.h> #include <string.h> #define N 3 typedef struct student { int id; char name[20]; char subject[20]; double perf; double mid; double final; double total; char level[10]; } STU; void input(STU [], int); void output(STU [], int); void calc(STU [], int); int fail(STU [], STU [], int); void sort(STU [], int); int main() { STU st[N], fst[N]; int k; printf("录入学生成绩信息:\n"); input(st, N); printf("\n成绩处理...\n"); calc(st, N); k = fail(st, fst, N); sort(st, N); printf("\n学生成绩排名情况:\n"); output(st, N); printf("\n不及格学生信息:\n"); output(fst, k); return 0; } void input(STU s[], int n) { int i; for(i = 0; i < n; i++) 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); } void output(STU s[], int n) { int i; printf("-----------------\n"); printf("学号 姓名 科目 平时 期中 期末 总评 等级\n"); for(i = 0; i<n; i++) 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); } void calc(STU s[],int n) { int i; for(i = 0; i < n; i++) { s[i].total = s[i].perf * 0.2 + s[i].mid * 0.2 + s[i].final * 0.6; if(s[i].total >= 90) strcpy(s[i].level, "优"); else if(s[i].total >= 80) strcpy(s[i].level, "良"); else if(s[i].total >= 70) strcpy(s[i].level, "中"); else if(s[i].total >= 60) strcpy(s[i].level, "及格"); else strcpy(s[i].level, "不及格"); } } int fail(STU s[], STU t[], int n) { int i, cnt = 0; for(i = 0; i < n; i++) if(s[i].total < 60) t[cnt++] = s[i]; return cnt; } void sort(STU s[], int n) { int i, j; STU t; for(i = 0; i < n-1; i++) for(j = 0; j < n-1-i; j++) if(s[j].total < s[j+1].total) { t = s[j]; s[j] = s[j+1]; s[j+1] = t; } }
运行截图

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


实验任务3.1
源代码
#include <stdlib.h> #include <stdio.h> #include <stdlib.h> #define N 80 typedef struct FilmInfo { char name[N]; char director[N]; char region[N]; int year; struct FilmInfo *next; } Film; void output(Film *head); // 遍历输出链表信息 Film *insert(Film *head, int n); // 向链表中插入n个结点,返回头指针 int main() { int n; // 结点数 Film *head; // 头指针变量,存放链表中第一个节点的地址 head = NULL; printf("输入影片数目: "); scanf("%d", &n); // 向链表中插入n部影片信息 head = insert(head, n); // 遍历输出链表中所有影片信息 printf("\n所有影片信息如下: \n"); output(head); system("pause"); return 0; } // 向链表中插入n个结点,从表头插入,返回头指针变量 Film *insert(Film *head, int n) { int i; Film *p; for(i = 1; i <= n; ++i) { p = (Film *)malloc(sizeof(Film)); printf("请输入第%d部影片信息: ", i); scanf("%s %s %s %d", p->name, p->director, p->region, &p->year); // 把结点从表头插入到链表中 p->next = head; head = p; // 更新头指针变量 } return head; } // 遍历输出链表信息 void output(Film *head) { Film *p; p = head; while(p != NULL) { printf("%-20s %-20s %-20s %d\n", p->name, p->director, p->region, p->year); p = p -> next; } }

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

实验任务4
源代码
1 #include <stdlib.h> 2 #include <stdio.h> 3 #define N 10 4 5 typedef struct { 6 char isbn[20]; // isbn号 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 Book x[N] = {{"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-0", "主机战争", "布莱克.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 printf("图书销量排名(按销售册数): \n"); 30 sort(x, N); 31 output(x, N); 32 33 printf("\n图书销售总额: %.2f\n", sales_amount(x, N)); 34 system("pause"); 35 return 0; 36 } 37 38 void output(Book x[], int n) { 39 printf("%-22s %-30s %-20s %-6s %s\n", "ISBN号", "书名", "作者", "售价", "销售册数"); 40 for (int i = 0; i < n; i++) { 41 printf("%-22s %-30s %-20s %-6.1f %d\n", 42 x[i].isbn, x[i].name, x[i].author, x[i].sales_price, x[i].sales_count); 43 } 44 } 45 46 47 void sort(Book x[], int n) { 48 Book temp; 49 for (int i = 0; i < n - 1; i++) { 50 for (int j = 0; j < n - 1 - i; j++) { 51 if (x[j].sales_count < x[j+1].sales_count) { 52 temp = x[j]; 53 x[j] = x[j+1]; 54 x[j+1] = temp; 55 } 56 } 57 } 58 } 59 60 double sales_amount(Book x[], int n) { 61 double total = 0.0; 62 for (int i = 0; i < n; i++) { 63 total += x[i].sales_price * x[i].sales_count; 64 } 65 return total; 66 }
运行截图

实验任务5
源代码
1 #include <stdlib.h> 2 #include <stdio.h> 3 4 typedef struct { 5 int year; 6 int month; 7 int day; 8 } Date; 9 10 // 函数声明 11 void input(Date *pd); // 输入日期给pd指向的Date变量 12 int day_of_year(Date d); // 返回日期d是这一年的第多少天 13 int compare_dates(Date d1, Date d2); // 比较两个日期: 14 // 如果d1在d2之前,返回-1; 15 // 如果d1在d2之后,返回1 16 // 如果d1和d2相同,返回0 17 18 void test1() { 19 Date d; 20 int i; 21 22 printf("输入日期:(以形如2025-12-19这样的形式输入)\n"); 23 for(i = 0; i < 3; ++i) { 24 input(&d); 25 printf("%d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d)); 26 } 27 } 28 29 void test2() { 30 Date Alice_birth, Bob_birth; 31 int i; 32 int ans; 33 34 printf("输入Alice和Bob出生日期:(以形如2025-12-19这样的形式输入)\n"); 35 for(i = 0; i < 3; ++i) { 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 printf("测试1: 输入日期, 打印输出这是一年中第多少天\n"); 51 test1(); 52 53 printf("\n测试2: 两个人年龄大小关系\n"); 54 test2(); 55 } 56 57 58 void input(Date *pd) { 59 60 scanf("%d-%d-%d", &pd->year, &pd->month, &pd->day); 61 } 62 63 64 int is_leap(int year) { 65 66 if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) 67 return 1; 68 return 0; 69 } 70 71 72 int day_of_year(Date d) { 73 74 int month_days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; 75 int total = 0; 76 77 for(int i = 0; i < d.month - 1; i++) { 78 total += month_days[i]; 79 } 80 total += d.day; 81 82 if(is_leap(d.year) && d.month > 2) { 83 total += 1; 84 } 85 return total; 86 } 87 88 int compare_dates(Date d1, Date d2) { 89 // 先比年份 90 if(d1.year < d2.year) 91 return -1; 92 if(d1.year > d2.year) 93 return 1; 94 // 年份相等比月份 95 if(d1.month < d2.month) 96 return -1; 97 if(d1.month > d2.month) 98 return 1; 99 // 月份相等比日期 100 if(d1.day < d2.day) 101 return -1; 102 if(d1.day > d2.day) 103 return 1; 104 // 年月日全部相等 105 system("pause"); 106 return 0; 107 }
运行截图

实验任务6
源代码
1 #include <stdio.h> 2 #include <string.h> 3 #include<stdlib.h> 4 5 enum Role {admin, student, teacher}; 6 7 typedef struct { 8 char username[20]; // 用户名 9 char password[20]; // 密码 10 enum Role type; // 账户类型 11 } Account; 12 13 14 // 函数声明 15 void output(Account x[], int n); // 输出账户数组x中n个账户信息,其中,密码用*替代显示 16 17 int main() { 18 Account x[] = {{"A1001", "123456", student}, 19 {"A1002", "123abcdef", student}, 20 {"A1009", "xyz12121", student}, 21 {"X1009", "9213071x", admin}, 22 {"C11553", "129dfg32k", teacher}, 23 {"X3005", "921kfmg917", student}}; 24 int n; 25 n = sizeof(x)/sizeof(Account); 26 output(x, n); 27 system("pause"); 28 return 0; 29 } 30 31 void output(Account x[], int n) { 32 for(int i = 0; i < n; i++) { 33 printf("%-10s", x[i].username); 34 35 int pwd_len = strlen(x[i].password); 36 for(int j = 0; j < pwd_len; j++) { 37 printf("*"); 38 } 39 printf("\t"); 40 switch(x[i].type) { 41 case admin: 42 printf("admin"); 43 break; 44 case student: 45 printf("student"); 46 break; 47 case teacher: 48 printf("teacher"); 49 break; 50 } 51 printf("\n"); 52 } 53 }
运行截图

实验任务7
源代码
1 #include<stdlib.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 typedef struct { 6 char name[20]; // 姓名 7 char phone[12]; // 手机号 8 int vip; // 是否为紧急联系人,是取1;否则取0 9 } Contact; 10 11 12 // 函数声明 13 void set_vip_contact(Contact x[], int n, char name[]); // 设置紧急联系人 14 void output(Contact x[], int n); // 输出x中联系人信息 15 void display(Contact x[], int n); // 按联系人姓名字典序升序显示信息,紧急联系人最先显示 16 17 18 #define N 10 19 int main() { 20 Contact list[N] = {{"刘一", "15510846604", 0}, 21 {"陈二", "18038747351", 0}, 22 {"张三", "18853253914", 0}, 23 {"李四", "13230584477", 0}, 24 {"王五", "15547571923", 0}, 25 {"赵六", "18856659351", 0}, 26 {"周七", "17705843215", 0}, 27 {"孙八", "15552933732", 0}, 28 {"吴九", "18077702405", 0}, 29 {"郑十", "18820725036", 0}}; 30 int vip_cnt, i; 31 char name[20]; 32 33 printf("显示原始通讯录信息: \n"); 34 output(list, N); 35 36 printf("\n输入要设置的紧急联系人个数: "); 37 scanf("%d", &vip_cnt); 38 39 printf("输入%d个紧急联系人姓名:\n", vip_cnt); 40 for(i = 0; i < vip_cnt; ++i) { 41 scanf("%s", name); 42 set_vip_contact(list, N, name); 43 } 44 45 printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n"); 46 display(list, N); 47 system("pause"); 48 return 0; 49 } 50 51 // 补足函数set_vip_contact实现 52 // 功能:将联系人数组x中,联系人姓名与name一样的人,设置为紧急联系人(即成员vip值设为1) 53 void set_vip_contact(Contact x[], int n, char name[]) { 54 for(int i = 0; i < n; i++) { 55 if(strcmp(x[i].name, name) == 0) { 56 x[i].vip = 1; 57 } 58 } 59 } 60 61 void output(Contact x[], int n) { 62 int i; 63 for(i = 0; i < n; ++i) { 64 printf("%-10s%-15s", x[i].name, x[i].phone); 65 if(x[i].vip) 66 printf("%5s", "*"); 67 printf("\n"); 68 } 69 } 70 71 void swap_contact(Contact *a, Contact *b) { 72 Contact temp = *a; 73 *a = *b; 74 *b = temp; 75 } 76 77 // 补足函数display实现 78 // 功能:显示联系人数组x中的联系人信息 79 // 按姓名字典序升序显示,紧急联系人显示在最前面 80 void display(Contact x[], int n) { 81 Contact temp_list[N]; 82 memcpy(temp_list, x, sizeof(Contact) * n); 83 84 for(int i = 0; i < n - 1; i++) { 85 for(int j = 0; j < n - 1 - i; j++) { 86 if(temp_list[j].vip == 0 && temp_list[j+1].vip == 1) { 87 swap_contact(&temp_list[j], &temp_list[j+1]); 88 } 89 else if(temp_list[j].vip == temp_list[j+1].vip 90 && strcmp(temp_list[j].name, temp_list[j+1].name) > 0) { 91 swap_contact(&temp_list[j], &temp_list[j+1]); 92 } 93 } 94 } 95 96 output(temp_list, n); 97 }
运行截图

浙公网安备 33010602011771号