Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. #include<stdio.h>
  2. typedef struct talmid{
  3. int code;
  4. double age;
  5. }std1;
  6. void bulidStudentFile(char *s);
  7. void printstudentFile(char *s);
  8. double AvgAge(char *s);
  9. void SortFile(char *s, char *nf);
  10. void main()
  11. {
  12. int run;
  13. char fname[30]="nbinf.bin";
  14. char sortfname[30]="nbinf.bin";
  15. double avg;
  16. bulidStudentFile(fname);
  17. printstudentFile(fname);
  18. avg=AvgAge(fname);
  19. printf("avg=%lf", avg);
  20. SortFile(fname, sortfname);
  21. printstudentFile(sortfname);
  22. scanf("%d", &run);
  23. }
  24. void bulidStudentFile(char *s)
  25. {
  26. int i, n;
  27. std1 talmid;
  28. FILE *fp;
  29. if((fp=fopen(s, "wb"))!=NULL)
  30. {
  31. printf("enter num of students \n");
  32. scanf("%d", &n);
  33. for(i=0; i<n ; i++)
  34. {
  35. printf("\n enter code and age \n");
  36. scanf("%d", &(talmid.code));
  37. scanf("%lf", &(talmid.age));
  38. fwrite(&talmid, sizeof(std1), 1, fp);
  39. }
  40. fclose(fp);
  41. }
  42. }
  43. void printstudentFile(char *s)
  44. {
  45. FILE *fp;
  46. std1 talmid;
  47. if((fp=fopen(s, "rb"))!=NULL)
  48. {
  49. fread(&talmid, sizeof(std1), 1, fp);
  50. while(!feof(fp))
  51. {
  52. printf("\n code=%d \n", talmid.code);
  53. printf("age=%lf \n", talmid.age);
  54. fread(&talmid, sizeof(std1), 1, fp);
  55. }
  56. fclose(fp);
  57. }
  58. }
  59. double AvgAge(char *s)
  60. {
  61. double avg=0;
  62. int count=0;
  63. FILE *fp;
  64. std1 talmid;
  65. if((fp=fopen(s, "rb"))!=NULL)
  66. {
  67. fread(&talmid, sizeof(std1), 1, fp);
  68. while(!feof(fp))
  69. {
  70. avg+=talmid.age;
  71. count++;
  72. fread(&talmid, sizeof(std1), 1, fp);
  73. }
  74. fclose(fp);
  75. }
  76. avg=avg/count;
  77. return avg;
  78. }
  79. void SortFile(char *s, char *nf)
  80. {
  81. FILE *fp;
  82. FILE *fp2;
  83. FILE *fp3;
  84. std1 talmid;
  85. int min;
  86. if((fp3=fopen(nf, "wb"))!=NULL)
  87. {
  88. while(!feof(fp3))
  89. {
  90. fp=fopen(s, "rb");
  91. fread(&talmid, sizeof(std1), 1, fp);
  92. min=talmid.code;
  93. fp2=fp;
  94. if((fp=fopen(s, "rb"))!=NULL)
  95. {
  96. fread(&talmid, sizeof(std1), 1, fp);
  97. while(!feof(fp))
  98. {
  99. if(min>talmid.code)
  100. {
  101. min=talmid.code;
  102. fp2=fp;
  103. }
  104. fread(&talmid, sizeof(std1), 1, fp);
  105. }
  106. talmid.code=-1;
  107. fwrite(&talmid, sizeof(std1), 1, fp2);
  108. fclose(fp);
  109. }
  110. talmid.code=min;
  111. fwrite(&talmid, sizeof(std1), 1, fp3);
  112. }
  113.  
  114. fclose(fp3);
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement