实验7
task.4
源代码:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #define N 10000 4 int main() { 5 char ch[N]; 6 int line = 1; 7 int n = 0; 8 int j = 0; 9 FILE* fp; 10 fp = fopen("data4.txt", "r"); 11 if (!fp) { 12 printf("fail to open the file to read\n"); 13 return 0; 14 } 15 int i = 0; 16 while (!feof(fp)) { 17 ch[i] = fgetc(fp); 18 if (ch[i] == '\n') { 19 line++; 20 } 21 if (ch[i] == EOF) { 22 break; 23 } 24 if (ch[i] == '\n' || ch[i] == ' ' || ch[i] == ' ') { 25 j++; 26 } 27 i++; 28 } 29 n = i - j; 30 fclose(fp); 31 printf("data4.txt统计结果:\n"); 32 printf("%-20s%-20d\n", "行数", line); 33 printf("%-20s%-20d", "字符数(不计空白符)", n); 34 return 0; 35 }
输出结果:
task.5
源代码:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <string.h> 4 5 #define N 10 6 7 typedef struct { 8 long id; // 准考证号 9 char name[20]; // 姓名 10 float objective; // 客观题得分 11 float subjective; // 操作题得分 12 float sum; // 总分 13 char result[10]; // 考试结果 14 } STU; 15 16 // 函数声明 17 void read(STU st[], int n); 18 void write(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 read(stu, N); 29 30 printf("\n对考生成绩进行统计...\n"); 31 cnt = process(stu, N, stu_pass); 32 33 printf("\n通过考试的名单:\n"); 34 output(stu, N); // 输出所有考生完整信息到屏幕 35 write(stu, N); // 输出考试通过的考生信息到文件 36 37 pass_rate = 1.0 * cnt / N; 38 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100); 39 40 return 0; 41 } 42 43 // 把所有考生完整信息输出到屏幕上 44 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 45 void output(STU st[], int n) { 46 int i; 47 48 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 49 for (i = 0; i < n; i++) 50 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); 51 } 52 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 54 void read(STU st[], int n) { 55 int i; 56 FILE* fin; 57 58 fin = fopen("examinee.txt", "r"); 59 if (!fin) { 60 printf("fail to open file\n"); 61 return; 62 } 63 64 while (!feof(fin)) { 65 for (i = 0; i < n; i++) 66 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 67 } 68 69 fclose(fin); 70 } 71 72 // 把通过考试的考生完整信息写入文件list_pass.txt 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 74 void write(STU s[], int n) { 75 FILE* fout; 76 fout = fopen("list_pass.txt", "w"); 77 if (!fout) { 78 printf("fail to open the file to write\n"); 79 return; 80 } 81 fprintf(fout, "%-20s %-20s %-20s %-20s %-20s %-20s\n", "准考证号", "姓名", "客观题得分", "操作题得分", "总分", "结果"); 82 for (int i = 0; i < n; i++) { 83 if (s[i].sum >= 60) { 84 fprintf(fout, "%-20ld %-20s %-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); 85 } 86 } 87 fclose(fout); 88 } 89 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 90 int process(STU st[], int n, STU st_pass[]) { 91 int cnt = 0; 92 for (int i = 0; i < N; i++) { 93 st[i].sum = st[i].objective + st[i].subjective; 94 if (st[i].sum >= 60) { 95 strcpy(st[i].result, "通过"); 96 st_pass[cnt] = st[i]; 97 cnt++; 98 } 99 else { 100 strcpy(st[i].result, "不通过"); 101 } 102 } 103 return cnt; 104 }
输出结果:
task.6
源代码:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 time_t current_time; 5 struct tm* local_time; 6 typedef struct STU 7 { 8 long long int id; 9 char* name; 10 char* class; 11 }STU; 12 void read(STU st[], int n) { 13 int i; 14 FILE* fin; 15 16 fin = fopen("E:\\Downloads\\somecode\\somecode\\list.txt", "r"); 17 if (!fin) { 18 printf("fail to open file\n"); 19 return; 20 } 21 22 while (!feof(fin)) { 23 for (i = 0; i < n; i++) 24 { 25 st[i].name = (char*)malloc(20 * sizeof(char)); 26 st[i].class = (char*)malloc(20 * sizeof(char)); 27 fscanf(fin, "%ld %s %s", &st[i].id, st[i].name, st[i].class); 28 } 29 } 30 31 fclose(fin); 32 } 33 void write(STU s[], int n, char* pass) { 34 int i; 35 FILE* fout; 36 37 fout = fopen(pass, "w"); 38 if (!fout) { 39 printf("fail to open file\n"); 40 return; 41 } 42 for (i = 0; i < n; i++) 43 fprintf(fout, "%ld\t%s\t%s\n", s[i].id, s[i].name, s[i].class); 44 fclose(fout); 45 } 46 int main() 47 { 48 STU st[81]; 49 read(st, 81); 50 char* pass; 51 pass = (char*)malloc(100 * sizeof(char)); 52 current_time = time(NULL); 53 local_time = localtime(¤t_time); 54 srand((unsigned int)time(NULL)); 55 printf("--------%d-%d-%d %d:%d:%d点名-----------\n", local_time->tm_year + 1900, local_time->tm_mon + 1, local_time->tm_mday, local_time->tm_hour, local_time->tm_min, local_time->tm_sec); 56 sprintf(pass, "E:\\Downloads\\somecode\\somecode\\%d-%d-%d.txt", local_time->tm_year + 1900, local_time->tm_mon + 1, local_time->tm_mday); 57 STU strandon[5]; 58 for (int i = 0; i < 5; i++) 59 { 60 int rand_num = rand() % 81; 61 strandon[i] = st[rand_num]; 62 printf("%ld\t%s\t%s\n", strandon[i].id, strandon[i].name, strandon[i].class); 63 } 64 write(strandon, 5, pass); 65 for (int i = 0; i < 81; i++) 66 { 67 free(st[i].name); 68 free(st[i].class); 69 } 70 free(pass); 71 }
输出结果:
标签:printf,int,time,st,STU,实验,pass From: https://www.cnblogs.com/wcy-0464/p/18639966