Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.25 KB | None | 0 0
  1. //#include"pch.h"
  2. #include<iostream>
  3. #include<stdlib.h>
  4. #include<string>
  5.  
  6. using namespace std;
  7.  
  8. class Vehicle
  9. {
  10.     int i;
  11.     int y;
  12.     int p;
  13. public:
  14.     void Add(int id, int year, int prize);
  15.     void Show();
  16.     int getid();
  17.     int getyear();
  18.     int getprize();
  19.  
  20. };
  21.  
  22. int Vehicle::getid(){
  23.     return this->i;
  24. }
  25. int Vehicle::getyear(){
  26.     return this->y;
  27. }
  28. int Vehicle::getprize(){
  29.     return this->p;
  30. }
  31.  
  32. void Vehicle::Add(int id, int year, int prize) {
  33.     i = id;
  34.     y = year;
  35.     p = prize;
  36. }
  37. void Vehicle::Show() {
  38.     cout << "ID number: " << i << endl;
  39.     cout << "Year of production: " << y << endl;
  40.     cout << "Prize: " << p << endl;
  41. }
  42.  
  43. void Create(Vehicle* &t, int N)
  44. {
  45.     t = (Vehicle*)malloc(N * sizeof(Vehicle));
  46. }
  47.  
  48. void Create(Vehicle** &t, int N)
  49. {
  50.     t = (Vehicle**)malloc(N * sizeof(Vehicle*));
  51.     for (int i = 0; i < N; i++) {
  52.         (t)[i] = (Vehicle*)malloc(N * sizeof(Vehicle));
  53.     }
  54. }
  55.  
  56. void Remove(Vehicle* &t) {
  57.     free(t);
  58.     t = NULL;
  59. }
  60.  
  61. void Remove(Vehicle** &t, int N) {
  62.     for (int i = 0; i < N; i++) {
  63.         free(t[i]);
  64.     }
  65.     free(t);
  66.     t = NULL;
  67. }
  68.  
  69. void Add(Vehicle* &t, int N) {
  70.     int id;
  71.     int year;
  72.     int prize;
  73.     for (int i = 0; i < N; i++) {
  74.         cout << "Add ID number: " << endl;
  75.         cin >> id;
  76.         cout << "Add year of production: " << endl;
  77.         cin >> year;
  78.         cout << "Add prize: " << endl;
  79.         cin>>prize;
  80.         t[i].Add(id, year, prize);
  81.     }
  82.     cout << "Choose one of menu options: " << endl;
  83. }
  84.  
  85. void Add(Vehicle** &t, int N) {
  86.     int id;
  87.     int year;
  88.     int prize;
  89.     for (int i = 0; i < N; i++) {
  90.         cout << "Add ID number: " << endl;
  91.         cin >> id;
  92.         cout << "Add year of production: " << endl;
  93.         cin >> year;
  94.         cout << "Add prize: " << endl;
  95.         cin >> prize;
  96.         t[i]->Add(id, year, prize);
  97.     }
  98.     cout << "Choose one of menu options: " << endl;
  99. }
  100. //**************************************************************
  101. void AddNext(Vehicle* &t, int &N, int id, int year, int prize){
  102.     Vehicle* temp = NULL;
  103.     Create(temp, (N+1));
  104.     for(int i = 0; i < N; i++){
  105.         temp[i].Add(t[i].getid(),t[i].getyear(), t[i].getprize());
  106.     }
  107.     temp[N].Add(id, year, prize);
  108.     Remove(t);
  109.     N++;
  110.     t = temp;
  111.         cout << "Choose one of menu options: " << endl;
  112. }
  113.  
  114. void AddNext(Vehicle** &t, int &N, int id, int year, int prize){
  115.     Vehicle** temp = NULL;
  116.     Create(temp, (N+1));
  117.     for(int i = 0; i < N; i++){
  118.         temp[i]->Add(t[i]->getid(), t[i]->getyear(), t[i]->getprize());
  119.     }
  120.     temp[N]->Add(id, year, prize);
  121.     Remove(t, N);
  122.     N++;
  123.     t = temp;
  124.         cout << "Choose one of menu options: " << endl;
  125. }
  126. //****************************************************************
  127.  
  128. void RemoveNext(Vehicle* &t, int &N, int k){
  129.     int j = 0;
  130.     Vehicle* temp = NULL;
  131.     Create(temp, (N-1));
  132.     for(int i = 0; i < (N); i++){
  133.         if((k-1) != i){
  134.             temp[j].Add(t[i].getid(), t[i].getyear(), t[i].getprize());
  135.             j++;
  136.         }
  137.     }
  138.     Remove(t);
  139.     N--;
  140.     t = temp;
  141.         cout << "Choose one of menu options: " << endl;
  142. }
  143.  
  144. void RemoveNext(Vehicle** &t, int &N, int k){
  145.     int j = 0;
  146.     Vehicle ** temp = NULL;
  147.     Create(temp, (N-1));
  148.     for(int i = 0; i < (N); i++){
  149.         if((k-1) != i){
  150.             temp[j]->Add(t[i]->getid(), t[i]->getprize(), t[i]->getprize());
  151.             j++;
  152.         }
  153.     }
  154.     Remove(t, N);
  155.     N--;
  156.     t = temp;
  157.         cout << "Choose one of menu options: " << endl;
  158. }
  159. //**********************************************************
  160.  
  161. void Show(Vehicle* t, int N) {
  162.     cout << "List of vehicles: " << endl;
  163.     for (int i = 0; i < N; i++) {
  164.         t[i].Show();
  165.     }
  166.     cout << "Choose one of menu options: " << endl;
  167. }
  168.  
  169. void Show(Vehicle** t, int N) {
  170.     cout << "List of vehicles: " << endl;
  171.     for (int i = 0; i < N; i++) {
  172.         t[i]->Show();
  173.     }
  174.     cout << "Choose one of menu options: " << endl;
  175. }
  176.  
  177. int Menu() {
  178.  
  179.     int Size1, Size2;
  180.     int Year1, Year2;
  181.     int Prize1, Prize2;
  182.     int RemoveNext1, RemoveNext2;
  183.     int AddNext1, AddNext2;
  184.     Vehicle*vehicle1 = NULL;
  185.     Vehicle**vehicle2 = NULL;
  186.     int nrinstruction;
  187.  
  188.     cout
  189.         << "----------------------------------------\n"
  190.         << "----------  MENU - selection  ----------\n"
  191.         << "---Press 1 - intialize data on 1 wsk ---\n"
  192.         << "---Press 2 - intialize data on 2 wsk ---\n"
  193.         << "-------------Press 0 - exit-------------\n"
  194.         << "----------------------------------------\n";
  195.        
  196. do
  197. {
  198.     cin >> nrinstruction;
  199.         switch (nrinstruction)
  200.         {
  201.        
  202.         case 1:
  203.             int nrinstruction1;
  204.             system("CLS");
  205.         cout << "----------  MENU - selection  ----------\n"
  206.         << "---Press 1 - intialize data on 1 wsk ---\n"
  207.         << "----------Press 2 - show 1 wsk----------\n"
  208.         << "---------Press 3 - remove 1 wsk---------\n"
  209.         << "----------Press 4 - add 1 wsk-----------\n"
  210.         << "-----------Press 0 - go back------------\n"
  211.         << "----------------------------------------\n";
  212.         do
  213.         {
  214.         cin >> nrinstruction1;
  215.         switch (nrinstruction1)
  216.         {
  217.         case 1:
  218.             cout << "Type how much data you want to have:" << endl;
  219.             cin >> Size1;
  220.             Create(vehicle1, Size1);
  221.             Add(vehicle1, Size1);
  222.         break;
  223.            
  224.         case 2:
  225.             Show(vehicle1, Size1);
  226.         break;
  227.                    
  228.         case 3:
  229.             cout << "Chose id number: \n";
  230.             cin >> RemoveNext1;
  231.             RemoveNext(vehicle1, Size1, RemoveNext1);
  232.         break;
  233.            
  234.         case 4:
  235.             cout << "Add elements by ID number: \n";
  236.             cin >> AddNext1;
  237.             cout << "Add year of production: \n";
  238.             cin >> Year1;
  239.             cout << "Add prize: \n";
  240.             cin >> Prize1;
  241.             AddNext(vehicle1, Size1, AddNext1, Year1, Prize1);
  242.         break;
  243.        
  244.         case 0:
  245.             system("CLS");
  246.                 cout << "<-- Menu\n";
  247.                 return 1;
  248.             default:
  249.                 cout << "Choose one of options: 1 - add amount, 2 - show data, 3 - remove data, 4 - add data, 0 - back to menu \n";
  250.             break;
  251.         }  
  252.     } while (nrinstruction1 !=0);
  253.     Remove(vehicle1);
  254.     break;
  255.    
  256.         case 2:
  257.             int nrinstruction2;
  258.         cout << "----------  MENU - selection  ----------\n"
  259.         << "---Press 1 - intialize data on 2 wsk ---\n"
  260.         << "----------Press 2 - show 2 wsk----------\n"
  261.         << "---------Press 3 - remove 2 wsk---------\n"
  262.         << "----------Press 4 - add 2 wsk-----------\n"
  263.         << "-----------Press 0 - go back------------\n"
  264.         << "----------------------------------------\n";
  265.         do
  266.         {
  267.         cin >> nrinstruction2;
  268.         switch (nrinstruction2)
  269.         {
  270.         case 1:
  271.             cout << "Type how much data you want to have:" << endl;
  272.             cin >> Size2;
  273.             Create(vehicle2, Size2);
  274.             Add(vehicle2, Size2);
  275.         break;
  276.            
  277.         case 2:
  278.             Show(vehicle2, Size2);
  279.         break;
  280.            
  281.         case 3:
  282.             cout << "Chose id number: \n";
  283.             cin >> RemoveNext2;
  284.             RemoveNext(vehicle2, Size2, RemoveNext2);
  285.         break;
  286.            
  287.         case 4:
  288.             cout << "Add elements by ID number: \n";
  289.             cin >> AddNext2;
  290.             cout << "Add year of production: \n";
  291.             cin >> Year2;
  292.             cout << "Add prize: \n";
  293.             cin >> Prize2;
  294.             AddNext(vehicle2, Size2, AddNext2, Year2, Prize2);
  295.         break;
  296.        
  297.             case 0:
  298.                 system("CLS");
  299.                 cout << "<-- Menu \n";
  300.                 return 1;
  301.             default:
  302.                 cout << "Choose one of options: 1 - add amount, 2 - show data, 3 - remove data, 4 - add data, 0 - back to menu \n";
  303.             break;
  304.         }
  305.         } while (nrinstruction2 != 0);
  306.         Remove (vehicle2, Size2);
  307.         break;
  308.         case 0:
  309.             cout << "Exiting \n";
  310.             return 0;
  311.             default:
  312.                 cout << "Choose one of options: 1 - 1 wsk, 2 - 2 wsk, 0 - exit \n";
  313.                 break;
  314.             }
  315.         } while (nrinstruction);
  316.     }
  317.    
  318. int main()
  319. {
  320.     while (Menu());
  321.     return 0;
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement