#include <stdio.h> #include <string.h> #define N 5 // 定义结构体类型struct student,并定义STU为其别名 typedef struct student { long no; char name[20]; int score; } STU; // 函数声明 void input(STU s[], int n); int findMinlist(STU s[], STU t[], int n); void output(STU s[], int n); int main() { STU stu[N], minlist[N]; int count; printf("录入%d个学生信息\n", N); input(stu, N); printf("\n统计最低分人数和学生信息...\n"); count = findMinlist(stu, minlist, N); printf("\n一共有%d个最低分,信息如下:\n", count); output(minlist, count); return 0; }// 输入n个学生信息,存放在结构体数组s中 void input(STU s[], int n) { int i; for (i = 0; i < n; i++) scanf("%ld %s %d", &s[i].no, s[i].name, &s[i].score); }// 输出结构体s中n个元素信息 void output(STU s[], int n) { int i; for (i = 0; i < n; i++) printf("%ld %s %d\n", s[i].no, s[i].name, s[i].score); } // 在结构体数组s中,查找最低分学生的记录,将其存入结构体数组t中 // 形参n是结构体数组s中元素个数 // 函数返回最低分的学生人数 int findMinlist(STU s[], STU t[], int n) { int num = 0,i = 0,score = 1e8 ; for(i=0;i<n;++i){ if(s[i].score < score) score = s[i].score; } printf("\n***score=%d***\n",score); for(int i=0;i<n;++i){ if(s[i].score==score){ t[num].score=s[i].score; memcpy(t[num].name,s[i].name,sizeof t[num].name); t[num].no = s[num].no; num ++; } } return num; }
实验二
#include <stdio.h> #include <string.h> #define N 10 // 定义结构体类型struct student,并定义其别名为STU typedef struct student { long int id; //输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 char name[20]; float objective; /*客观题得分*/ float subjective; /*操作题得分*/ float sum; char level[10]; } STU; // 函数声明 void input(STU s[], int n); void output(STU s[], int n); void process(STU s[], int n); int main() { STU stu[N]; printf("录入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分 (<=60)\n", N); input(stu, N); printf("\n对考生信息进行处理: 计算总分,确定等级\n"); process(stu, N); printf("\n打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 \n"); output(stu, N); return 0; }// 录入考生信息:准考证号,姓名,客观题得分,操作题得分 void input(STU s[], int n) { int i=0; for(i=0;i<n;++i){ scanf("%d%s%f%f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective); } } void output(STU s[], int n) { int i=0; printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t等级\n"); for(i=0;i<n;++i){ printf("%d\t%s\t%.2f\t%.2f\t%.2f\t%s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); } } void swap(STU *a,STU *b){ STU tmp; tmp.id = a->id; memcpy(tmp.name,a->name,sizeof tmp.name); tmp.objective = a->objective; tmp.subjective = a->subjective; tmp.sum = a->sum; memcpy(tmp.level,a->level,sizeof tmp.level); a->id = b->id; memcpy(a->name,b->name,sizeof a->name); a->objective = b->objective; a->subjective = b->subjective; a->sum = b->sum; memcpy(a->level,b->level,sizeof a->level); b->id = tmp.id; memcpy(b->name,tmp.name,sizeof tmp.name); b->objective = tmp.objective; b->subjective = tmp.subjective; b->sum = tmp.sum; memcpy(b->level,tmp.level,sizeof tmp.level); } void process(STU s[], int n) { // 补足代码 // ××× int j=0,i=0; double good = 0.1, med = 0.5; char gd[10] = "优秀";char md[10]="合格"; char bd[10]="不合格"; for(i=0;i<n;++i){ s[i].sum = s[i].objective + s[i].subjective; } for(i=0;i<n;++i) for(j=i+1;j<n;++j) if(s[i].sum > s[j].sum){ swap(&s[i],&s[j]); } for(int i=0;i<n;++i){ if((double)(i+1)/n<=good) memcpy(s[i].level,gd,sizeof gd); else if((double)(i+1)/n<=med) memcpy(s[i].level,md,sizeof md); else memcpy(s[i].level,bd,sizeof bd); } }
实验三
#include <stdio.h> #include <string.h> #define N 10 // 定义结构体类型struct student,并定义其别名为STU typedef struct student { long int id; //输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 char name[20]; float objective; /*客观题得分*/ float subjective; /*操作题得分*/ float sum; char level[10]; } STU; // 函数声明 void input(STU s[], int n); void output(STU s[], int n); void process(STU s[], int n); int main() { freopen("examinee.txt", "r", stdin); freopen("list.txt", "w", stdout); STU stu[N]; printf("录入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分 (<=60)\n", N); input(stu, N); printf("\n对考生信息进行处理: 计算总分,确定等级\n"); process(stu, N); printf("\n打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 \n"); output(stu, N); return 0; }// 录入考生信息:准考证号,姓名,客观题得分,操作题得分 void input(STU s[], int n) { // freopen(""); int i=0; for(i=0;i<n;++i){ scanf("%d%s%f%f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective); } } void output(STU s[], int n) { int i=0; printf("准考证号 姓名 客观题得分 操作题得分 总分 等级\n"); for(i=0;i<n;++i){ printf("%d\t %s\t%.2f\t%.2f\t%.2f\t%s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); } } void swap(STU *a,STU *b){ STU tmp; tmp.id = a->id; memcpy(tmp.name,a->name,sizeof tmp.name); tmp.objective = a->objective; tmp.subjective = a->subjective; tmp.sum = a->sum; memcpy(tmp.level,a->level,sizeof tmp.level); a->id = b->id; memcpy(a->name,b->name,sizeof a->name); a->objective = b->objective; a->subjective = b->subjective; a->sum = b->sum; memcpy(a->level,b->level,sizeof a->level); b->id = tmp.id; memcpy(b->name,tmp.name,sizeof tmp.name); b->objective = tmp.objective; b->subjective = tmp.subjective; b->sum = tmp.sum; memcpy(b->level,tmp.level,sizeof tmp.level); } void process(STU s[], int n) { // 补足代码 // ××× int j=0,i=0; double good = 0.1, med = 0.5; char gd[10] = "优秀";char md[10]="合格"; char bd[10]="不合格"; for(i=0;i<n;++i){ s[i].sum = s[i].objective + s[i].subjective; } for(i=0;i<n;++i) for(j=i+1;j<n;++j) if(s[i].sum > s[j].sum){ swap(&s[i],&s[j]); } for(int i=0;i<n;++i){ if((double)(i+1)/n<=good) memcpy(s[i].level,gd,sizeof gd); else if((double)(i+1)/n<=med) memcpy(s[i].level,md,sizeof md); else memcpy(s[i].level,bd,sizeof bd); } }