Experiment 7
Task 4
![image-20251224215417178]()
#include <stdio.h>
int main(){
FILE* fp;
fp=fopen("./data4.txt","r");
char c;
int lines=0,chars=0;
while((c=fgetc(fp)) != EOF){
if (c != ' ' && c != '\t' && c != '\n'){
chars++;
}
else if(c == '\n'){
lines++;
}
}
fclose(fp);
printf("data4.txt统计结果:\n行数:\t\t%d\n字符数:\t%d",lines+1,chars);
return 0;
}
![image-20251224220612994]()
Task 5
![image-20251224220727827]()
#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, N); // 输出考试通过的考生信息到文件
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 st[], int n) {
int i;
FILE* fout;
fout = fopen("./list_pass.txt","w");
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", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
fclose(fout);
}
// 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
int process(STU st[], int n, STU st_pass[]) {
int i,ans=0;
for(i=0;i<n;i++){
st_pass[i].id = st[i].id;
strcpy(st_pass[i].name,st[i].name);
st_pass[i].objective = st[i].objective;
st_pass[i].subjective = st[i].subjective;
st[i].sum = st[i].subjective+st[i].objective;
st_pass[i].sum = st[i].sum;
if (st[i].sum >= 60.0){
strcpy(st_pass[i].result,"通过");
strcpy(st[i].result,"通过");
//printf("Log:%lf\t%d:%s\n",st[i].sum,st[i].id,st[i].result);
ans++;
}
else{
strcpy(st_pass[i].result,"不通过");
strcpy(st[i].result,"不通过");
}
}
return ans;
}
![image-20251224223333416]()
Task 7
![image-20251224223458803]()
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
typedef struct{
char info[50];//太小不行!!
char id[20];
}STU;
int cmp(const void *a, const void *b) {
const STU *pa = (const STU *)a;
const STU *pb = (const STU *)b;
return strcmp(pa->id,pb->id);
}
void strcpyid(char dest[],char src[]){
int i=0;
for(i;src[i]>='0' && src[i]<='9';i++){
dest[i]=src[i];
}
}
int main()
{
int random_variable,i=0,flag[100]={0},j;
char lines[100][100],filename[20];
FILE* fin;
FILE* fout;
fin = fopen("./list.txt","r");
//格式化获取当前时间->filename=time_str+".txt";
time_t t = time(NULL);
strftime(filename,sizeof(filename),"%Y%m%d",localtime(&t));
strcpy(filename+strlen(filename),".txt");//添加偏移量实现拼接
fout=fopen(filename,"w");
//加载文件
while(fgets(lines[i],100,fin) != NULL){
i++;
}
printf("Persisent path: ./%s\n",filename);
printf("-----获奖名单-----\n");
STU stu[5];
j = 5;
while(j){
srand(time(NULL));
random_variable = rand()%i;
if (flag[random_variable] != 1){
flag[random_variable] = 1;
strcpy(stu[5-j].info,lines[random_variable]);
strcpyid(stu[5-j].id,lines[random_variable]);
j--;
}
}
//for(j=0;j<5;j++){printf("%s",stu[j].info);}
qsort(stu,sizeof(stu)/sizeof(stu[0]),sizeof(stu[0]),cmp);//偷懒失败!cmp必须函数内强制转化类型??!
for(j=0;j<5;j++){
printf("%s",stu[j]);
fprintf(fout,"%s",stu[j]);
}
fclose(fin);
fclose(fout);
return 0;
}
![image]()