实验任务四:
源代码:
task4.c
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #define N 10000 4 5 int main() { 6 char ch[N]; 7 int line = 1; 8 int n = 0; 9 int j = 0; 10 FILE* fp; 11 fp = fopen("C:/Users/lenovo/Desktop/实验7数据文件及部分代码/data4.txt", "r"); 12 if (fp == NULL) { 13 printf("Failed to open the file for reading.\n"); 14 return 0; 15 } 16 int i = 0; 17 int readChar; 18 while ((readChar = fgetc(fp)) != EOF) { 19 ch[i] = readChar; 20 if (ch[i] == '\n') { 21 line++; 22 } 23 if (ch[i] == '\n' || ch[i] == ' ' || ch[i] == '\t') { 24 j++; 25 } 26 i++; 27 } 28 n = i - j; 29 fclose(fp); 30 printf("data4.txt 统计结果:\n"); 31 printf("%-20s: %-20d\n", "行数", line); 32 printf("%-20s: %-20d\n", "字符数(不计空白格)", n); 33 return 0; 34 }
运行结果:
实验任务五:
源代码:
task5.c
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<string.h> 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 void read(STU st[], int n); 16 void write(STU st[], int n); 17 void output(STU st[], int n); 18 int process(STU st[], int n, STU st_pass[]); 19 20 int main() { 21 STU stu[N], stu_pass[N]; 22 int cnt; 23 double pass_rate; 24 25 printf("从文件读入%d个考生信息……\n", N); 26 read(stu, N); 27 28 printf("\n对考生成绩进行统计..."); 29 cnt = process(stu, N, stu_pass); 30 31 printf("\n通过考试的名单:\n"); 32 output(stu, N); 33 write(stu, N); 34 35 pass_rate = 1.0 * cnt / N; 36 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100); 37 38 return 0; 39 } 40 41 void output(STU st[], int n) { 42 int i; 43 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 44 for (i = 0; i < n; i++) 45 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, 46 st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 47 } 48 void read(STU st[], int n) { 49 int i; 50 FILE* fin; 51 fin = fopen("C:\\Users\\lenovo\\Desktop\\实验7数据文件及部分代码\\examinee.txt", "r"); 52 if (!fin) { 53 printf("fail to open file\n"); 54 return; 55 } 56 while (!feof(fin)) { 57 for (i = 0; i < n; i++) 58 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, 59 &st[i].objective, &st[i].subjective); 60 } 61 fclose(fin); 62 } 63 64 void write(STU st[], int n) { 65 int i; 66 FILE* fp; 67 68 fp = fopen("list_pass.txt", "w"); 69 70 if (fp == NULL) { 71 printf("fail to open file to write\n"); 72 return; 73 } 74 75 fprintf(fp, "%-20s%-20s%-20s%-20s%-20s%-20s\n", "准考证号", "姓名", "客观题得分", "操作题得分", "总分", "结果"); 76 for (i = 0; i < n; i++) { 77 if (st[i].sum >= 60) 78 fprintf(fp, "%-20ld%-20s%-20.2f%-20.2f%-20.2f%-20s\n", st[i].id, 79 st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 80 } 81 fclose(fp); 82 } 83 84 int process(STU st[], int n, STU st_pass[]) { 85 int i, cnt = 0; 86 87 for (i = 0; i < n; i++) { 88 st[i].sum = st[i].objective + st[i].subjective; 89 90 if (st[i].sum >= 60) { 91 strcpy(st[i].result, "通过"); 92 st_pass[cnt++] = st[i]; 93 } 94 else 95 strcpy(st[i].result, "不通过"); 96 } 97 return cnt; 98 }
运行结果:
实验任务六:
源代码:
task6.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 #define N 80 6 #define M 64 7 #define K 5 8 9 int main() { 10 char** s = (char**)malloc(N * sizeof(char*)); 11 if (!s) { 12 perror("内存分配失败"); 13 return 1; 14 } 15 16 for (int i = 0; i < N; i++) { 17 s[i] = (char*)malloc(M * sizeof(char)); 18 if (!s[i]) { 19 for (int j = 0; j < i; j++) { 20 free(s[j]); 21 } 22 free(s); 23 perror("内存分配失败"); 24 return 1; 25 } 26 } 27 28 FILE* f = fopen("C:/Users/lenovo/Desktop/实验7数据文件及部分代码/list.txt", "r"); 29 if (!f) { 30 perror("无法打开输入文件"); 31 for (int i = 0; i < N; i++) { 32 free(s[i]); 33 } 34 free(s); 35 return 1; 36 } 37 38 int j = 0; 39 while (fgets(s[j], M, f) != NULL) { 40 j++; 41 } 42 fclose(f); 43 44 int m[N]; 45 for (int i = 0; i < N; i++) { 46 m[i] = 0; 47 } 48 49 int p[K]; 50 int c = 0; 51 srand((unsigned)time(NULL)); 52 while (c < K) { 53 int r = rand() % N; 54 if (m[r] == -1) { 55 continue; 56 } 57 else { 58 m[r] = -1; 59 p[c++] = r; 60 } 61 } 62 63 for (int i = 0; i < K - 1; i++) { 64 for (int j = i + 1; j < K; j++) { 65 if (strcmp(s[p[i]], s[p[j]]) > 0) { 66 int t = p[i]; 67 p[i] = p[j]; 68 p[j] = t; 69 } 70 } 71 } 72 73 printf("-----------随机抽点名单-----------\n"); 74 for (int i = 0; i < K; i++) { 75 printf("%s", s[p[i]]); 76 } 77 78 char fn[16]; 79 printf("\n-----------保存到文件-----------\n输入文件名:"); 80 scanf_s("%s", fn); 81 FILE* fo = fopen(fn, "w"); 82 if (!fo) { 83 perror("无法打开输出文件"); 84 return 1; 85 } 86 87 for (int i = 0; i < K; i++) { 88 fprintf(fo, "%s", s[p[i]]); 89 } 90 fclose(fo); 91 printf("\n文档保存成功!"); 92 93 for (int i = 0; i < N; i++) { 94 free(s[i]); 95 } 96 free(s); 97 98 return 0; 99 }
运行结果:
选做:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 6 #define N 80 7 8 typedef struct { 9 char num[20]; 10 char name[20]; 11 char class[40]; 12 } STU; 13 14 STU st[N]; 15 STU stu[5]; 16 17 void read(); 18 void func(); 19 void bubble(); 20 21 int main() { 22 read(); 23 func(); 24 return 0; 25 } 26 27 void read() { 28 FILE* fin = fopen("C:\\Users\\lenovo\\Desktop\\实验7数据文件及部分代码\\list.txt", "r"); 29 if (!fin) { 30 printf("无法打开文件进行读取\n"); 31 return; 32 } 33 int number; 34 int i = 0; 35 while (1) { 36 number = fscanf(fin, "%s %s %s", st[i].num, st[i].name, st[i].class); 37 if (number != 3) { 38 break; 39 } 40 i++; 41 } 42 fclose(fin); 43 } 44 45 void func() { 46 srand(time(NULL)); 47 int cnt[5] = { 0 }; 48 int sign; 49 for (int i = 0; i < 5; ) { 50 int t = rand() % 80; 51 sign = 1; 52 for (int j = 0; j < 5; j++) { 53 if (cnt[j] == t) { 54 sign = 0; 55 break; 56 } 57 } 58 if (sign) { 59 cnt[i] = t; 60 i++; 61 } 62 } 63 64 time_t ct; 65 time(&ct); 66 struct tm* p; 67 p = localtime(&ct); 68 char currenttime[20]; 69 strftime(currenttime, sizeof(currenttime), "%Y%m%d", p); 70 printf("当前时间:%s\n", currenttime); 71 printf("-------------%s抽点名单--------------------\n", currenttime); 72 char fn[20]; 73 sprintf(fn, "%s.txt", currenttime); 74 FILE* fp = fopen(fn, "w"); 75 if (!fp) { 76 printf("无法打开文件进行写入\n"); 77 return; 78 } 79 for (int i = 0; i < 5; i++) { 80 stu[i] = st[cnt[i]]; 81 } 82 bubble(); 83 for (int i = 0; i < 5; i++) { 84 printf("%-20s %-20s %-40s\n", stu[i].num, stu[i].name, stu[i].class); 85 fprintf(fp, "%-20s %-20s %-40s\n", stu[i].num, stu[i].name, stu[i].class); 86 } 87 printf("\n文件保存成功!\n"); 88 fclose(fp); 89 } 90 91 void bubble() { 92 for (int i = 0; i < 4; i++) { 93 for (int j = 0; j < 4 - i; j++) { 94 if (strcmp(stu[j].num, stu[j + 1].num) > 0) { 95 STU temp = stu[j]; 96 stu[j] = stu[j + 1]; 97 stu[j + 1] = temp; 98 } 99 } 100 } 101 }
运行结果:
标签:文件,stu,int,编程,st,++,STU,实验,printf From: https://www.cnblogs.com/666666A/p/18639544