Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. #define STR_SIZE 30
  6. #define STRING_SIZE 130
  7. #define MIN_COUNT 40
  8. #define MAX_COUNT 100
  9. #define ERROR_REGISTR 1
  10. #define ERROR_SYMBOL 2
  11. #define SUCCESS 0
  12.  
  13. /*Фамилия Имя Отчество Факультет Форма обучения(бюджетная(проживание в общежитии, размер стипендии)
  14. * /платная(стоимость обучения в год в рублях, средний балл за последнюю сессию))
  15. */
  16. #define STR_SIZE 30
  17.  
  18. typedef struct
  19. {
  20. char dormitory_accommodation[4]; // Проживает в общежитии (Да / Нет)
  21. char scholarships[4]; // Получает стипендию (Да / Нет)
  22. }budgetary_form;
  23.  
  24. typedef struct
  25. {
  26. float average_mark;
  27. }paid_form;
  28.  
  29. typedef union
  30. {
  31. budgetary_form budgetary;
  32. paid_form paid;
  33. }forms;
  34.  
  35. typedef struct
  36. {
  37. char surname[STR_SIZE]; // Фамилия
  38. char name[STR_SIZE]; // Имя
  39. char patronymic[STR_SIZE]; // Отчество
  40. char faculty[STR_SIZE]; // факультет
  41. char form[STR_SIZE];
  42. forms form_of_education; // форма обучения
  43. }students;
  44.  
  45.  
  46. int fill_str(char *p_string, char *string, int first_is_upper)
  47. {
  48. int j = 0; int k = 0;
  49. for(j = 0; string[j] != ' ' && string[j] != '\n'; j++)
  50. {
  51. if (j == 0 && first_is_upper)
  52. {
  53. if (string[j] >= 'A' && string[j] <= 'Z')
  54. {
  55. p_string[j] = string[j];
  56. continue;
  57. }
  58. if (string[j] >= 'a' && string[j] <= 'z')
  59. {
  60. printf("Not correct registr of char.");
  61. return ERROR_REGISTR;
  62. }
  63. else
  64. {
  65. printf("Not correct symbol of char.");
  66. return ERROR_SYMBOL;
  67. }
  68. }
  69. if ((j != 0 || !first_is_upper) && (string[j] >= 'a' && string[j] <= 'z'))
  70. {
  71. p_string[j] = string[j];
  72. }
  73. else
  74. {
  75. printf("Not correct symbol of char.");
  76. return ERROR_SYMBOL;
  77. }
  78. }
  79. p_string[j] = '\0';
  80. j += 1;
  81. for (k = 0; k < strlen(string) - j; k++)
  82. {
  83. string[k] = string[k + j];
  84. }
  85. return SUCCESS;
  86. }
  87.  
  88. int fill_faculty(char* p_string, char* string)
  89. {
  90. int j = 0; int k = 0;
  91. for(j = 0; string[j] != ' ' && string[j] != '\n'; j++)
  92. {
  93. if (string[j] >= 'A' && string[j] <= 'Z')
  94. {
  95. p_string[j] = string[j];
  96. }
  97. else
  98. {
  99. printf("Not correct symbol\n");
  100. return ERROR_SYMBOL;
  101. }
  102. }
  103. p_string[j] = '\0';
  104. j += 1;
  105. for (k = 0; k < strlen(string) - j; k++)
  106. {
  107. string[k] = string[k + j];
  108. }
  109.  
  110. return SUCCESS;
  111. }
  112.  
  113. int fill_mark(float* mark, char* string)
  114. {
  115. *mark = 0;
  116. if (string[0] >= '0' && string[0] <= '5' && string[1] == '.' && string[2] >= '0'
  117. && string[2] <= '9' && string[3] == '\n')
  118.  
  119. *mark += (string[0] - '0') * 1.0 + (string[2] - '0') * 0.1;
  120. else
  121. {
  122. printf("Averange mark is in wrong format\n");
  123. return ERROR_SYMBOL;
  124. }
  125. return SUCCESS;
  126. }
  127.  
  128. int string_parse(char* string, students* table)
  129. {
  130. int temp;
  131. temp = fill_str(table->surname, string, 1);
  132. if (temp != SUCCESS)
  133. return temp;
  134. temp = fill_str(table->name, string, 1);
  135. if (temp != SUCCESS)
  136. return temp;
  137. temp = fill_str(table->patronymic, string, 1);
  138. if (temp != SUCCESS)
  139. return temp;
  140. temp = fill_faculty(table->faculty, string);
  141. if (temp != SUCCESS)
  142. return temp;
  143. temp =fill_str(table->form, string, 0);
  144. if (temp != SUCCESS)
  145. return temp;
  146. if (strcmp(table->form, "paid") == SUCCESS)
  147. {
  148. fill_mark(&table->form_of_education.paid.average_mark, string);
  149. }
  150. else if (strcmp(table->form, "budgetary") == SUCCESS)
  151. {
  152. fill_str(table->form_of_education.budgetary.dormitory_accommodation, string, 1);
  153. fill_str(table->form_of_education.budgetary.scholarships, string, 1);
  154. }
  155. else
  156. {
  157. printf("Not correct form of education");
  158. return ERROR_SYMBOL;
  159. }
  160. return SUCCESS;
  161. }
  162.  
  163. int filling_table_from_file(students* table) // Считывание в таблицу данных из файла
  164. {
  165. int count = 0;
  166. FILE *f = fopen("table_student.txt", "rt");
  167. rewind(f);
  168. while(1)
  169. {
  170. if (feof(f))
  171. {
  172. break;
  173. }
  174. char string[STRING_SIZE] = "";
  175. fgets(string, 250, f);
  176. if(strcmp("end\n", string) != SUCCESS)
  177. {
  178. int rc = string_parse(string, &table[count]);
  179. if (rc != SUCCESS)
  180. {
  181. fclose(f);
  182. return rc;
  183. }
  184. }
  185. count++;
  186. }
  187. fclose(f);
  188. return SUCCESS;
  189. }
  190.  
  191. void print_tab(students* table, const int temp_table_size)
  192. {
  193. printf("\nTOTAL: %d\n\n", temp_table_size);
  194. printf("┌-------------------------------------------------------------------------------------------------------------------------------------------------┐\n");
  195. printf("| № | Surname | Name | Patronymic | Faculty | Form | Dormitory accommodation | Scholarships | Averange mark |\n");
  196. printf("|----|--------------------|---------------|---------------------|-----------|------------|-------------------------|--------------|---------------|\n");
  197. for (int i = 0; i < temp_table_size; i++)
  198. {
  199. printf("| %d", i + 1);
  200. if (i < 9)
  201. printf(" |");
  202. if (i >= 9 && i < 99)
  203. printf(" |");
  204. if (i == 99)
  205. printf("|");
  206. printf(" %s", table[i].surname);
  207. for(int j = 0; j < 19 - strlen(table[i].surname); j++)
  208. {
  209. printf(" ");
  210. }
  211. printf("|");
  212. printf(" %s", table[i].name);
  213. for(int j = 0; j < 14 - strlen(table[i].name); j++)
  214. {
  215. printf(" ");
  216. }
  217. printf("|");
  218. printf(" %s", table[i].patronymic);
  219. for(int j = 0; j < 20 - strlen(table[i].patronymic); j++)
  220. {
  221. printf(" ");
  222. }
  223. printf("|");
  224. printf(" %s", table[i].faculty);
  225. for(int j = 0; j < 10 - strlen(table[i].faculty); j++)
  226. {
  227. printf(" ");
  228. }
  229. printf("|");
  230. printf(" %s", table[i].form);
  231. for(int j = 0; j < 11 - strlen(table[i].form); j++)
  232. {
  233. printf(" ");
  234. }
  235. printf("|");
  236. if (strcmp(table[i].form, "paid") == SUCCESS)
  237. {
  238. for (int j = 0; j < 25; j++)
  239. printf(" ");
  240. printf("|");
  241. for (int j = 0; j < 14; j++)
  242. printf(" ");
  243. printf("| ");
  244. printf("%.1f", table[i].form_of_education.paid.average_mark);
  245. for(int j = 0; j < 11; j++)
  246. printf(" ");
  247. printf("|\n");
  248. }
  249. else
  250. {
  251. printf( " %s", table[i].form_of_education.budgetary.dormitory_accommodation);
  252. for (int j = 0; j < 24 - strlen(table[i].form_of_education.budgetary.dormitory_accommodation); j++)
  253. printf(" ");
  254. printf("|");
  255. printf( " %s", table[i].form_of_education.budgetary.scholarships);
  256. for (int j = 0; j < 13 - strlen(table[i].form_of_education.budgetary.scholarships); j++)
  257. printf(" ");
  258. printf("|");
  259. for (int j = 0; j < 15; j++)
  260. printf(" ");
  261. printf("|\n");
  262. }
  263. }
  264. printf("└-------------------------------------------------------------------------------------------------------------------------------------------------┘\n");
  265. }
  266.  
  267. int add_new_student()
  268.  
  269. int main(void)
  270. {
  271. students *table_students = (students*)malloc(MIN_COUNT * sizeof(students));
  272. //*copy_table = (students*)malloc(MIN_COUNT * sizeof(students));
  273.  
  274. printf("This program works with table, containing information about from 40 to 100 students of BMSTU. "
  275. "It has information about its: surname, name, patronymic, faculty"
  276. "\nif the student is on budgetary form of education, here is information about dormitory accommodation(YES/NO), scholarships(YES/NO)\n"
  277. "if the staudent is on paid form of education, here is information about average mark\n"
  278. "You can do several things with the table entering the special key of the task.\n");
  279. int temp_table_size = MIN_COUNT;
  280. filling_table_from_file(table_students);
  281. free(table_students);
  282. for (; ;)
  283. {
  284. printf("LIST OF TASKS\n"
  285. "enter 1 to show the table\n"
  286. "press 2 to add new student\n"
  287. "press 3 to delete the special student\n"
  288. "enter 4 to show sorted key table\n"
  289. "press 5 to show sorted table\n"
  290. "enter 6 to show sorted by the key table (insert sort)\n"
  291. "enter 7 to show sorted by the key table (bubble sort)\n"
  292. "enter 8 to show efficiency of different sorts\n"
  293. "enter 9 to find student on the paid form with spaecial average mark\n"
  294. "enter 10 to close the program\n\n");
  295. int s;
  296. scanf("%d", &s);
  297. switch (s)
  298. {
  299. case 1:
  300. print_tab(table_students, temp_table_size);
  301.  
  302. }
  303. break;
  304. }
  305. free(table_students);
  306.  
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement