实验六
实验任务1
程序代码:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.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];//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); 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.0lf %4.0lf %4.0lf %4.1lf %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,k; for(i=0,k=0;i<n;i++){ if(s[i].total<60) t[k++]=s[i]; } return k; } 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[i].total<s[i+1].total){ t=s[i]; s[i]=s[i+1]; s[i+1]=t; } } } }
运行结果:

实验任务2
程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 10 #define M 80 typedef struct{ char name[M]; char author[M]; }Book; int main(){ Book x[N]={{"《一九九四》","乔治.奥威尔"}, {"《美丽新世界》","赫胥黎"}, {"《昨日的世界》","斯蒂芬.茨威格"}, {"《万历十五年》","黄仁宇"}, {"《一只特立独行的猪》","王小波"}, {"《百年孤独》","马尔克斯"}, {"《查令十字街84号》","海莲.汉芙"}, {"《只是孩子》","帕蒂.史密斯"}, {"《刀锋》","毛姆"}, {"《沉默的大多数》","王小波"}}; int i; char name[M]; printf("所有图书信息:\n"); for(i=0;i<N;i++){ printf("%-30s %-20s\n",x[i].name,x[i].author); } printf("输入作者名:"); scanf("%s",name); for(i=0;i<N;i++){ if(strcmp(x[i].author,name)==0) printf("%-30s %-20s\n",x[i].name,x[i].author); } system("pause"); return 0; }
运行结果:

实验任务3
程序代码:
#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; } }
运行结果:

实验任务4
程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 100 typedef struct{ char num[10]; int s1; int s2; double sum; char level[10]; }STU; int fun(STU a[],int n, STU h[]); int main(){ STU s[N]={{"GA05", 85, 76}, {"GA03", 76, 90}, {"GA02", 69, 90}, {"GA04", 85, 56}, {"GA01", 91, 95}, {"GA07", 72, 80}, {"GA08", 64, 45}, {"GA06", 87, 98}, {"GA015", 85, 86}, {"GA013", 91, 97} }; STU h[N]; int i, k, n = 10; k = fun(s, n, h); printf("There are :\n"); for(i = 0; i < k; i++) printf("%s %d %d %.2f %s\n", h[i].num, h[i].s1, h[i].s2, h[i].sum, h[i].level); system("pause"); return 0; } int fun (STU a[], int n, STU h[]) { int i,j; double sum_=0.0,pj; for(i=0;i<n;i++) a[i].sum=0.3*a[i].s2+0.7*a[i].s1; for(i=0;i<n;i++) sum_+=a[i].sum; pj=sum_/(n*1.0); for(i=0,j=0;i<n;i++) if(a[i].sum>pj){ strcpy(a[i].level,"均分以上"); h[j++]=a[i]; } return --j; }
运行结果:

实验任务5
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 5
typedef struct student {
char name[10];
int num;
int maths;
int computer;
int english;
int sum;
char level[10];
} STU;
void fun(STU a[], int n);
int main() {
STU s[6*N]={ {"A001", 1, 34, 67, 80},
{"B003", 3, 78, 87, 90},
{"A002", 2, 90, 98, 99},
{"B002", 4, 56, 78, 98},
{"A005", 5, 35, 67, 79} };
int i;
fun(s, N);
for(i = 0; i < N; i++)
printf("%s %d %d %d %d %d %s\n", s[i].name, s[i].num, s[i].maths,
s[i].computer, s[i].english, s[i].sum, s[i].level);
system("pause");
return 0;
}
void fun(STU a[], int n) {
int i,max,min;
for(i=0;i<n;i++)
a[i].sum=a[i].maths+a[i].computer+a[i].english;
for(i=0,max=a[0].sum,min=a[0].sum;i<n-1;i++){
if(a[i+1].sum>max)
max=a[i+1].sum;
if(a[i+1].sum<min)
min=a[i+1].sum;
}
for(i=0;i<n;i++){
if(a[i].sum==max)
strcpy(a[i].level,"优秀");
else{
if(a[i].sum==min)
strcpy(a[i].level,"不及格");
else
strcpy(a[i].level,"合格");
}
}
}
运行结果:

实验任务6
程序代码:
#include <stdio.h> #include <stdlib.h> #define N 5 typedef struct student { long no; char name[20]; int score; } STU; void input(STU s[], int n); int find_min_list(STU s[], STU t[], int n); void output(STU s[], int n); int main() { STU stu[N], min_list[N]; int count; printf("录入%d个学生信息\n", N); input(stu, N); printf("\n统计最低分人数和学生信息...\n"); count = find_min_list(stu, min_list, N); printf("\n一共有%d个最低分,信息如下:\n", count); output(min_list, count); system("pause"); return 0; } void input(STU s[], int n) { int i; for(i=0;i<n;i++) scanf("%d %s %d",&s[i].no,s[i].name,&s[i].score); } void output(STU s[], int n) { int i; for(i=0;i<n;i++) printf("%d %s %d\n",s[i].no,s[i].name,s[i].score); } int find_min_list(STU s[], STU t[], int n) { int i,j,k; for(i=0,j=s[0].score;i<n;i++) if(s[i].score<j) j=s[i].score; for(i=0,k=0;i<n;i++) if(s[i].score==j) t[k++]=s[i]; return k; }
运行结果:


浙公网安备 33010602011771号