1 #include<stdio.h>
2 #include<stdlib.h>
3 #define N 80
4 int main(){
5 int i=0,j;
6 int linecount=0;
7 int charcount=0;
8 char info[N][80];
9 FILE *fp;
10
11 fp=fopen("d:\\data4.txt","r");
12 if(!fp){
13 printf("fail to open file to read\n");
14 return;
15 }
16 while(fgets(info[i],80,fp)!=NULL){
17 linecount++;
18 i++;
19 }
20 fclose(fp);
21
22 printf("data4.txt统计结果:\n");
23 for(i=0;i<N;i++)
24 for(j=0;j<N;j++)
25 if(info[i][j]>32&&info[i][j]<127)
26 charcount+=1;
27 printf("行数:\t\t%10d\n",linecount);
28 printf("字符数(不计空白符):%4d\n",charcount);
29 system("pause");
30 return 0;
31 }
![]()
1 #include <stdio.h>
2 #include <string.h>
3 #include<stdlib.h>
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 system("pause");
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 st[], int n) {
74 int i;
75 char str[]="通过";
76 FILE *fin;
77
78 fin = fopen("list_pass.txt", "w");
79 if (!fin) {
80 printf("fail to open fileto write\n");
81 return;
82 }
83 fprintf(fin,"准考证号\t姓名\t\t客观题得分\t操作题得分\t总分\t\t结果\n");
84 for (i = 0; i < n; i++)
85 if(!strcmp(st[i].result,str))
86 fprintf(fin,"%ld\t%s\t%.2f\t\t%.2f\t\t%.2f\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
87
88 fclose(fin);
89 }
90 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
91 int process(STU st[], int n, STU st_pass[]) {
92 int i,count=0;
93 char str1[]="通过",str2[]="未通过";
94 for(i=0;i<n;i++){
95 st[i].sum=st[i].objective+st[i].subjective;
96 if(st[i].sum>=60){
97 strcpy(st[i].result,str1);
98 count+=1;
99 }
100 else
101 strcpy(st[i].result,str2);
102 }
103 return count;
104 }
![]()
![]()
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<time.h>
4 #define N 80
5 int main(){
6 char info[N][80];
7 int flag[N]={0};
8 int i;
9 int lucky_index;
10 int win[N];
11 char filename[N];
12 FILE *fp;
13
14 fp = fopen("D:\\list.txt", "r");
15 if(!fp) {
16 printf("fail to open file to read\n");
17 return;
18 }
19
20 for(i=0;i<N;i++)
21 fgets(info[i],80,fp);
22 fclose(fp);
23
24 srand(time(NULL));
25 printf("------中奖名单------\n");
26 for(i=0;i<5;i++){
27 lucky_index=rand()%N;
28 while(flag[lucky_index])
29 lucky_index=rand()%N;
30 flag[lucky_index]=1;
31 printf("%s",info[lucky_index]);
32 win[i]=lucky_index;
33 }
34 printf("------保存到文件------\n");
35 printf("输入文件名:");
36 scanf("%s",filename);
37 fp = fopen(filename, "w");
38 if(!fp) {
39 printf("fail to open file to write\n");
40 return;
41 }
42 for(i=0;i<5;i++){
43 fputs(info[win[i]],fp);
44 fputs("\n",fp);
45 }
46 fclose(fp);
47 printf("保存成功\n");
48 system("pause");
49 return 0;
50 }
![]()