任务四
简单文件读写
源码
| #include <stdio.h> |
| #include <stdlib.h> |
| |
| int main() |
| { |
| FILE *fp; |
| char ch; |
| int character = 0; |
| int line = 0; |
| |
| fp = fopen("data4.txt", "r"); |
| if (fp == NULL) |
| { |
| printf("fail to open\n"); |
| return 1; |
| } |
| |
| while ((ch = fgetc(fp)) != EOF) |
| { |
| if (ch == '\n') |
| line++; |
| if (ch != ' ' && ch != '\n' && ch != '\t') |
| character++; |
| } |
| |
| if (ftell > 0 && ch != '\n') |
| line++; |
| |
| printf("Total lines: %d\n", line); |
| printf("Total characters: %d\n", character); |
| |
| fclose(fp); |
| return 0; |
| } |
| |
结果

任务五
文件综合应用 考试
源码
| #include <stdio.h> |
| #include <string.h> |
| |
| #define N 10 |
| |
| typedef struct |
| { |
| long id; |
| char name[20]; |
| float objective; |
| float subjective; |
| float sum; |
| char result[10]; |
| } STU; |
| |
| |
| void read(STU st[], int n); |
| void write(STU st[], int n); |
| void output(STU st[], int n); |
| int process(STU st[], int n, STU st_pass[]); |
| |
| int main() |
| { |
| STU stu[N], stu_pass[N]; |
| int cnt; |
| double pass_rate; |
| |
| printf("Add %d students from file...\n", N); |
| read(stu, N); |
| |
| printf("\nFiguring...\n"); |
| cnt = process(stu, N, stu_pass); |
| |
| printf("\nStudents passed the exam:\n"); |
| output(stu, N); |
| write(stu, N); |
| |
| pass_rate = 1.0 * cnt / N; |
| printf("\nPass Rank: %.2f%%\n", pass_rate * 100); |
| |
| return 0; |
| } |
| |
| |
| |
| void output(STU st[], int n) |
| { |
| int i; |
| |
| printf("Number\tName\tObj_Score\tOpe_Score\tTotal_Score\tResult\n"); |
| for (i = 0; i < n; i++) |
| printf("%ld\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); |
| } |
| |
| |
| void read(STU st[], int n) |
| { |
| int i; |
| FILE *fin; |
| |
| fin = fopen("examinee.txt", "r"); |
| if (!fin) |
| { |
| printf("fail to open file\n"); |
| return; |
| } |
| |
| while (!feof(fin)) |
| { |
| for (i = 0; i < n; i++) |
| fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); |
| } |
| |
| fclose(fin); |
| } |
| |
| |
| |
| void write(STU st[], int n) |
| { |
| int i, cnt = 0; |
| FILE *fp; |
| |
| fp = fopen("list_pass.txt", "w"); |
| if (fp == NULL) |
| { |
| printf("fail to open\n"); |
| return; |
| } |
| |
| for (i = 0; i < n; i++) |
| { |
| if (st[i].sum >= 60) |
| { |
| cnt++; |
| fprintf(fp, "%ld %s %.2f %.2f %.2f %s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); |
| } |
| } |
| |
| fclose(fp); |
| |
| printf("%d students passed the exam\n", cnt); |
| } |
| |
| |
| int process(STU st[], int n, STU st_pass[]) |
| { |
| int i, cnt = 0; |
| |
| for (i = 0; i < n; i++) |
| { |
| st[i].sum = st[i].objective + st[i].subjective; |
| if (st[i].sum >= 60) |
| { |
| cnt++; |
| strcpy(st[i].result, "Passed"); |
| strcpy(st_pass[cnt - 1].name, st[i].name); |
| st_pass[cnt - 1].id = st[i].id; |
| st_pass[cnt - 1].objective = st[i].objective; |
| st_pass[cnt - 1].subjective = st[i].subjective; |
| st_pass[cnt - 1].sum = st[i].sum; |
| strcpy(st_pass[cnt - 1].result, st[i].result); |
| } |
| else |
| { |
| strcpy(st[i].result, "Failed"); |
| } |
| } |
| |
| return cnt; |
| } |
结果

任务六
文件综合应用 点名
源码
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <time.h> |
| |
| #define M 100 |
| |
| typedef struct |
| { |
| int id; |
| char name[20]; |
| char class[100]; |
| } STU; |
| |
| void shuffle(char *str[], int n) |
| { |
| for (int i = n - 1; i > 0; i--) |
| { |
| int j = rand() % (i + 1); |
| char *temp = str[i]; |
| str[i] = str[j]; |
| str[j] = temp; |
| } |
| } |
| |
| void sort(char *str[], int n) |
| { |
| STU stu[M]; |
| for (int i = 0; i < n; i++) |
| { |
| if (sscanf(str[i], "%d\t%s\t%s", &stu[i].id, stu[i].name, stu[i].class) != 3) |
| { |
| printf("Error parsing string: %s\n", str[i]); |
| return; |
| } |
| } |
| for (int i = 0; i < n - 1; i++) |
| { |
| for (int j = i + 1; j < n; j++) |
| { |
| if (stu[i].id > stu[j].id) |
| { |
| STU temp = stu[i]; |
| stu[i] = stu[j]; |
| stu[j] = temp; |
| } |
| } |
| } |
| for (int i = 0; i < n; i++) |
| { |
| snprintf(str[i], M, "%d\t%s\t%s", stu[i].id, stu[i].name, stu[i].class); |
| } |
| } |
| |
| void save_to_file(char *str[], int n) |
| { |
| printf("# press \"c\" to set a custom file name\n"); |
| printf("# press any other key to use default file name\n\n"); |
| printf("choice: "); |
| char choice; |
| char filename[100]; |
| FILE *fp; |
| scanf(" %c", &choice); |
| |
| if (choice == 'c') |
| { |
| printf("enter a file name: "); |
| scanf("%99s", filename); |
| fp = fopen(filename, "w"); |
| } |
| else |
| { |
| time_t t = time(NULL); |
| strftime(filename, sizeof(filename), "%Y-%m-%d_%H-%M-%S.txt", localtime(&t)); |
| fp = fopen(filename, "w"); |
| } |
| |
| if (fp == NULL) |
| { |
| printf("\nfail to open\n"); |
| return; |
| } |
| |
| for (int i = 0; i < n; i++) |
| { |
| if (fprintf(fp, "%s\n", str[i]) < 0) |
| { |
| printf("Error\n"); |
| break; |
| } |
| } |
| |
| fclose(fp); |
| printf("\nfile saved as %s\n", filename); |
| } |
| |
| int main() |
| { |
| FILE *fp = fopen("list.txt", "r"); |
| if (fp == NULL) |
| { |
| printf("fail to open\n"); |
| return 1; |
| } |
| |
| char *data[M]; |
| int cnt = 0; |
| |
| while (cnt < M && !feof(fp)) |
| { |
| data[cnt] = malloc(M * sizeof(char)); |
| if (fgets(data[cnt], M, fp) != NULL) |
| { |
| data[cnt][strcspn(data[cnt], "\n")] = 0; |
| cnt++; |
| } |
| else |
| { |
| free(data[cnt]); |
| cnt--; |
| } |
| } |
| fclose(fp); |
| |
| srand(time(NULL)); |
| shuffle(data, cnt); |
| |
| printf("---------\tRandom Picking\t---------\n\n"); |
| char *picked[5]; |
| for (int i = 0; i < 5; i++) |
| picked[i] = data[i]; |
| |
| sort(picked, 5); |
| |
| for (int i = 0; i < 5; i++) |
| printf("%s\n", picked[i]); |
| |
| printf("\n\n---------\tSave to File\t---------\n\n"); |
| |
| save_to_file(picked, 5); |
| |
| for (int i = 0; i < cnt; i++) |
| free(data[i]); |
| |
| return 0; |
| } |
| |
结果


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)