1.实验任务4
程序task4.c源码1 #include <stdio.h> 2 #define N 10 3 #define M 100 4 5 6 int main(){ 7 char x[N][M]; 8 int i,j,cnt=0,count=0; 9 FILE *fp; 10 11 fp = fopen("data4.txt", "r"); 12 if(!fp) { 13 printf("fail to open file to read\n"); 14 return; 15 } 16 17 for (i = 0; i < N && fgets(x[i], M, fp) != NULL; ++i) { 18 ++count; 19 } 20 21 for (i = 0; i < count; ++i) { 22 j = 0; 23 while (x[i][j] != '\0') { 24 char ch = x[i][j]; 25 26 if (ch != ' ' && ch != '\n' && ch != '\r') { 27 ++cnt; 28 } 29 ++j; 30 } 31 } 32 33 34 35 fclose(fp); 36 37 printf("%s\n","data4.txt统计结果:"); 38 printf("行数:%-20d\n",count); 39 printf("字符数(不计空白符):%-20d\n",cnt); 40 41 return 0; 42 }
运行结果截图
5. 实验任务5 完整源码task5.c
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 // 待补足 75 int i; 76 77 FILE *fp; 78 79 fp=fopen("list_pass.txt","w"); 80 if(!fp){ 81 printf("fail to open file\n"); 82 return; 83 } 84 85 fprintf(fp, "%-15s%-25s%-20s%-20s%-20s%-20s\n","准考证号","姓名","客观题得分","操作题得分","总分","结果"); 86 for(i=0;i<n;i++){ 87 if(strcmp(s[i].result,"通过")==0) 88 fprintf(fp, "%-15ld%-25s%-20.2f%-20.2f%-20.2f%-20s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 89 } 90 91 fclose(fp); 92 } 93 94 95 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 96 int process(STU st[], int n, STU st_pass[]) { 97 // 待补足 98 int i,j; 99 for(i=0,j=0;i<n;i++){ 100 st[i].sum=st[i].objective+st[i].subjective; 101 if(st[i].sum>=60){ 102 strcpy(st[i].result,"通过"); 103 st_pass[j++]=st[i]; 104 } 105 else{ 106 strcpy(st[i].result,"未通过"); 107 } 108 } 109 110 return j; 111 }
运行结果截图(包括屏幕输出截图,和,文件list_pass.txt内容截图)
6. 实验任务6 此部分书写内容: 必做部分 源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define N 80 6 7 typedef struct{ 8 char id[10]; 9 char name[10]; 10 char class[20]; 11 }stu; 12 13 void read(stu s[N]); 15 void choose(stu s[],int n); 16 17 int main() { 18 stu s[N]; 19 int i; 20 21 read(s); 22 choose(s,N); 23 24 return 0; 25 } 26 27 void read(stu s[N]){ 28 int i=0; 29 FILE *fin; 30 fin=fopen("list.txt","r"); 31 if(!fin){ 32 printf("fail to open file\n"); 33 return; 34 } 35 36 while(fscanf(fin, "%s %s %s", s[i].id, s[i].name, s[i].class) != EOF) 37 i++; 38 39 fclose(fin); 40 } 41 42 void choose(stu s[],int n){ 43 FILE *fp; 44 45 int i,j,k=0,g; 46 int c[5]; 47 char filename[20]; 49 srand(time(NULL)); 50 52 for (i = 0; k< 5; i++) { 53 j=rand()%n; 54 int is_duplicate = 0; 55 for(g=0;g<k;g++){ 56 if(j==c[g]) 57 is_duplicate = 1; 58 break; 59 } 60 if (!is_duplicate) 61 c[k++]=j; 62 } 63 64 printf("------------------随机抽点名单------------------\n"); 65 for (i = 0; i < 5; i++) { 66 printf("%s\t%s\t%s\n", s[c[i]].id, s[c[i]].name, s[c[i]].class); 67 } 68 69 printf("\n------------------保存到文件------------------\n"); 70 printf("输入文件名: "); 71 scanf("%s",filename); 72 fp=fopen(filename,"w"); 73 if(!fp){ 74 printf("fail to open the file"); 75 return; 76 } 77 for (i = 0; i < 5; i++) { 78 fprintf(fp,"%s\t%s\t%s\n", s[c[i]].id, s[c[i]].name, s[c[i]].class); 79 } 80 fclose(fp); 81 printf("保存成功!\n"); 82 83 84 85 }
运行测试截图
选做部分* 如果选做,请附上源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define N 80 6 7 typedef struct { 8 char id[10]; 9 char name[10]; 10 char class[20]; 11 } stu; 12 13 void read(stu s[N]); 14 void choose(stu s[], int n); 15 void generate_filename(char *filename); 16 17 int main() { 18 stu s[N]; 19 read(s); 20 choose(s, N); 21 return 0; 22 } 23 24 void read(stu s[N]) { 25 int i = 0; 26 FILE *fin; 27 fin = fopen("list.txt", "r"); 28 if (!fin) { 29 printf("fail to open file\n"); 30 return; 31 } 32 while (fscanf(fin, "%s %s %s", s[i].id, s[i].name, s[i].class) != EOF) 33 i++; 34 fclose(fin); 35 } 36 37 void choose(stu s[], int n) { 38 FILE *fp; 39 int i, j, k = 0, g; 40 stu t; 41 stu selected[5]; 42 int c[5]; 43 char filename[20]; 44 srand(time(NULL)); 45 46 for (i = 0; k < 5; i++) { 47 j = rand() % n; 48 int is_duplicate = 0; 49 for (g = 0; g < k; g++) { 50 if (j == c[g]) { 51 is_duplicate = 1; 52 break; 53 } 54 } 55 if (!is_duplicate) 56 c[k++] = j; 57 } 58 59 for (i = 0; i < 5; i++) { 60 selected[i] = s[c[i]]; 61 } 62 63 for (i = 0; i < k; i++) { 64 for (j = 0; j < k - 1 - i; j++) { 65 if (strcmp(selected[j].id, selected[j + 1].id) > 0) { 66 t = selected[j]; 67 selected[j] = selected[j + 1]; 68 selected[j + 1] = t; 69 } 70 } 71 } 72 73 printf("------------------随机抽点名单------------------\n"); 74 for (i = 0; i < 5; i++) { 75 printf("%s\t%s\t%s\n", selected[i].id, selected[i].name, selected[i].class); 76 } 77 78 printf("\n------------------保存到文件------------------\n"); 79 generate_filename(filename); 80 fp = fopen(filename, "w"); 81 if (!fp) { 82 printf("fail to open the file"); 83 return; 84 } 85 for (i = 0; i < 5; i++) { 86 fprintf(fp, "%s\t%s\t%s\n", selected[i].id, selected[i].name, selected[i].class); 87 } 88 fclose(fp); 89 printf("保存成功!\n"); 90 } 91 92 void generate_filename(char *filename) { 93 time_t now; 94 struct tm *local; 95 char date[20]; 96 time(&now); 97 local = localtime(&now); 98 strftime(date, sizeof(date), "%Y%m%d", local); 99 sprintf(filename, "%s.txt", date); 100 }
运行测试截图
标签:int,++,st,stu,实验,printf,fin From: https://www.cnblogs.com/gwy1004/p/18622785