实验七
任务四
#include <stdio.h> int main() { FILE *fp; char ch; int line = 0, charNum = 0; int flag = 0; fp = fopen("data4.txt","r"); if(fp == NULL) { printf("找不到data4.txt!\n"); system("pause"); return 0; } while((ch = fgetc(fp)) != EOF) { flag = 1; if(ch == '\n') { line++; flag = 0; } if(ch != ' ' && ch != '\n' && ch != '\t') charNum++; } if(flag == 1) line++; fclose(fp); printf("data4.txt统计结果:\n"); printf("行数: %d\n",line); printf("字符数(不计空白符): %d\n",charNum); system("pause"); return 0; }

任务五
#include <stdio.h> #include <string.h> #include <stdlib.h> #define N 10 typedef struct { long id; char name[20]; float objective; float subjective; float sum; char result[10]; } STU; void read(STU st[], int n); void write(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; int fileok = 1; printf("从文件读入%d个考生信息:已完成\n", N); read(stu, N, &fileok); if(fileok == 0) { system("pause"); return 0; } printf("\n对考生成绩进行统计:已完成\n"); cnt = process(stu, N, stu_pass); printf("\n所有考生完整信息:\n"); output(stu, N); printf("\n通过考试的名单写入文件:已完成!\n"); write(stu_pass, cnt); pass_rate = 1.0 * cnt / N; printf("\n本次等级考试通过率:%.2f%%\n", pass_rate*100); system("pause"); 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].result); } } void read(STU st[], int n, int *flag) { int i; FILE *fin; fin = fopen("C:\\Users\\派大睿\\documents\\Visual Studio 2010\\Projects\\实验七\\实验七\\examinee.txt", "r"); if (!fin) { printf("文件打开失败,检查路径!\n"); *flag = 0; return; } 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); *flag = 1; } int process(STU st[], int n, STU st_pass[]) { int i, num = 0; for(i = 0; i < n; i++) { st[i].sum = st[i].objective + st[i].subjective; if(st[i].sum >= 60) { strcpy(st[i].result, "通过"); st_pass[num] = st[i]; num++; } else { strcpy(st[i].result, "不通过"); } } return num; } void write(STU st[], int n) { int i; FILE *fout; fout = fopen("C:\\Users\\派大睿\\documents\\v\\list_pass.txt", "w"); for(i = 0; i < n; i++) { fprintf(fout,"%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].result); } fclose(fout); }

任务六
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #define N 80 typedef struct { char id[15]; char name[10]; char cls[20]; }Student; void sortStu(Student win[], int n) { int i,j; Student temp; for(i = 0; i < n - 1; i++) { for(j = 0; j < n - 1 - i; j++) { if(strcmp(win[j].id, win[j+1].id) > 0) { temp = win[j]; win[j] = win[j+1]; win[j+1] = temp; } } } } int main() { Student stu[N], winner[5]; int flag[N]={0}; FILE *fp; int i,count = 0, rnd; char filename[20]; time_t now; struct tm *t; fp = fopen("list.txt","r"); if(fp == NULL) { printf("文件打开失败\n"); system("pause"); return 0; } for(i = 0; i < N; i++) { fscanf(fp,"%s %s %s",stu[i].id,stu[i].name,stu[i].cls); } fclose(fp); srand((unsigned)time(NULL)); while(count < 5) { rnd = rand()%N; if(flag[rnd]==0) { flag[rnd]=1; winner[count] = stu[rnd]; count++; } } sortStu(winner,5); now = time(NULL); t = localtime(&now); sprintf(filename,"%04d%02d%02d.txt",t->tm_year+1900,t->tm_mon+1,t->tm_mday); printf("----------%s中奖名单----------\n",filename); for(i=0;i<5;i++) { printf("%s %s %s\n",winner[i].id,winner[i].name,winner[i].cls); } fp = fopen(filename,"w"); for(i=0;i<5;i++) { fprintf(fp,"%s %s %s\n",winner[i].id,winner[i].name,winner[i].cls); } fclose(fp); printf("文件保存成功!\n"); system("pause"); return 0; }



浙公网安备 33010602011771号