Part1: 驗證性實驗 編程
驗證性實驗2數組
若是事先不知道學生人數,函數
改變後的代碼以下:spa
// 從文本文件file1.dat中讀取數據,找出最高分和最低分學生信息,並輸出在屏幕上 #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; }
運行結果爲:3d
二進制文件與文本文件的區別:code
驗證性實驗3程序源碼及運行結果:blog
fout = fopen("file3.dat", "w");
驗證性實驗4程序源碼及運行結果:排序
fout = fopen("file4.dat", "wb");
結合實驗三和實驗四可知:input
以寫的方式打開或建立二進制文件爲:「wb」;源碼
以寫的方式打開或建立文本文件爲「w」。
文本文件儲存量大,速度慢,但直觀,便於對字符操做;
而二進制文件存儲量小、速度快、便於存放中間結果,但不直觀。
寫一個簡單的程序,嘗試從二進制文件file4.dat中讀出數據,並在屏幕上顯示,以此查看文件file4.dat的內 容。給出這個程序源碼和運行截圖:
#include <stdio.h> #include <stdlib.h> #define N 10 typedef struct student { int num; char name[20]; int score; }STU; int main(){ int i; STU st[N]; FILE *fout; fout = fopen("file4.dat", "rb"); if( !fout ) { // 若是打開失敗,則輸出錯誤提示信息,而後退出程序 printf("fail to open file4.dat\n"); exit(0); } // 從fout指向的數據文件file4.dat中讀取數據到結構體數組st for(i=0; i<N; i++) { fread(&st[i],sizeof(STU),1,fout); printf("%-6d%-10s%3d\n", st[i].num, st[i].name, st[i].score); } fclose(fout); // 關閉fout指向的文件file4.dat return 0; }
Part2: 編程練習
代碼以下:
#include <stdio.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) { int i; FILE *fp; fp=fopen("examinee.txt","r") ; for(i=0;i<N;i++) { fscanf(fp,"%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective); } fclose(fp); } // 輸出考生完整信息: 准考證號,姓名,客觀題得分,操做題得分,總分,等級 // 不只輸出到屏幕上,還寫到文本文件result.txt中 void output(STU s[], int n) { int i; FILE *fp; fp=fopen("result.txt","w"); for(i=0;i<N;i++){ fprintf(fp,"%ld %8s %10lf %10lf %10lf %5s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); printf("%ld %8s %10lf %10lf %10lf %5s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); } fclose(fp); } // 對考生信息進行處理:計算總分,排序,肯定等級 void process(STU s[], int n) { int i,j; STU temp; for(i=0;i<N;i++){ s[i].sum=s[i].subjective+s[i].objective; } for(i=0;i<N-1;i++){ for(j=0;j<N-i-1;i++){ if(s[j+1].sum>s[j].sum){ temp=s[j]; s[j]=s[j+1]; s[j+1]=temp; } } } for(i=0;i<N;i++){ if((i+1)<=0.1*N) strcpy(s[i].level,"優秀"); if((i+1)>0.1*N&&(i+1)<=0.5*N) strcpy(s[i].level,"合格"); if((i+1)>0.5*N) strcpy(s[i].level,"不合格"); } }
運行結果:
Part3: 拓展綜合應用
代碼以下:
#include <stdio.h> #include <stdlib.h> #define N 80 typedef struct student { int number; int num; char name[20]; char c[20]; }STU; int main(){ int i; STU st[N]; int a[5]; FILE *fout,*fin; fout = fopen("list.txt", "r"); if( !fout ) { // 若是打開失敗,則輸出錯誤提示信息,而後退出程序 printf("fail to open list.txt\n"); exit(0); } // 從fout指向的數據文件list.txt中讀取數據到結構體數組st for(i=0; i<N; i++) { fscanf(fout, "%d %d %s %s", &st[i].number, &st[i].num,st[i].name, st[i].c); } for(i=0; i<5; i++) { a[i]= (rand() % N)-1; } // 以寫方式打開/建立文件lucky.txt fin = fopen("lucky.txt", "w"); if( !fin ) { // 若是打開失敗,則輸出錯誤提示信息,而後退出程序 printf("fail to open lucky.txt\n"); exit(0); } // 將排序後的數組st中數據輸出到屏幕 for(i=0; i<5; i++) { printf("%5d %10d %10s %15s\n", st[a[i]].number, st[a[i]].num,st[a[i]].name, st[a[i]].c); // 將排序後的數組st中數據寫到文件lucky.txt fprintf(fout,"%5d %10d %10s %15s\n", st[a[i]].number, st[a[i]].num,st[a[i]].name, st[a[i]].c); } fclose(fin); fclose(fout); return 0; }
運行結果: