#include<stdio.h> #include<stdlib.h> int main(){ FILE *fin, *fout; char ch; fin = fopen("1.txt","r"); if(fin == NULL){ printf("fail to open 1 "); exit(0); } fout = fopen("2.txt","w");//如果加上了路径就会在所给路径中创建。 if(fout == NULL){ printf("fail to open 2 "); exit(0); } while(!feof(fin)){ ch = fgetc(fin);//fscanf(fin,"%c",&ch);可以改为这个 if(ch >= 'a' && ch <= 'z'){ ch = ch - 32; } fputc(ch,fout);//fprintf(fout,"%c",ch);可以改为这个 } fclose(fin); fclose(fout); return 0; }
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct student{ long num; char name[20]; int score; }STU; #define len sizeof(STU) #define N 10 int main(){ FILE *fp; fp = fopen("3.txt","r"); if(fp == NULL){ printf("fail to open 3"); exit(0); } STU s; int i; STU stmax ,stmin; for(i = 0 ; i < N ; i++){ //稍作修改能改为while if(i == 0){ fscanf(fp, "%d %s %d", &s.num ,s.name,&s.score); stmin = s; stmax = s; } else{ fscanf(fp, "%d %s %d", &s.num ,s.name,&s.score); if(s.score < stmin.score) stmin = s; else if (s.score > stmax.score) stmax = s; } } fclose(fp); printf("最高分学生信息: %5d%15s%5d ", stmax.num ,stmax.name , stmax.score ); printf("最低分学生信息: %5d%15s%5d ", stmin.num ,stmin.name , stmin.score ); return 0; }
#include<stdio.h> #include<stdlib.h> #define N 10 typedef struct student{ int num; char name[20]; int score; }STU; void sort(STU *pst, int n); int main(){ FILE *fin, *fout; STU st[N]; int i; fin = fopen("3.txt","r"); if(fin == NULL){ printf("fail to open 3 "); exit(0); } for( i = 0 ; i < N; i++) fscanf(fin, "%d %s %d",&st[i].num ,st[i].name ,&st[i].score); fclose(fin); sort(st,N); fout = fopen("4.txt","w");; if(fout == NULL){ printf("fail to open 4"); exit(0); } for( i = 0 ; i < N ; i++){ printf("%-6d%-10s%3d ",st[i].num ,st[i].name,st[i].score); fprintf(fout,"%-6d%-10s%3d ",st[i].num ,st[i].name,st[i].score); } fclose(fout); return 0; } void sort(STU *pst, int n){ STU *pi,*pj,t; for(pj = pst; pi < pst + n - 1; pi++) for(pj = pi + 1; pj < pst + n; pj++) if(pi->score < pj->score){ t = *pi; *pi = *pj; *pj = t; } }
#include<stdio.h> #include<stdlib.h> #define N 10 typedef struct student{ int num; char name[20]; int score; }STU; void sort(STU s[], int n); int main(){ FILE *fp1, *fp2,*fp3; STU s[N]; int i; fp1 = fopen("3.txt","r"); if(fp1 == NULL){ printf("fail to open3 "); exit(0); } for( i = 0 ;i < N; i++) fscanf(fp1,"%d %s %d",&s[i].num,s[i].name,&s[i].score); fclose(fp1); sort(s,N); fp2 = fopen("5.txt","wb"); for(i = 0 ; i < N ; i++){ printf("%d %s %d ",s[i].num, s[i].name, s[i].score); fwrite(&s[i], sizeof(STU),1,fp2); } fclose(fp2); printf(" "); fp3 = fopen("5.txt","rb"); if(fp3 == NULL){ printf("fail to open "); exit(0); } for(i = 0 ; i< N; i++){ fread(&s[i],sizeof(STU),1,fp3); printf("%d %s %d ",s[i].num,s[i].name,s[i].score); } fclose(fp3); return 0; } void sort(STU s[], int n){ int i, j,min; STU temp; for( i = n - 1; i > 0; i--){ j = i - 1; min = s[i].score; for(;j >= 0 ;j--){ if(s[j].score < min){ min = s[j].score; temp = s[i]; s[i] = s[j]; s[j] = temp; } } } }
#include<stdio.h> #include<string.h> #include<stdlib.h> #define N 10 typedef struct student{ int num; char name[20]; float kg; float zg; float sum; char level[10]; }STU; int main(){ FILE *fin, *fout; int i,j, perfect, pass,min; STU s[N]; fin = fopen("6.txt","r"); if(fin == NULL){ printf("fail to open 6.txt "); exit(0); } for(i = 0 ; i < N; i++){ fscanf(fin,"%d %s %f %f",&s[i].num ,s[i].name, &s[i].kg ,&s[i].zg ); s[i].sum = s[i].kg + s[i].zg; } fclose(fin); STU temp; for(i = N - 1 ;i > 0 ; i--){ j = i - 1; min = s[i].sum; for( ; j >= 0;j--){ if( s[j].sum < min){ min = s[j].sum; temp = s[i]; s[i] = s[j]; s[j] = temp; } } } perfect = N * 0.1; pass = N * 0.5; for(i = 0 ; i < N ;i++){ if(i < perfect ){ strcpy(s[i].level , "优"); } else if(i < pass){ strcpy(s[i].level , "合格"); } else{ strcpy(s[i].level , "不合格"); } } fout = fopen("7.txt","w"); if(fout == NULL){ printf("fail to open 7.txt "); exit(0); } for( i = 0 ; i < N; i++){ fprintf(fout, "%-6d%-15s%-8.2f%-8.2f%-8.2f%-10s ",s[i].num,s[i].name,s[i].kg,s[i].zg,s[i].sum,s[i].level); printf("%-6d%-15s%-8.2f%-8.2f%-8.2f%-10s ",s[i].num,s[i].name,s[i].kg,s[i].zg,s[i].sum,s[i].level); } fclose(fout); return 0; }
#include<stdio.h> #include<time.h> #include<stdlib.h> #define N 5 typedef struct student{ int num; int num1; char name[20]; int class1; }STU; int main(){ FILE *fin, *fout; STU s[12]; int a[N + 1]; a[0] = 0; int i , j, c; for( i = 1 ; i <= N ;i++){ do{ srand(time(0)); a[i] = rand() % 13; for( j = 0 ; j < i ;j++){//确保抽到的人不重复 c = 0; if(a[i] == a[j]){ c = 1; break; } } }while(c); } fin = fopen("8.txt","r"); if( fin == NULL){ printf("fail to open 8.txt"); exit(0); } for( i = 0 ; i < 12; i++){ fscanf(fin, "%d %d %s %d",&s[i].num ,&s[i].num1, s[i].name,&s[i].class1); } fclose(fin); fout = fopen("9.txt","w"); if(fout == NULL){ printf("fail to open 9.txt "); exit(0); } for( i = 0; i < 12 ;i++){ for(j = 1 ;j <= N; j++) { c = 0; if(s[i].num == a[j]){ c = 1; break; } } if(c){ printf("%d %5d %-15s %d ",s[i].num,s[i].num1,s[i].name,s[i].class1); fprintf(fout,"%d %5d %-15s %d ",s[i].num,s[i].num1,s[i].name,s[i].class1); } } fclose(fout); return 0; }
实验四两个实验合在一起了,实验五打完了才发现老师有给代码,实验六随机数生成的很慢,以及整个实验无法正常的读入中文,待解决。