yu-hong-yan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

实验任务4:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 int is_word(char x);
 4 int main(){
 5     FILE *fp;
 6     char ch;
 7     int i,line=1,count=0;
 8     
 9     fp=fopen("d:\\data4.txt","r");
10     if(!fp)
11     {
12         printf("fail to open file to read\n");
13         return 0;
14     }
15     
16     while((ch=fgetc(fp))!=EOF)
17     {
18     
19         if(is_word(ch))
20         {
21             count++;
22         }
23         if(ch=='\n')
24         {
25             line++;
26         }
27     }
28     fclose(fp);
29     printf("data4.txt统计结果:\n");
30     printf("行数: %20d\n", line);
31     printf("字符数(不计空白符): %7d\n", count);
32     
33     return 0;
34 }
35 int is_word(char x)
36 {
37     if(x!='\n'&&x!=' '&&x!='\t')
38     return 1;
39     else
40     return 0;
41 }

实验任务5:

  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 // 函数声明
 16 void read(STU st[], int n);
 17 void write(STU st[], int n);
 18 void output(STU st[], int n);
 19 int process(STU st[], int n, STU st_pass[]);
 20 
 21 int main() {
 22     STU stu[N], stu_pass[N];
 23     int cnt;
 24     double pass_rate;
 25 
 26     printf("从文件读入%d个考生信息...\n", N);
 27     read(stu, N);
 28 
 29     printf("\n对考生成绩进行统计...\n");
 30     cnt = process(stu, N, stu_pass);
 31 
 32     printf("\n通过考试的名单:\n");
 33     output(stu, N);   // 输出所有考生完整信息到屏幕
 34     write(stu, N);    // 输出考试通过的考生信息到文件
 35 
 36     pass_rate = 1.0 * cnt / N;
 37     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 38 
 39     return 0;
 40 }
 41 
 42 // 把所有考生完整信息输出到屏幕上
 43 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 44 void output(STU st[], int n) {
 45     int i;
 46     
 47     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 48     for (i = 0; i < n; i++)
 49         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);
 50 }
 51 
 52 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 53 void read(STU st[], int n) {
 54     int i;
 55     FILE *fin;
 56 
 57     fin = fopen("d:\\examinee.txt", "r");
 58     if (!fin) {
 59         printf("fail to open file\n");
 60         return;
 61     }
 62 
 63     while (!feof(fin)) {
 64         for (i = 0; i < n; i++)
 65             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 66     }
 67 
 68     fclose(fin);
 69 }
 70 
 71 // 把通过考试的考生完整信息写入文件list_pass.txt
 72 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 73 void write(STU s[], int n) {
 74     int i;
 75     STU stu_pass[N];
 76     int t=0;
 77     FILE *fp;
 78     for(i=0;i<n;i++)
 79     {
 80         if(s[i].sum>=60)
 81         {
 82             stu_pass[t++]=s[i];
 83         }
 84          
 85     }
 86     fp=fopen("d:\\list_pass.txt","w");
 87     if(fp==NULL)
 88     {
 89         printf("fail to open file to write\n");
 90         return;
 91     }
 92     fprintf(fp,"准考证号      姓名         客观题得分       操作题得分          总分            结果\n");
 93     for (i = 0; i < t; i++)
 94         fprintf(fp,"%d\t%10s\t%15.2f\t%15.2f\t%15.2f\t%10s\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);
 95     
 96     fclose(fp);
 97     
 98 }
 99 
100 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
101 int process(STU st[], int n, STU st_pass[]) {
102     int i,t=0;
103     for(i=0;i<n;i++)
104     {
105         st[i].sum=st[i].objective+st[i].subjective;
106         if(st[i].sum>=60)
107         {
108             strcpy(st[i].result,"通过");
109             st_pass[t++]=st[i]; 
110         }
111         else
112         strcpy(st[i].result,"不通过"); 
113     }
114     return t;
115 }

实验任务6:

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define N 80
 5 typedef struct{
 6     long int id;
 7     char name[20];
 8     char class_name[30];
 9 }STU;
10 int repeated(STU st[],STU random_select[],int random_index,int n);
11 void read(STU st[],int n);
12 void process(STU st[],int n,STU random_select[]);
13 void output(STU st[],int n);
14 int write(STU st[],int n,char *file_name);
15 int main()
16 {
17 STU stu[N],stu_select[N];
18     char file_name[20];
19     read(stu,N);
20     process(stu,N,stu_select);
21     printf("--------------随机抽点名单--------------\n");
22     output(stu_select,5);
23     printf("---------------保存到文件---------------\n");
24     printf("输入文件名:");
25     scanf("%s",file_name);
26     if(write(stu_select,5,file_name))
27     printf("保存成功!\n");
28     else
29     printf("保存失败!\n");
30         return 0;
31     
32 }
33 void output(STU st[],int n)
34 {
35     int i;
36     for (i = 0; i < n; i++)
37              printf("%ld\t%s\t%s\n", st[i].id, st[i].name, st[i].class_name);
38 }
39 void read(STU st[],int n)
40 {
41    int i;   
42    FILE *fin;
43  
44     fin = fopen("d:\\list.txt", "r");     
45     if (!fin) {        
46     printf("fail to open file\n");
47         return;     
48         }
49 
50     while (!feof(fin)) 
51     {       
52     for (i = 0; i < n; i++)
53     fscanf(fin, "%ld %s %s", &st[i].id, st[i].name, st[i].class_name);
54     }
55     fclose(fin);
56 }
57 void process(STU st[], int n, STU random_select[]) {     
58     int i,random_index;
59     srand((unsigned int)time(NULL));
60     for(i=0;i<5;i++)    
61     {         
62     random_index=rand()%n;
63     while(repeated(st,random_select,random_index,i))
64         {
65             random_index=rand()%n;
66         }
67     random_select[i] = st[random_index];
68 }
69 }
70 int write(STU st[], int n,char *file_name) {
71     int i;
72     FILE *fp;
73     
74     fp=fopen(file_name,"w");
75     if(fp==NULL)
76     {
77         printf("fail to open file to write\n");
78         return;
79     }
80 
81     for (i = 0; i < n; i++)
82         fprintf(fp,"%ld\t\t%10s\t\t%10s\n", st[i].id, st[i].name, st[i].class_name);
83     
84     fclose(fp);
85     return 1;
86     
87 }
88 int repeated(STU st[],STU random_select[],int random_index,int n)
89 {
90     int i;
91     for(i=0;i<n;i++)
92     {
93         if(st[random_index].id==random_select[i].id)
94         return 1;
95     }
96     return 0;
97 }

 

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <time.h>
  4 #define N 80
  5 typedef struct{
  6     long int id;
  7     char name[20];
  8     char class_name[30];
  9     
 10 }STU;
 11 int repeated(STU st[],STU random_select[],int random_index,int n);
 12 void read(STU st[],int n);
 13 void process(STU st[],int n,STU random_select[]);
 14 void output(STU st[],int n);
 15 int write(STU st[],int n,char *file_name);
 16 int main()
 17 {
 18     time_t t=time(NULL);
 19     STU stu[N],stu_select[N];
 20     char file_name[20];
 21     read(stu,N);
 22     process(stu,N,stu_select);
 23     
 24     strftime(file_name,sizeof file_name,"%Y%m%d.txt",localtime(&t));
 25        
 26     printf("--------------%s随机抽点名单--------------\n",file_name);
 27     output(stu_select,5);
 28     
 29     if(write(stu_select,5,file_name))
 30     printf("文件保存成功!\n");
 31     else
 32     printf("文件保存失败!\n");
 33     
 34     return 0;
 35     
 36 }
 37 void output(STU st[],int n)
 38 {
 39     int i;
 40     for (i = 0; i < n; i++)
 41         printf("%ld\t%s\t%s\n", st[i].id, st[i].name, st[i].class_name);
 42 }
 43 void read(STU st[],int n)
 44 {
 45     int i;
 46     FILE *fin;
 47 
 48     fin = fopen("d:\\list.txt", "r");
 49     if (!fin) {
 50         printf("fail to open file\n");
 51         return;
 52     }
 53 
 54     while (!feof(fin)) {
 55         for (i = 0; i < n; i++)
 56             fscanf(fin, "%ld %s %s", &st[i].id, st[i].name, st[i].class_name);
 57     }
 58 
 59     fclose(fin);
 60 }
 61 void process(STU st[], int n, STU random_select[]) {
 62     int i,j,random_index;
 63     STU t;
 64     srand((unsigned int)time(NULL));
 65     for(i=0;i<5;i++)
 66     {
 67         random_index=rand()%n;
 68         while(repeated(st,random_select,random_index,i))
 69         {
 70             random_index=rand()%n;
 71         }
 72         random_select[i] = st[random_index];
 73         }
 74 for(i=0;i<4;i++)
 75 {
 76     for(j=i+1;j<5;j++)
 77     {
 78         if(random_select[i].id>random_select[j].id)
 79         {
 80             t=random_select[i];
 81             random_select[i]=random_select[j];
 82             random_select[j]=t;
 83         }
 84     }
 85 }
 86 }
 87 
 88 
 89 int write(STU st[], int n,char *file_name) {
 90     int i;
 91     FILE *fp;
 92     
 93     fp=fopen(file_name,"w");
 94     if(fp==NULL)
 95     {
 96         printf("fail to open file to write\n");
 97         return;
 98     }
 99 
100     for (i = 0; i < n; i++)
101         fprintf(fp,"%ld\t\t%10s\t\t%10s\n", st[i].id, st[i].name, st[i].class_name);
102     
103     fclose(fp);
104     return 1;
105     
106 }
107 int repeated(STU st[],STU random_select[],int random_index,int n)
108 {
109     int i;
110     for(i=0;i<n;i++)
111     {
112         if(st[random_index].id==random_select[i].id)
113         return 1;
114     }
115     return 0;
116 }

 

posted on 2024-12-29 10:21  於鸿艳  阅读(7)  评论(0编辑  收藏  举报