实验七
任务四
点击查看代码
#include <stdio.h>
int main() {
FILE* f = fopen("C:\\Users\\Lenovo\\Desktop\\实验七\\data4.txt.txt", "r");
int lines = 0, chars = 0, c;
while ((c = fgetc(f)) != EOF) {
if (c == '\n') lines++;
if (c != ' ' && c != '\n' && c != '\t' && c != '\r')
chars++;
}
fclose(f);
printf("data4.txt统计结果:\n");
printf("行数: %d\n", lines + 1);
printf("字符数(不计空白符): %d\n", chars);
return 0;
}

任务五
点击查看代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define N 10
typedef struct {
long id; // 准考证号
char name[20]; // 姓名
float objective; // 客观题得分(40分制)
float subjective; // 操作题得分(60分制)
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对考生成绩进行统计...\n");
cnt = process(stu, N, stu_pass);
printf("\n所有考生完整信息:\n");
output(stu, 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\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;
errno_t err = fopen_s(&fin, "C:\\Users\\Lenovo\\Desktop\\实验七\\examinee.txt.txt", "r");
if (err != 0 || fin == NULL) {
printf("fail to open file examinee.txt\n");
return;
}
for (i = 0; i < n; i++) {
int ret = fscanf_s(fin, "%ld %s %f %f", &st[i].id, st[i].name, (unsigned)(sizeof(st[i].name) / sizeof(st[i].name[0])), &st[i].objective, &st[i].subjective);
if (ret != 4) {
printf("第%d个考生信息读取失败\n", i + 1);
break;
}
}
fclose(fin);
}
void write(STU s[], int n) {
int i;
FILE* fout;
errno_t err = fopen_s(&fout, "C:\\Users\\Lenovo\\Desktop\\实验七\\examinee.txt.txt", "w");
if (err != 0 || fout == NULL) {
printf("fail to open file list_pass.txt\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",
s[i].id, s[i].name, s[i].objective,
s[i].subjective, s[i].sum, s[i].result);
}
fclose(fout);
printf("\n通过考生信息已保存到list_pass.txt\n");
}
int process(STU st[], int n, STU st_pass[]) {
int pass_count = 0;
size_t result_len = sizeof(st[0].result) / sizeof(st[0].result[0]);
for (int i = 0; i < n; i++) {
st[i].sum = st[i].objective + st[i].subjective;
if (st[i].sum >= 60.0) {
strncpy_s(st[i].result, result_len, "通过", _TRUNCATE);
st_pass[pass_count] = st[i];
pass_count++;
}
else {
strncpy_s(st[i].result, result_len, "未通过", _TRUNCATE);
}
}
return pass_count;
}

任务六
点击查看代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 80
#define WINNERS 5
#define MAX_FILENAME 100
#define MAX_ID_LEN 20
#define MAX_NAME_LEN 20
typedef struct {
char id[MAX_ID_LEN];
char name[MAX_NAME_LEN];
} Student;
int readStudents(Student students[], const char* filename);
void randomSelect(Student students[], int total, Student winners[], int count);
void displayWinners(Student winners[], int count);
void writeToFile(Student winners[], int count, const char* filename);
void sortByID(Student winners[], int count);
int main() {
Student students[N];
Student winners[WINNERS];
char filename[MAX_FILENAME];
int count;
srand(time(NULL));
count = readStudents(students, "C:\\Users\\Lenovo\\Desktop\\实验七\\list.txt");
if (count == 0) {
printf("错误:未能读取到学生信息\n");
return 1;
}
if (count < WINNERS) {
printf("错误:学生数量不足%d人(当前只有%d人)\n", WINNERS, count);
return 1;
}
randomSelect(students, count, winners, WINNERS);
sortByID(winners, WINNERS);
printf("\n=== 中奖名单 ===\n");
displayWinners(winners, WINNERS);
printf("\n请输入要保存的文件名:");
fgets(filename, MAX_FILENAME, stdin);
filename[strcspn(filename, "\n")] = '\0';
if (strlen(filename) == 0) {
strcpy_s(filename, MAX_FILENAME, "中奖名单.txt");
}
writeToFile(winners, WINNERS, filename);
printf("\n中奖名单已保存到文件:%s\n", filename);
return 0;
}
int readStudents(Student students[], const char* filename) {
FILE* file;
int i = 0;
if (fopen_s(&file, filename, "r") != 0 || file == NULL) {
printf("无法打开文件:%s\n", filename);
return 0;
}
printf("正在读取文件:%s\n", filename);
while (i < N && fscanf_s(file, "%19s %19s",
students[i].id, (unsigned)_countof(students[i].id),
students[i].name, (unsigned)_countof(students[i].name)) == 2) {
i++;
}
fclose(file);
printf("成功读取 %d 名学生信息\n", i);
return i;
}
void randomSelect(Student students[], int total, Student winners[], int count) {
int* flag = (int*)calloc(total, sizeof(int));
int selected = 0;
int random_index;
if (flag == NULL) {
printf("内存分配失败!\n");
exit(1);
}
printf("\n开始随机抽奖...\n");
printf("总参与人数:%d人,将抽取%d名幸运者\n", total, count);
while (selected < count) {
random_index = rand() % total;
if (flag[random_index] == 0) {
winners[selected] = students[random_index];
flag[random_index] = 1;
selected++;
printf("第%d位中奖者:%s %s\n", selected,
winners[selected - 1].id, winners[selected - 1].name);
}
}
free(flag);
}
void displayWinners(Student winners[], int count) {
printf("序号\t学号\t\t姓名\n");
printf("--------------------------------\n");
for (int i = 0; i < count; i++) {
printf("%d\t%s\t\t%s\n", i + 1, winners[i].id, winners[i].name);
}
}
void writeToFile(Student winners[], int count, const char* filename) {
FILE* file;
if (fopen_s(&file, filename, "w") != 0 || file == NULL) {
printf("无法创建文件:%s\n", filename);
return;
}
fprintf(file, "=== 中奖名单 ===\n");
fprintf(file, "抽取时间:%s", ctime(&(time_t) { time(NULL) }));
fprintf(file, "序号\t学号\t\t姓名\n");
fprintf(file, "--------------------------------\n");
for (int i = 0; i < count; i++) {
fprintf(file, "%d\t%s\t\t%s\n", i + 1, winners[i].id, winners[i].name);
}
fclose(file);
}
void sortByID(Student winners[], int count) {
Student temp;
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (strcmp(winners[j].id, winners[j + 1].id) > 0) {
temp = winners[j];
winners[j] = winners[j + 1];
winners[j + 1] = temp;
}
}
}
}


浙公网安备 33010602011771号