Advertisement
constk

Lab_Files_Part1

Mar 2nd, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <Windows.h>
  6.  
  7. const char top[] = "\xC9\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCB\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCB\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCB\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCB\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBB";
  8. const char bottom[] = "\xC8\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBC";
  9. const char line[] = "\xBA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBA\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xBA";
  10.  
  11. struct NOTE
  12. {
  13. char name[80];
  14. char secondName[80];
  15. char phoneNumber[12];
  16. int dateOfBirth[3];//day.month.year
  17. };
  18.  
  19. int readNotes(NOTE*, int);
  20. void showNotes(NOTE*, int);
  21. void sortNotes(NOTE*, int);
  22. void showNote(NOTE& note);
  23. void findPersonIn(NOTE*, int);
  24. int writeNotesToFile(NOTE*, int, const char[40], FILE*);
  25. int readNotesFromFile(NOTE*, int, const char[40], FILE*);
  26. void showHelp();
  27.  
  28. int main()
  29. {
  30. FILE *file = NULL;
  31. const int N = 100;
  32. unsigned n = 0;
  33. struct NOTE note[N];
  34. bool exit = false;
  35. bool dataIsRead = false;
  36. char control[10] = "";
  37. const char fileName[40] = "Database.txt";
  38.  
  39. showHelp();
  40.  
  41. do
  42. {
  43. puts("");
  44. scanf("%s", control);
  45.  
  46. if (!strcmp(control, "SHOW"))
  47. {
  48. if (dataIsRead)
  49. showNotes(note, n);
  50. else
  51. puts("#There is nothing to show. Database is empty.");
  52. }
  53. else if (!strcmp(control, "READ"))
  54. {
  55. n = readNotes(note, N);
  56. dataIsRead = true;
  57. }
  58. else if (!strcmp(control, "FIND"))
  59. {
  60. if (dataIsRead)
  61. findPersonIn(note, n);
  62. else
  63. puts("#There is nothing to seek. Database is empty.");
  64. }
  65. else if (!strcmp(control, "SORT"))
  66. {
  67. if (dataIsRead)
  68. sortNotes(note, n);
  69. else
  70. puts("#There is nothing to sort. Database is empty.");
  71. }
  72. else if (!strcmp(control, "FWRITE"))
  73. {
  74. if (dataIsRead)
  75. {
  76. if (writeNotesToFile(note, n, fileName, file))
  77. puts("File was rewritten succesfully.");
  78. else
  79. puts("#Can't rewrite file");
  80. }
  81. else
  82. puts("#There is nothing to write. Database is empty.");
  83. }
  84. else if (!strcmp(control, "FREAD"))
  85. {
  86. if (n = readNotesFromFile(note, N, fileName, file))
  87. {
  88. puts("Data was read from file succesfully");
  89. dataIsRead = true;
  90. }
  91. else
  92. puts("#Can't read data from file");
  93. }
  94. else if (!strcmp(control, "EXIT"))
  95. {
  96. exit = true;
  97. }
  98. else
  99. {
  100. puts("#Unknown kommand. Try again.");
  101. }
  102. } while (!exit);
  103. return 0;
  104. }
  105.  
  106. //Function to input array of notes from the keyboard
  107. int readNotes(NOTE* note, int N)
  108. {
  109. int n = 0;
  110. printf("How many notes you want to read?: ");
  111. scanf("%d", &n);
  112. if (n < 0)
  113. {
  114. printf("Really?\n");
  115. return 0;
  116. }
  117. if (n <= N)
  118. {
  119. for (int i = 0; i < n; i++)
  120. {
  121. printf("Note: %d\n", i + 1);
  122.  
  123. printf("\tInput name: ");
  124. scanf("%s", note[i].name);
  125.  
  126. printf("\tInput second name: ");
  127. scanf("%s", note[i].secondName);
  128.  
  129. printf("\tInput phone number: ");
  130. scanf("%s", note[i].phoneNumber);
  131.  
  132. printf("\tInput date of birth (d.m.y): ");
  133. scanf("%d.%d.%d", &note[i].dateOfBirth[0], &note[i].dateOfBirth[1], &note[i].dateOfBirth[2]);
  134. }
  135. }
  136. else
  137. {
  138. printf("It's too much. Maximum size is %d\n", N);
  139. return 0;
  140. }
  141. return n;
  142. }
  143.  
  144. //Function to print the table
  145. void showNotes(NOTE* note, int N)
  146. {
  147. printf("%s\n", top);
  148. printf("\xBA number \xBA name \xBA second name \xBA number \xBA date of birth \xBA\n");
  149. for (int i = 0; i < N; i++)
  150. {
  151. printf("%s\n", line);
  152. printf("\xBA%7d \xBA", i + 1);
  153. printf("%13s \xBA", note[i].name);
  154. printf("%14s \xBA", note[i].secondName);
  155. printf("%13s \xBA", note[i].phoneNumber);
  156. printf(" %02d.%02d.%04d \xBA\n", note[i].dateOfBirth[0], note[i].dateOfBirth[1], note[i].dateOfBirth[2]);
  157. }
  158. printf("%s\n", bottom);
  159. }
  160.  
  161. //Function to sort array of notes
  162. void sortNotes(NOTE* note, int N)
  163. {
  164. NOTE t;
  165. for (int i = 0; i < N; i++)
  166. {
  167. for (int j = 0; j < N - 1; j++)
  168. {
  169. if (strncmp(note[j].phoneNumber, note[j + 1].phoneNumber, 3) > 0)
  170. {
  171. t = note[j];
  172. note[j] = note[j + 1];
  173. note[j + 1] = t;
  174. }
  175. }
  176. }
  177.  
  178. }
  179.  
  180. //Function to print one note
  181. void showNote(NOTE& note)
  182. {
  183. printf("Name: %s\n", note.name);
  184. printf("Second name: %s\n", note.secondName);
  185. printf("Phone number: %s\n", note.phoneNumber);
  186. printf("Date of birth: %02d.%02d.%04d\n", note.dateOfBirth[0], note.dateOfBirth[1], note.dateOfBirth[2]);
  187. }
  188.  
  189. //Function to search persons in array of notes
  190. void findPersonIn(NOTE* note, int N)
  191. {
  192. char secondName[80];
  193. bool personFound = false;
  194. printf("\nInput second name: ");
  195. scanf("%s", secondName);
  196. puts("");
  197.  
  198. for (int i = 0; i < N; i++)
  199. {
  200. if (!strcmp(note[i].secondName, secondName))
  201. {
  202. showNote(note[i]);
  203. personFound = true;
  204. }
  205. }
  206.  
  207. if (!personFound)
  208. puts("\nWhere is no such person.\n");
  209. }
  210.  
  211. //Function to rewrite the file by array of notes
  212. int writeNotesToFile(NOTE* note, int N, const char fileName[40], FILE* file)
  213. {
  214. if ((file = fopen(fileName, "w")) == NULL)
  215. {
  216. perror("#Can't open file to rewrite");
  217. return 0;
  218. }
  219. else
  220. {
  221. for (int i = 0; i < N; i++)
  222. {
  223. fwrite(note + i, sizeof(struct NOTE), 1, file);
  224. }
  225. }
  226. fclose(file);
  227.  
  228. return 1;
  229. }
  230.  
  231. //Function to get array of notes from the file
  232. int readNotesFromFile(NOTE* note, int N, const char fileName[40], FILE* file)
  233. {
  234. int n = 0;
  235. if ((file = fopen(fileName, "r")) == NULL)
  236. {
  237. perror("#Can't open file to read");
  238. return 0;
  239. }
  240. else
  241. {
  242. while ( fread(note + n, sizeof(struct NOTE), 1, file) )
  243. n++;
  244. }
  245. fclose(file);
  246.  
  247. return n;
  248. }
  249.  
  250. //Function to show hint
  251. void showHelp()
  252. {
  253. puts("Write SHOW to output notes to the screen");
  254. puts("Write READ to input notes from the keyboard");
  255. puts("Write FIND to search person in notes");
  256. puts("Write SORT to sort notes");
  257. puts("Write FWRITE to rewrite file by notes");
  258. puts("Write FREAD to get notes from file");
  259. puts("Write EXIT to close programm");
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement