1. 实验任务1
【验证性实验】
2. 实验任务2
【验证性实验】
3. 实验任务3
【验证性实验】
4. 实验任务4
task4源代码:
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main(){ 5 FILE *fp; 6 int t=0; 7 char a; 8 9 fp=fopen("D:/c语言/实验7/data4.txt.txt","r"); 10 11 if (fp==NULL){ 12 printf("fail to open\n"); 13 system("pause"); 14 return 1; 15 } 16 17 while(1){ 18 a=fgetc(fp); 19 if(a==EOF) 20 break; 21 else if(a==' '||a=='\n') 22 continue; 23 else 24 t++; 25 } 26 27 printf("data4.txt中共包含字符数(不计空白符):%d\n",t); 28 29 fclose(fp); 30 31 system("pause"); 32 return 0; 33 }
task4运行截图:
5. 实验任务5
task5源代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define N 10 6 7 typedef struct { 8 long int id; 9 char name[20]; 10 float objective; // 客观题得分 11 float subjective; // 操作题得分 12 float sum; // 总分 13 char ans[10]; // 考试结果 14 } STU; 15 16 // 函数声明 17 void finput(STU st[], int n); 18 void foutput(STU st[], int n); 19 void output(STU st[], int n); 20 int process(STU st[], int n, STU st_pass[]); 21 22 int main() { 23 STU stu[N], stu_pass[N]; 24 int cnt; 25 double pass_rate; 26 27 printf("从文件读入%d个考生信息...\n", N); 28 finput(stu, N); 29 30 printf("\n对考生成绩进行统计...\n"); 31 cnt = process(stu, N, stu_pass); 32 33 printf("\n通过考试的名单:\n"); 34 output(stu, N); // 输出到屏幕 35 foutput(stu, N); // 输出到文件 36 37 pass_rate = 1.0 * cnt / N; 38 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 39 40 system("pause"); 41 return 0; 42 } 43 44 // 把通过考试的考生完整信息输出到屏幕上 45 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 46 void output(STU st[], int n) { 47 int i; 48 49 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 50 for (i = 0; i < n; i++) 51 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); 52 } 53 54 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 55 void finput(STU st[], int n) { 56 int i; 57 FILE *fin; 58 59 fin = fopen("D:/c语言/实验7/examinee.txt", "r"); 60 if (fin == NULL) { 61 printf("fail to open file\n"); 62 exit(0); 63 } 64 65 while (!feof(fin)) { 66 for (i = 0; i < n; i++) 67 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 68 } 69 70 fclose(fin); 71 } 72 73 // 把通过考试的考生完整信息写入文件list_pass.txt 74 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 75 void foutput(STU s[], int n) { 76 FILE *fout; 77 int i; 78 79 // 保存到文件 80 fout = fopen("D:/c语言/实验7/list.txt", "w"); 81 if (!fout) { 82 printf("fail to open or create list_pass.txt\n"); 83 exit(0); 84 } 85 86 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 87 88 for (i = 0; i < n; i++) 89 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); 90 91 fclose(fout); 92 } 93 94 95 96 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 97 int process(STU st[], int n, STU st_pass[]) { 98 FILE *fp; 99 int i,num=0; 100 101 fp=fopen("D:/c语言/实验7/examinee.txt","r"); 102 103 if (fp==NULL){ 104 printf("fail to open\n"); 105 system("pause"); 106 return 1; 107 } 108 109 for(i=0;i<n;i++){ 110 st[i].sum = st[i].objective + st[i].subjective; 111 if(st[i].sum>=60){ 112 strcpy(st[i].ans,"pass"); 113 num++;} 114 else 115 strcpy(st[i].ans,"fail");} 116 117 return num; 118 }
task5运行截图:
6. 实验任务6
【必做部分】
task6源代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <time.h> 5 #define N 80 6 7 typedef struct{ 8 int id; 9 char name[10]; 10 char classname[20]; 11 }STU; 12 13 int main(){ 14 FILE *fp; 15 int i,m; 16 STU a[N]; 17 18 fp=fopen("D:/c语言/实验7/list2.txt","r"); 19 20 if(fp==NULL){ 21 printf("fail to open\n"); 22 system("pause"); 23 return 1;} 24 25 for(i=0;i<N;i++) 26 fscanf(fp,"%d%s%s",&a[i].id,a[i].name,a[i].classname); 27 28 fclose(fp); 29 30 fp=fopen("D:/c语言/实验7/lucky.txt.txt","w"); 31 32 srand((unsigned int)time(0)); 33 for(i=0;i<5;i++){ 34 //srand((unsigned int)time(0)); 35 m=rand()%80+1; 36 fprintf(fp,"%d\t%s\t%s\n",a[m].id,a[m].name,a[m].classname); 37 printf("%d\t%s\t%s\n",a[m].id,a[m].name,a[m].classname); 38 } 39 40 fclose(fp); 41 42 system("pause"); 43 return 0; 44 }
task6运行截图:
【选做部分】
源代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <time.h> 5 #define N 80 6 7 typedef struct { 8 int id; 9 char name[10]; 10 char classname[20]; 11 }STU; 12 13 int main() { 14 FILE* fp; 15 int i, m, k, j; 16 int tum; 17 char tump[50]; 18 STU a[N]; 19 STU b[5]; 20 21 fp = fopen("D:/c语言/实验7/list2.txt", "r"); 22 23 if (fp == NULL) { 24 printf("fail to open\n"); 25 system("pause"); 26 return 1; 27 } 28 29 for (i = 0; i < N; i++) 30 fscanf(fp, "%d%s%s", &a[i].id, a[i].name, a[i].classname); 31 32 fclose(fp); 33 34 fp = fopen("D:/c语言/实验7/lucky.txt.txt", "w"); 35 36 srand((unsigned int)time(0)); 37 b[1].id=1; 38 b[2].id=2; 39 b[3].id=3; 40 b[4].id=4; 41 b[5].id=5; 42 for (i = 0; i < 5; i++) { 43 //srand((unsigned int)time(0)); 44 m = rand() % 80 + 1; 45 /*fprintf(fp,"%d\t%s\t%s\n",a[m].id,a[m].name,a[m].classname); 46 printf("%d\t%s\t%s\n",a[m].id,a[m].name,a[m].classname);*/ 47 b[i].id = a[m].id; 48 if (b[1].id == b[2].id||b[1].id == b[3].id||b[1].id == b[4].id||b[1].id == b[5].id||b[2].id == b[3].id||b[2].id == b[4].id||b[2].id == b[5].id||b[3].id == b[4].id||b[3].id == b[5].id||b[4].id == b[5].id){ 49 i--; 50 continue;} 51 else { 52 strcpy(b[i].name, a[m].name); 53 strcpy(b[i].classname, a[m].classname); 54 } 55 } 56 for (i = 0; i < 5; i++) { 57 for (j = i; j < 5; j++) { 58 if (b[i].id > b[j].id) { 59 tum = b[i].id; 60 b[i].id = b[j].id; 61 b[j].id = tum; 62 63 strcpy(tump, b[i].name); 64 strcpy(b[i].name, b[j].name); 65 strcpy(b[j].name, tump); 66 67 strcpy(tump, b[i].classname); 68 strcpy(b[i].classname, b[j].classname); 69 strcpy(b[j].classname, tump); 70 } 71 } 72 } 73 74 for (i = 0; i < 5; i++) { 75 fprintf(fp, "%d\t%s\t%s\n", b[i].id, b[i].name, b[i].classname); 76 printf("%d\t%s\t%s\n", b[i].id, b[i].name, b[i].classname); 77 } 78 fclose(fp); 79 80 system("pause"); 81 return 0; 82 }
运行截图:
/*以时间命名txt文件有待研究*/
标签:fp,文件,name,int,编程,st,实验,t%,id From: https://www.cnblogs.com/sunria/p/17910876.html