Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Z3.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include<iostream>
- using namespace std;
- struct spis
- {
- int inf;
- spis*prev, *next;
- };
- void create(spis**, spis**, int);
- void output(spis*);
- void del(spis**);
- void sort(spis*);
- void task_3(spis*);
- void merge(spis*, spis*);
- void zad3();
- void zad4();
- void zad5();
- int menu();
- void(*masf[])() = { zad3,zad4,zad5 };
- int main()
- {
- setlocale(0, "");
- while (1)
- (*masf[menu()])();
- system("pause");
- return 0;
- }
- int menu()
- {
- int flag;
- cout << "1 - Задание 3" << endl;
- cout << "2 - Задание 4" << endl;
- cout << "3 - Задание 5" << endl;
- cout << "4 - Выход" << endl;
- cin >> flag;
- if (flag == 4)
- exit(0);
- return flag - 1;
- }
- void zad3()
- {
- spis*begin = NULL, *end = NULL;
- int k, n;
- do
- {
- cout << "Введите число: ";
- cin >> n;
- create(&begin, &end, n);
- cout << "Продолжить? (1 - да , 0 - нет): ";
- cin >> k;
- } while (k != 0);
- cout << "Исходный список: ";
- output(begin);
- task_3(begin);
- del(&begin);
- }
- void zad4()
- {
- spis*begin1 = NULL, *end1 = NULL, *begin2 = NULL, *end2 = NULL;
- int k, n;
- cout << "Список 1:" << endl;
- do
- {
- cout << "Введите число: ";
- cin >> n;
- create(&begin1, &end1, n);
- cout << "Продолжить? (1 - да , 0 - нет): ";
- cin >> k;
- } while (k != 0);
- cout << "Список 2:" << endl;
- do
- {
- cout << "Введите число: ";
- cin >> n;
- create(&begin2, &end2, n);
- cout << "Продолжить? (1 - да , 0 - нет): ";
- cin >> k;
- } while (k != 0);
- cout << "Исходный список M1: ";
- sort(begin1);
- output(begin1);
- cout << "Исходный список M2: ";
- sort(begin2);
- output(begin2);
- cout << "Новый список M: ";
- del(&begin1);
- del(&begin2);
- }
- void zad5()
- {
- }
- void create(spis**begin, spis**end, int n)
- {
- spis*t = new spis;
- if (*begin == NULL)
- {
- t->inf = n;
- t->next = t->prev = NULL;
- *begin = *end = t;
- }
- else
- {
- t->inf = n;
- t->prev = *end;
- t->next = NULL;
- (*end)->next = t;
- *end = t;
- }
- }
- void del(spis**begin)
- {
- spis *t;
- while (*begin != NULL)
- {
- t = *begin;
- *begin = (*begin)->next;
- delete t;
- }
- }
- void output(spis*begin2)
- {
- spis*t;
- t = begin2;
- while (t != NULL)
- {
- cout << t->inf << " ";
- t = t->next;
- }
- cout << endl;
- }
- void task_3(spis*begin)
- {
- spis*t;
- int sum = 0, n = 0;
- t = begin;
- while (t != NULL)
- {
- n++;
- sum += t->inf;
- t = t->next;
- }
- cout << "Сумма: " << sum / n << endl;
- }
- void sort(spis *begin)
- {
- spis *t;
- t = begin->next;
- int x;
- while (t)
- {
- x = t->inf;
- spis *b;
- b = t->prev;
- while (b != NULL && x < b->inf)
- {
- b->next->inf = b->inf;
- b = b->prev;
- }
- if (b == NULL) begin->inf = x;
- else b->next->inf = x;
- t = t->next;
- }
- }
- void merge(spis*begin1, spis*begin2)
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement