NUIST 《程序设计基础》 实验7
第七次实验课~
Task 4
<task4.c>
源代码
点击查看代码
#include <stdio.h>
int main() {
FILE *data4;
data4 = fopen("data4.txt", "r");
if(!data4) { printf("Open file failed.(data4.txt)"); return 1; }
int ch, lines=0, chars=0;
while(~(ch = fgetc(data4))) {/*
if(ch == "\n") lines++;
if(ch >= 'A' && ch <= 'z') chars++;*/
switch (ch) {
case ' ':
case '\r':
case '\t':
break;
case '\n':
lines++;
break;
default:
chars++;
break;
}
}
lines++;
fclose(data4);
printf("data4.txt统计结果: \n行数: %d\n字符数(不计空白符): %d", lines, chars);
}
运行结果

Task 5
<task5.c>
源代码
点击查看代码
#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);
}
// 从文本文件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);
}
// 把通过考试的考生完整信息写入文件list_pass.txt
// 准考证号,姓名,客观题得分,操作题得分,总分,结果
void write(STU s[], int n) {
FILE *passtxt;
passtxt = fopen("list_pass.txt", "w");
if (!passtxt) {
printf("Open file failed(list_pass.txt)");
return 1;
}
fprintf(passtxt, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
for (int i = 0; i < n; i++) {
fprintf(passtxt, "%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(passtxt);
}
// 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
int process(STU st[], int n, STU st_pass[]) {
int j=0;
for(int i=0; i<n; i++) {
st[i].sum = st[i].objective + st[i].subjective;
strcpy(st[i].result, st[i].sum > 60 ? "通过" : "未通过");
if(st[i].sum > 60)
st_pass[j++] = st[i];
}
return j;
}
运行结果


Task 6
<task6.c>
源代码
点击查看代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
long long xh;
char name[50];
char class[50];
} stu;
int read(stu list[]) {
FILE *stulist;
int n=0;
stulist = fopen("list.txt", "r");
if(!stulist) {
printf("list.txt open failed");
return 1;
}
while (~fscanf(stulist, "%lld %s %s", &list[n].xh, list[n].name, list[n].class)) n++;
return n;
}
void bubblesort(stu win[100]) {
for (int i = 0; i < 4; i++) {
int swapped = 0;
for (int j = 0; j < 5 - i - 1; j++) {
if (win[j].xh > win[j + 1].xh) {
stu tmp = win[j];
win[j] = win[j + 1];
win[j + 1] = tmp;
swapped = 1;
}
}
if (swapped == 0) break;
}
}
void output(int n, stu win[]) {
time_t t = time(NULL);
struct tm tm = *localtime(&t);
char filename[64];
snprintf(filename, sizeof(filename), "%04d%02d%02d.txt", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
FILE *out = fopen(filename, "w");
if (!out) printf("error...");
for (int i = 0; i < n; i++) {
fprintf(out, "%lld %s %s\n", win[i].xh, win[i].name, win[i].class);
}
fclose(out);
}
int main()
{
stu list[100];
int n = read(list);
//for(int i=0; i<n; i++) printf("%lld %s %s\n", list[i].xh, list[i].name, list[i].class);
int r = 114514;
stu win[100];
for(int i=0; i<5; i++) {
srand(((unsigned)time(NULL) + r) % (i+1145)*1419);
r = rand();
int realr = r % n;
win[i] = list[realr];
}
bubblesort(win);
for(int i=0; i<5; i++) {
printf("%lld %s %s\n", win[i].xh, win[i].name, win[i].class);
}
output(5, win);
return 0;
}
运行结果



浙公网安备 33010602011771号