其他分享
首页 > 其他分享> > 标题七

标题七

作者:互联网

#include <stdio.h> 
#include <stdlib.h>

#define N 10


typedef struct student {
	int num;
	char name[20];
	int score;
}STU;

int main() {
	STU st, stmax, stmin;
	int i;
	FILE *fp;
	

	fp = fopen("file1.dat", "r");
	if( !fp ) {  
		printf("fail to open file1.dat\n");
		exit(0);
	}
	
	stmax.score = 0;	
	stmin.score = 100;	
	while(!feof(fp)) {
		fscanf(fp, "%d %s %d", &st.num, st.name, &st.score);  
		
		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);

	system("pause");
	return 0;
}

  

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int N = 10;


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); 
	
	system("pause");
	return 0;
} 


void input(STU s[], int n) {
	// 补足代码
	FILE *fin;
	fin=fopen("examinee.txt","r");
	if(fin==NULL){
		printf("fail to open examinee.txt\n");
		exit(0);
	}
	int i;
	for(i=0;i<n;i++)
	fscanf(fin,"%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective);
	fclose(fin); 
}


void output(STU s[], int n) {
	// 补足代码
	int i;
	FILE *fout;
	fout=fopen("result.txt","w");
	if(fout==NULL){
		printf("fail to open result,txt\n");
		exit(0);
	}
	for(i=0;i<n;i++){
		printf("%-9ld %-10s %-13.2f %-10.2f %-7.2f %-8s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
	    fprintf(fout,"%-9ld %-10s %-13.2f %-10.2f %-7.2f %-8s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
	}
	fclose(fout);
}
    


void process(STU s[], int n) {
	// 补足代码 
	int i;
	for(i=0;i<n;i++)
	    s[i].sum=s[i].objective+s[i].subjective;
}
void sort(STU s[],int n) {
	int i,j;
	STU temp;
	for(i=0;i<n-1;i++){
		for(j=0;j<n-1-i;j++)
			if(s[j].sum<s[j+1].sum){
				temp=s[j];
				s[j]=s[j+1];
				s[j+1]=temp; 
			}
	}
}
void proce(STU s[],int n){
	int i;
	strcpy(s[0].level,"优秀");
	for(i=1;0<i<5;i++)
	strcpy(s[i].level,"合格");
	for(i=5;4<i<10;i++)
	strcpy(s[i].level,"不合格");
}

  

二进制文件与文本文件差别:文本文件只能存储字符型常量,二进制文件能存储各种变量。

总结:写程序过程中还是会出现小错误,比如说if(fin==NULL)写成了if(fin=NULL).

标签:stmax,int,stmin,st,STU,score,标题
来源: https://www.cnblogs.com/cyh010430/p/12109543.html