Advertisement
Guest User

E3

a guest
Feb 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. void push_back(int *&mass, int &N, const int value);
  8. void pop_back(int *&mass, int &N);
  9. void push_front(int *&mass, int &N, const int value);
  10. void pop_front(int *&mass, int &N);
  11.  
  12. int main()
  13. {
  14.     int N, ch, exit = 0;
  15.     cout << "Enter integer values: ";
  16.     cin >> N;
  17.     int *mass = new int[N];
  18.     ifstream ff("text.txt");
  19.     if (!ff) {
  20.         cout << "Sorry, we can't fle$$$ your file!";
  21.         return 0;
  22.     }
  23.        
  24.     for (int i = 0; i < N; i++)
  25.     {
  26.         ff >> mass[i];
  27.         cout << mass[i] << " ";
  28.     }
  29.     ff.close();
  30.  
  31.     do {
  32.         cout << endl << "1) Task 1" << endl << "2) Task 2" << endl << "3) Task 3" << endl << "4) Task 4" << endl << "5) Task 5" << endl << "6) Task 6" << endl << "7) Task 7" << endl << "8) Task 8" << endl << "9) Exit" << endl;
  33.         cin >> ch;
  34.  
  35.         if (ch == 1) {
  36.             int newEl;
  37.             cout << "Enter the new element: ";
  38.             cin >> newEl;
  39.             push_back(mass, N, newEl);
  40.             for (int i = 0; i < N; i++)
  41.             {
  42.                 ff >> mass[i];
  43.                 cout << mass[i] << " ";
  44.             }
  45.         }
  46.  
  47.         else if (ch == 2) {
  48.             pop_back(mass, N);
  49.             for (int i = 0; i < N; i++)
  50.             {
  51.                 ff >> mass[i];
  52.                 cout << mass[i] << " ";
  53.             }
  54.  
  55.         }
  56.  
  57.         else if (ch == 3) {
  58.             int newEl3;
  59.             cout << "Enter the new element: ";
  60.             cin >> newEl3;
  61.             push_front(mass, N, newEl3);
  62.             for (int i = 0; i < N; i++)
  63.             {
  64.                 ff >> mass[i];
  65.                 cout << mass[i] << " ";
  66.             }
  67.         }
  68.  
  69.         else if (ch == 4) {
  70.             pop_front(mass, N);
  71.             for (int i = 0; i < N; i++)
  72.             {
  73.                 ff >> mass[i];
  74.                 cout << mass[i] << " ";
  75.             }
  76.         }
  77.  
  78.         else if (ch == 9) exit = 1;
  79.  
  80.         else cout << endl << "We are apologize, but this part of program doesn't work." << endl << "Please, choose a correct task from menu or ask admin what to do and stand by." << endl;
  81.     } while (exit != 1);
  82.  
  83.    
  84.  
  85.     delete[] mass;
  86.     return 0;
  87. }
  88.  
  89.  
  90. void push_back(int *&mass, int &N, const int value)
  91. {
  92.     int *newMass = new int[N + 1];
  93.     for (int i = 0; i < N; i++)
  94.     {
  95.         newMass[i] = mass[i];
  96.     }
  97.     newMass[N] = value;
  98.     N++;
  99.  
  100.     delete[] mass;
  101.     mass = newMass;
  102. }
  103.  
  104. void pop_back(int *&mass, int &N)
  105. {
  106.     N--;
  107.     int *newMass = new int[N];
  108.     for (int i = 0; i < N; i++)
  109.     {
  110.         newMass[i] = mass[i];
  111.     }
  112.  
  113.     delete[] mass;
  114.     mass = newMass;
  115. }
  116.  
  117. void push_front(int *&mass, int &N, const int value)
  118. {
  119.     cout << "Enter N: ";
  120.     cin >> N;
  121.     int *newMass = new int[N];
  122.     newMass[0] = N;
  123.     for (int i = 1; i < N; i++)
  124.     {
  125.         newMass[i] = mass[i - 1];
  126.     }
  127.  
  128.     delete[] mass;
  129.     mass = newMass;
  130. }
  131.  
  132. void pop_front(int *&mass, int &N)
  133. {
  134.  
  135.     int *newMass = new int[N];
  136.     for (int i = 0; i < N - 1; ++i)
  137.     {
  138.         newMass[i] = mass[i + 1];
  139.     }
  140.     N--;
  141.  
  142.     delete[] mass;
  143.     mass = newMass;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement