Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- using namespace std;
- ofstream log_file; // файл лога
- class Cell
- {
- public:
- double val;
- Cell* next;
- Cell* prev;
- Cell(double v)
- {
- val = v;
- next = NULL;
- prev = NULL;
- }
- };
- class List
- {
- private:
- Cell* head = NULL;
- Cell* tail = NULL;
- public:
- Cell* begin() {
- return head;
- }
- Cell* end() {
- return tail;
- }
- int size() {
- int c = 0;
- Cell* h = head;
- while (h != NULL) {
- h = h->next;
- c++;
- }
- return c;
- }
- void push_front(double v)
- {
- if (head == NULL)
- {
- head = new Cell(v);
- tail = head;
- }
- else
- {
- Cell* n = new Cell(v);
- n->next = head;
- head->prev = n;
- head = n;
- }
- }
- void push_back(double v)
- {
- if (head == NULL) {
- head = new Cell(v);
- tail = head;
- }
- else
- {
- Cell* n = new Cell(v);
- n->prev = tail;
- tail->next = n;
- tail = n;
- }
- }
- void remove(int index) {
- Cell* elem = head;
- for (int i = 0; i < index; i++) {
- elem = elem->next;
- }
- elem->prev->next = elem->next;
- elem->next->prev = elem->prev;
- }
- int find(double elem) {
- Cell* curr = head;
- int i = 0;
- while (curr != NULL)
- {
- if (curr->val == elem) {
- return i;
- }
- curr = curr->next;
- i++;
- }
- return -1;
- }
- void clear() {
- int s = size();
- for (int i = 0; i < s; i++) {
- remove(0);
- }
- }
- void print()
- {
- Cell* curr = head;
- while (curr != NULL)
- {
- cout << curr->val << " ";
- curr = curr->next;
- }
- cout << endl;
- }
- };
- // считывает число
- int get_int_input() {
- bool only_nums = false;
- string in;
- while (!only_nums) {
- cin >> in;
- only_nums = true;
- for (int i = 0; i < in.length(); i++) {
- if (!(in[i] >= '0' && in[i] <= '9')) {
- only_nums = false;
- }
- }
- if (!only_nums) {
- cout << "Incorect input: ";
- log_file << "WARNING: getting incorect input " << in << endl;
- }
- }
- return stoi(in);
- }
- void task1(List& data) {
- log_file << "call task1 function" << endl;
- int n;
- cout << "Enert n: ";
- n = get_int_input();
- if (n >= 2) {
- //List data;
- for (int i = 0; i < n; i++) {
- double elem;
- cout << "Enter " << i + 1 << " element: ";
- cin >> elem;
- data.push_back(elem);
- }
- double ans = 1;
- Cell* start = data.begin();
- Cell* end = data.end();
- for (int i = 0; i < n; i++) {
- double elem;
- elem = start->val * end->val;
- start = start->next;
- end = end->prev;
- ans = ans * elem;
- }
- cout << "Answer is " << ans << endl;
- }
- else {
- cout << "ERROR: n is too small" << endl;
- log_file << "ERROR: n is too small" << endl;
- }
- }
- void task1_file(List& data) {
- log_file << "call task1_file function" << endl;
- string in_file;
- string out_file;
- cout << "Enter input filename: ";
- cin >> in_file;
- cout << "Enter output filename: ";
- cin >> out_file;
- ifstream in(in_file);
- ofstream out(out_file);
- int n;
- in >> n;
- if (n >= 2) {
- //List data;
- for (int i = 0; i < n; i++) {
- double elem;
- //cout << "Enter " << i + 1 << " element: ";
- in >> elem;
- data.push_back(elem);
- }
- double ans = 1;
- Cell* start = data.begin();
- Cell* end = data.end();
- for (int i = 0; i < n; i++) {
- double elem;
- elem = start->val * end->val;
- start = start->next;
- end = end->prev;
- ans = ans * elem;
- }
- out << "Answer is " << ans << endl;
- }
- else {
- out << "ERROR: n is too small" << endl;
- }
- in.close();
- out.close();
- }
- void add_elem(List& data) {
- log_file << "call add_elem function" << endl;
- cout << "Enter element: ";
- int elem = get_int_input();
- data.push_back(elem);
- }
- void find_elem(List& data) {
- log_file << "call find_elem function" << endl;
- cout << "Enter element: ";
- int elem = get_int_input();
- cout << "elem on index " << data.find(elem) << endl;
- }
- void remove_elem(List& data) {
- log_file << "call remove_elem function" << endl;
- cout << "Enter element Index: ";
- int index = get_int_input();
- if (index < 0 || index >= data.size()) {
- cout << "ERROR: Incorect index";
- log_file << "ERROR: Incorect index";
- }
- else {
- data.remove(index);
- }
- }
- void clear_list(List& data) {
- log_file << "call clear function" << endl;
- data.clear();
- }
- void print_list(List& data) {
- log_file << "call print_list function" << endl;
- data.print();
- }
- int main()
- {
- log_file.open("log_file.txt");
- vector<void(*)(List&)> menu; // массив указателей на функции
- // функции
- menu.push_back(task1);
- menu.push_back(task1_file);
- menu.push_back(add_elem);
- menu.push_back(find_elem);
- menu.push_back(remove_elem);
- menu.push_back(clear_list);
- menu.push_back(print_list);
- List data;
- bool exit = false;
- while (!exit) {
- cout << "1) Variant task" << endl;
- cout << "2) Variant task with files" << endl;
- cout << "3) Add element" << endl;
- cout << "4) Find element" << endl;
- cout << "5) Remove element" << endl;
- cout << "6) Clear list" << endl;
- cout << "7) Print list" << endl;
- cout << "0) Exit" << endl;
- cout << "Select item: ";
- int input = get_int_input();
- if (input < 0 || input > menu.size()) {
- cout << endl << "Invalid item" << endl << endl;
- }
- else {
- if (input != 0) {
- menu[input - 1](data);
- }
- else {
- exit = true;
- }
- }
- }
- log_file << "Program successfully ended" << endl;
- log_file.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement