实验七
实验一:验证性实验
实验二:验证性实验
实验三:验证性实验
实验四:
#include <stdio.h> #include <stdlib.h> // 为system("pause") int main() { FILE *fp; char ch; int line = 0; int charCount = 0; fp = fopen("data4.txt", "r"); if (fp == NULL) { printf("文件打开失败!\n"); return 1; } while ((ch = fgetc(fp)) != EOF) { if (ch == '\n') { line++; } / else if (ch != ' ' && ch != '\t') { charCount++; } } fclose(fp); printf("data4.txt统计结果:\n"); printf("行数: %d\n", line); printf("字符数(不计空白符): %d\n", charCount); system("pause"); return 0; }

实验五:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <windows.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() { SetConsoleOutputCP(936); 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, N); printf("\n通过考试的名单写入文件:已完成!\n"); 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%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("文件打开失败,请检查txt存放路径!\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); } int process(STU st[], int n, STU st_pass[]) { int i, pass_num = 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_num] = st[i]; pass_num++; } else { strcpy(st[i].result, "未通过"); } } return pass_num; } void write(STU st[], int n) { int i; FILE *fout; fout = fopen("list_pass.txt", "w"); if (!fout) { printf("fail to open write file\n"); return; } fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n"); for (i = 0; i < n; i++) { fprintf(fout, "%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); } fclose(fout); }

实验六:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <windows.h> #define MAX_STUDENT 80 #define WIN_COUNT 5 typedef struct { long id; char name[20]; char className[40]; } Student; int main(void) { SetConsoleOutputCP(936); Student stuArr[MAX_STUDENT]; int total = 0; int selected[MAX_STUDENT] = {0}; Student winner[WIN_COUNT]; char saveFileName[50]; FILE *fpRead, *fpWrite; fpRead = fopen("list.txt", "r"); if (fpRead == NULL) { printf("Error: Cannot open list.txt\n"); system("pause"); return 1; } while (total < MAX_STUDENT && fscanf(fpRead, "%ld %s ", &stuArr[total].id, stuArr[total].name) == 2) { fgets(stuArr[total].className, sizeof(stuArr[total].className), fpRead); stuArr[total].className[strcspn(stuArr[total].className, "\n")] = '\0'; total++; } fclose(fpRead); if(total < WIN_COUNT) { printf("Insufficient number of students\n"); system("pause"); return 1; } srand((unsigned)time(NULL)); for(int i = 0; i < WIN_COUNT; ) { int idx = rand() % total; if(!selected[idx]) { selected[idx] = 1; winner[i++] = stuArr[idx]; } } printf("===== Winner List =====\n"); printf("ID\t\tName\t\tClass\n"); for(int i = 0; i < WIN_COUNT; i++) { printf("%ld\t%s\t%s\n",winner[i].id,winner[i].name,winner[i].className); } printf("\nEnter output filename:"); scanf("%s", saveFileName); fpWrite = fopen(saveFileName, "w"); fprintf(fpWrite, "Winner List\n"); fprintf(fpWrite, "ID\tName\tClass\n"); for(int i = 0; i < WIN_COUNT; i++) { fprintf(fpWrite, "%ld\t%s\t%s\n",winner[i].id,winner[i].name,winner[i].className); } fclose(fpWrite); printf("Data saved to %s successfully\n", saveFileName); system("pause"); return 0; }


浙公网安备 33010602011771号