Advertisement
Guest User

Untitled

a guest
May 15th, 2020
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <cmath>
  8. #include <cstdlib>
  9. #include <math.h>
  10.  
  11. using namespace std;
  12. #define pi 3.141592653
  13. int x;
  14. long double low, high, step;
  15. struct person { //описание структуры
  16. char f[30]; //фамилия
  17. char d[30]; //должность
  18. float zp,pr; //зарплата, премия
  19. char prof; //профсоюз
  20. long double low, high, step;
  21. int x;
  22.  
  23. person *next; //указатель на следующего в списке
  24. };
  25.  
  26. person vvesti() { //ввод элемента с клавиатуры
  27. person p;
  28. \
  29. low=0;
  30. high=pi/4;
  31. step=pi/40;
  32.  
  33.  
  34.  
  35.  
  36. return p;
  37. }
  38.  
  39. int show (person *head) { //показ всего списка с определением количества элементов
  40. int count=0;
  41. if (head) while (1) {
  42. low=0;
  43. high=pi/4;
  44. step=pi/40;
  45. std::cout << " X | Y(X) |\n----------+---------------+" << std::endl;
  46. for ( double x = low; x <= high; x += step )
  47. std::cout << ' ' << std::fixed << std::setprecision(3) << std::left << std::setw(9) << x << "| "
  48. << std::left << std::setw(14) << sin(3*x)+cos(2*x)
  49. << "\n----------+---------------+" << std::endl;
  50. count++;
  51. if (head->next == NULL) break;
  52. head = head->next;
  53. }
  54. printf ("\nAll=%d",count);
  55. return count;
  56. }
  57. void search (person *head, char *st) { //поиск в списке строки st
  58. if (head==NULL) return;
  59. person *next = head;
  60. do {
  61. char *find = strstr (next->f, st);
  62. if (find!=NULL) printf ("\n%s",next->f);
  63. if (next->next==NULL) break;
  64. next = next->next;
  65. } while (1);
  66. }
  67.  
  68. void copy1 (person *to, person *from) { //копирование одной записи
  69. strcpy (to->f, from->f);
  70. strcpy (to->d, from->d);
  71. to->zp = from->zp; to->pr = from->pr; to->prof = from->prof;
  72. }
  73.  
  74. person *add1 (person *head, person *st) { //добавление элемента st в начало списка
  75. //*st уже заполнен данными
  76. person *current = new person;
  77. copy1 (current, st); //копирует 1 запись
  78. if (head==NULL) current->next = NULL;
  79. else {
  80. current->next = head;
  81. head = current;
  82. }
  83. return current;
  84. }
  85.  
  86. person *add2 (person *head, person *st) { //добавление элемента st в конец списка
  87. person *last = NULL;
  88. if (head!=NULL) {
  89. last=head;
  90. while (last->next!=NULL) last=last->next;
  91. }
  92. person *current = new person;
  93. copy1 (current,st);
  94. current->next = NULL;
  95. if (last) last->next = current;
  96. return current;
  97. }
  98.  
  99. person *delete1 (person *head0, int n) { //удаление элемента по номеру 1..N
  100. // удалит элемент номер n и вернет указ.на нач.
  101. person *head = head0;
  102. if (head==NULL) return NULL;
  103. if (n==1) { //удаляем первый
  104. person *ptr = head->next;
  105. delete head;
  106. return ptr;
  107. }
  108. person *prev = NULL, *start = head; int i=1;
  109. while (i<n) { //Ищем n-ый элемент
  110. prev = head; head = head->next;
  111. if (head==NULL) return start;
  112. i++;
  113. }
  114. person *ptr = head->next;
  115. delete head;
  116.  
  117. prev->next = ptr;
  118. return start;
  119. }
  120.  
  121. person *sort (person *ph) { //сортировка списка методом вставок
  122. person *q, *p, *pr, *out=NULL;
  123. while (ph != NULL) {
  124. q = ph; ph = ph->next; //исключить эл-т
  125. //ищем, куда его включить:
  126. for (p=out,pr=NULL ; p!=NULL &&
  127. strcmp(q->f,p->f)>0; pr=p,p=p->next) ;
  128. //или ваш критерий, когда переставлять!
  129. if (pr==NULL) { q->next=out;out=q; } //в нач.
  130. else { q->next=p; pr->next=q; } //после пред.
  131. }
  132. return out;
  133. }
  134.  
  135. int main() { //меню и главная программа
  136. person *head = NULL;
  137. while (1) {
  138. printf ("\n 1. Show all \
  139. \n 2. Add in head \
  140. \n 3. Add in tail \
  141. \n 4. Delete by number \
  142. \n 5. Sort by name \
  143. \n 0. Exit ");
  144. fflush (stdin);
  145. char c;
  146. scanf ("%c",&c);
  147. person p;
  148. person *cur;
  149. int n, all;
  150. switch (c) {
  151. case '1': show (head);
  152. break;
  153. case '2': p = vvesti(); head=add1(head,&p);
  154. break;
  155. case '3': p = vvesti();
  156. cur = add2 (head,&p);
  157. if (head==NULL) head=cur;
  158. break;
  159. case '4':
  160. all = show(head);
  161. while (1) {
  162. printf ("\nVvedi nomer (1-%d): ",all);
  163. fflush (stdin); scanf("%d",&n);
  164. if (n>=1 && n<=all) break;
  165. }
  166. head = delete1 (head,n);
  167. break;
  168. case '5': head = sort (head);
  169. break;
  170. case '0': exit (0); break;
  171. }
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement