task4
#include<stdio.h> int func(){ int ans,i; char c; FILE *fp; fp=fopen("data4.txt","r"); while(fp==NULL){ printf("fail to open file\n"); return 0;} i = 0; while(!feof(fp)) { c=fgetc(fp); if(c!=' '&&c!='\n') i++; } fclose(fp); ans = i - 1; return ans; } int main(){ int ans; ans=func(); if(ans!=0) printf("date4.txt中共包含字元數(不計空白符):%d",ans); return 0; }
task5
#include <stdio.h> #include <string.h> #include <stdlib.h> #define N 10 typedef struct { long int id; char name[20]; float objective; // 客觀題得分 float subjective; // 操作題得分 float sum; // 總分 char ans[10]; // 考試結果 } STU; // 函式宣告 void finput(STU st[], int n); void foutput(STU st[], int n); void output(STU st[], int n); int process(STU st[], int n, STU st_pass[]); int main() { STU stu[N], stu_pass[N]; int cnt; double pass_rate; printf("從檔案讀入%d個考生資訊...\n", N); finput(stu, N); printf("\n對考生成績進行統計...\n"); cnt = process(stu, N, stu_pass); printf("\n透過考試的名單:\n"); output(stu_pass, cnt); // 輸出到螢幕 foutput(stu_pass, cnt); // 輸出到檔案 pass_rate = 1.0 * cnt / N; printf("\n本次等級考試透過率: %.2f%%\n", pass_rate*100); return 0; } // 把透過考試的考生完整資訊輸出到螢幕上 // 准考證號,姓名,客觀題得分,操作題得分,總分,結果 void output(STU st[], int n) { int i; printf("准考證號\t姓名\t客觀題得分\t操作題得分\t總分\t\t結果\n"); for (i = 0; i < n; i++) printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].ans); } // 從文字檔案examinee.txt讀入考生資訊:准考證號,姓名,客觀題得分,操作題得分 void finput(STU st[], int n) { int i; FILE *fin; fin = fopen("examinee.txt", "r"); if (fin == NULL) { printf("fail to open file\n"); exit(0); } while (!feof(fin)) { for (i = 0; i < n; i++) fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); } fclose(fin); } // 把透過考試的考生完整資訊寫入檔案list_pass.txt // 准考證號,姓名,客觀題得分,操作題得分,總分,結果 void foutput(STU s[], int n) { FILE *fout; int i; // 儲存到檔案 fout = fopen("list_pass.txt", "w"); if (!fout) { printf("fail to open or create list_pass.txt\n"); exit(0); } fprintf(fout, "准考證號\t\t姓名\t\t客觀題得分\t操作題得分\t總分\t\t結果\n"); for (i = 0; i < n; i++) fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].ans); fclose(fout); } // 對考生資訊進行處理:計算每位考生考試總分、結果;統計考試透過的人數 int process(STU st[], int n, STU st_pass[]) { int i,j; for(i=0,j=0;i<n;++i){ st[i].sum=st[i].objective+st[i].subjective; if(st[i].sum>=60){ strcpy(st[i].ans,"pass"); st_pass[j++]=st[i];} else strcpy(st[i].ans,"fail");} return j; }
task6
#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 100 struct STU { long int id; char name[N]; char cla[N]; } st[N], lucky[N]; void func1(void); int func2(void); void func3(void); int main() { int j, n; func1(); srand((unsigned)time(NULL)); for (j = 0; j < 5; j++) { n = func2(); lucky[j] = st[n]; printf("%ld\t %s\t %s\t\n", lucky[j].id, lucky[j].name, lucky[j].cla); } func3(); getchar(); return 0; } void func1(void) { int i; FILE *fp; fp = fopen("list.txt", "r"); if (fp == NULL) { printf("Failed to open file\n"); return; } i = 0; while (fscanf(fp, "%ld %s %s", &st[i].id, st[i].name, st[i].cla) == 3 && i < N) { ++i; } fclose(fp); } int func2(void) { return rand() % 80; } void func3(void) { int j; FILE *fout; fout = fopen("lucky.txt", "w"); if (fout == NULL) { printf("Failed to open file\n"); return; } for (j = 0; j < 5; j++) { fprintf(fout, "%ld\t %s\t %s\t\n", lucky[j].id, lucky[j].name, lucky[j].cla); } fclose(fout); }