Advertisement
Yuk1hiro

Lab_2

May 6th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.90 KB | None | 0 0
  1. Лабораторная №2:
  2. Array.cpp:
  3. #include <iostream>
  4. #include <string>
  5. #include <cstdio>
  6. #include "Array.h"
  7.  
  8. //Methods for class "Array:
  9.  
  10. //Creating Array.
  11. void Array::createArray(){
  12.     cout << "\n\tStarting creating of array. Enter size (should be >= 0):" << endl << "-> ";
  13.     cin >> size;
  14.     if(size < 0){ //Checking size.
  15.         cout << "\n\n\t\aError #I1: Size of array shouldn't be less, than 0! And you entered: " << size << "." << endl;
  16.         exit(11);
  17.     }
  18.     element = new Element[size]; //Creating array.
  19. }
  20.  
  21. //Entering Array.
  22. void Array::enterArray(){
  23.     cout << "\nStarted entering of array.\n" << endl;
  24.     cout << "Array size is: " << size << ".\n";
  25.     cout << "Now enter values for each element of array: " << endl;
  26.     string tmp = "";
  27.     for(int i = 0; i < size; i++){ //Cycle for entering array of elements.
  28.    
  29.         retry1: //***LABEL RETRY1***This label is using if the user entered an empty string.
  30.        
  31.         element[i].setValue(""); //Just to make right retry.
  32.         cout << "Element [" << i+1 << "] = "; //Beautiful printing.
  33.         cin >> tmp;
  34.         element[i].setValue(tmp);//Entering values.
  35.         if(element[i].getValue() == ""){ //If entered value is null .
  36.        
  37.             retry2: //***LABEL RETRY2***This label is using if the user entered something other than Y or N.
  38.            
  39.             tmp = ""; //Just to have right checking.
  40.             cout << "\nAre You shure, that this element is empty? (Y/N): ";
  41.             cin >> tmp;
  42.             if(tmp == "Y") continue; //If Y: Continuing to next iteration.
  43.             else if(tmp == "N") goto retry1; //If N: Allow the user to enter the value again by going to label "retry1".
  44.             else {
  45.                 cout << "Error #I2: You should answer with Y or N. (Your answer is: " << tmp << "). Try again." << endl; //If not Y or N:
  46.                 goto retry2;                                                            //Moving user to label "retry2".
  47.             }
  48.         }
  49.     }
  50.     cout << "\nNice. Array was successfully created." << endl;
  51. }
  52.  
  53. //Printing array.
  54. void Array::showArray(){
  55.     cout << "Size: " << size << endl;
  56.     cout << "Array:\n{\n" << endl;
  57.     for(int i = 0; i < size; i++){
  58.         cout << "\tElement [" << i+1 << "] = " << element[i].getValue() << ";\n";
  59.     }
  60.     cout << "}" << endl;
  61. }
  62.  
  63. //Add element to array.
  64. void Array::addElement(){
  65.     cout << "\nStarted adding element." << endl;
  66.     Element *temp = new Element[size + 1]; //Creating temporary array to add element.
  67.     for(int i = 0; i < size; i++){
  68.         temp[i].setValue(element[i].getValue()); //Copying all elements from Array.
  69.     }
  70.     string v;
  71.     size++; //Increasing size.
  72.     delete[] element; //Deleting pointer.
  73.     element = temp; //Now element is pointer for bigger array.
  74.     temp = 0;
  75.     retry1: //***LABEL RETRY1***This label is using if the user entered an empty string.
  76.     cout << "\nNow enter value of element, which you want to add:" << endl << "-> ";
  77.     cin >> v;
  78.     element[size-1].setValue(v);
  79.     if(element[size-1].getValue() == ""){ //If entered value is null .
  80.  
  81.         retry2: //***LABEL RETRY2***This label is using if the user entered something other than Y or N.
  82.         string tmp = ""; //Just to have right checking.
  83.         cout << "\nAre You shure, that this element is empty? (Y/N): ";
  84.         cin >> tmp;
  85.         if(tmp == "N") goto retry1; //If N: Allow the user to enter the value again by going to label "retry1".
  86.         else if(tmp != "Y"){
  87.             cout << "Error #I2: You should answer with Y or N. (Your answer is: " << tmp << "). Try again." << endl; //If not Y or N:
  88.             goto retry2;                                                            //Moving user to label "retry2".
  89.         }
  90.     }
  91.     cout << "\nAdding new element ended successfully.\n";
  92. }
  93.  
  94. //Checking occurence of element in array.
  95. bool Array::checkOccurenceOfElement(string tmp){
  96.     for(int i = 0; i < size; i++){
  97.         if(tmp == element[i].getValue()) return true;
  98.     }
  99.     return false;
  100. }
  101.  
  102. //Removing one element.
  103. void Array::removeElement(){
  104.     cout << "\nStarted removing of element." << endl;
  105.     this->showArray();
  106.     cout << "If you want to delete element by index, enter 1. \nIf you want to remove element by value, enter 2." << endl;
  107.     cout << "Your answer -> ";
  108.     int check = 0;
  109.     Element *temp;
  110.     cin >> check;  
  111.     switch(check){
  112.         case 1: //If user decided to delete element by index in array.
  113.         {
  114.             int index = 0;
  115.             cout << "\nDeleting element by index." << endl;
  116.             cout << "Now enter index of element, you want to delete: ";
  117.             cin >> index;
  118.             if(index <= 0 || index > size) { //Checking index to be right.
  119.                 cout << "\nError #I3: Index should be less, than " << size << ", and bigger, than 0." << endl;
  120.                 exit(13);
  121.             }
  122.             for(int i = index - 1; i < size-1; i++) //If checked successfull, deleting:
  123.                 element[i].setValue(element[i+1].getValue()); //In result: last element will be not needed.
  124.             Element *temp = new Element[size-1]; //creating temporary object to decreese size of array
  125.             for(int i = 0; i < size-1; i++)
  126.                 temp[i].setValue(element[i].getValue()); //Copying to temp
  127.             delete[] element; //Clearing original
  128.             element = temp; //Pointing original to temp
  129.             size--; //Minus size;
  130.             temp = 0; //"Destroying temp"
  131.             cout << "\nSuccessfully removed.\n";
  132.             break;
  133.         }
  134.         case 2:
  135.         {
  136.             cout << "\Deleting element by value." << endl;
  137.             cout << "Enter value of element, which you need to remove: ";
  138.             string inStr; //Creating buffer string
  139.             cin >> inStr;
  140.             if(!this->checkOccurenceOfElement(inStr)) { //checking for occurence
  141.                 cout << "\nElement not found." << endl;
  142.                 break;
  143.             }
  144.             int tInd; //if element found getting
  145.             for(int i = 0; i < size; i++){
  146.                 if(element[i].getValue() == inStr) //if element equals to needed
  147.                     tInd = i;                      //remember it's index
  148.             }
  149.             for(int i = tInd; i < size-1; i++)
  150.                 element[i].setValue(element[i+1].getValue()); //moving elements for 1 to left (with destroying needed element)
  151.             Element *temp = new Element[size-1]; //same as with indexes
  152.             for(int i = 0; i < size-1; i++)
  153.                 temp[i].setValue(element[i].getValue());
  154.             delete[] element;
  155.             size--;
  156.             element = temp;
  157.             temp = 0;
  158.            
  159.             cout << "\nSuccessfully removed.\n";
  160.             break;
  161.         }
  162.         default: { //If user's answer != 1 or 2
  163.             cout << "\nError #I4: Your answer should be 1 or 2.\n";
  164.             exit(14);
  165.             break;
  166.         }
  167.     }
  168. }
  169.  
  170. Array.h:
  171.  
  172. #pragma once
  173. #include <iostream>
  174. #include <string>
  175. #include <cstdio>
  176.  
  177. using namespace std;
  178.  
  179. class Element
  180. {
  181. private:
  182.     string value; //Value of element;
  183. public:
  184.     Element() { value = ""; } //Default constructor;
  185.     Element(string inValue) { value = inValue; } //Condstructor with parameter;
  186.     Element(const Element& el) { value = el.value; } //Copy constructor;
  187.     string getValue() { // get value of element
  188.         return value;
  189.     }
  190.     void setValue(string val){ //set value of element
  191.         value = val;
  192.     }
  193.     ~Element() {   //Destructor
  194.         value = "";//Just making this empty
  195.     }
  196. };
  197.  
  198.  
  199. class Array
  200. {
  201. private:
  202.     long int size; //Size of array
  203.     Element *element; //pointer to create array of objects || example of agregation.
  204. public:
  205.     Array() { size = 0; } //Default constructor
  206.     Array(long int inSize){//Constuctor with params;
  207.         if(inSize < 0){ //Checking size.
  208.             cout << "\n\n\tError #I1: Size of array shouldn't be less, than 0! And you entered: " << inSize << "." << endl;
  209.             exit(11);
  210.         }
  211.         size = inSize;
  212.     }
  213.     Array(const Array& a){ //Copy constructor;
  214.         size = a.size;
  215.         element = a.element;
  216.     }
  217.     void createArray(); //Create array of Elements;
  218.     void enterArray(); //Entering array of Elements;
  219.     void showArray(); //Printing all data about array;
  220.     void addElement(); //Add one element to array;
  221.     void removeElement(); //Remove one element from array;
  222.     bool checkOccurenceOfElement(string); //Check if element is in array;
  223.    
  224.     ~Array(){ //destructor
  225.         delete[] element;
  226.         cout << "\n\n\t\tSYSINFO: Object deleted.\n\n";
  227.     }
  228. };
  229.  
  230. main.cpp:
  231.  
  232. #include <iostream>
  233. #include <string>
  234. #include <cstdio>
  235. #include "Array.h"
  236.  
  237. int main()
  238. {
  239.     Array ar1;
  240.     ar1.createArray();
  241.     ar1.enterArray();
  242.     ar1.showArray();
  243.     ar1.addElement();
  244.     ar1.showArray();
  245.     ar1.checkOccurenceOfElement("abcde") ? cout << "Found" << endl : cout << "Not Found" << endl;
  246.     ar1.removeElement();
  247.     ar1.showArray();
  248.     return 0;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement