Advertisement
qberik

Untitled

Oct 17th, 2022
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8.  
  9. ofstream log_file; // файл лога
  10.  
  11. class Cell
  12. {
  13. public:
  14. double val;
  15. Cell* next;
  16. Cell* prev;
  17. Cell(double v)
  18. {
  19. val = v;
  20. next = NULL;
  21. prev = NULL;
  22. }
  23. };
  24.  
  25. class List
  26. {
  27. private:
  28. Cell* head = NULL;
  29. Cell* tail = NULL;
  30. public:
  31.  
  32. Cell* begin() {
  33. return head;
  34. }
  35.  
  36. Cell* end() {
  37. return tail;
  38. }
  39.  
  40. int size() {
  41. int c = 0;
  42. Cell* h = head;
  43. while (h != NULL) {
  44. h = h->next;
  45. c++;
  46. }
  47. return c;
  48. }
  49.  
  50. void push_front(double v)
  51. {
  52. if (head == NULL)
  53. {
  54. head = new Cell(v);
  55. tail = head;
  56. }
  57. else
  58. {
  59. Cell* n = new Cell(v);
  60. n->next = head;
  61. head->prev = n;
  62. head = n;
  63. }
  64. }
  65.  
  66. void push_back(double v)
  67. {
  68. if (head == NULL) {
  69. head = new Cell(v);
  70. tail = head;
  71. }
  72. else
  73. {
  74. Cell* n = new Cell(v);
  75. n->prev = tail;
  76. tail->next = n;
  77. tail = n;
  78. }
  79. }
  80.  
  81.  
  82. void remove(int index) {
  83. Cell* elem = head;
  84. for (int i = 0; i < index; i++) {
  85. elem = elem->next;
  86. }
  87. elem->prev->next = elem->next;
  88. elem->next->prev = elem->prev;
  89. }
  90.  
  91. int find(double elem) {
  92. Cell* curr = head;
  93. int i = 0;
  94. while (curr != NULL)
  95. {
  96. if (curr->val == elem) {
  97. return i;
  98. }
  99. curr = curr->next;
  100. i++;
  101. }
  102. return -1;
  103. }
  104.  
  105. void clear() {
  106. int s = size();
  107. for (int i = 0; i < s; i++) {
  108. remove(0);
  109. }
  110. }
  111.  
  112. void print()
  113. {
  114. Cell* curr = head;
  115. while (curr != NULL)
  116. {
  117. cout << curr->val << " ";
  118. curr = curr->next;
  119. }
  120. cout << endl;
  121. }
  122. };
  123.  
  124.  
  125. // считывает число
  126. int get_int_input() {
  127.  
  128. bool only_nums = false;
  129. string in;
  130. while (!only_nums) {
  131. cin >> in;
  132. only_nums = true;
  133. for (int i = 0; i < in.length(); i++) {
  134. if (!(in[i] >= '0' && in[i] <= '9')) {
  135. only_nums = false;
  136. }
  137. }
  138. if (!only_nums) {
  139. cout << "Incorect input: ";
  140. log_file << "WARNING: getting incorect input " << in << endl;
  141. }
  142. }
  143. return stoi(in);
  144. }
  145.  
  146.  
  147. void task1(List& data) {
  148. log_file << "call task1 function" << endl;
  149. int n;
  150. cout << "Enert n: ";
  151. n = get_int_input();
  152.  
  153. if (n >= 2) {
  154.  
  155. //List data;
  156.  
  157. for (int i = 0; i < n; i++) {
  158.  
  159. double elem;
  160. cout << "Enter " << i + 1 << " element: ";
  161. cin >> elem;
  162. data.push_back(elem);
  163.  
  164. }
  165.  
  166. double ans = 1;
  167. Cell* start = data.begin();
  168. Cell* end = data.end();
  169. for (int i = 0; i < n; i++) {
  170. double elem;
  171. elem = start->val * end->val;
  172. start = start->next;
  173. end = end->prev;
  174. ans = ans * elem;
  175. }
  176.  
  177. cout << "Answer is " << ans << endl;
  178. }
  179. else {
  180. cout << "ERROR: n is too small" << endl;
  181. log_file << "ERROR: n is too small" << endl;
  182. }
  183. }
  184.  
  185.  
  186.  
  187. void task1_file(List& data) {
  188. log_file << "call task1_file function" << endl;
  189. string in_file;
  190. string out_file;
  191. cout << "Enter input filename: ";
  192. cin >> in_file;
  193. cout << "Enter output filename: ";
  194. cin >> out_file;
  195.  
  196. ifstream in(in_file);
  197. ofstream out(out_file);
  198. int n;
  199. in >> n;
  200.  
  201.  
  202. if (n >= 2) {
  203.  
  204. //List data;
  205.  
  206. for (int i = 0; i < n; i++) {
  207.  
  208. double elem;
  209. //cout << "Enter " << i + 1 << " element: ";
  210. in >> elem;
  211. data.push_back(elem);
  212.  
  213. }
  214.  
  215. double ans = 1;
  216. Cell* start = data.begin();
  217. Cell* end = data.end();
  218. for (int i = 0; i < n; i++) {
  219. double elem;
  220. elem = start->val * end->val;
  221. start = start->next;
  222. end = end->prev;
  223. ans = ans * elem;
  224. }
  225.  
  226. out << "Answer is " << ans << endl;
  227. }
  228. else {
  229. out << "ERROR: n is too small" << endl;
  230. }
  231.  
  232. in.close();
  233. out.close();
  234. }
  235.  
  236.  
  237.  
  238.  
  239. void add_elem(List& data) {
  240. log_file << "call add_elem function" << endl;
  241. cout << "Enter element: ";
  242. int elem = get_int_input();
  243. data.push_back(elem);
  244. }
  245.  
  246. void find_elem(List& data) {
  247. log_file << "call find_elem function" << endl;
  248. cout << "Enter element: ";
  249. int elem = get_int_input();
  250. cout << "elem on index " << data.find(elem) << endl;
  251. }
  252.  
  253. void remove_elem(List& data) {
  254. log_file << "call remove_elem function" << endl;
  255. cout << "Enter element Index: ";
  256. int index = get_int_input();
  257. if (index < 0 || index >= data.size()) {
  258. cout << "ERROR: Incorect index";
  259. log_file << "ERROR: Incorect index";
  260. }
  261. else {
  262. data.remove(index);
  263. }
  264. }
  265.  
  266. void clear_list(List& data) {
  267. log_file << "call clear function" << endl;
  268. data.clear();
  269. }
  270.  
  271. void print_list(List& data) {
  272. log_file << "call print_list function" << endl;
  273. data.print();
  274. }
  275.  
  276.  
  277. int main()
  278. {
  279. log_file.open("log_file.txt");
  280.  
  281. vector<void(*)(List&)> menu; // массив указателей на функции
  282. // функции
  283. menu.push_back(task1);
  284. menu.push_back(task1_file);
  285. menu.push_back(add_elem);
  286. menu.push_back(find_elem);
  287. menu.push_back(remove_elem);
  288. menu.push_back(clear_list);
  289. menu.push_back(print_list);
  290.  
  291. List data;
  292.  
  293. bool exit = false;
  294.  
  295. while (!exit) {
  296.  
  297. cout << "1) Variant task" << endl;
  298. cout << "2) Variant task with files" << endl;
  299. cout << "3) Add element" << endl;
  300. cout << "4) Find element" << endl;
  301. cout << "5) Remove element" << endl;
  302. cout << "6) Clear list" << endl;
  303. cout << "7) Print list" << endl;
  304. cout << "0) Exit" << endl;
  305. cout << "Select item: ";
  306.  
  307. int input = get_int_input();
  308.  
  309. if (input < 0 || input > menu.size()) {
  310. cout << endl << "Invalid item" << endl << endl;
  311. }
  312. else {
  313.  
  314. if (input != 0) {
  315. menu[input - 1](data);
  316. }
  317. else {
  318. exit = true;
  319. }
  320. }
  321. }
  322.  
  323. log_file << "Program successfully ended" << endl;
  324. log_file.close();
  325. return 0;
  326. }
  327.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement