实验7
任务4
源代码
#include <stdio.h>
#include<stdlib.h>
int main(){
int ch;
int line=0;
int charans=0;
int flagnewline=1;
FILE *fp=fopen("data4.txt","r");
if(fp==NULL){
printf("打开文件失败!\n");
return 0;
}
while((ch=fgetc(fp))!=EOF){
if(ch=='\n'){
line++;
flagnewline=1;
}
else if(ch!=' '&&ch!='\t'&&ch!='\r'){
charans++;
flagnewline=0;
}
}
if(!flagnewline)
line++;
fclose(fp);
printf("data4.txt统计结果:\n");
printf("行数:%d\n",line);
printf("字符数(不计空白符):%d\n",charans);
return 0;
}
结果截图

任务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);
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 passnum=0;
int i;
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[passnum++]=st[i];
}
else
strcpy(st[i].result,"未通过");
}
return passnum;
}
// 把通过考试的考生完整信息写入文件list_pass.txt
// 准考证号,姓名,客观题得分,操作题得分,总分,结果
void write(STU st[], int n) {
int i;
FILE *fp=fopen("list_pass.txt","w");
if(!fp){
printf("写入文件失败\n");
return;}
fprintf(fp,"准考证号\t姓名\t\t客观题得分\t\t操作题得分\t\t总分\t\t结果\n");
for(i=0;i<n;i++){
fprintf(fp,"%ld\t%12s\t%12.2f\t\t%12.2f\t\t%12.2f\t\t%12s\n",st[i].id,st[i].name,st[i].objective,st[i].subjective,st[i].sum,st[i].result);
}
fclose(fp);
}
结果截图


任务6
源代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define max_stu 80
#define luck_count 5
typedef struct
{
char id[20];
char name[30];
char classname[50];
} stu;
stu allstu[max_stu];
int stutotal = 0;
int readstu();
void getluck(stu luck[]);
void sortluck(stu luck[]);
void savebyinput(stu luck[]);
void savebydate(stu luck[]);
int main()
{
stu luckarr[luck_count];
if (readstu() <= 0)
{
system("pause");
return 1;
}
getluck(luckarr);
sortluck(luckarr);
savebyinput(luckarr);
savebydate(luckarr);
system("pause");
return 0;
}
int readstu()
{
FILE *fp;
fp = fopen("list.txt", "r");
if (fp == NULL)
{
printf("无法打开list.txt,请检查文件位置\n");
return -1;
}
memset(allstu, 0, sizeof(allstu));
stutotal = 0;
while (fscanf(fp, "%s %s %s", allstu[stutotal].id, allstu[stutotal].name, allstu[stutotal].classname) == 3)
{
stutotal++;
if (stutotal >= max_stu)
break;
}
fclose(fp);
printf("成功读取%d位学生信息\n", stutotal);
return stutotal;
}
void getluck(stu luck[])
{
int flag[max_stu] = {0};
int i, cnt, pos;
cnt = 0;
srand((unsigned)time(NULL));
while (cnt < luck_count)
{
pos = rand() % stutotal;
if (flag[pos] == 0)
{
flag[pos] = 1;
luck[cnt++] = allstu[pos];
}
}
}
void sortluck(stu luck[])
{
int i, j;
stu temp;
for (i = 0; i < luck_count - 1; i++)
{
for (j = 0; j < luck_count - 1 - i; j++)
{
if (strcmp(luck[j].id, luck[j + 1].id) > 0)
{
temp = luck[j];
luck[j] = luck[j + 1];
luck[j + 1] = temp;
}
}
}
}
void savebyinput(stu luck[])
{
char filename[50];
FILE *fp;
int i;
printf("\n请输入保存文件名称:");
scanf("%s", filename);
fp = fopen(filename, "w");
if (fp == NULL)
{
printf("文件创建失败\n");
return;
}
fprintf(fp, "学号\t姓名\t班级\n");
printf("=====中奖名单=====\n");
for (i = 0; i < luck_count; i++)
{
fprintf(fp, "%s\t%s\t%s\n", luck[i].id, luck[i].name, luck[i].classname);
printf("%s\t%s\t%s\n", luck[i].id, luck[i].name, luck[i].classname);
}
fclose(fp);
printf("保存成功!\n");
}
void savebydate(stu luck[])
{
char filename[50];
FILE *fp;
time_t t;
struct tm *now;
int i;
t = time(NULL);
now = localtime(&t);
strftime(filename, sizeof(filename), "%Y%m%d.txt", now);
fp = fopen(filename, "w");
fprintf(fp, "学号\t姓名\t班级\n");
for (i = 0; i < luck_count; i++)
{
fprintf(fp, "%s\t%s\t%s\n", luck[i].id, luck[i].name, luck[i].classname);
}
fclose(fp);
printf("自动生成日期文件:%s\n", filename);
}
结果截图



浙公网安备 33010602011771号