Advertisement
Underhing

Untitled

May 20th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define NSTUDENTS 2
  5. #define NAME_LENGTH 16
  6. #define GROUP_LENGTH 7
  7.  
  8. struct student
  9. {
  10. char name[NAME_LENGTH];
  11. char group[GROUP_LENGTH];
  12. int ses[5];
  13. }students[NSTUDENTS],fstudents[NSTUDENTS],tmp;
  14.  
  15. int main()
  16. {
  17.  
  18. FILE *fp;
  19. int b, i, j, counter=0, f=0, n;
  20. char search[16];
  21. for (i=0; i<NSTUDENTS; i++)//
  22. {
  23. printf("enter the surname and initials <max 16 symbols>: ");
  24. gets(students[i].name);
  25. if (strlen(students[i].name)>NAME_LENGTH)
  26. {
  27. printf("Name is out of range");
  28. return EXIT_FAILURE;
  29. }
  30. printf("enter the group number <max 7 symbols>: ");
  31. scanf("%s", &students[i].group);
  32. getchar();
  33. if (strlen(students[i].group)>GROUP_LENGTH)
  34. {
  35. printf("Group name is out of range");
  36. return EXIT_FAILURE;
  37. }
  38. printf("enter the marks: \n");
  39. for (j=0; j<5; j++)
  40. {
  41. scanf("%d", &students[i].ses[j]);
  42. }
  43. getchar();
  44. }
  45.  
  46. i=0;
  47.  
  48. while (i<(NSTUDENTS-1))//
  49. {
  50. b=0;
  51. while (b<NSTUDENTS)
  52. if (students[i].name[b]>students[i+1].name[b])
  53. {
  54. tmp=students[i];
  55. students[i]=students[i+1];
  56. students[i+1]=tmp;
  57. b=10;
  58. i=0;
  59. }
  60. else
  61. {
  62. if (students[i].name[b]==students[i+1].name[b])
  63. {
  64. b++;
  65. }
  66. else
  67. {
  68. i++;
  69. b=10;
  70. }
  71. }
  72. }
  73. printf("\nsorted students: \n");//
  74. for (i=0; i<NSTUDENTS; i++)
  75. printf("\nstudent: %-32s group: %s", students[i].name, students[i].group);
  76. printf("\n");
  77. printf("\nstudents w/ a bad mark: \n");//
  78. for (i=0; i<NSTUDENTS; i++)
  79. {
  80. for (j=0; j<5; j++)
  81. {
  82. if (students[i].ses[j]==2)
  83. {
  84. printf("\nstudent: %-32s group: %s", students[i].name, students[i].group);
  85. counter++;
  86. break;
  87. }
  88. }
  89. }
  90. if (counter==0)
  91. printf("\nwe don't have such a students\n");
  92.  
  93. fp=fopen("structures.txt", "wb"); //
  94. if (fp!=NULL)
  95. {
  96. printf("\nthe file structures.txt was created successfully for writing\n") ;
  97. }
  98. else
  99. {
  100. fprintf(stderr,"the file can't be created or open\n");
  101. return EXIT_FAILURE;
  102. }
  103.  
  104. for (i=0; i<NSTUDENTS; i++)
  105. {
  106.  
  107. fprintf(fp, "%s %s ", students[i].name, students[i].group);
  108. for (j=0; j<5; j++)
  109. fprintf(fp, "%d ", students[i].ses[j]);
  110. fprintf(fp, "\n");
  111. }
  112. fclose(fp);
  113.  
  114. fp=fopen("structures.txt", "r+b"); //
  115. if (fp!=NULL)
  116. {
  117. printf("\nthe file structures.txt was created successfully for reading\n") ;
  118. }
  119. else
  120. {
  121. fprintf(stderr,"the file can't be created or open\n");
  122. return EXIT_FAILURE;
  123. }
  124.  
  125. fseek(fp,0,SEEK_SET);//
  126. for(i=0;i<NSTUDENTS;i++)
  127. {
  128. fgets(fstudents[i].name, strlen(students[i].name)+1, fp);//
  129. fseek(fp,1,SEEK_CUR);//
  130. fgets(fstudents[i].group, strlen(students[i].group)+1, fp);//
  131. fseek(fp,1,SEEK_CUR);//
  132. for(j=0;j<5;j++)
  133. {
  134. fscanf(fp,"%d", &fstudents[i].ses[j]);//
  135. fseek(fp,1,SEEK_CUR);
  136. }
  137. fseek(fp,1,SEEK_CUR);//
  138. }
  139.  
  140. printf("\nfile test: \n");//
  141. for (i=0; i<NSTUDENTS; i++)
  142. {
  143. printf("\nstudent: %-16s group: %-16s", fstudents[i].name, fstudents[i].group);
  144. printf("marks: ");
  145. for (j=0;j<5;j++)
  146. printf("%d ",fstudents[i].ses[j]);
  147. }
  148. printf("\n");
  149.  
  150. printf("\nSearching by Name\n");
  151. printf("Enter the name: ");
  152. fscanf(stdin, "%[^\n]", search);
  153. for (i=0; i<NSTUDENTS; i++)
  154. {
  155. if (strncmp(students[i].name, search, 4) == 0)
  156. {
  157. printf ("\nstudent: %s\t group: %s\t marks: ", students[i].name, students[i].group);
  158. for(j=0;j<5;j++)
  159. printf("%d ", students[i].ses[j]);
  160. printf("\n");
  161. f=1;
  162. }
  163. }
  164. if (!f)
  165. printf ("There is no %s\n", search);
  166. fclose(fp);
  167. return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement