Advertisement
Yuk1hiro

Lab_3

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