Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #include <cstdlib> // урегулировывает вопрос с переменой типов данных
  5.  
  6. #include <fstream>
  7.  
  8. using std::string;
  9. using std::cout;
  10. using std::cin;
  11. using std::ofstream;
  12.  
  13.  
  14. struct Student {
  15. string last_name;
  16. string street;
  17. int house;
  18. int apartment;
  19. };
  20.  
  21.  
  22. void write_int(Student* array, int SIZE_ARRAY, char filename[]) {
  23. // делает: пишет целое число в файл
  24.  
  25. // параметры:
  26. // student array: массив структуры
  27. // int SIZE: размер массива
  28. // char filename[]: имя файла для записи
  29.  
  30. // возвращает: ничего
  31. cout << "начинаю печать\n";
  32.  
  33. ofstream file;
  34. file.open(filename);
  35.  
  36. for (int i = 0; i < SIZE_ARRAY; i++) {
  37. file << array[i].last_name << " ";
  38. file << array[i].street << " ";
  39. file << array[i].house << " ";
  40. file << array[i].apartment << "\n";
  41. }
  42.  
  43. file.close();
  44.  
  45. }
  46.  
  47.  
  48. int menu() {
  49. // делает: выводит меню
  50.  
  51. // параметры:
  52.  
  53. // возвращает: целое число - выбор пользователя
  54. int user_choice;
  55.  
  56. cout << "\n[0] - check the last_name;\n";
  57. cout << "[1] - check the street;\n";
  58. cout << "[2] - check the house;\n";
  59. cout << "[3] - check the apartment.\n";
  60.  
  61. cin >> user_choice;
  62.  
  63. return user_choice;
  64. }
  65.  
  66.  
  67. int main() {
  68. int SIZE_ARRAY;
  69. cout << "Size: ";
  70. cin >> SIZE_ARRAY;
  71. // Student students[SIZE_ARRAY];
  72. Student *students;
  73. students = new Student[SIZE_ARRAY];
  74.  
  75. for (int i = 0; i < SIZE_ARRAY; i++) {
  76. cout << "\nStudents #" << i + 1 << "\n";
  77.  
  78. cout << "Input last_name of student: ";
  79. cin >> students[i].last_name;
  80.  
  81. cout << "Input street of " << students[i].last_name << ": ";
  82. cin >> students[i].street;
  83.  
  84. cout << "Input number house of " << students[i].last_name << ": ";
  85. cin >> students[i].house;
  86.  
  87. cout << "Input number of apartment of " << students[i].last_name << ": ";
  88. cin >> students[i].apartment;
  89. }
  90.  
  91. int user_choice = menu();
  92.  
  93. char value_for_search[100];
  94.  
  95. switch (user_choice) {
  96. case 0:
  97. cout << "last_name for search: ";
  98. cin >> value_for_search;
  99. break;
  100.  
  101. case 1:
  102. cout << "street for search: ";
  103. cin >> value_for_search;
  104. break;
  105.  
  106. case 2:
  107. cout << "house for search: ";
  108. cin >> value_for_search;
  109. break;
  110.  
  111. case 3:
  112. cout << "apartment for search: ";
  113. cin >> value_for_search;
  114. break;
  115.  
  116. default:
  117. break;
  118.  
  119. }
  120.  
  121. int count_param_in_array = 0;
  122.  
  123. for (int i = 0; i < SIZE_ARRAY; i++) {
  124. if (user_choice == 0) {
  125. if (students[i].last_name == value_for_search) {
  126. count_param_in_array++;
  127. }
  128. }
  129.  
  130. else if (user_choice == 1) {
  131. if (students[i].street == value_for_search) {
  132. count_param_in_array++;
  133. }
  134. }
  135.  
  136. else if (user_choice == 2) {
  137. if (students[i].house == atoi(value_for_search)) {
  138. count_param_in_array++;
  139. }
  140. }
  141.  
  142. else if (user_choice == 3) {
  143. if (students[i].apartment == atoi(value_for_search)) {
  144. count_param_in_array++;
  145. }
  146. }
  147. }
  148.  
  149. cout << count_param_in_array << " answers found for this request\n";
  150.  
  151. write_int(students, SIZE_ARRAY, "file.csv");
  152.  
  153. return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement