实验七

任务4

#include <stdio.h>
#include <ctype.h>

int main() {
FILE *fp;
char ch;
int line_count = 0;
int char_count = 0;


fp = fopen("data4.txt", "r");
if (fp == NULL) {
printf("无法打开文件!\n");
return 1;
}


while ((ch = fgetc(fp)) != EOF) {

if (ch == '\n') {
line_count++;
}

if (!isspace(ch)) {
char_count++;
}
}

if (line_count == 0 && char_count > 0) {
line_count = 1;
} else if (ch == EOF && (char_count > 0 || line_count > 0)) {
line_count++;
}


fclose(fp);


printf("data4.txt统计结果:\n");
printf("行数:%d\n", line_count);
printf("字符数(不计空白符):%d\n", char_count);

return 0;
}

image

 任务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 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;
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);
}


void write(STU s[], int n) {
FILE *fout;
int i;
fout = fopen("list_pass.txt", "w");
if (!fout) {
printf("fail to create file\n");
return;
}

fprintf(fout, "准考证号\t姓名\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;
}

image

 

任务6

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define STUDENT_NUM 80
#define WIN_NUM 5


typedef struct {
char id[20];
char name[20];
char className[30];
} Student;


void readStudents(Student stu[], int n) {
FILE *fp = fopen("list.txt", "r");
if (fp == NULL) {
printf("无法打开list.txt文件!\n");
exit(1);
}
for (int i = 0; i < n; i++) {
fscanf(fp, "%s %s %s", stu[i].id, stu[i].name, stu[i].className);
}
fclose(fp);
}


void randomSelect(Student stu[], int total, Student win[], int winNum) {
int selected[STUDENT_NUM] = {0};
srand((unsigned int)time(NULL));

for (int i = 0; i < winNum; ) {
int idx = rand() % total;
if (!selected[idx]) {
win[i] = stu[idx];
selected[idx] = 1;
i++;
}
}
}


void printWinners(Student win[], int winNum) {
printf("----------中奖名单----------\n");
for (int i = 0; i < winNum; i++) {
printf("%s\t%s\t%s\n", win[i].id, win[i].name, win[i].className);
}
printf("----------保存到文件----------\n");
}


void saveWinners(Student win[], int winNum) {
char filename[50];
printf("输入文件名:");
scanf("%s", filename);

FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("文件创建失败!\n");
return;
}
for (int i = 0; i < winNum; i++) {
fprintf(fp, "%s\t%s\t%s\n", win[i].id, win[i].name, win[i].className);
}
fclose(fp);
printf("保存成功!\n");
}

int main() {
Student allStu[STUDENT_NUM];
Student winStu[WIN_NUM];


readStudents(allStu, STUDENT_NUM);
randomSelect(allStu, STUDENT_NUM, winStu, WIN_NUM);

printWinners(winStu, WIN_NUM);

saveWinners(winStu, WIN_NUM);

return 0;
}

image

 

posted @ 2025-12-31 18:08  程夕  阅读(1)  评论(1)    收藏  举报