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

实验七

task4

 1 #include<stdio.h>
 2 #include<ctype.h>
 3 
 4 int main(){
 5     int line=0,chars=0,ch;
 6     FILE *fp;
 7     
 8     fp=fopen("C:\\Users\\ZhuanZ(无密码)\\Desktop\\实验7数据文件及部分代码_gbk\\data4.txt","r");
 9     printf("data4.txt统计结果:\n");
10     if(!fp){
11         perror("data4.txt");
12         return 1;
13     }
14     while((ch=fgetc(fp))!=EOF){
15         if(!isspace(ch)){
16             chars++;
17         }
18     if(ch=='\n'){
19         line++;
20     }
21 }
22     ch=fgetc(fp);
23     if(ch!='\n')
24     line++;
25     
26     fclose(fp);
27     printf("字符数:%d\n",chars);
28     printf("行数:%d",line);
29     return 0;
30 }

image

 ```

task5

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

image

image

```

task6

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #define N 100
 5 
 6 int main()
 7 {
 8     char s[N][N];
 9     FILE *fp,*fp_out;
10     int i,n;
11     
12     fp=fopen("C:\\Users\\ZhuanZ(无密码)\\Desktop\\实验7数据文件及部分代码_gbk\\list.txt","r");
13     if(!fp){
14         perror("list.txt");
15         return 1;
16     }
17     i=0;
18     while(fgets(s[i],N,fp)!=NULL)
19     ++i;
20     n=i;
21     
22     char filename[N];
23     printf("输入文件名:");
24     scanf("%s",&filename);
25     fp_out=fopen(filename,"w");
26     if(!fp_out){
27         perror(filename);
28         return 1;
29     }
30     
31     int t;
32     int flag[N]={0};
33     srand(time(NULL));
34     for(i=0;i<5;++i){
35     do{
36     t=rand()%81;
37 }
38     while(flag[t]==1);
39         flag[t]=1;
40     printf("%s",s[t]);
41     fprintf(fp_out,"%s",s[t]);
42 
43 }
44     fclose(fp_out);
45     return 0;
46     
47 }

image

image

 

http://www.gsyq.cn/news/154006.html

相关文章:

  • 物联网智能灯具哪家品质好:最新官方排名品质测评 - 品牌测评家
  • 游戏手柄电池选购指南:性价比、性能与环保面面观 - 工业品网
  • 刘诗诗元气高马尾造型美出圈!剪彩时细节动作尽显温柔底色
  • 支持RV32E的单周期NPC
  • 无短板的网络体验!华为5A通信,让生活更“丝滑”
  • 中兴BE7200 MAX Wi-Fi7路由器首发679元:万兆SFP、全2.5G网口
  • HTML数据看板快速开发:DeepSeek生成代码+浏览器直接渲染实操指南
  • 软件工程学期回顾
  • 【课程设计/毕业设计】基于SpringBoot和Vue.js的智慧社区管理系统基于Vue.js的在线智慧社区服务平台【附源码、数据库、万字文档】
  • 写小说类型
  • 20251222 - 强连通分量 总结
  • 绩效考核需要考核什么内容?
  • 提高信噪比的操作
  • 抽象圣诞树
  • 全球化部署 多活多区域写入 → 汇总中心同步方案
  • 从化文旅宣传策划公司哪家好:98%用户满意度的优企现身 - 品牌测评家
  • 安徽省宣城市国控集团党委书记、董事长钱邦青一行到访国联股份卫多多
  • PS学习基础笔记
  • 机器学习时间特征处理:循环编码(Cyclical Encoding)与其在预测模型中的应用
  • 4 倍扩容 + 700 + 流程图极速展示!ProDB×TDengine 赋能泰州石化
  • Flash download tool
  • 计算机基础小题
  • 从数据瓶颈到ROAS飙升21%!Skygo牵手热力引擎,按下游戏增长快进键
  • 元旦
  • SQL 经典面试题
  • 2025信创大事件盘点:从“根基”到“生态”,自主之路迈入新纪元
  • 2025年终AI搜索优化服务商TOP推荐:影响大模型答案的核心变量全解析 - 速递信息
  • 2025国内最新风管/通风管/软管/高温管/伸缩管品牌首要推荐嵘鑫风管:服务于广州广东湖南等地,优质厂家深耕通风领域,这家实力出圈 - 全局中转站
  • uniapp开发微信公众号使用fixed固定定位,苹果手机出现内容不显示问题
  • 英伟达与AI芯片竞争对手Groq达成授权协议并聘用其CEO