尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

Experiment7

Experiment7
📅 发布时间:2026/6/20 4:18:27

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

 

相关新闻

  • GB/T 7714-2015标准与Zotero集成配置完全指南
  • YOLOv10-Pose发布:多人姿态估计GPU批处理技巧
  • 反射

最新新闻

  • 2026年可靠的青岛办公工学椅/青岛人体工学椅/工学椅/商务久坐工学椅公司哪家好 - 行业平台推荐
  • 2026年比较好的惠州 LED屏/会议室LED屏厂家对比推荐 - 品牌宣传支持者
  • MC68060处理器信号控制与MMU架构:嵌入式系统稳定性的硬件基石
  • 嵌入式系统性能优化:深入解析ColdFire微控制器Cache与SRAM配置实战
  • 2026年口碑好的苏州卷材 EVA/无味 EVA/EVA优质厂家汇总推荐 - 品牌宣传支持者
  • 龙虾智能体进阶实战:自定义Skill插件开发与多模型适配方案

日新闻

  • 信任的进化:技术实现详解——如何用JavaScript构建博弈论模拟器
  • Terrakube自定义工作流:如何集成OPA、Infracost等工具扩展IaC能力
  • grunt-concurrent快速入门:5分钟学会并行运行Grunt任务

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号