实验结论
1.task4
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
FILE* fp;
int ch;
int line_count = 0;
int char_count = 0;
int last_is_newline = 0;
// 打开data4.txt文件
if ((fp = fopen("data4.txt", "r")) == NULL) {
printf("fail");
return 1;
}
while ((ch = fgetc(fp)) != EOF) {
if (ch == '\n') {
line_count++;
last_is_newline = 1;
}
else {
last_is_newline = 0;
}
if (ch > 32) {
char_count++;
}
}
if (last_is_newline == 0) {
line_count++;
}
fclose(fp);
printf("data4.txt统计结果:\n");
printf("行数:\t\t %d\n", line_count);
printf("字符数(不计空白符): %d\n", char_count);
return 0;
}
- 运行结果截图
![2]()
2.task5
- 代码
#define _CRT_SECURE_NO_WARNINGS
#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对考生成绩进行统计: 已完成\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\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);
}
// 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
void read(STU st[], int n) {
int i;
FILE* fin;
fin = fopen("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);
}
// 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数
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];
cnt++;
}
else {
strcpy(st[i].result, "未通过");
}
}
return cnt;
}
// 把通过考试的考生完整信息写入文件list_pass.txt
// 准考证号,姓名,客观题得分,操作题得分,总分,结果
void write(STU st[], int n) {
FILE* fp = fopen("list_pass.txt", "w");
if (fp == NULL) {
printf("fail");
return;
}
fprintf(fp, "准考证号 姓名 客观题得分 操作题得分 总分 结果\n");
for (int i = 0;i < n;i++) {
fprintf(fp, "%-30ld%-10s%-10.2f%-10.2f%-10.2f%-10s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
}
fclose(fp);
}
- 运行结果截图
![5]()
![6]()
3.task6
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_STU 80
#define PICK_NUM 5
// 定义学生结构体
typedef struct {
char id[20];
char name[20];
char className[30];
} Student;
// 1. 读取list.txt全部学生数据
int readStudent(Student stu[])
{
FILE* fp = fopen("list.txt", "r");
if (fp == NULL)
{
printf("无法打开list.txt文件\n");
return 0;
}
int count = 0;
// 循环读取每一行学生信息
while (fscanf(fp, "%s %s %s", stu[count].id, stu[count].name, stu[count].className) == 3)
{
count++;
}
fclose(fp);
return count;
}
// 2. 随机抽取指定数量不重复学生
void randomSelect(Student allStu[], int total, Student winStu[])
{
int flag[MAX_STU] = { 0 }; // 标记数组:0未抽取,1已抽取
int num = 0;
srand((unsigned int)time(NULL)); // 设置随机种子,保证每次随机结果不同
while (num < PICK_NUM)
{
int index = rand() % total; // 随机生成数组下标
if (flag[index] == 0)
{
flag[index] = 1;
winStu[num++] = allStu[index];
}
}
}
// 3. 将中奖名单写入自定义文件名的文件
void saveToFile(Student winStu[], char fileName[])
{
FILE* fp = fopen(fileName, "w");
if (fp == NULL)
{
printf("文件创建失败\n");
return;
}
fprintf(fp, "========中奖名单========\n");
for (int i = 0; i < PICK_NUM; i++)
{
fprintf(fp, "%s\t%s\t%s\n", winStu[i].id, winStu[i].name, winStu[i].className);
}
fclose(fp);
}
int main()
{
Student allStudent[MAX_STU];
Student winStudent[PICK_NUM];
char saveFileName[50];
int totalNum = readStudent(allStudent);
if (totalNum == 0)
return 1;
// 随机抽奖
randomSelect(allStudent, totalNum, winStudent);
// 屏幕输出中奖名单
printf("========中奖名单========\n");
for (int i = 0; i < PICK_NUM; i++)
{
printf("%s\t%s\t%s\n", winStudent[i].id, winStudent[i].name, winStudent[i].className);
}
printf("\n输入文件名:");
scanf("%s", saveFileName);
strcat(saveFileName, ".txt");
saveToFile(winStudent, saveFileName);
printf("保存成功!\n");
system("pause");
return 0;
}
- 运行结果截图
![7]()
![8]()
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_STU 80
#define PICK_NUM 5
typedef struct {
char id[20];
char name[20];
char className[30];
} Student;
int readStudent(Student stu[])
{
FILE* fp = fopen("list.txt", "r");
if (fp == NULL)
{
printf("无法打开list.txt文件\n");
return 0;
}
int count = 0;
while (fscanf(fp, "%s %s %s", stu[count].id, stu[count].name, stu[count].className) == 3)
{
count++;
}
fclose(fp);
return count;
}
void randomSelect(Student allStu[], int total, Student winStu[])
{
int flag[MAX_STU] = { 0 };
int num = 0;
srand((unsigned int)time(NULL));
while (num < PICK_NUM)
{
int index = rand() % total;
if (flag[index] == 0)
{
flag[index] = 1;
winStu[num++] = allStu[index];
}
}
}
// 按学号字符串升序排列
void sortById(Student arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (strcmp(arr[j].id, arr[j + 1].id) > 0)
{
Student temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// 获取系统日期作为文件名
void getDateFileName(char fileName[])
{
time_t now = time(NULL);
struct tm* t = localtime(&now);
strftime(fileName, 50, "%Y%m%d.txt", t);
}
void saveToFile(Student winStu[], char fileName[])
{
FILE* fp = fopen(fileName, "w");
if (fp == NULL)
{
printf("文件创建失败\n");
return;
}
fprintf(fp, "========%s中奖名单========\n", fileName);
for (int i = 0; i < PICK_NUM; i++)
{
fprintf(fp, "%s\t%s\t%s\n", winStu[i].id, winStu[i].name, winStu[i].className);
}
fclose(fp);
}
int main()
{
Student allStudent[MAX_STU];
Student winStudent[PICK_NUM];
char fileName[50];
int totalNum = readStudent(allStudent);
if (totalNum == 0)
return 1;
randomSelect(allStudent, totalNum, winStudent);
sortById(winStudent, PICK_NUM);
// 屏幕打印排序后的中奖名单
printf("========中奖名单(按学号升序)========\n");
for (int i = 0; i < PICK_NUM; i++)
{
printf("%s\t%s\t%s\n", winStudent[i].id, winStudent[i].name, winStudent[i].className);
}
getDateFileName(fileName);
saveToFile(winStudent, fileName);
printf("\n文件自动保存为:%s,保存成功!\n", fileName);
system("pause");
return 0;
}
- 运行结果截图
![9]()
![0]()