实验7
任务4
源代码
#include<stdio.h> #define N 100 int main(){ char x[N]; int i,j,a,b; i=j=b=0; a=1; FILE *fp; fp=fopen("data4.txt","r"); if(!fp){ printf("fail to open file to read\n"); return; } while(x[i]!=EOF){ x[i]=fgetc(fp); i++; } while(x[j]!=EOF){ if(x[j]==' '||x[j]=='\n'||x[j]=='\t'){ if(x[j]=='\n') a++; } else b++; j++; } printf("data4.txt统计结果: \n 行数: \t %d \n字符数(不计空白符) \t %d",a,b); return 0; }
图片

任务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("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 FILE *fp; 76 fp=fopen("list_pass.txt","w"); 77 if(!fp){ 78 printf("fail to open file to write\n"); 79 return; 80 } 81 for(i=0;i<n;i++){ 82 if(s[i].sum>=60){ 83 fprintf(fp,"%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].result); 84 } 85 } 86 fclose(fp); 87 } 88 // ¶Ô¿¼ÉúÐÅÏ¢½øÐд¦Àí£º¼ÆËãÿλ¿¼Éú¿¼ÊÔ×Ü·Ö¡¢½á¹û£»Í³¼Æ¿¼ÊÔͨ¹ýµÄÈËÊý 89 int process(STU st[], int n, STU st_pass[]) { 90 int i,cnt,j; 91 cnt=j=0; 92 for(i=0;i<n;i++){ 93 st[i].sum=st[i].objective+st[i].subjective; 94 if(st[i].sum>=60){ 95 cnt++; 96 strcpy(st[i].result,"ͨ¹ý"); 97 st_pass[j]=st[i]; 98 j++; 99 } 100 else strcpy(st[i].result,"²»Í¨¹ý"); 101 } 102 return cnt; 103 104 }
图片


实验6
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #define N 80 5 typedef struct{ 6 int id; 7 char name[10]; 8 char class[20]; 9 }STU; 10 void read(STU x[]); 11 void write(STU y[],char *fn); 12 int main(){ 13 STU x[N]; 14 STU y[5]; 15 read(x); 16 int z[5]; 17 int i,j; 18 int num; 19 srand(time(0)); 20 for(i=0;i<5;i++){ 21 num=rand()%80; 22 z[i]=num; 23 } 24 for(i=0;i<5;i++){ 25 for(j=0;j<N;j++){ 26 if(x[j].id%100==z[i]){ 27 y[i]=x[i]; 28 continue; 29 } 30 } 31 } 32 printf("--------------随机抽点名单-------------\n"); 33 for(j=0;j<5;j++){ 34 printf("%-20d %-20s %-20s\n",y[j].id,y[j].name,y[j].class); 35 } 36 printf("--------------保存到文件--------------\n"); 37 printf("输入文件名:"); 38 char fn[100]; 39 scanf("%99s",fn); 40 printf("保存成功"); 41 write(y,fn); 42 return 0; 43 } 44 void read(STU x[]){ 45 int i=0; 46 FILE *fp; 47 fp=fopen("list.txt","r"); 48 if(!fp){ 49 printf("fail to open file\n"); 50 return; 51 } 52 while(!feof(fp)){ 53 fscanf(fp,"%d %s %s",&x[i].id,x[i].name,x[i].class); 54 i++; 55 } 56 fclose(fp); 57 } 58 void write(STU y[],char *fn){ 59 int i; 60 FILE *fp; 61 fp = fopen(fn,"w"); 62 if(!fp){ 63 printf("fail to open file\n"); 64 return; 65 } 66 for(i=0;i<5;i++){ 67 fprintf(fp,"%d %s %s\n",y[i].id,y[i].name,y[i].class); 68 } 69 fclose(fp); 70 }
图片



浙公网安备 33010602011771号