Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. /*В структурах хранится информация о студентах. Одним из элементов
  2. структуры является объединение, в котором, в зависимости от места жительства
  3. (иногородний студент или нет), информация задается в виде :
  4. область …, район …, город …, ул.…, д.…, кв.…;
  5. По запросу из командной строки, в зависимости от вида информации, вывести
  6. данные о тех или иных студентах, используя указатели на функции.*/
  7. #include "stdio.h"
  8. #include "stdlib.h"
  9. #include "string.h"
  10. struct students{
  11.  
  12. char* name;
  13. char* surname;
  14. char* patronymic;
  15. int unit;
  16.  
  17. union {
  18.  
  19. char* region;
  20. char* borough;
  21. char* city;
  22. char* street;
  23. int house;
  24. int flat;
  25.  
  26. }placeOfLiving;
  27. };
  28.  
  29. char* charStringInput(){
  30.  
  31. char *string = (char*)malloc(sizeof(char)* 50);
  32. if (!string){
  33. printf("Memory error\n");
  34. return 0;
  35. }
  36. fflush(stdin);
  37. gets(string);
  38. fflush(stdin);
  39. int i = 0;
  40. while (string[i] != '\0'){
  41. if (!('a' <= string[i]) && (string[i] <= 'z') || (('A' <= string[i] ) && (string[i] <= 'Z'))){
  42. printf("This string cannot contain any numbers or symbols besides letters!\n Please,input write string : ");
  43. charStringInput();
  44. }
  45. i++;
  46. }
  47. string = (char*)realloc(string, sizeof(char)*i);
  48. if (!string){
  49. printf("Memory error\n");
  50. return 0;
  51. }
  52. string[i] = '\0';
  53. return string;
  54. }
  55.  
  56. int integerInput(){
  57. int number;
  58. if (!scanf("%d", &number) || (number < 1)){
  59. printf("Please,enter number once more\n");
  60. fflush(stdin);
  61. }
  62. fflush(stdin);
  63. return number;
  64. }
  65.  
  66. struct students* initStruct(){
  67. struct students *oneStudent = (struct students*)malloc(sizeof(struct students));
  68. if (!oneStudent){
  69. printf("Memory error\n");
  70. return 0;
  71. }
  72. printf("Enter the name of student: ");
  73. oneStudent->name = charStringInput();
  74.  
  75. printf("Enter the surname of student: ");
  76. oneStudent->surname = charStringInput();
  77.  
  78. printf("Enter the patronymic of student: ");
  79. oneStudent->patronymic = charStringInput();
  80.  
  81. printf("What information about a student do you want to record?\n");
  82. printf("1 - region\n2 - borough\n3 - city\n4- street\n5-number of a house\n6-number of a flat\n Choice: ");
  83. while (!(scanf_s("%d", &oneStudent->unit))){
  84. rewind(stdin);
  85. printf("Error: cannot use symbols and numbers greater than 3 and less than 1!\nOption: ");
  86. }
  87. switch (oneStudent->unit) {
  88. case 1:
  89. printf("Enter the region of this student : ");
  90. oneStudent->placeOfLiving.region = charStringInput();
  91. break;
  92. case 2:
  93. printf("Enter the borough where the student lives: ");
  94. oneStudent->placeOfLiving.borough = charStringInput();
  95. break;
  96. case 3:
  97. printf("Enter city where the student lives: ");
  98. oneStudent->placeOfLiving.city = charStringInput();
  99. break;
  100.  
  101. case 4:
  102. printf("Enter street where the student lives: ");
  103. oneStudent->placeOfLiving.street = charStringInput();
  104. break;
  105. case 5:
  106. printf("Enter the number of house where the student lives: ");
  107. oneStudent->placeOfLiving.house = integerInput();
  108. break;
  109. case 6:
  110. printf("Enter the number of flat where the student lives: ");
  111. oneStudent->placeOfLiving.flat = integerInput();
  112. break;
  113. }
  114. return oneStudent;
  115. }
  116.  
  117. void printStudent(struct students** table, int i){
  118.  
  119. printf("Name of %d student is %s\n", i + 1, table[i]->name);
  120. printf("Surname of %d student is %s\n", i + 1, table[i]->surname);
  121. printf("Patrinymic of %d student is %s\n", i + 1, table[i]->patronymic);
  122.  
  123. switch (table[i]->unit) {
  124. case 1:
  125. printf("The region of this student is %s\n", table[i]->placeOfLiving.region);
  126. break;
  127. case 2:
  128. printf("The borough of this student is %s\n", table[i]->placeOfLiving.borough);
  129. break;
  130. case 3:
  131. printf("The city of this student is %s\n", table[i]->placeOfLiving.city);
  132. break;
  133.  
  134. case 4:
  135. printf("The street of this student is %s\n", table[i]->placeOfLiving.street);
  136. break;
  137. case 5:
  138. printf("The hosue of this student is %d\n", table[i]->placeOfLiving.house);
  139. break;
  140. case 6:
  141. printf("The flat of this student is %d\n", table[i]->placeOfLiving.flat);
  142. break;
  143. }
  144.  
  145. }
  146.  
  147. struct students** createTable(struct students** table, int* numberOfStudents){
  148. table = (struct students**)malloc(sizeof(struct students*));
  149. int size = 0;
  150. char answer;
  151. bool statement = true;
  152. do {
  153.  
  154. printf("\nDo you want add students?(enter 'y' or 'n'):");
  155. answer = getchar();
  156. fflush(stdin);
  157. switch (answer) {
  158.  
  159. case 'y': {
  160. table = (struct students**)realloc(table, sizeof(struct students*)*(*numberOfStudents + 1));
  161. if (!table){
  162. printf("Memory error\n");
  163. return 0;
  164. }
  165. table[*numberOfStudents] = (struct students*)malloc(sizeof(struct students*));
  166. if (!table[*numberOfStudents]){
  167. printf("Memory error\n");
  168. return 0;
  169. }
  170. table[*numberOfStudents] = initStruct();
  171. (*numberOfStudents)++;
  172. break;
  173. }
  174.  
  175. case 'n':{
  176. statement = false;
  177. break;
  178. }
  179.  
  180. default:{
  181. printf("\nError,please enter 'y' or 'n'");
  182. break;
  183. }
  184.  
  185. }
  186. } while (statement);
  187. return table;
  188. }
  189.  
  190. int searchRightStringInCommandLine(struct students** table, char* str){
  191. int i = 0;
  192. if ((str[i] <= 'Z') && (str[i] >= 'A')){
  193. str[i] = char(((int)str[i] + 32));
  194. }
  195. if (strcmp(str, "region") == 0 ) {
  196. return 1;
  197. }
  198. if (strcmp(str, "borough") == 0 ) {
  199. return 2;
  200. }
  201. if (strcmp(str, "city") == 0) {
  202. return 3;
  203. }
  204. if (strcmp(str, "street")) {
  205. return 4;
  206. }
  207. if (strcmp(str, "house")) {
  208. return 5;
  209. }
  210. if (strcmp(str, "flat")) {
  211. return 6;
  212. }
  213. return 0;
  214. }
  215.  
  216. int main(int argc, char **argv) {
  217. if (!argv[1]){
  218. printf("Command line is empty!\n");
  219. return 0;
  220. }
  221. int numberOfStudents = 0;
  222.  
  223. struct students ** table = (students**)malloc(sizeof(students*));
  224. students**(*pointerToCreating)(students** table, int* numberOfStudents);
  225. pointerToCreating = createTable;
  226. table = pointerToCreating(table, &numberOfStudents);
  227.  
  228. int choice;
  229. bool exitVariable = true;
  230. do{
  231. printf("1-to print table\n2-to search students for a given item\n3-to exit\n Choice: ");
  232. scanf("%d", &choice);
  233. fflush(stdin);
  234. switch (choice){
  235. case 1:{
  236. for (int i = 0; i < numberOfStudents; i++){
  237. printStudent(table, i);
  238. }
  239. break;
  240. }
  241. case 2:{
  242. bool flag = false;
  243. int j = 1;
  244. char* str;
  245. for (int i = 0; i < numberOfStudents; i++){
  246. while (argv[j]){
  247. int number = searchRightStringInCommandLine(table, argv[j]);
  248. if (number == table[i]->unit){
  249. flag = true;
  250. printStudent(table, i);
  251. }
  252. j++;
  253. }
  254. j = 1;
  255. }
  256. if (flag == false){
  257. printf("There no students suitable to arguments of command line\n");
  258. }
  259. break;
  260. }
  261. case 3:{
  262. exitVariable = false;
  263. break;
  264. }
  265. default:{
  266. printf("Please,enter your choice!\n");
  267. }
  268. }
  269. } while (exitVariable);
  270.  
  271. return 0;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement