复试编程训练十二

亦凉 2023-07-18 09:42 109阅读 0赞

111.从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件”file.txt”中保存,输入的字符串以“!”结束。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. char ch;
  6. FILE *fp;
  7. if((fp=fopen("file.txt","w"))==NULL)
  8. {
  9. printf("can't open file!\n");
  10. exit(0);
  11. }
  12. while((ch=getchar())!='!')
  13. {
  14. if(ch>='a'&&ch<='z') //小写字母转换成大写字母
  15. ch=ch-32;
  16. fputc(ch,fp); //存入文件中
  17. putchar(ch); //输出到屏幕上
  18. }
  19. putchar('\n');
  20. fclose(fp);
  21. return 0;
  22. }

运行结果:

20200326161407359.PNG

20200326161440189.PNG

112.有两个磁盘文件“A”和“B”,各放一行字母,今要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件“C”中去

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. char a[100],ch,temp;
  6. int i,j,k;
  7. FILE *fp1,*fp2,*fp3;
  8. if((fp1=fopen("file1.txt","r"))==NULL) //将文件file1中的字母存放到数组a中
  9. {
  10. printf("can't open file!\n");
  11. exit(0);
  12. }
  13. i=0;
  14. while((ch=fgetc(fp1))!=EOF) //若用!feof(fp1),后面会多复制空格,所以不能用
  15. {
  16. a[i++]=ch;
  17. putchar(ch);
  18. }
  19. fclose(fp1);
  20. if((fp2=fopen("file2.txt","r"))==NULL) //将文件file2中的字母存放到数组a中
  21. {
  22. printf("can't open file!\n");
  23. exit(0);
  24. }
  25. while((ch=fgetc(fp2))!=EOF)
  26. {
  27. a[i++]=ch;
  28. putchar(ch);
  29. }
  30. fclose(fp2);
  31. a[i]='\0';
  32. for(j=0; j<i-1; j++) //进行冒泡排序
  33. for(k=0; k<i-1-j; k++)
  34. {
  35. if(a[k]>a[k+1])
  36. {
  37. temp=a[k];
  38. a[k]=a[k+1];
  39. a[k+1]=temp;
  40. }
  41. }
  42. printf("\n%s",a);
  43. if((fp3=fopen("file3.txt","w"))==NULL) //将数组a中的字母存到文件file3中
  44. {
  45. printf("can't open file!\n");
  46. exit(0);
  47. }
  48. for(j=0; j<i; j++)
  49. fputc(a[j],fp3);
  50. fclose(fp3);
  51. putchar('\n');
  52. return 0;
  53. }

运行结果:

2020032616502265.PNG

20200326165040877.PNG

113.有5个学生,每个学生有3门课程的成绩,从键盘输入学生数据(包括学号、姓名,3门课程成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件“stud”中。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define N 3
  4. struct Student //学生结构体
  5. {
  6. long num;
  7. char name[15];
  8. float score[3];
  9. float avg;
  10. } stud[N];
  11. int main()
  12. {
  13. int i,j;
  14. float sum;
  15. FILE *fp;
  16. for(i=0; i<N; i++) //从键盘输入数据
  17. {
  18. sum=0;
  19. scanf("%ld %s ",&stud[i].num,stud[i].name);
  20. for(j=0; j<3; j++)
  21. {
  22. scanf("%f",&stud[i].score[j]);
  23. sum+=stud[i].score[j];
  24. }
  25. stud[i].avg=sum/3.0;
  26. }
  27. if((fp=fopen("stud.txt","w"))==NULL)
  28. {
  29. printf("can't open file!\n");
  30. exit(0);
  31. }
  32. for(i=0; i<N; i++) //写入文件stud.txt中
  33. {
  34. if(fwrite(&stud[i],sizeof(struct Student),1,fp)!=1)
  35. printf("File write error!\n");
  36. }
  37. fclose(fp);
  38. if((fp=fopen("stud.txt","r"))==NULL)
  39. {
  40. printf("can't open file!\n");
  41. exit(0);
  42. }
  43. printf("打印文件数据:\n");
  44. for(i=0; i<N; i++) //从文件stud.txt中读出数据
  45. {
  46. fread(&stud[i],sizeof(struct Student),1,fp);
  47. printf("%ld %s ",stud[i].num,stud[i].name);
  48. for(j=0; j<3; j++)
  49. printf("%.2f ",stud[i].score[j]);
  50. printf("%.2f\n",stud[i].avg);
  51. }
  52. fclose(fp);
  53. return 0;
  54. }

运行结果:

20200327104008140.PNG

114.将上例“stud”文件中的学生数据,按平均成绩进行排序处理,将已排好序的学生存入一个新文件“stud_sort”中。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define N 3
  4. struct Student //学生结构体
  5. {
  6. long num;
  7. char name[15];
  8. float score[3];
  9. float avg;
  10. } stud[N];
  11. int main()
  12. {
  13. int i,j;
  14. FILE *fp;
  15. struct Student temp;
  16. if((fp=fopen("stud.txt","r"))==NULL)
  17. {
  18. printf("can't open file!\n");
  19. exit(0);
  20. }
  21. printf("stud.txt文件中已有数据:\n");
  22. for(i=0; i<N; i++) //从文件stud.txt中读出数据
  23. {
  24. fread(&stud[i],sizeof(struct Student),1,fp);
  25. printf("%ld %s ",stud[i].num,stud[i].name);
  26. for(j=0; j<3; j++)
  27. printf("%.2f ",stud[i].score[j]);
  28. printf("%.2f\n",stud[i].avg);
  29. }
  30. fclose(fp);
  31. for(i=0; i<N-1; i++) //进行冒泡排序
  32. for(j=0; j<N-i-1; j++)
  33. if(stud[j].avg>stud[j+1].avg)
  34. {
  35. temp=stud[j];
  36. stud[j]=stud[j+1];
  37. stud[j+1]=temp;
  38. }
  39. if((fp=fopen("stud_sort.txt","w"))==NULL)
  40. {
  41. printf("can't open file!\n");
  42. exit(0);
  43. }
  44. for(i=0; i<N; i++) //写入stud_sort.txt文件中
  45. {
  46. if(fwrite(&stud[i],sizeof(struct Student),1,fp)!=1)
  47. printf("File write error!\n");
  48. }
  49. fclose(fp);
  50. if((fp=fopen("stud_sort.txt","r"))==NULL)
  51. {
  52. printf("can't open file!\n");
  53. exit(0);
  54. }
  55. printf("stud_sort.txt文件中的数据:\n");
  56. for(i=0; i<N; i++) //从文件stud_sort.txt中读出数据
  57. {
  58. fread(&stud[i],sizeof(struct Student),1,fp);
  59. printf("%ld %s ",stud[i].num,stud[i].name);
  60. for(j=0; j<3; j++)
  61. printf("%.2f ",stud[i].score[j]);
  62. printf("%.2f\n",stud[i].avg);
  63. }
  64. fclose(fp);
  65. return 0;
  66. }

运行结果:

20200327105326822.PNG

115.将上例已排序的学生成绩进行插入处理。插入一个学生的3门课程成绩,程序先计算新插入学生的平均成绩,然后将它按成绩高低插入,插入后建立一个新文件。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define N 3
  4. struct Student //学生结构体
  5. {
  6. long num;
  7. char name[15];
  8. float score[3];
  9. float avg;
  10. } stud[N+1];
  11. int main()
  12. {
  13. int i,j;
  14. FILE *fp;
  15. struct Student temp;
  16. float sum=0;
  17. scanf("%ld %s ",&stud[3].num,stud[3].name);//输入一个学生的数据
  18. scanf("%f %f %f",&stud[3].score[0],&stud[3].score[1],&stud[3].score[2]);
  19. sum=stud[3].score[0]+stud[3].score[1]+stud[3].score[2];
  20. stud[3].avg=sum/3.0;
  21. if((fp=fopen("stud_sort.txt","r"))==NULL)
  22. {
  23. printf("can't open file!\n");
  24. exit(0);
  25. }
  26. for(i=0; i<N; i++) //从文件stud_sort.txt中读出数据
  27. fread(&stud[i],sizeof(struct Student),1,fp);
  28. fclose(fp);
  29. for(i=0; i<N; i++) //进行冒泡排序
  30. for(j=0; j<N-i; j++)
  31. if(stud[j].avg>stud[j+1].avg)
  32. {
  33. temp=stud[j];
  34. stud[j]=stud[j+1];
  35. stud[j+1]=temp;
  36. }
  37. if((fp=fopen("stud_sort1.txt","w"))==NULL)
  38. {
  39. printf("can't open file!\n");
  40. exit(0);
  41. }
  42. for(i=0; i<N+1; i++) //写入stud_sort1.txt文件中
  43. {
  44. if(fwrite(&stud[i],sizeof(struct Student),1,fp)!=1)
  45. printf("File write error!\n");
  46. }
  47. fclose(fp);
  48. if((fp=fopen("stud_sort1.txt","r"))==NULL)
  49. {
  50. printf("can't open file!\n");
  51. exit(0);
  52. }
  53. printf("读出stud_sort1.txt文件中的数据:\n");
  54. for(i=0; i<N+1; i++) //读出stud_sort1.txt文件中的数据
  55. {
  56. fread(&stud[i],sizeof(struct Student),1,fp);
  57. printf("%ld %s ",stud[i].num,stud[i].name);
  58. for(j=0; j<3; j++)
  59. printf("%.2f ",stud[i].score[j]);
  60. printf("%.2f\n",stud[i].avg);
  61. }
  62. fclose(fp);
  63. return 0;
  64. }

运行结果:

20200327134616891.PNG

116.有一磁盘文件“Employee”,内放职工的数据。每个职工的数据包括职工姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度。今要求将职工名、工资的信息单独抽出来另建一个简明的职工工资文件。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define N 3
  5. struct Employee //职工结构体
  6. {
  7. long num;
  8. char name[15];
  9. char sex;
  10. int age;
  11. char addr[20];
  12. int salary;
  13. char health[8];
  14. char clas[10];
  15. } employee[N];
  16. struct Emp //简洁职工结构体
  17. {
  18. char name[15];
  19. int salary;
  20. } emp[N];
  21. int main()
  22. {
  23. FILE *fp;
  24. int i;
  25. printf("输入职工详细信息:\n");
  26. for(i=0; i<N; i++) //键盘输入职工详细信息
  27. scanf("%ld %s %c %d %s %d %s %s",&employee[i].num,employee[i].name,&employee[i].sex,&employee[i].age,employee[i].addr,&employee[i].salary,employee[i].health,employee[i].clas);
  28. if((fp=fopen("employee.txt","w"))==NULL)
  29. {
  30. printf("can't open file!\n");
  31. exit(0);
  32. }
  33. for(i=0; i<N; i++) //写入employee.txt文件中
  34. {
  35. if(fwrite(&employee[i],sizeof(struct Employee),1,fp)!=1)
  36. printf("File write error!\n");
  37. }
  38. fclose(fp);
  39. if((fp=fopen("employee.txt","r"))==NULL)
  40. {
  41. printf("can't open file!\n");
  42. exit(0);
  43. }
  44. for(i=0; i<N; i++) //读出employee.txt文件中的数据,并传数据
  45. {
  46. fread(&employee[i],sizeof(struct Employee),1,fp);
  47. strcpy(emp[i].name,employee[i].name);
  48. emp[i].salary=employee[i].salary;
  49. }
  50. fclose(fp);
  51. if((fp=fopen("emp.txt","w"))==NULL)
  52. {
  53. printf("can't open file!\n");
  54. exit(0);
  55. }
  56. for(i=0; i<N; i++) //将数据写入emp.txt文件中
  57. {
  58. if(fwrite(&emp[i],sizeof(struct Emp),1,fp)!=1)
  59. printf("File write error!\n");
  60. }
  61. fclose(fp);
  62. if((fp=fopen("emp.txt","r"))==NULL)
  63. {
  64. printf("can't open file!\n");
  65. exit(0);
  66. }
  67. printf("打印简略职工信息:\n");
  68. for(i=0; i<N; i++) //读出emp.txt文件中的数据
  69. {
  70. fread(&emp[i],sizeof(struct Emp),1,fp);
  71. printf("%s %d\n",emp[i].name,emp[i].salary);
  72. }
  73. fclose(fp);
  74. return 0;
  75. }

运行结果:

20200327143104565.PNG

117.从上题“职工工资文件”中删去一个职工的数据,再存回原文件。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define N 3
  5. struct Emp //简洁职工结构体
  6. {
  7. char name[15];
  8. int salary;
  9. } emp[N];
  10. int main()
  11. {
  12. FILE *fp;
  13. int i,j;
  14. char n[15]; //要删除的职工的名字
  15. printf("输入职工工资文件数据:\n");
  16. for(i=0; i<N; i++) //键盘输入职工工资文件数据
  17. scanf("%s %d",emp[i].name,&emp[i].salary);
  18. if((fp=fopen("emp.txt","w"))==NULL)
  19. {
  20. printf("can't open file!\n");
  21. exit(0);
  22. }
  23. for(i=0; i<N; i++)
  24. {
  25. if(fwrite(&emp[i],sizeof(struct Emp),1,fp)!=1)
  26. printf("File write error!\n");
  27. }
  28. fclose(fp);
  29. if((fp=fopen("emp.txt","r"))==NULL)
  30. {
  31. printf("can't open file!\n");
  32. exit(0);
  33. }
  34. printf("读出职工工资文件数据:\n");
  35. for(i=0; i<N; i++) //读出emp.txt文件中的数据
  36. {
  37. fread(&emp[i],sizeof(struct Emp),1,fp);
  38. printf("%s %d\n",emp[i].name,emp[i].salary);
  39. }
  40. fclose(fp);
  41. printf("输入要删除的职工的名字:\n"); //进行删除
  42. scanf("%s",n);
  43. for(i=0; i<N; i++)
  44. if(strcmp(emp[i].name,n)==0)
  45. break;
  46. for(j=i+1; j<N; j++)
  47. {
  48. strcpy(emp[j-1].name,emp[j].name);
  49. emp[j-1].salary=emp[j].salary;
  50. }
  51. if((fp=fopen("emp.txt","w"))==NULL)
  52. {
  53. printf("can't open file!\n");
  54. exit(0);
  55. }
  56. for(i=0; i<N-1; i++) //将前N-1个数据写入emp.txt文件中
  57. {
  58. if(fwrite(&emp[i],sizeof(struct Emp),1,fp)!=1)
  59. printf("File write error!\n");
  60. }
  61. fclose(fp);
  62. if((fp=fopen("emp.txt","r"))==NULL)
  63. {
  64. printf("can't open file!\n");
  65. exit(0);
  66. }
  67. printf("读出删除后的职工工资文件数据:\n");
  68. for(i=0; i<N-1; i++) //读出emp.txt文件中的数据
  69. {
  70. fread(&emp[i],sizeof(struct Emp),1,fp);
  71. printf("%s %d\n",emp[i].name,emp[i].salary);
  72. }
  73. fclose(fp);
  74. return 0;
  75. }

运行结果:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xZXzYyNA_size_16_color_FFFFFF_t_70

118.从键盘输入若干行字符(每行长度不等),输入后把它们存储到一磁盘文件中。再从该文件中读入这些数据,将其中小写字母转换成大写字母后,在屏幕上输出。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. int flag=1,i;
  6. char c,str[80];
  7. FILE *fp;
  8. if((fp=fopen("text.txt","w"))==NULL)
  9. {
  10. printf("can't open file!\n");
  11. exit(0);
  12. }
  13. while(flag==1) //逐次输入字符串
  14. {
  15. printf("Input string:");
  16. gets(str);
  17. fprintf(fp,"%s",str);
  18. fprintf(fp,"\n");
  19. printf("Continue?");
  20. c=getchar();
  21. if(c=='N'||c=='n')
  22. flag=0;
  23. getchar();
  24. }
  25. fclose(fp);
  26. if((fp=fopen("text.txt","r"))==NULL)
  27. {
  28. printf("can't open file!\n");
  29. exit(0);
  30. }
  31. printf("读取文件中的字符串,将小写字母转换成大写字母\n");
  32. while(fscanf(fp,"%s",str)!=EOF) //读取字符串
  33. {
  34. for(i=0; str[i]!='\0'; i++)
  35. if(str[i]>='a'&&str[i]<='z')
  36. str[i]-=32;
  37. printf("%s\n",str);
  38. }
  39. fclose(fp);
  40. return 0;
  41. }

运行结果:

202003271542336.PNG

#

发表评论

表情:
评论列表 (有 0 条评论,109人围观)

还没有评论,来说两句吧...

相关阅读

    相关 复试编程训练

    111.从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件"file.txt"中保存,输入的字符串以“!”结束。 include <

    相关 复试编程训练

    41.给一个百分制成绩,要求输出等级‘A’,‘B’,‘C’,‘D’,‘E’。90分以上为‘A’,80~89分为‘B’,70~79分为‘C’,60~69分为‘D’,60分以下为