实验六
task 4
#include <stdio.h> #define N 10 typedef struct { char isbn[20]; // isbn号 char name[80]; // 书名 char author[80]; // 作者 double sales_price; // 售价 int sales_count; // 销售册数 } Book; void output(Book x[], int n); void sort(Book x[], int n); double sales_amount(Book x[], int n); int main() { Book x[N] = {{"978-7-229-14156-1", "源泉", "安.兰德", 84, 59}, {"978-7-5133-5261-1", "李白来到旧金山", "谭夏阳", 48, 16}, {"978-7-5617-4347-8", "陌生人日记", "周怡芳", 72.6, 27}, {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49}, {"978-7-5046-9568-0", "数据化决策", "道格拉斯·W·哈伯德", 49, 42}, {"978-7-5133-4388-6", "美好时代的背后", "凯瑟琳.布", 34.5, 39}, {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55}, {"978-7-5321-5691-7", "何为良好生活", "陈嘉映", 29.5 , 31}, {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42}, {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44}}; printf("图书销量排名: \n"); sort(x, N); output(x, N); printf("\n图书销售总额: %.2f\n", sales_amount(x, N)); return 0; } void output(Book x[], int n){ int i; printf("ISBN 书名 作者 售价 销售册数\n"); for(i=0;i<n;i++){ printf("%-13s\t %-30s\t %-28s\t %-10.1lf\t %-4d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count); } } void sort(Book x[], int n){ int i, j; Book t; for(i = 0; i < n-1; i++){ for(j = 0; j < n-1-i; j++){ if(x[j].sales_count < x[j+1].sales_count) { t = x[j]; x[j] = x[j+1]; x[j+1]= t;}}} } double sales_amount(Book x[], int n){ int i; double sum=0.0; for(i=0;i<n;i++){ sum=sum+x[i].sales_count*x[i].sales_price;} return sum; }
task 5
#include <stdio.h> typedef struct { int year; int month; int day; } Date; void input(Date *pd); int day_of_year(Date d); int compare_dates(Date d1, Date d2); void test1(); void test2(); int main() { printf("测试1: 输入日期, 打印输出这是一年中第多少天\n"); test1(); printf("\n测试2: 两个人年龄大小关系\n"); test2(); } void test1() { Date d; int i; printf("输入日期:(以形如2024-06-01这样的形式输入)\n"); for(i = 0; i < 3; ++i) { input(&d); printf("%04d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d)); } } void test2() { Date Alice_birth, Bob_birth; int i; int ans; printf("输入Alice和Bob出生日期:(以形如2005-08-11这样的形式输入)\n"); for(i = 0; i < 3; ++i) { input(&Alice_birth); input(&Bob_birth); ans = compare_dates(Alice_birth, Bob_birth); if(ans == 0) printf("Alice和Bob一样大\n\n"); else if(ans == -1) printf("Alice比Bob大\n\n"); else printf("Alice比Bob小\n\n"); } } void input(Date *pd) { scanf("%d-%d-%d", &pd->year, &pd->month, &pd->day); } int day_of_year(Date d) { int i,dath=0; for(i=1;i<d.month;i++){ if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){ dath+=31; } else if(i==2) {if((d.year%4==0&&d.year%100!=0)||d.year%400==0) dath+=29; else dath+=28; } else dath+=30; } dath+=d.day; return dath; } int compare_dates(Date d1, Date d2) { if(d1.year>d2.year) return 1; else if(d1.year<d2.year) return -1; else {if(d1.month>d2.month) return 1; else if(d1.month<d2.month) return -1; else {if(d1.day>d2.day) return 1; else if(d1.day<d2.day) return -1; else return 0; } } }
task 6
#include <stdio.h> #include <string.h> enum Role {admin, student, teacher}; typedef struct { char username[20]; char password[20]; enum Role type; } Account; void output(Account x[], int n); int main() { Account x[] = {{"A1001", "123456", student}, {"A1002", "123abcdef", student}, {"A1009", "xyz12121", student}, {"X1009", "9213071x", admin}, {"C11553", "129dfg32k", teacher}, {"X3005", "921kfmg917", student}}; int n; n = sizeof(x)/sizeof(Account); output(x, n); return 0; } void output(Account x[], int n) { int i,j; for (i = 0; i < n; i++) { printf("%s\t", x[i].username); for (j = 0; j < strlen(x[i].password); j++) { printf("*"); } printf(" \t"); switch (x[i].type) { case admin: printf("admin\n"); break; case student: printf("student\n"); break; case teacher: printf("teacher\n"); break; } } }