c实验7
实验任务4 #include<stdio.h> #define M 80 int main() { char ch; FILE *fp; fp=fopen("d:/data4.txt","r"); if(fp==NULL) { printf("fail to open file\n"); return 1; } ch=fgetc(fp); int i=0; while(ch!=EOF) { if(ch!=' '&&ch!='\n'&&ch!=' ') i++; ch=fgetc(fp); } fclose(fp); printf("data4.txt中共包含字符数(不计空白符):%d",i); return 0; }
实验任务5 #include<stdio.h> #include<string.h> #include<stdlib.h> #define N 10 typedef struct { long int id; char name[20]; double objective; double subjective; double sum; char level[10]; }STU; void input(STU s[],int n); void output(STU s[],int n); void process(STU s[],int n); int main() { STU stu[N]; printf("从文件读入%d个考生信息:准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n",N); input(stu,N); printf("\n对考生信息进行处理:计算总分,确定等级\n"); process(stu,N); printf("\n打印考生完整信息,并保存到文件中"); output(stu,N); return 0; } void input(STU s[],int n) { int i; FILE *fin; fin=fopen("examinee.txt","r"); if(fin==NULL) { printf("fail to open file\n"); exit(0); } while(!feof(fin)) { for(i=0;i<n;i++) fscanf(fin,"%ld %s %lf %lf",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective); } fclose(fin); } void output(STU s[],int n) { FILE *fout; int i; printf("\n"); printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); for(i=0;i<n;i++) printf("%ld\t\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\t\t%s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); fout=fopen("result.txt","w"); if(!fout) { printf("fail to open or create result.txt\n"); exit(0); } fprintf(fout,"准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); for(i=0;i<n;i++) fprintf(fout,"%ld\t\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\t\t%s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); fclose(fout); } void process(STU s[],int n) { int i,j; STU t; for(i=0;i<n;i++) s[i].sum=s[i].objective+s[i].subjective; for(i=0;i<n;i++) for(j=0;j<n-i-1;j++) if(s[j].sum<s[j+1].sum) { t=s[j]; s[j]=s[j+1]; s[j+1]=t; } for(i=0;i<n;i++) { if(i+1<=n/10.0) strcpy(s[i].level,"优秀"); else if(i+1>n/10.0&&i+1<=n/2.0) strcpy(s[i].level,"合格"); else strcpy(s[i].level,"不合格"); } }
实验任务6(简单版) #include<stdio.h> #include<stdlib.h> #define N 80 #define M 5 #include<time.h> int main() { FILE *fp,*fout; int i,lu; char s[N][N]; fp=fopen("list.txt","r"); if(fp==NULL) { printf("fail to open file\n"); return 1; } for(i=0;i<N;i++) fgets(s[i],N,fp); fclose(fp); fout=fopen("lucky.txt","w"); if(fout==NULL) { printf("fail to open or create lucky.txt\n"); exit(0); } srand(time(0)); for(i=0;i<M;i++) { lu=rand()%N; printf("%s",s[lu]); fprintf(fout,"%s",s[lu]); } fclose(fout); return 0; }
实验任务6 #include<stdio.h> #include<stdlib.h> #define N 80 #define M 5 #include<time.h> #include<string.h> int main() { FILE *fp,*fout; int i,lu; int a[N]={0}; char s[N][N]; fp=fopen("list.txt","r"); if(fp==NULL) { printf("fail to open file\n"); return 1; } for(i=0;i<N;i++) fgets(s[i],N,fp); fclose(fp); time_t t=time(NULL); struct tm my_time; my_time.tm_year=localtime(&t)->tm_year; my_time.tm_mon=localtime(&t)->tm_mon; my_time.tm_mday=localtime(&t)->tm_mday; char str[26]; char buff[70]; strftime(buff,sizeof buff,"%Y%m%d",&my_time); strcat(buff,".txt"); fout=fopen(buff,"w"); if(fout==NULL) { printf("fail to open or create lucky.txt\n"); exit(0); } srand(time(0)); i=0; while(i<=M) { lu=rand()%N; if(a[lu]==0) { i+=1; printf("%s",s[lu]); fprintf(fout,"%s",s[lu]); } a[lu]=1; } fclose(fout); return 0; }
本次实验中我进一步巩固了各种读写文件的函数的用法。需要注意的地方,一是用完文件后要记得关闭,二是实验任务6中用while要确保i的初值为0,若用完for而不改i的值就用while,就会造成错误。利用老师提供的两个链接实现c语言中以日期为文件名,虽然比python的datetime库复杂,但是也是可以实现的,我从中巩固了strcat的用法,更深入地理解了结构体赋值的方法,受益匪浅。