实验6
task1:
#include<stdio.h> #include<string.h> #include<stdlib.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); system("pause"); 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; } }
task2:
#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 10 #define M 80 typedef struct { char name[M]; char author[M]; }book; int main(){ book x[N]={{"《一九八四》", "乔治.奥威尔"}, {"《美丽新世界》", "赫胥黎"}, {"《昨日的世界》", "斯蒂芬.茨威格"}, {"《万历十五年》", "黄仁宇"}, {"《一只特立独行的猪》", "王小波"}, {"《百年孤独》", "马尔克斯"}, {"《查令十字街84号》", "海莲.汉芙"}, {"《只是孩子》", "帕蒂.史密斯"}, {"《刀锋》", "毛姆"}, {"《沉默的大多数》", "王小波"} }; book *ptr; int i; char author[M]; int found; printf("---------------所有图书信息--------------\n"); for(ptr=x;ptr<x+N;++ptr) printf("%-30s%-30s\n",ptr->name,ptr->author); printf("\n-----------------按照作者查询图书-------------------\n"); printf("输入作者名:"); gets(author); found=0; for(ptr=x;ptr<x+N;++ptr) if(strcmp(ptr->author,author)==0){ found=1; printf("%-30s%-30s\n",ptr->name,ptr->author); } if(!found) printf("暂未收录该作者书籍!\n"); system("pause"); return 0; }
task3_1:
#include<stdio.h> #include<string.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); int main(){ int n; Film *head; head=NULL; printf("输入影片数目:"); scanf("%d",&n); head=insert(head,n); printf("\n所有影片信息如下:\n"); output(head); system("pause"); return 0; } 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 ; } }
task3_2:
#include<stdio.h> #include<string.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); int main(){ int n; Film *head; Film *p; p=(Film *)malloc(sizeof(Film)); p->next=NULL; head=p; printf("输入影片数目:"); scanf("%d",&n); head=insert(head,n); printf("\n所有影片信息如下:\n"); output(head); system("pause"); return 0; } 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->next =p; } return head; } void output(Film *head){ Film *p; p=head->next ; while(p!=NULL){ printf("%-20s %-20s %-20s %d\n",p->name ,p->director ,p->region,p->year ); p=p->next ; } }
task4:
#include<stdio.h> #include<stdlib.h> #define N 10 typedef struct{ char isbn[20]; 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-5327-6082-4", "门将之死", "罗纳德.伦", 42, 51}, {"978-7-308-17047-5", "自由与爱之地:入以色列记", "云也退", 49, 30}, {"978-7-5404-9344-8", "伦敦人", "克莱格泰勒", 68, 27}, {"978-7-5447-5246-6", "软件体的生命周期", "特德姜", 35, 90}, {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49}, {"978-7-5133-5750-0", "主机战争", "布莱克.J.哈里斯", 128, 42}, {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44}, {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42}, {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55}, {"978-7-229-14156-1", "源泉", "安.兰德", 84, 59}}; printf("图书销量排名(按销售册数):\n"); sort(x,N); output(x,N); printf("\n图书销售总额:%.2f\n",sales_amount(x,N)); system("pause"); return 0; } void output(book x[],int n){ book *ptr; printf("%-30s%-30s%-30s%-10s%-10s\n","isbn号","书名","作者","售价","销售册数"); for(ptr=x;ptr<x+N;++ptr) printf("%-30s%-30s%-30s%-10.1lf%-10d\n",ptr->isbn , ptr->name, ptr->author,ptr->sales_price ,ptr->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 sales[N]; double total=0; for(i=0;i<n;i++){ sales[i]=(x[i].sales_count)*(x[i].sales_price); total+=sales[i]; } return total; }
task5:
#include<stdio.h> #include<stdlib.h> typedef struct{ int year; int month; int day; }date; void input(date *pd); int day_of_year(date d); double compare_dates(date d1,date d2); void test1(){ date d; int i; printf("输入日期:\n"); for(i=0;i<3;++i){ input(&d); printf("%d-%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出生日期:\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"); } } int main(){ printf("测试1: 输入日期, 打印输出这是一年中第多少天\n"); test1(); printf("\n测试2: 两个人年龄大小关系\n"); test2(); system("pause"); return 0; } void input(date *pd){ scanf("%d-%d-%d",&pd->year ,&pd->month ,&pd->day ); } int day_of_year(date d){ int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int sum=0; int i; if((d.year %4==0&&d.year %100!=0)||(d.year %400==0)) month[2]=29; for(i=1;i<d.month ;++i) sum+=month[i]; sum+=d.day ; return sum; } double 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; } } }
task6:
#include <stdio.h> #include <string.h> #include<stdlib.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); system("pause"); return 0; } void output(Account x[], int n) { int i,j; for (i = 0; i < n; i++) { printf(" %-15s ", x[i].username); printf("\t"); for ( j = 0; j < strlen(x[i].password); j++) { printf("*"); } for(j=0;j<(15-strlen(x[i].password ));j++) printf(" "); switch (x[i].type) { case admin: printf("admin\n"); break; case student: printf("student\n"); break; case teacher: printf("teacher\n"); break; } } }
task7:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 10 typedef struct{ char name[20]; char phone[12]; int vip; }contact; void output(contact x[],int n); void set_vip_contact(contact x[],int n,char name[]); void display(contact x[],int n); int main(){ contact list[N] = {{"刘一", "15510846604", 0}, {"陈二", "18038747351", 0}, {"张三", "18853253914", 0}, {"李四", "13230584477", 0}, {"王五", "15547571923", 0}, {"赵六", "18856659351", 0}, {"周七", "17705843215", 0}, {"孙八", "15552933732", 0}, {"吴九", "18077702405", 0}, {"郑十", "18820725036", 0}}; int vip_cnt,i; char name[N]; printf("显示原始通讯录信息: \n"); output(list,N); printf("\n输入要设置的紧急联系人个数:"); scanf("%d",&vip_cnt); printf("输入%d个紧急联系人姓名:\n",vip_cnt); for(i=0;i<vip_cnt;i++){ scanf("%s",name); set_vip_contact(list,N,name); } printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n"); display(list,N); system("pause"); return 0; } void output(contact x[],int n){ int i; for(i=0;i<n;i++){ printf("%-10s%-15s",x[i].name,x[i].phone); if(x[i].vip) printf("%5s","*"); printf("\n"); } } void set_vip_contact(contact x[],int n,char name[]){ int i; for(i=0;i<n;i++){ if(strcmp(x[i].name,name)==0 ){ x[i].vip =1; break; } } } void display(contact x[],int n){ int i,j; contact t; for (i = 0; i < n - 1; i++){ for(j=0;j<n-1-i;j++){ if((x[j].vip ==x[j+1].vip &&strcmp(x[j].name ,x[j+1].name )>0)||x[j].vip <x[j+1].vip ){ t=x[j]; x[j]=x[j+1]; x[j+1]=t; } } } output(x,n); }