当前位置: 首页 > news >正文

Experiment7

Task4:

 1 #include <stdio.h>
 2 #include <stdio.h>
 3 #include <ctype.h>
 4 
 5 int main() {
 6     FILE *file = fopen("data4.txt", "r");
 7     if (file == NULL) {
 8         printf("无法打开文件 data4.txt\n");
 9         return 1;
10     }
11 
12     int line_count = 0;
13     int char_count = 0;
14     int c;
15     int in_line = 0; 
16 
17     while ((c = fgetc(file)) != EOF) {
18         if (c == '\n') {
19             line_count++;
20             in_line = 0;
21         } else {
22             in_line = 1;
23             if (!isspace(c)) {
24                 char_count++;
25             }
26         }
27     }
28     if (in_line) {
29         line_count++;
30     }
31 
32     fclose(file);
33     printf("data4.txt统计结果:\n");
34     printf("行数:%d\n", line_count);
35     printf("字符数(不计空白符):%d\n", char_count);
36 
37     return 0;
38 }

Task4

Task5:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 10
 4 typedef struct {
 5 long id; // 准考证号
 6 char name[20]; // 姓名
 7 float objective; // 客观题得分
 8 float subjective; // 操作题得分
 9 float sum; // 总分
10 char result[10]; // 考试结果
11 } STU;
12 // 函数声明
13 void read(STU st[], int n);
14 void write(STU st[], int n);
15 void output(STU st[], int n);
16 int process(STU st[], int n, STU st_pass[]);
17 int main() {
18 STU stu[N], stu_pass[N];
19 int cnt;
20 double pass_rate;
21 printf("从文件读入%d个考生信息...\n", N);
22 read(stu, N);
23 printf("\n对考生成绩进行统计...\n");
24 cnt = process(stu, N, stu_pass);
25 printf("\n通过考试的名单:\n");
26 output(stu, N); // 输出所有考生完整信息到屏幕
27 write(stu, N); // 输出考试通过的考生信息到文件
28 pass_rate = 1.0 * cnt / N;
29 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
30 return 0;
31 }
32 // 把所有考生完整信息输出到屏幕上
33 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
34 void output(STU st[], int n) {
35 int i;
36 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
37 for (i = 0; i < n; i++)
38 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name,
39 st[i].objective, st[i].subjective, st[i].sum, st[i].result);
40 }
41 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
42 void read(STU st[], int n) {
43 int i;
44 FILE *fin;
45 fin = fopen("examinee.txt", "r");
46 if (!fin) {
47 printf("fail to open file\n");
48 return;
49 }
50 for (i = 0; i < n; i++)
51 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective,
52 &st[i].subjective);
53 fclose(fin);
54 }
55 void write(STU s[],int n){
56     int i;
57     FILE *fout;
58     fout = fopen("list_pass.txt","w");
59     if(!fout){
60         printf("fail to creat file\n");
61         return;
62     }
63     fprintf(fout,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
64     for(i=0;i<n;i++){
65         fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n",
66                 s[i].id,
67                 s[i].name,
68                 s[i].objective,
69                 s[i].subjective,
70                 s[i].sum,
71                 s[i].result);
72     }
73     fclose(fout);
74     printf("通过名单已成功写入文件 list_pass.txt\n");
75 }
76 int process(STU st[],int n,STU st_pass[]){
77     int i;
78     int pass_count=0;
79     for(i=0;i<n;i++){
80         st[i].sum=st[i].objective+st[i].subjective;
81         if(st[i].sum>=60){
82             strcpy(st[i].result,"通过");
83             st_pass[pass_count]=st[i];
84             pass_count++;
85         }
86         else{
87             strcpy(st[i].result,"未通过");
88         }
89     }
90     return pass_count;
91 }

Task5

Task6:

#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#define N 100
#define M 5
int main(){char s[N][N];char hit[M][N];int has_hit[N]={0};FILE *fin,*fout;int i,n,lucky_i,cnt=0;char filename[80];fin = fopen("list.txt","r");if(!fin){perror("list.txt");return 1;} i=0;while(fgets(s[i],N ,fin)!=NULL){++i;}n=i;srand(time(0));cnt=0;for(i=0;i<M;++i){lucky_i=rand()%n;if(has_hit[lucky_i])continue;has_hit[lucky_i]=1;strcpy(hit[i],s[lucky_i]);cnt++;}printf("中奖名单:\n");for(i=0;i<M;++i)printf("%s",hit[i]);printf("Enter fileName:");gets(filename);fout=fopen(filename,"w");if(!fout){perror(filename);return 1;}for(i=0;i<M;++i)fprintf(fout,"%s",hit[i]);fclose(fin);fclose(fout);return 0;
}

Task6.1

Task6.2

 

http://www.jsqmd.com/news/154776/

相关文章:

  • Swagger生成接口文档
  • 2025年浑南新初一补课学校哪个好,新初一补习班/新高一补课班/补课班/新高一补习班/成绩提升/外教/新高一补习/补课班怎么选 - 品牌推荐师
  • YOLO在停车场管理中的应用:车牌识别GPU集群部署
  • YOLOv10-Pose发布:多人姿态估计GPU批处理技巧
  • 实验:WPF-FBG测试
  • 反射
  • YOLO模型镜像支持GPU Isolation,保障多租户安全
  • YOLO模型镜像支持GPU Isolation,保障多租户安全
  • 在512MB記憶體中運行百萬級資料處理:Python記憶體優化的極限挑戰
  • YOLO单阶段检测原理详解:为什么它能实现实时推理?
  • Day70(7)-F:\硕士阶段\Java\课程资料\1、黑马程序员Java项目《苍穹外卖》企业级开发实战\sky-take-out-缓存redis
  • YOLO在体育动作分析中的应用:运动员轨迹GPU追踪
  • YOLO在体育动作分析中的应用:运动员轨迹GPU追踪
  • 快速构建MCP Server应用指南
  • Python工程師年薪從80萬到300萬:我掌握的10個高階技能清單
  • 学期回顾随笔
  • YOLO模型镜像集成Fluent Bit,GPU日志统一收集
  • 学Simulink--基础储能管理场景实例:基于Simulink的锂电池Thevenin等效电路建模与仿真
  • 你寫的 type hints,暴露了你的技術思維『基因序列』
  • 从技术理想主义到人性清醒:一位 Java 架构师关于“善意”与“回报”的深度顿悟
  • YOLO目标检测灰度发布:多版本模型共享GPU资源
  • Docker持久化存储完全指南:从新手到专家的数据管理技巧
  • HTTP和https的区别?
  • 从 Java 工程师到产品经理:技术人转型产品岗的系统化实战指南
  • YOLO在农业植保中的应用:无人机喷洒依赖GPU识别
  • YOLO在智慧城市中的应用:千万级摄像头靠GPU分析
  • YOLOv9-C-Dynamic发布:动态推理路径节省GPU资源
  • YOLO训练数据增强过度?可能导致GPU过拟合
  • 又好一阵子没有写博文了,乘着周日来写点
  • YOLOv9-C-Ghost发布:Ghost Bottleneck降低GPU计算量