1. 程式人生 > 實用技巧 >實驗七 檔案

實驗七 檔案

實驗任務3

1、螢幕上正確輸出了按分數由高到低排序的資訊

2、生成了檔案file3.dat 且資料資訊直觀可讀

實驗任務4

子任務1

1、螢幕上正確輸出了按分數由高到低排序的學生資訊

2、生成了檔案file4.dat 但是裡面的資料是亂碼?????

子任務2

#include<stdio.h>
#include<stdlib.h>
#define N 10

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

int main(){
	FILE *fin;
	STU st[N];
	int i;
	fin=fopen("file4.dat","rb");
	if(!fin){
		printf("fial to open file4.dat\n");
		exit(0);
	}
	fread(st,sizeof(STU),N,fin);
	fclose(fin);
	for(i=0;i<N;i++)
		printf("%-6d%-10s%3d\n",st[i].num,st[i].name,st[i].score);
	return 0;
}

實驗任務5

#include <stdio.h>
#include<stdlib.h>
#include <string.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;
} 

// 從文字檔案examinee.txt讀入考生資訊:准考證號,姓名,客觀題得分,操作題得分
void input(STU s[], int n) {
	 FILE *fin;
	 int i;
	 fin=fopen("examinee.txt","r");
	 if(!fin){
	 	printf("fail to open examee.txt\n");
	 	exit(0);
	 } 
	 for(i=0;i<N;i++)
		fscanf(fin,"%d%s%f%f",&s[i].id,&s[i].name,&s[i].objective,&s[i].subjective);
	fclose(fin);
}

// 輸出考生完整資訊: 准考證號,姓名,客觀題得分,操作題得分,總分,等級
// 不僅輸出到螢幕上,還寫到文字檔案result.txt中 
void output(STU s[], int n) {
	FILE *fout;
	int i;
	fout=fopen("result.txt","w");
	if(!fout){
		printf("fail to open result.txt\n");
		exit(0);
	}	
	for(i=0;i<N;i++){
		printf("%-8d%-10s%-8.2f%-8.2f%-8.2f%s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
		fprintf(fout,"%-8d%-10s%-8.2f%-8.2f%-8.2f%s\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) {
	STU t;
	int i,j;
	for(i=0;i<n;i++){
		s[i].sum=s[i].objective+s[i].subjective;
	}
	for(i=0;i<n-1;i++){
		for(j=i+1;j<n;j++){
			if(s[i].sum<s[j].sum){
				t=s[i];
				s[i]=s[j];
				s[j]=t;
			}
		}
	}
	for(i=0;i<n;i++){
		if(i<=n*0.1)
			strcpy(s[i].level,"優秀");
		else if(i>n*0.1&&i<=n*0.5)
			strcpy(s[i].level,"合格");
		else
			strcpy(s[i].level,"不合格"); 
	}
}

實驗總結

1、標頭檔案真的要注意,實驗5光注意補足程式碼,然後因為用到了exit(0),又沒有加上標頭檔案,後來才發現

2、不得不說,檔案真的方便好多/淚目.jpg

BB:

C語言走到了尾聲才突然驚覺真的是結束了,之前寫實驗寫到罵罵咧咧也終究是結束了。

對於這門課程學的也不是很紮實,算是比較大的遺憾吧,有些點它單獨講,哎我懂了,可一旦要綜合運用,就有很多問題了。

有些時候也不是不想問問題,只是都不知道是什麼問題導致我覺得某個點很難理解,這個實驗很難……

感觸就是:也許學好一門課程不一定要熱愛,但興趣決定卻能夠幫助更好地學習,而興趣又是從一次又一次小小的成功中積累起來,一切的一切需要從頭開始就不落 下。不管學習什麼,難在開頭,貴在堅持。

慶幸的是,我總歸在這一學期的學習中學到了一些知識之外的理兒。

最後,感謝C語言,讓我認識到不一樣的電腦世界。