task4
1 #include <stdio.h> 2 int main() { 3 FILE *fp; 4 fp=fopen("data4.txt","r"); 5 int lines = 0, chars = 0; 6 int i; 7 char s[100]; 8 9 if (fp == NULL) { 10 printf("fail to open file\n"); 11 return 1; 12 } 13 14 while (fgets(s, 100, fp)) { 15 lines++; 16 i = 0; 17 while (s[i] != '\0') { 18 if (s[i] != ' ' && s[i] != '\t' && s[i] != '\n') { 19 chars++; 20 } 21 i++; 22 } 23 } 24 25 fclose(fp); 26 printf("data4.txt统计结果:\n"); 27 printf("行数: %d\n", lines); 28 printf("字符数(不计空白符): %d\n", chars); 29 return 0; 30 }

task5
1 #include <stdio.h> 2 #include <string.h> 3 #define _CRT_SECURE_NO_WARNINGS 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[], 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, N); 34 35 pass_rate = 1.0 * cnt / N; 36 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 37 getchar(); 38 getchar(); 39 return 0; 40 } 41 42 43 void output(STU st[], int n) { 44 int i; 45 46 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 47 for (i = 0; i < n; i++) 48 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); 49 } 50 51 52 void read(STU st[], int n) { 53 int i; 54 FILE *fin; 55 56 fin = fopen("examinee.txt", "r"); 57 if (!fin) { 58 printf("fail to open file\n"); 59 return; 60 } 61 62 while (!feof(fin)) { 63 for (i = 0; i < n; i++) 64 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 65 } 66 67 fclose(fin); 68 } 69 70 void write(STU s[], int n) { 71 int i; 72 FILE *fin; 73 74 fin=fopen("D:\C语言任务\list.txt", "w"); 75 if (!fin) { 76 printf("fail to open file\n"); 77 return; 78 } 79 80 fprintf(fin,"准考证号\t姓名\t\t客观题得分\t\t操作题得分\t\t总分\t\t结果\n"); 81 for (i = 0; i < n; i++) { 82 if(s[i].sum>=60) 83 fprintf(fin,"%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 84 } 85 fclose(fin); 86 } 87 int process(STU st[], int n, STU st_pass[]) { 88 int passnum=0; 89 int i; 90 int j=0; 91 for(i=0;i<n;i++) { 92 st[i].sum=st[i].subjective+st[i].objective; 93 if(st[i].sum>=60) { 94 strcpy(st[i].result,"通过"); 95 96 st_pass[j]=st[i]; 97 j++; 98 passnum++; 99 } 100 else 101 strcpy(st[i].result,"不通过"); 102 103 } 104 return passnum; 105 106 107 }

task6
1 #include<stdio.h> 2 #include<string.h> 3 #include<time.h> 4 #include<stdlib.h> 5 6 #define N 5 7 #define M 80 8 9 struct student 10 { 11 char id[10]; 12 char name[10]; 13 char num_class[30]; 14 }STU; 15 16 int main() 17 { 18 struct student x[M], y[N]; 19 char name_file[50]; 20 char filePath[200]; 21 int i=0,j=0, k,number; 22 FILE *fp; 23 srand(time(NULL)); 24 25 printf("------------------20250525中奖名单-------------------\n"); 26 27 fp = fopen("list.txt","r"); 28 29 if(!fp) 30 { 31 printf("fail to open file to read\n"); 32 return 0; 33 } 34 35 while(!feof(fp)) 36 { 37 number = fscanf(fp, "%s%s%s", x[i].id, x[i].name,x[i].num_class); 38 if(number != 3) 39 break; 40 i++; 41 } 42 fclose(fp); 43 44 45 while(j<5) 46 { 47 k=rand()%79; 48 y[j++]=x[k]; 49 printf("%-10s%-10s%-30s\n", x[k].id, x[k].name, x[k].num_class); 50 } 51 52 printf("输入保存文件名:"); 53 scanf("%s",name_file); 54 sprintf(filePath, "list.txt", name_file); 55 56 fp=fopen(filePath,"w"); 57 58 if(fp==NULL) 59 { 60 printf("fail to open file to write\n"); 61 return 0; 62 } 63 64 for(j=0;j<N;++j) 65 { 66 fprintf(fp,"%-10s%-10s%-30s\n",y[j].id, y[j].name,y[j].num_class); 67 } 68 fclose(fp); 69 70 return 0; 71 }

浙公网安备 33010602011771号