实验6
demo1
#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;//创建实例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]; //st用于记录学生信息,fst记录不及格学生信息 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; } }
demo2
#include <stdio.h> #include <string.h> // 用于 strcpy 和其他字符串函数 #define N 10 typedef struct { char isbn[20]; // ISBN编号 char name[100]; // 书名 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-5477-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-1105-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)); // 输出总销售额 return 0; } double sales_amount(Book x[], int n) { double ans = 0; int i; for (i = 0; i < n; i++) { ans += x[i].sales_price * x[i].sales_count; // 计算销售总额 } return ans; } void output(Book x[], int n) { int i; printf("ISBN号 书名 作者 售价 销售册数\n"); for (i = 0; i < n; i++) { printf("%-20s%-30s%-20s%-10.2f%-10d\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; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { // 按销售册数排序 if (x[j].sales_count < x[j + 1].sales_count) { Book temp={"978-7-1105-0509-5", "无穷的开始:世界进步的本源", "戴维.多伊奇", 37.5, 55}; temp=x[j]; x[j]=x[j+1]; x[j+1]=temp; } } } }
demo3
#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); 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; } }
demo4
#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(){ Date d; int i; printf("输入日期:(以形如2024-12-16这样的形式输入)\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的出生日期:(以形如2024-12-16这样的形式输入)\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(); } void input(Date *pd){ scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day); } int day_of_year(Date d){ int n; if(d.year%4==0){ //润年 if(d.month==1) n=d.day; else if(d.month==2) n=d.day+31; else if(d.month==3) n=d.day+60; else if(d.month==4) n=d.day+91; else if(d.month==5) n=d.day+121; else if(d.month==6) n=d.day+152; else if(d.month==7) n=d.day+182; else if(d.month==8) n=d.day+213; else if(d.month==9) n=d.day+244; else if(d.month==10) n=d.day+274; else if(d.month==11) n=d.day+305; else if(d.month==12) n=d.day+335; } else if(d.year%4!=0){ if(d.month==1) n=d.day; else if(d.month==2) n=d.day+31; else if(d.month==3) n=d.day+59; else if(d.month==4) n=d.day+90; else if(d.month==5) n=d.day+120; else if(d.month==6) n=d.day+151; else if(d.month==7) n=d.day+181; else if(d.month==8) n=d.day+212; else if(d.month==9) n=d.day+243; else if(d.month==10) n=d.day+273; else if(d.month==11) n=d.day+304; else if(d.month==12) n=d.day+334; } return n; } 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.year==d2.year){ if(day_of_year(d1)>day_of_year(d2)) return 1; if(day_of_year(d1)<day_of_year(d2)) return -1; if(day_of_year(d1)==day_of_year(d2)) return 0; }
demo5
#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(){ Date d; int i; printf("输入日期:(以形如2024-12-16这样的形式输入)\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的出生日期:(以形如2024-12-16这样的形式输入)\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(); } void input(Date *pd){ scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day); } int day_of_year(Date d){ int n; if(d.year%4==0){ //润年 if(d.month==1) n=d.day; else if(d.month==2) n=d.day+31; else if(d.month==3) n=d.day+60; else if(d.month==4) n=d.day+91; else if(d.month==5) n=d.day+121; else if(d.month==6) n=d.day+152; else if(d.month==7) n=d.day+182; else if(d.month==8) n=d.day+213; else if(d.month==9) n=d.day+244; else if(d.month==10) n=d.day+274; else if(d.month==11) n=d.day+305; else if(d.month==12) n=d.day+335; } else if(d.year%4!=0){ if(d.month==1) n=d.day; else if(d.month==2) n=d.day+31; else if(d.month==3) n=d.day+59; else if(d.month==4) n=d.day+90; else if(d.month==5) n=d.day+120; else if(d.month==6) n=d.day+151; else if(d.month==7) n=d.day+181; else if(d.month==8) n=d.day+212; else if(d.month==9) n=d.day+243; else if(d.month==10) n=d.day+273; else if(d.month==11) n=d.day+304; else if(d.month==12) n=d.day+334; } return n; } 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.year==d2.year){ if(day_of_year(d1)>day_of_year(d2)) return 1; if(day_of_year(d1)<day_of_year(d2)) return -1; if(day_of_year(d1)==day_of_year(d2)) return 0; }
demo6
#include <stdio.h> #include <string.h> enum Role {admin,student,teacher}; typedef struct{ char username[20]; char password[20]; enum Role type; //枚举类型enum是c语言中一种用户定义的数据类型,它可以将一组相关的常量用一个名字表示,枚举类型实际上是为一组整数定义了别名,使得这些值更加有意义 }Account; //函数申明 void output(Account x[],int n); //输出账户数组x中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; int w[100]; for(i=0;i<n;i++){ w[i]=strlen(x[i].password); } struct Star{ char star[30]; char name[10]; }; struct Star m[n]; for(i=0;i<n;i++){ for(j=0;j<w[i];j++) { m[i].star[j]='*';} m[i].star[j]='\0'; } char admin[]="admin"; char teacher[]="teacher"; char student[]="student"; for (i = 0; i < n; i++) { enum Role r = x[i].type; // 获取账户角色 switch (r) { case 0: strcpy(m[i].name, "admin"); // 复制角色名称 break; case 1: strcpy(m[i].name, "student"); break; case 2: strcpy(m[i].name, "teacher"); break; default: strcpy(m[i].name, "unknown"); } printf("%-30s%-20s%-20s\n",x[i].username,m[i].star,m[i].name ); } }
demo7
#include <stdio.h> #include <string.h> typedef struct{ char name[20];//姓名 char name[]="小明"; char phone[12]; int vip; }Contact; //函数声明 void set_vip_contact(Contact x[],int n, char name[]); void output(Contact x[],int n); void display(Contact x[],int n); #define N 10 int main(){ Contact list[N]={ {"刘一","15510846604",0}, {"陈二","18038747351",0}, {"张三","18853253914",0}, {"李四","13230584477",0}, {"王五","15547571923",0}, {"赵六","18856659351",0}, {"周七","17705843215",0}, {"孙八","14442933732",0}, {"吴九","18077702405",0}, {"郑十","18820725036",0} }; int vip_cnt,i; char name[20]; 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); return 0; } 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; } } } void display(Contact x[],int n){ int j=0; int i; Contact au; for(i=0;i<n;i++){ if(x[i].vip==1){ au=x[i]; x[i]=x[j]; x[j]=au; j=j+1; } } int m; for(i=0;i<j-1;i++){ for(m=0;m<j-i-1;m++) if (strcmp(x[m].name,x[m+1].name)>0){ au=x[m]; x[m]=x[m+1]; x[m+1]=au; } } m=0; for(i=j;i<n-1;i++){ for(m=j;m<n-i+j-1;m++) if(strcmp(x[m].name,x[m+1].name)>0){ au=x[m]; x[m]=x[m+1]; x[m+1]=au; } } output(x,n); } 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==1) printf("%5s","*"); printf("\n"); } }