标签:span 打开文件 alt py函数 结构 sub output void inf
PART1
#include <stdio.h> #include <stdlib.h> #define N 10 // 定义一个结构体类型STU typedef struct student { int num; char name[20]; int score; }STU; int main() { STU st, stmax, stmin; int i; FILE *fp; // 以只读文本方式打开文件file1.dat fp = fopen("file1.dat", "r"); if( !fp ) { // 如果打开失败,则输出错误提示信息,然后退出程序 printf("fail to open file1.dat\n"); exit(0); } stmax.score = 0; // 先假定最高分是0,后面如发现比当前最高分还高的分数,就更新最高分 stmin.score = 100; // 先假定最低分是100分,后面如发现比当前最低分更低的分数,就更新最低分 for(i=0; i<N; i++) { fscanf(fp, "%d %s %d", &st.num, st.name, &st.score); // 从fp指定的文件中格式化读取一个学生信息 if(st.score > stmax.score) stmax = st; else if(st.score < stmin.score) stmin = st; } fclose(fp); printf("最高分学生信息: %5d%15s%5d\n", stmax.num, stmax.name, stmax.score); printf("最低分学生信息: %5d%15s%5d\n", stmin.num, stmin.name, stmin.score); return 0; }
#include <stdio.h> #include <stdlib.h> #define N 10 // 定义一个结构体类型STU typedef struct student { int num; char name[20]; int score; }STU; int main() { STU st, stmax, stmin; int i; FILE *fp; // 以只读文本方式打开文件file1.dat fp = fopen("file1.dat", "r"); if( !fp ) { // 如果打开失败,则输出错误提示信息,然后退出程序 printf("fail to open file1.dat\n"); exit(0); } stmax.score = 0; // 先假定最高分是0,后面如发现比当前最高分还高的分数,就更新最高分 stmin.score = 100; // 先假定最低分是100分,后面如发现比当前最低分更低的分数,就更新最低分 while( !feof(fp) ) { fscanf(fp, "%d %s %d", &st.num, st.name, &st.score); // 从fp指定的文件中格式化读取一个学生信息 if(st.score > stmax.score) stmax = st; else if(st.score < stmin.score) stmin = st; } fclose(fp); printf("最高分学生信息: %5d%15s%5d\n", stmax.num, stmax.name, stmax.score); printf("最低分学生信息: %5d%15s%5d\n", stmin.num, stmin.name, stmin.score); return 0; }
PART2
#include <stdio.h> #include <string.h> #include <stdlib.h> const int 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) { FILE *fp; fp=fopen("examinee.txt","r"); if(fp==NULL) { printf("not open\n"); exit(0); } int i; for(i=0;i<n;i++){ fscanf(fp,"%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective); } } //输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 void output(STU s[], int n) { FILE *fp1; fp1=fopen("result.txt","w"); if(fp1==NULL) { printf("not open\n"); exit(0); } int i; for(i=0;i<n;i++){ printf("%ld %s %f %f %f %s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); fprintf(fp1,"%ld %s %f %f %f %s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); /*读入文件result.txt中*/ } fclose(fp1); } // 对考生信息进行处理:计算总分,排序,确定等级 void process(STU s[], int n) { // 补足代码 // ××× int i,j; STU t; for(i=0;i<n;i++){ s[i].sum=s[i].objective+s[i].subjective; /*计算总分,总分=客观题分+操作题分*/ } for(i=0;i<n-1;i++){ /*冒泡排序法排序,从高分到低分*/ for(j=0;j<n-1-j;j++){ if(s[j].sum<s[j+1].sum){ t=s[j]; s[j]=s[j+1]; s[j+1]=t; } } } strcpy(s[0].level,"优秀" ); /*判断等级,用strcpy函数*/ for(i=1;i<=4;i++){ strcpy(s[i].level,"合格"); } for(i=5;i<=9;i++){ strcpy(s[i].level,"不合格"); } }
这门课就这样结束了,考试加油。
标签:span 打开文件 alt py函数 结构 sub output void inf
原文地址:https://www.cnblogs.com/333y/p/11080094.html