实验7
1 #include<stdio.h> 2 int main() { 3 FILE* fp; 4 fp = fopen("data4.txt", "r"); 5 char ch; 6 int a = 1, b = 0; 7 while ((ch = fgetc(fp)) != EOF) { 8 if (ch == '\n') { 9 a++; 10 } 11 if (ch != ' '&&ch!='\n'&&ch!='\t') 12 { 13 b++; 14 } 15 } 16 fclose(fp); 17 printf("data4.txt统计结果:\n行数:%d\n字符数(不计空白符):%d", a, b); 18 }
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 10 5 6 typedef struct { 7 long id; // 准考证号 8 char name[20]; // 姓名 9 float objective; // 客观题得分 10 float subjective; // 操作题得分 11 float sum; // 总分 12 char result[10]; // 考试结果 13 } STU; 14 15 // 函数声明 16 void read(STU st[], int n); 17 void write(STU st[], int n); 18 void output(STU st[], int n); 19 int process(STU st[], int n, STU st_pass[]); 20 21 int main() { 22 STU stu[N], stu_pass[N]; 23 int cnt; 24 double pass_rate; 25 26 printf("从文件读入%d个考生信息...\n", N); 27 read(stu, N); 28 29 printf("\n对考生成绩进行统计...\n"); 30 cnt = process(stu, N, stu_pass); 31 32 printf("\n通过考试的名单:\n"); 33 output(stu, N); // 输出所有考生完整信息到屏幕 34 write(stu, N); // 输出考试通过的考生信息到文件 35 36 pass_rate = 1.0 * cnt / N; 37 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100); 38 39 return 0; 40 } 41 42 // 把所有考生完整信息输出到屏幕上 43 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 44 void output(STU st[], int n) { 45 int i; 46 47 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 48 for (i = 0; i < n; i++) 49 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); 50 } 51 52 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 53 void read(STU st[], int n) { 54 int i; 55 FILE* fin; 56 57 fin = fopen("examinee.txt", "r"); 58 if (!fin) { 59 printf("fail to open file\n"); 60 return; 61 } 62 63 while (!feof(fin)) { 64 for (i = 0; i < n; i++) 65 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 66 } 67 68 fclose(fin); 69 } 70 71 // 把通过考试的考生完整信息写入文件list_pass.txt 72 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 73 void write(STU s[], int n) { 74 FILE* fp; 75 fp = fopen("list_pass.txt", "w"); 76 if (!fp) { 77 printf("fail to open file\n"); 78 return; 79 } 80 for (int i = 0; i < n; i++) { 81 if (s[i].sum >= 60) { 82 fprintf(fp, "%ld %s %f %f %f %s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 83 } 84 } 85 } 86 87 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 88 int process(STU st[], int n, STU st_pass[]) { 89 int count = 0; 90 for (int i = 0; i < n; i++) { 91 st[i].sum = st[i].objective + st[i].subjective; 92 if (st[i].sum >= 60) { 93 strcpy(st[i].result, "合格"); 94 st_pass[count] = st[i]; 95 count++; 96 } 97 else { 98 strcpy(st[i].result, "不合格"); 99 } 100 } 101 return count; 102 }
1 #include <stdio.h> 2 #include <string.h> 3 #include "stdlib.h" 4 #include "time.h" 5 typedef struct { 6 long id; // 准考证号 7 char name[20]; // 姓名 8 char class[20]; // 9 } STU; 10 void in(STU stu[]) 11 { 12 FILE *fp= fopen("D:\\快捷访问\\下载\\实验7数据文件及部分代码\\实验7数据文件及部分代码\\list.txt","r"); 13 int i=0; 14 for (i = 0; i < 80; ++i) { 15 fscanf(fp,"%ld%s%s",&stu[i].id,stu[i].name,stu[i].class); 16 } 17 fclose(fp); 18 }; 19 char* gettime() 20 { 21 time_t ct= time(NULL); 22 struct tm *t= localtime(&ct); 23 static char out[1000];//为什么extern不行,以及如何用指针完成该操作 24 strftime(out,100,"D:\\快捷访问\\下载\\实验7数据文件及部分代码\\实验7数据文件及部分代码\\%Y%m%d.txt",t); 25 return out; 26 } 27 int main() { 28 STU stu[80]; 29 in(stu); 30 int i=0; 31 int num[80]={0}; 32 srand(time(NULL)); 33 for (i = 0; i < 5; ) { 34 int index=rand()%80; 35 if(num[index]==0) { 36 num[index] = 1; 37 i++; 38 } 39 } 40 printf("------------随机抽点名单------------\n"); 41 FILE *fp= fopen((const char*)gettime(),"w");//自动获取时间 42 if (!fp) { 43 perror("文件打开失败"); 44 return 1; // 提示用户问题并退出 45 } 46 for (i = 0; i < 80; ++i) { 47 if(num[i]==1) { 48 fprintf(fp, "%ld\t%-6s\t%s\n", stu[i].id, stu[i].name, stu[i].class); 49 printf("%ld\t%-6s\t%s\n", stu[i].id, stu[i].name, stu[i].class); 50 } } 51 fclose(fp); 52 printf("------------保存到文件------------\n输入文件名:"); 53 char name[1000],finalname[1000]="";//编译器默认初始化为空格,一定要初始化 54 scanf("%s",name); 55 strcat(finalname,"D:\\快捷访问\\下载\\实验7数据文件及部分代码\\实验7数据文件及部分代码\\"); 56 strcat(finalname,name); 57 //printf("%s\n",finalname); 58 fp= fopen(finalname,"w"); 59 if (!fp) { 60 perror("文件打开失败"); 61 return 1; 62 } 63 for (i = 0; i < 80; ++i) { 64 if(num[i]==1) { 65 fprintf(fp, "%ld\t%-6s\t%s\n", stu[i].id, stu[i].name, stu[i].class); 66 } } 67 fclose(fp); 68 printf("保存成功!"); 69 70 71 return 0; 72 }