7
4
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #define N 80 #define M 100 typedef struct { char name[N]; char author[N]; }Book; void write(); void read(); int main() { printf("测试1:把图书信息写入文本信息\n"); write(); printf("\n测试2:从文本文件读取图书信息,打印输出到屏幕\n"); read(); return 0; } void write() { Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"}, {"《灯塔》", "克里斯多夫.夏布特"}, {"《人的局限性》", "塞缪尔.约翰生"}, {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, {"《大地之上》", "罗欣顿·米斯特里"}, {"《上学记》", "何兆武"}, {"《命运》", "蔡崇达"} }; int n, i; FILE* fp; n = sizeof(x) / sizeof(x[0]); fp = fopen("date1.txt", "w"); if (fp == NULL) { printf("fail to open file to write \n"); return; } for (i = 0; i < n; ++i) fprintf(fp, "%-40s %-20s\n", x[i].name, x[i].author); fclose(fp); } void read() { Book x[N]; int i, n; FILE* fp; fp = fopen("date1.txt", "r"); if (fp == NULL) { printf("fail to oprn file to read\n"); return; } i = 0; while (fscanf(fp, "%s%s", x[i].name, x[i].author) != EOF) ++i; n = i; for (i = 0; i < n; i++) printf("%d.%-40s%-20s\n", i + 1, x[i].name, x[i].author); fclose(fp); }

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 s[], 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对考生成绩进行统计...\n"); cnt = process(stu, N, stu_pass); printf("\n通过考试的名单:\n"); output(stu_pass, cnt); write(stu_pass, cnt); 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结果\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\\沈CL\\Desktop\\新建文件夹\\examinee.txt", "r"); if (!fin) { printf("fail to open file\n"); return; } 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 s[], int n) { FILE *fout; int i; fout = fopen("list_pass.txt", "w"); if (!fout) { printf("无法创建文件 list_pass.txt\n"); return; } fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n"); for (i = 0; i < n; i++) { fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); } fclose(fout); } int process(STU st[], int n, STU st_pass[]) { int i, pass_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[pass_cnt] = st[i]; pass_cnt++; } else { strcpy(st[i].result, "不通过"); } } return pass_cnt; }

6
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #define N 80 #define M 5 int main() { FILE *fin, *fout; char students[N][50]; int flag[N] = {0}; int chosen[M]; char filename[100]; int i, index, count = 0; fin = fopen("C:\\Users\\沈CL\\Desktop\\新建文件夹\\list.txt", "r"); if (!fin) { printf("无法打开文件 list.txt\n"); return 1; } for (i = 0; i < N; i++) { if (fgets(students[i], 50, fin) == NULL) break; students[i][strcspn(students[i], "\n")] = 0; } fclose(fin); srand(time(NULL)); while (count < M) { index = rand() % N; if (flag[index] == 0) { flag[index] = 1; chosen[count] = index; count++; } } printf("请输入输出文件名:"); scanf("%s", filename); fout = fopen(filename, "w"); if (!fout) { printf("无法创建文件 %s\n", filename); return 1; } printf("中奖名单如下:\n"); fprintf(fout, "中奖名单如下:\n"); for (i = 0; i < M; i++) { printf("%d. %s\n", i + 1, students[chosen[i]]); fprintf(fout, "%d. %s\n", i + 1, students[chosen[i]]); } fclose(fout); printf("结果已保存到文件 %s\n", filename); return 0; }

7
浙公网安备 33010602011771号