酒泉市网站建设_网站建设公司_GitHub_seo优化
2025/12/25 21:35:02 网站建设 项目流程

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

 

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询