实验7
task 4
#include<stdio.h> #define N 3 void write(); int main() { write(); char ch; int i = 0, k = 0; FILE* fp; fp = fopen("data4.txt", "r"); if (!fp) { printf("fail to open file to read\n"); return; } while (!feof(fp)) { ch = fgetc(fp); if (ch == EOF) break; if (ch != ' ' && ch != '\n' ) i++; if (ch == '\n') k++; } fclose(fp); printf("data4.txt统计结果:\n"); printf("行数:%20d\n", k); printf("字符数(不计空白符):%5d", i); return 0; } void write() { char* ptr[N] = { "0123456789-0123456789", "nuist2025", "cosmos galaxy" }; int i; FILE* fp; fp = fopen("data4.txt", "w"); if (!fp) { printf("fail to open file to write\n"); return; } for (i = 0;i < N;++i) { fputs(ptr[i], fp); fputs("\n", fp); } fclose(fp); }

task 5
#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("从文件读入%d个考生信息……\n", N); read(stu, N); printf("\n对考生成绩进行统计..."); cnt = process(stu, N, stu_pass); printf("\n通过考试的名单:\n"); output(stu, N); write(stu, N); pass_rate = 1.0 * cnt / N; printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100); return 0; } void output(STU st[], int n) { int i; printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); for (i = 0; i < n; i++) 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); } void read(STU st[], int n) { int i; FILE* fin; fin = fopen("C:\\Users\\admin\\Downloads\\实验7数据文件及部分代码\\实验7数据文件及部分代码\\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; FILE* fp; fp = fopen("list_pass.txt", "w"); if (fp == NULL) { printf("fail to open file to write\n"); return; } fprintf(fp,"%-20s%-20s%-20s%-20s%-20s%-20s\n", "准考证号", "姓名", "客观题得分", "操作题得分", "总分", "结果"); for (i = 0;i < n;i++) { if (st[i].sum >= 60) fprintf(fp,"%-20ld%-20s%-20.2f%-20.2f%-20.2f%-20s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); } fclose(fp); } 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) { strcpy(st[i].result, "通过"); st_pass[cnt++] = st[i]; } else strcpy(st[i].result, "不通过"); } return cnt; }


task 6
#include<stdio.h> #include<stdlib.h> #define N 80 typedef struct { long id; char name[20]; char class[50]; }STU; void read(STU x[], int n); void random(STU x[], int n,STU choose[]); int main() { STU x[N], choose[N]; char a[20]; int i; FILE* fp; read(x, N); printf("————随机抽点名单————\n"); random(x, N,choose); printf("————保存到文件————\n"); printf("输入文件名:"); scanf("%s", a); fp = fopen(a, "w"); if (!fp) { printf("fail to open file to write\n"); return; } else printf("保存成功!"); for (i = 0;i < 5;i++) fprintf(fp, "%-15ld %-10s %-10s\n", choose[i].id, choose[i].name, choose[i].class); fclose(fp); return 0; } void read(STU x[], int n) { int i; FILE* fp; fp = fopen("C:\\Users\\admin\\Downloads\\实验7数据文件及部分代码\\实验7数据文件及部分代码\\list.txt", "r"); if (!fp) { printf("fail to open file\n"); return; } while (!feof(fp)) { for (i = 0;i < n;i++) fscanf(fp, "%ld %s %s", &x[i].id, x[i].name, x[i].class); } fclose(fp); } void random(STU x[], int n, STU choose[]) { int k,i,cnt=0; for (i = 0;i < 5;i++) { k = rand() % (n - 1); if (x[k].id != 0) { printf("%-15ld %-10s %-10s\n", x[k].id, x[k].name, x[k].class); choose[cnt++] = x[k]; x[k].id = 0; } } }


浙公网安备 33010602011771号