Advertisement
Yuk1hiro

Lab 5

May 27th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.88 KB | None | 0 0
  1. Файл main.cpp:
  2.  
  3. #include "Array.cpp"
  4.  
  5. int main()
  6. {
  7.     Array<string, int> ar1;
  8.     ar1.createArray();
  9.     ar1.enterArray();
  10.     ar1.showArray();
  11.     ar1.addElement();
  12.     ar1.showArray();
  13.     ar1.checkOccurenceOfElement("abcde") ? cout << "Found" << endl : cout << "Not Found" << endl;
  14.     ar1.removeElement();
  15.     ar1.showArray();
  16.     return 0;
  17. }
  18.  
  19. Файл Array.cpp:
  20.  
  21. #include "Array.h"
  22.  
  23. //Methods for class "Array:
  24.  
  25. //Creating Array.
  26. template <class E, class Y> void Array<E, Y>::createArray(){
  27.     lab1:
  28.     cout << "\n\tStarting creating of array. Enter size (should be >= 0):" << endl << "-> ";
  29.     cin >> size;
  30.     if(size < 0){ //Checking size.
  31.         cout << "\n\n\t\aError #I1: Size of array shouldn't be less, than 0! And you entered: " << size << "." << endl;
  32.         goto lab1;
  33.     }
  34.     element = new Element<E>[size]; //Creating array.
  35. }
  36.  
  37. //Entering Array.
  38. template <class E, class Y> void Array<E, Y>::enterArray(){
  39.     cout << "\nStarted entering of array.\n" << endl;
  40.     cout << "Array size is: " << size << ".\n";
  41.     cout << "Now enter values for each element of array: " << endl;
  42.     E tmp = "";
  43.     for(Y i = 0; i < size; i++){ //Cycle for entering array of elements.
  44.    
  45.         retry1: //***LABEL RETRY1***This label is using if the user entered an empty string.
  46.        
  47.         element[i].setValue(""); //Just to make right retry.
  48.         cout << "Element [" << i+1 << "] = "; //Beautiful printing.
  49.         cin >> tmp;
  50.         element[i].setValue(tmp);//Entering values.
  51.         if(element[i].getValue() == ""){ //If entered value is null .
  52.        
  53.             retry2: //***LABEL RETRY2***This label is using if the user entered something other than Y or N.
  54.            
  55.             tmp = ""; //Just to have right checking.
  56.             cout << "\nAre You shure, that this element is empty? (Y/N): ";
  57.             cin >> tmp;
  58.             if(tmp == "Y") continue; //If Y: Continuing to next iteration.
  59.             else if(tmp == "N") goto retry1; //If N: Allow the user to enter the value again by going to label "retry1".
  60.             else {
  61.                 cout << "Error #I2: You should answer with Y or N. (Your answer is: " << tmp << "). Try again." << endl; //If not Y or N:
  62.                 goto retry2;                                                            //Moving user to label "retry2".
  63.             }
  64.         }
  65.     }
  66.     cout << "\nNice. Array was successfully created." << endl;
  67. }
  68.  
  69. //Printing array.
  70. template <class E, class Y> void Array<E, Y>::showArray(){
  71.     cout << "Size: " << size << endl;
  72.     cout << "Array:\n{\n" << endl;
  73.     for(Y i = 0; i < size; i++){
  74.         cout << "\tElement [" << i+1 << "] = " << element[i].getValue() << ";\n";
  75.     }
  76.     cout << "}" << endl;
  77. }
  78.  
  79. //Add element to array.
  80. template <class E, class Y> void Array<E, Y>::addElement(){
  81.     cout << "\nStarted adding element." << endl;
  82.     Element<E> *temp = new Element<E>[size + 1]; //Creating temporary array to add element.
  83.     for(Y i = 0; i < size; i++){
  84.         temp[i].setValue(element[i].getValue()); //Copying all elements from Array.
  85.     }
  86.     E v;
  87.     size++; //Increasing size.
  88.     delete[] element; //Deleting pointer.
  89.     element = temp; //Now element is pointer for bigger array.
  90.     temp = 0;
  91.     retry1: //***LABEL RETRY1***This label is using if the user entered an empty string.
  92.     cout << "\nNow enter value of element, which you want to add:" << endl << "-> ";
  93.     cin >> v;
  94.     element[size-1].setValue(v);
  95.     if(element[size-1].getValue() == ""){ //If entered value is null .
  96.  
  97.         retry2: //***LABEL RETRY2***This label is using if the user entered something other than Y or N.
  98.         E tmp = ""; //Just to have right checking.
  99.         cout << "\nAre You shure, that this element is empty? (Y/N): ";
  100.         cin >> tmp;
  101.         if(tmp == "N") goto retry1; //If N: Allow the user to enter the value again by going to label "retry1".
  102.         else if(tmp != "Y"){
  103.             cout << "Error #I2: You should answer with Y or N. (Your answer is: " << tmp << "). Try again." << endl; //If not Y or N:
  104.             goto retry2;                                                            //Moving user to label "retry2".
  105.         }
  106.     }
  107.     cout << "\nAdding new element ended successfully.\n";
  108. }
  109.  
  110. //Checking occurence of element in array.
  111. template <class E, class Y> bool Array<E, Y>::checkOccurenceOfElement(E tmp){
  112.     for(Y i = 0; i < size; i++){
  113.         if(tmp == element[i].getValue()) return true;
  114.     }
  115.     return false;
  116. }
  117.  
  118. //Removing one element.
  119. template <class E, class Y> void Array<E, Y>::removeElement(){
  120.     cout << "\nStarted removing of element." << endl;
  121.     this->showArray();
  122.     lab3:
  123.     cout << "If you want to delete element by index, enter 1. \nIf you want to remove element by value, enter 2." << endl;
  124.     cout << "Your answer -> ";
  125.     int check = 0;
  126.     cin >> check;  
  127.     switch(check){
  128.         case 1: //If user decided to delete element by index in array.
  129.         {
  130.             lab2:
  131.             Y index = 0;
  132.             cout << "\nDeleting element by index." << endl;
  133.             cout << "Now enter index of element, you want to delete: ";
  134.             cin >> index;
  135.             if(index <= 0 || index > size) { //Checking index to be right.
  136.                 cout << "\nError #I3: Index should be less, than " << size << ", and bigger, than 0." << endl;
  137.                 goto lab2;
  138.             }
  139.             for(Y i = index - 1; i < size-1; i++) //If checked successfull, deleting:
  140.                 element[i].setValue(element[i+1].getValue()); //In result: last element will be not needed.
  141.             Element<E> *temp = new Element<E>[size-1]; //creating temporary object to decreese size of array
  142.             for(Y i = 0; i < size-1; i++)
  143.                 temp[i].setValue(element[i].getValue()); //Copying to temp
  144.             delete[] element; //Clearing original
  145.             element = temp; //Pointing original to temp
  146.             size--; //Minus size;
  147.             temp = 0; //"Destroying temp"
  148.             cout << "\nSuccessfully removed.\n";
  149.             break;
  150.         }
  151.         case 2:
  152.         {
  153.             cout << "\nDeleting element by value." << endl;
  154.             cout << "Enter value of element, which you need to remove: ";
  155.             E inStr; //Creating buffer string
  156.             cin >> inStr;
  157.             if(!this->checkOccurenceOfElement(inStr)) { //checking for occurence
  158.                 cout << "\nElement not found." << endl;
  159.                 break;
  160.             }
  161.             Y tInd; //if element found getting
  162.             for(Y i = 0; i < size; i++){
  163.                 if(element[i].getValue() == inStr) //if element equals to needed
  164.                     tInd = i;                      //remember it's index
  165.             }
  166.             for(Y i = tInd; i < size-1; i++)
  167.                 element[i].setValue(element[i+1].getValue()); //moving elements for 1 to left (with destroying needed element)
  168.             Element<E> *temp = new Element<E>[size-1]; //same as with indexes
  169.             for(Y i = 0; i < size-1; i++)
  170.                 temp[i].setValue(element[i].getValue());
  171.             delete[] element;
  172.             size--;
  173.             element = temp;
  174.             temp = 0;
  175.            
  176.             cout << "\nSuccessfully removed.\n";
  177.             break;
  178.         }
  179.         default: { //If user's answer != 1 or 2
  180.             cout << "\nError #I4: Your answer should be 1 or 2.\n";
  181.             goto lab3;
  182.             break;
  183.         }
  184.     }
  185. }
  186.  
  187. //Специализация для E = string & Y = int
  188. template <>
  189. class Array<string, int>
  190. {
  191. private:
  192.     int size; //Size of array
  193.     Element<string> *element; //pointer to create array of objects || example of agregation.
  194. public:
  195.     Array() { size = 0; } //Default constructor
  196.     Array(int inSize){//Constuctor with params;
  197.         if(inSize < 0){ //Checking size.
  198.             cout << "\n\n\tError #I1: Size of array shouldn't be less, than 0! And you entered: " << inSize << "." << endl;
  199.             cout << "\tSize of array has been setted to 1." << endl;
  200.             inSize = 1;
  201.         }
  202.         size = inSize;
  203.     }
  204.     Array(const Array<string, int>& a){ //Copy constructor;
  205.         size = a.size;
  206.         element = a.element;
  207.     }
  208.     void createArray() { //Create array of Elements;
  209.         lab1:
  210.         cout << "\n\tStarting creating of array. Enter size (should be >= 0):" << endl << "-> ";
  211.         cin >> size;
  212.         if(size < 0){ //Checking size.
  213.             cout << "\n\n\t\aError #I1: Size of array shouldn't be less, than 0! And you entered: " << size << "." << endl;
  214.             goto lab1;
  215.         }
  216.         element = new Element<string>[size]; //Creating array.
  217.     }
  218.     void enterArray() { //Entering array of Elements;
  219.         cout << "\nStarted entering of array.\n" << endl;
  220.         cout << "Array size is: " << size << ".\n";
  221.         cout << "Now enter values for each element of array: " << endl;
  222.         string tmp = "";
  223.         for(int i = 0; i < size; i++){ //Cycle for entering array of elements.
  224.             retry1: //***LABEL RETRY1***This label is using if the user entered an empty string.
  225.            
  226.             element[i].setValue(""); //Just to make right retry.
  227.             cout << "Element [" << i+1 << "] = "; //Beautiful printing.
  228.             cin >> tmp;
  229.             element[i].setValue(tmp);//Entering values.
  230.             if(element[i].getValue() == ""){ //If entered value is null .
  231.            
  232.                 retry2: //***LABEL RETRY2***This label is using if the user entered something other than Y or N.
  233.                
  234.                 tmp = ""; //Just to have right checking.
  235.                 cout << "\nAre You shure, that this element is empty? (Y/N): ";
  236.                 cin >> tmp;
  237.                 if(tmp == "Y") continue; //If Y: Continuing to next iteration.
  238.                 else if(tmp == "N") goto retry1; //If N: Allow the user to enter the value again by going to label "retry1".
  239.                 else {
  240.                     cout << "Error #I2: You should answer with Y or N. (Your answer is: " << tmp << "). Try again." << endl; //If not Y or N:
  241.                     goto retry2;                                                            //Moving user to label "retry2".
  242.                 }
  243.             }
  244.         }
  245.         cout << "\nNice. Array was successfully created." << endl;
  246.     }
  247.     void showArray() { //Printing all data about array;
  248.         cout << "Size: " << size << endl;
  249.         cout << "Array:\n{\n" << endl;
  250.         for(int i = 0; i < size; i++){
  251.             cout << "\tElement [" << i+1 << "] = " << element[i].getValue() << ";\n";
  252.         }
  253.         cout << "}" << endl;
  254.     }
  255.     void addElement() { //Add one element to array;
  256.         cout << "\nStarted adding element." << endl;
  257.         Element<string> *temp = new Element<string>[size + 1]; //Creating temporary array to add element.
  258.         for(int i = 0; i < size; i++){
  259.             temp[i].setValue(element[i].getValue()); //Copying all elements from Array.
  260.         }
  261.         string v;
  262.         size++; //Increasing size.
  263.         delete[] element; //Deleting pointer.
  264.         element = temp; //Now element is pointer for bigger array.
  265.         temp = 0;
  266.         retry1: //***LABEL RETRY1***This label is using if the user entered an empty string.
  267.         cout << "\nNow enter value of element, which you want to add:" << endl << "-> ";
  268.         cin >> v;
  269.         element[size-1].setValue(v);
  270.         if(element[size-1].getValue() == ""){ //If entered value is null .
  271.  
  272.             retry2: //***LABEL RETRY2***This label is using if the user entered something other than Y or N.
  273.             string tmp = ""; //Just to have right checking.
  274.             cout << "\nAre You shure, that this element is empty? (Y/N): ";
  275.             cin >> tmp;
  276.             if(tmp == "N") goto retry1; //If N: Allow the user to enter the value again by going to label "retry1".
  277.             else if(tmp != "Y"){
  278.                 cout << "Error #I2: You should answer with Y or N. (Your answer is: " << tmp << "). Try again." << endl; //If not Y or N:
  279.                 goto retry2;                                                            //Moving user to label "retry2".
  280.             }
  281.         }
  282.         cout << "\nAdding new element ended successfully.\n";
  283.     }
  284.     void removeElement() { //Remove one element from array;
  285.         cout << "\nStarted removing of element." << endl;
  286.         this->showArray();
  287.         lab3:
  288.         cout << "If you want to delete element by index, enter 1. \nIf you want to remove element by value, enter 2." << endl;
  289.         cout << "Your answer -> ";
  290.         int check = 0;
  291.         cin >> check;  
  292.         switch(check){
  293.             case 1: //If user decided to delete element by index in array.
  294.             {
  295.                 lab2:
  296.                 int index = 0;
  297.                 cout << "\nDeleting element by index." << endl;
  298.                 cout << "Now enter index of element, you want to delete: ";
  299.                 cin >> index;
  300.                 if(index <= 0 || index > size) { //Checking index to be right.
  301.                     cout << "\nError #I3: Index should be less, than " << size << ", and bigger, than 0." << endl;
  302.                     goto lab2;
  303.                 }
  304.                 for(int i = index - 1; i < size-1; i++) //If checked successfull, deleting:
  305.                     element[i].setValue(element[i+1].getValue()); //In result: last element will be not needed.
  306.                 Element<string> *temp = new Element<string>[size-1]; //creating temporary object to decreese size of array
  307.                 for(int i = 0; i < size-1; i++)
  308.                     temp[i].setValue(element[i].getValue()); //Copying to temp
  309.                 delete[] element; //Clearing original
  310.                 element = temp; //Pointing original to temp
  311.                 size--; //Minus size;
  312.                 temp = 0; //"Destroying temp"
  313.                 cout << "\nSuccessfully removed.\n";
  314.                 break;
  315.             }
  316.             case 2:
  317.             {
  318.                 cout << "\nDeleting element by value." << endl;
  319.                 cout << "Enter value of element, which you need to remove: ";
  320.                 string inStr; //Creating buffer string
  321.                 cin >> inStr;
  322.                 if(!this->checkOccurenceOfElement(inStr)) { //checking for occurence
  323.                     cout << "\nElement not found." << endl;
  324.                     break;
  325.                 }
  326.                 int tInd; //if element found getting
  327.                 for(int i = 0; i < size; i++){
  328.                     if(element[i].getValue() == inStr) //if element equals to needed
  329.                         tInd = i;                      //remember it's index
  330.                 }
  331.                 for(int i = tInd; i < size-1; i++)
  332.                     element[i].setValue(element[i+1].getValue()); //moving elements for 1 to left (with destroying needed element)
  333.                 Element<string> *temp = new Element<string>[size-1]; //same as with indexes
  334.                 for(int i = 0; i < size-1; i++)
  335.                     temp[i].setValue(element[i].getValue());
  336.                 delete[] element;
  337.                 size--;
  338.                 element = temp;
  339.                 temp = 0;
  340.                
  341.                 cout << "\nSuccessfully removed.\n";
  342.                 break;
  343.             }
  344.             default: { //If user's answer != 1 or 2
  345.                 cout << "\nError #I4: Your answer should be 1 or 2.\n";
  346.                 goto lab3;
  347.                 break;
  348.             }
  349.         }
  350.     }
  351.     bool checkOccurenceOfElement(string tmp) { //Check if element is in array;
  352.         for(int i = 0; i < size; i++){
  353.             if(tmp == element[i].getValue()) return true;
  354.         }
  355.         return false;
  356.     }
  357.    
  358.     ~Array(){ //destructor
  359.         delete[] element;
  360.         cout << "\n\n\t\tSYSINFO: Object deleted.\n\n";
  361.     }
  362. };
  363.  
  364.  
  365. Файл Array.h:
  366.  
  367. #pragma once
  368. #include <iostream>
  369. #include <string>
  370. #include <cstdio>
  371.  
  372. using namespace std;
  373.  
  374. template<class E>
  375. class Element
  376. {
  377. private:
  378.     E value; //Value of element;
  379. public:
  380.     Element() { value = ""; } //Default constructor;
  381.     Element(E inValue) { value = inValue; } //Condstructor with parameter;
  382.     Element(const Element<E>& el) { value = el.value; } //Copy constructor;
  383.     E getValue() { // get value of element
  384.         return value;
  385.     }
  386.     void setValue(E val){ //set value of element
  387.         value = val;
  388.     }
  389.     ~Element() {   //Destructor
  390.         value = "";//Just making this empty
  391.     }
  392. };
  393.  
  394. template <class E, class Y>
  395. class Array
  396. {
  397. private:
  398.     Y size; //Size of array
  399.     Element<E> *element; //pointer to create array of objects || example of agregation.
  400. public:
  401.     Array() { size = 0; } //Default constructor
  402.     Array(Y inSize){//Constuctor with params;
  403.         if(inSize < 0){ //Checking size.
  404.             cout << "\n\n\tError #I1: Size of array shouldn't be less, than 0! And you entered: " << inSize << "." << endl;
  405.             cout << "\tSize of array has been setted to 1." << endl;
  406.             inSize = 1;
  407.         }
  408.         size = inSize;
  409.     }
  410.     Array(const Array<E, Y>& a){ //Copy constructor;
  411.         size = a.size;
  412.         element = a.element;
  413.     }
  414.     void createArray(); //Create array of Elements;
  415.     void enterArray(); //Entering array of Elements;
  416.     void showArray(); //Printing all data about array;
  417.     void addElement(); //Add one element to array;
  418.     void removeElement(); //Remove one element from array;
  419.     bool checkOccurenceOfElement(E); //Check if element is in array;
  420.    
  421.     ~Array(){ //destructor
  422.         delete[] element;
  423.         cout << "\n\n\t\tSYSINFO: Object deleted.\n\n";
  424.     }
  425. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement