实验七

任务四

简单文件读写

源码

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char ch;
int character = 0;
int line = 0;
fp = fopen("data4.txt", "r");
if (fp == NULL)
{
printf("fail to open\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF)
{
if (ch == '\n')
line++;
if (ch != ' ' && ch != '\n' && ch != '\t')
character++;
}
if (ftell > 0 && ch != '\n')
line++;
printf("Total lines: %d\n", line);
printf("Total characters: %d\n", character);
fclose(fp);
return 0;
}

结果

任务五

文件综合应用 考试

源码

#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("Add %d students from file...\n", N);
read(stu, N);
printf("\nFiguring...\n");
cnt = process(stu, N, stu_pass);
printf("\nStudents passed the exam:\n");
output(stu, N); // 输出所有考生完整信息到屏幕
write(stu, N); // 输出考试通过的考生信息到文件
pass_rate = 1.0 * cnt / N;
printf("\nPass Rank: %.2f%%\n", pass_rate * 100);
return 0;
}
// 把所有考生完整信息输出到屏幕上
// 准考证号,姓名,客观题得分,操作题得分,总分,结果
void output(STU st[], int n)
{
int i;
printf("Number\tName\tObj_Score\tOpe_Score\tTotal_Score\tResult\n");
for (i = 0; i < n; i++)
printf("%ld\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;
}
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);
}
// 把通过考试的考生完整信息写入文件list_pass.txt
// 准考证号,姓名,客观题得分,操作题得分,总分,结果
void write(STU st[], int n)
{
int i, cnt = 0;
FILE *fp;
fp = fopen("list_pass.txt", "w");
if (fp == NULL)
{
printf("fail to open\n");
return;
}
for (i = 0; i < n; i++)
{
if (st[i].sum >= 60)
{
cnt++;
fprintf(fp, "%ld %s %.2f %.2f %.2f %s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
}
}
fclose(fp);
printf("%d students passed the exam\n", cnt);
}
// 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
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)
{
cnt++;
strcpy(st[i].result, "Passed");
strcpy(st_pass[cnt - 1].name, st[i].name);
st_pass[cnt - 1].id = st[i].id;
st_pass[cnt - 1].objective = st[i].objective;
st_pass[cnt - 1].subjective = st[i].subjective;
st_pass[cnt - 1].sum = st[i].sum;
strcpy(st_pass[cnt - 1].result, st[i].result);
}
else
{
strcpy(st[i].result, "Failed");
}
}
return cnt;
}

结果

任务六

文件综合应用 点名

源码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define M 100
typedef struct
{
int id;
char name[20];
char class[100];
} STU;
void shuffle(char *str[], int n)
{
for (int i = n - 1; i > 0; i--)
{
int j = rand() % (i + 1);
char *temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
void sort(char *str[], int n)
{
STU stu[M];
for (int i = 0; i < n; i++)
{
if (sscanf(str[i], "%d\t%s\t%s", &stu[i].id, stu[i].name, stu[i].class) != 3)
{
printf("Error parsing string: %s\n", str[i]);
return;
}
}
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (stu[i].id > stu[j].id)
{
STU temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
for (int i = 0; i < n; i++)
{
snprintf(str[i], M, "%d\t%s\t%s", stu[i].id, stu[i].name, stu[i].class);
}
}
void save_to_file(char *str[], int n)
{
printf("# press \"c\" to set a custom file name\n");
printf("# press any other key to use default file name\n\n");
printf("choice: ");
char choice;
char filename[100];
FILE *fp;
scanf(" %c", &choice);
if (choice == 'c')
{
printf("enter a file name: ");
scanf("%99s", filename);
fp = fopen(filename, "w");
}
else
{
time_t t = time(NULL);
strftime(filename, sizeof(filename), "%Y-%m-%d_%H-%M-%S.txt", localtime(&t));
fp = fopen(filename, "w");
}
if (fp == NULL)
{
printf("\nfail to open\n");
return;
}
for (int i = 0; i < n; i++)
{
if (fprintf(fp, "%s\n", str[i]) < 0)
{
printf("Error\n");
break;
}
}
fclose(fp);
printf("\nfile saved as %s\n", filename);
}
int main()
{
FILE *fp = fopen("list.txt", "r");
if (fp == NULL)
{
printf("fail to open\n");
return 1;
}
char *data[M];
int cnt = 0;
while (cnt < M && !feof(fp))
{
data[cnt] = malloc(M * sizeof(char));
if (fgets(data[cnt], M, fp) != NULL)
{
data[cnt][strcspn(data[cnt], "\n")] = 0;
cnt++;
}
else
{
free(data[cnt]);
cnt--;
}
}
fclose(fp);
srand(time(NULL));
shuffle(data, cnt);
printf("---------\tRandom Picking\t---------\n\n");
char *picked[5];
for (int i = 0; i < 5; i++)
picked[i] = data[i];
sort(picked, 5);
for (int i = 0; i < 5; i++)
printf("%s\n", picked[i]);
printf("\n\n---------\tSave to File\t---------\n\n");
save_to_file(picked, 5);
for (int i = 0; i < cnt; i++)
free(data[i]);
return 0;
}

结果

posted @   Churk  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示