Advertisement
Guest User

luc 5 ru

a guest
Apr 24th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. struct Student {
  7. int nr;
  8. char familie[30];
  9. char nume[30];
  10. char adresa[30];
  11. char telefon[20];
  12. char specialitate[30];
  13. int clasa;
  14. float nota;
  15. Student *next;
  16. } *p, *q, *q1;
  17.  
  18. void swap(Student *l1, Student *l2) {
  19. l1->nr = l2->nr;
  20. strcpy(l1->familie, l2->familie);
  21. strcpy(l1->nume, l2->nume);
  22. strcpy(l1->adresa, l2->adresa);
  23. strcpy(l1->telefon, l2->telefon);
  24. strcpy(l1->specialitate, l2->specialitate);
  25. l1->clasa = l2->clasa;
  26. l1->nota = l2->nota;
  27. }
  28.  
  29. void Create() {
  30. int n, i;
  31.  
  32. cout << "Введите количество студентов:";
  33. cin >> n;
  34.  
  35. if (n <= 0) {
  36. p = NULL;
  37. cout << "Количество не может быть отрицательной." << endl;
  38. } else {
  39. q = new Student;
  40. q->nr = 1;
  41.  
  42. cout << "Введите фамилию, имя, адрес, телефон, специализацию, класс и оценку для первого студента: ";
  43. cin >> q->familie >> q->nume >> q->adresa >> q->telefon >> q->specialitate >> q->clasa >> q->nota;
  44. q->next = NULL;
  45. p = q;
  46.  
  47. for (i = 2; i <= n; i++) {
  48. q1 = new Student;
  49. q1->nr = i;
  50. cout << "Введите фамилию, имя, адрес, телефон, специализацию, класс и оценку для " << i
  51. << "-го студента: ";
  52. cin >> q1->familie >> q1->nume >> q1->adresa >> q1->telefon >> q1->specialitate >> q1->clasa >> q1->nota;
  53. q1->next = NULL;
  54. q->next = q1;
  55. q = q1;
  56. }
  57. }
  58. }
  59.  
  60. void Show() {
  61. if (p) {
  62. cout << "Список студентов: " << endl;
  63. q = p;
  64. while (q) {
  65. cout << q->nr
  66. << "\t" << q->familie
  67. << "\t" << q->nume
  68. << "\t" << q->adresa
  69. << "\t" << q->telefon
  70. << "\t" << q->specialitate
  71. << "\t" << q->clasa
  72. << "\t" << q->nota << endl;
  73. q = q->next;
  74. }
  75. } else
  76. cout << "Список пустой." << endl;
  77. }
  78.  
  79. void Add() {
  80. if (p) {
  81. q = p;
  82. int i = 1;
  83. while (q->next) {
  84. i = q->nr;
  85. q = q->next;
  86. }
  87. i += 2;
  88. q1 = new Student;
  89. q1->nr = i;
  90. cout << "Введите фамилию, имя, адрес, телефон, специализацию, класс и оценку для " << i
  91. << "-го студента: ";
  92. cin >> q1->familie >> q1->nume >> q1->adresa >> q1->telefon >> q1->specialitate >> q1->clasa >> q1->nota;
  93. q1->next = NULL;//q->next;
  94. q->next = q1;
  95. q = q1;
  96. } else {
  97. cout << "Список пустой. Создание списка:" << endl;
  98. Create();
  99. }
  100. }
  101.  
  102. Student *Find() {
  103. int nr;
  104. if (p == NULL) {
  105. return NULL;
  106. }
  107. cout << "Введите номер студента: ";
  108. cin >> nr;
  109.  
  110. q = p;
  111. while ((q) && (q->nr != nr)) {
  112. q = q->next;
  113. }
  114. if (q)
  115. cout << q->nr
  116. << "\t" << q->familie
  117. << "\t" << q->nume
  118. << "\t" << q->adresa
  119. << "\t" << q->telefon
  120. << "\t" << q->specialitate
  121. << "\t" << q->clasa
  122. << "\t" << q->nota << endl;
  123. else
  124. cout << "Студент не найден!";
  125.  
  126. return q;
  127. }
  128.  
  129. void Edit() {
  130. Show();
  131. Student *q;
  132. q = Find();
  133. if (q) {
  134. cout << "Введите фамилию, имя, адрес, телефон, специализацию, класс и оценку для нужного студента: ";
  135. cin >> q1->familie >> q1->nume >> q1->adresa >> q1->telefon >> q1->specialitate >> q1->clasa >> q1->nota;
  136. cout << "Обновленный список студентов: " << endl;
  137. Show();
  138. }
  139. }
  140.  
  141. bool Delete() {
  142. Show();
  143. Student *q;
  144. q = Find();
  145. if (q) {
  146. if (q == p) {
  147. p = p->next;
  148. } else {
  149. q1 = p;
  150. while (q1->next != q)
  151. q1 = q1->next;
  152. if (q == p)
  153. p = p->next;
  154. q1->next = q->next;
  155. }
  156. delete q;
  157. cout << "Обновленный список студентов: " << endl;
  158. Show();
  159. return true;
  160. } else
  161. return false;
  162. }
  163.  
  164. bool Sort() {
  165. Student *l;
  166. l = new Student;
  167. bool change;
  168. if (p == NULL)
  169. return false;
  170. do {
  171. q = p;
  172. change = false;
  173. while (q->next) {
  174. if (q->nr < q->next->nr) {
  175. swap(l, q);
  176. swap(q, q->next);
  177. swap(q->next, l);
  178. change = true;
  179. }
  180. q = q->next;
  181. }
  182. } while (change);
  183. return true;
  184. }
  185.  
  186. void DeleteList() {
  187. p = NULL;
  188. }
  189.  
  190. int main(int argc, char **argv) {
  191. int menu;
  192. do {
  193. cout << "Создание списка...........1" << endl;
  194. cout << "Показать список студентов.2" << endl;
  195. cout << "Добавить студента.........3" << endl;
  196. cout << "Редактировать студента....4" << endl;
  197. cout << "Удалить студента..........5" << endl;
  198. cout << "Поиск студента............6" << endl;
  199. cout << "Сортировать список........7" << endl;
  200. cout << "Удалить список............8" << endl;
  201. cout << "Выход из программы........0" << endl;
  202. cin >> menu;
  203. switch (menu) {
  204. case 1:
  205. Create();
  206. break;
  207. case 2:
  208. Show();
  209. break;
  210. case 3:
  211. Add();
  212. break;
  213. case 4:
  214. Edit();
  215. break;
  216. case 5:
  217. if (Delete())
  218. cout << "Удаление было успешно!" << endl;
  219. else
  220. cout << "Удаление было не успешно!" << endl;
  221. break;
  222. case 6:
  223. Find();
  224. break;
  225. case 7:
  226. if (Sort()) {
  227. cout << "Отсортированный список: " << endl;
  228. Show();
  229. } else
  230. cout << "Список пустой! Сортировка была не успешной" << endl;
  231. break;
  232. case 8:
  233. DeleteList();
  234. break;
  235. }
  236. } while (menu != 0);
  237. return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement