Advertisement
Yuk1hiro

Lab_4

May 6th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.28 KB | None | 0 0
  1. Лабораторная №4:
  2.  
  3. Array.cpp:
  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. void Array::removeElement(){
  108.     cout << "\nStarted removing of element." << endl;
  109.     this->showArray();
  110.     cout << "If you want to delete element by index, enter 1. \nIf you want to remove element by value, enter 2." << endl;
  111.     cout << "Your answer -> ";
  112.     int check = 0;
  113.     Element *temp;
  114.     cin >> check;  
  115.     switch(check){
  116.         case 1: //If user decided to delete element by index in array.
  117.         {
  118.             int index = 0;
  119.             cout << "\nDeleting element by index." << endl;
  120.             cout << "Now enter index of element, you want to delete: ";
  121.             cin >> index;
  122.             if(index <= 0 || index > size) {
  123.                 cout << "\nError #I3: Index should be less, than " << size << ", and bigger, than 0." << endl;
  124.                 exit(13);
  125.             }
  126.             for(int i = index - 1; i < size-1; i++)
  127.                 element[i].setValue(element[i+1].getValue());
  128.             Element *temp = new Element[size-1];
  129.             for(int i = 0; i < size-1; i++)
  130.                 temp[i].setValue(element[i].getValue());
  131.             delete[] element;
  132.             element = temp;
  133.             size--;
  134.             temp = 0;
  135.             cout << "\nSuccessfully removed.\n";
  136.             break;
  137.         }
  138.         case 2:
  139.         {
  140.             cout << "\Deleting element by value." << endl;
  141.             cout << "Enter value of element, which you need to remove: ";
  142.             string inStr;
  143.             cin >> inStr;
  144.             if(!this->checkOccurenceOfElement(inStr)) {
  145.                 cout << "\nElement not found." << endl;
  146.                 break;
  147.             }
  148.             int tInd;
  149.             for(int i = 0; i < size; i++){
  150.                 if(element[i].getValue() == inStr)
  151.                     tInd = i;
  152.             }
  153.             for(int i = tInd; i < size-1; i++)
  154.                 element[i].setValue(element[i+1].getValue());
  155.             Element *temp = new Element[size-1];
  156.             for(int i = 0; i < size-1; i++)
  157.                 temp[i].setValue(element[i].getValue());
  158.             delete[] element;
  159.             size--;
  160.             element = temp;
  161.             temp = 0;
  162.            
  163.             cout << "\nSuccessfully removed.\n";
  164.             break;
  165.         }
  166.         default: {
  167.             cout << "\nError #I4: Your answer should be 1 or 2.\n";
  168.             exit(14);
  169.             break;
  170.         }
  171.     }
  172. }
  173.  
  174. Array operator+(const Array& a1, const Array& a2){
  175.     Array a3;
  176.     Array temp;
  177.     temp.size = a1.size + a2.size;
  178.     temp.element = new Element[temp.size];
  179.    
  180.     int j = 0;
  181.     for(int i = 0; i < a1.size; i++){
  182.         if(!a2.checkOccurenceOfElement(a1.element[i].getValue())){
  183.             temp.element[j].setValue(a1.element[i].getValue());
  184.             j++;
  185.         }
  186.     }
  187.     a3.size = j + a2.size;
  188.     a3.element = new Element[a3.size];
  189.     for(int i = 0; i < j; i++){
  190.         a3.element[i].setValue(temp.element[i].getValue());
  191.     }
  192.     for(int i = 0; i < a2.size; i++){
  193.         a3.element[j].setValue(a2.element[i].getValue());
  194.         j++;
  195.     }
  196.     return a3;
  197. }
  198.  
  199. Array& Array::operator =(const Array& a){
  200.     this->size = a.size;
  201.     this->element = new Element[this->size];
  202.     for(int i = 0; i < this->size; i++)
  203.         this->element[i].setValue(a.element[i].getValue());
  204.     return *this;
  205. }
  206.  
  207. Array operator *(const Array& a1, const Array& a2){
  208.     Array a3;
  209.     Array temp;
  210.     temp.size = a1.size + a2.size;
  211.     temp.element = new Element[temp.size];
  212.    
  213.     int j = 0;
  214.     for(int i = 0; i < a1.size; i++){
  215.         if(a2.checkOccurenceOfElement(a1.element[i].getValue())){
  216.             temp.element[j].setValue(a1.element[i].getValue());
  217.             j++;
  218.         }
  219.     }
  220.     a3.size = j;
  221.     a3.element = new Element[a3.size];
  222.     for(int i = 0; i < j; i++){
  223.         a3.element[i].setValue(temp.element[i].getValue());
  224.     }
  225.     return a3;
  226. }
  227.  
  228. Array operator -(const Array& a1, const Array& a2){
  229.     Array a3;
  230.     Array temp;
  231.     temp.size = a1.size + a2.size;
  232.     temp.element = new Element[temp.size];
  233.    
  234.     int j = 0;
  235.     for(int i = 0; i < a1.size; i++){
  236.         if(!a2.checkOccurenceOfElement(a1.element[i].getValue())){
  237.             temp.element[j].setValue(a1.element[i].getValue());
  238.             j++;
  239.         }
  240.     }
  241.     for(int i = 0; i < a2.size; i++){
  242.         if(!a1.checkOccurenceOfElement(a2.element[i].getValue())){
  243.             temp.element[j].setValue(a2.element[i].getValue());
  244.             j++;
  245.         }
  246.     }
  247.     a3.size = j;
  248.     a3.element = new Element[a3.size];
  249.     for(int i = 0; i < j; i++){
  250.         a3.element[i].setValue(temp.element[i].getValue());
  251.     }
  252.     return a3;
  253. }
  254.  
  255. ostream& operator << (ostream& s, const Array& a){
  256.     s << "[Size: " << a.size << "]" << endl;
  257.     s << "Array:\n{\n" << endl;
  258.     for(int i = 0; i < a.size; i++){
  259.         s << "\tElement [" << i+1 << "] = " << a.element[i].getValue() << ";\n";
  260.     }
  261.     s << "}" << endl;
  262.     return s;
  263. }
  264.  
  265. istream& operator >> (istream& s, Array& a){
  266.     cout << "\n\tStarting creating of array. Enter size (should be >= 0):" << endl << "-> ";
  267.     s >> a.size;
  268.     if(a.size < 0){ //Checking size.
  269.         cout << "\n\n\t\aError #I1: Size of array shouldn't be less, than 0! And you entered: " << a.size << "." << endl;
  270.         exit(11);
  271.     }
  272.     //Creating array.
  273.     a.element = new Element[a.size];
  274.     for(int i = 0; i < a.size; i++){
  275.         a.element[i].setValue("0");
  276.     }
  277.     cout << "Now enter values for each element of array: " << endl;
  278.     string tmp = "";
  279.     for(int i = 0; i < a.size; i++){ //Cycle for entering array of elements.
  280.    
  281.         retry1: //***LABEL RETRY1***This label is using if the user entered an empty string.
  282.        
  283.         a.element[i].setValue(""); //Just to make right retry.
  284.         cout << "Element [" << i+1 << "] = "; //Beautiful printing.
  285.         s >> tmp;
  286.         a.element[i].setValue(tmp);//Entering values.
  287.         if(a.element[i].getValue() == ""){ //If entered value is null .
  288.        
  289.             retry2: //***LABEL RETRY2***This label is using if the user entered something other than Y or N.
  290.            
  291.             tmp = ""; //Just to have right checking.
  292.             cout << "\nAre You shure, that this element is empty? (Y/N): ";
  293.             s >> tmp;
  294.             if(tmp == "Y") continue; //If Y: Continuing to next iteration.
  295.             else if(tmp == "N") goto retry1; //If N: Allow the user to enter the value again by going to label "retry1".
  296.             else {
  297.                 cout << "Error #I2: You should answer with Y or N. (Your answer is: " << tmp << "). Try again." << endl; //If not Y or N:
  298.                 goto retry2;                                                            //Moving user to label "retry2".
  299.             }
  300.         }
  301.     }
  302.     return s;
  303. }
  304.  
  305.  
  306. Array.h:
  307. #pragma once
  308. #include <iostream>
  309. #include <string>
  310. #include <cstdio>
  311.  
  312. using namespace std;
  313.  
  314. class Element
  315. {
  316. private:
  317.     string value; //Value of element;
  318. public:
  319.     Element() { value = ""; } //Default constructor;
  320.     Element(string inValue) { value = inValue; } //Condstructor with parameter;
  321.     Element(const Element& el) { value = el.value; } //Copy constructor;
  322.     string getValue() const {
  323.         return value;
  324.     }
  325.     void setValue(string val){
  326.         value = val;
  327.     }
  328.     ~Element() {   //Destructor
  329.         value = "";//Just making this empty
  330.     }
  331. };
  332.  
  333.  
  334. class Array
  335. {
  336. private:
  337.     long int size; //Size of array
  338.     Element *element; //pointer to create array of objects || example of agregation.
  339. public:
  340.     Array() { size = 1; }
  341.     Array(long int inSize){//Constuctor with params;
  342.         if(inSize < 0){ //Checking size.
  343.             cout << "\n\n\tError #I1: Size of array shouldn't be less, than 0! And you entered: " << inSize << "." << endl;
  344.             exit(11);
  345.         }
  346.         size = inSize;
  347.     }
  348.     Array(const Array& a){ //Copy constructor;
  349.         size = a.size;
  350.         element = a.element;
  351.     }
  352.     void createArray(); //Create array of Elements;
  353.     void enterArray(); //Entering array of Elements;
  354.     void showArray() const; //Printing all data about array;
  355.     void addElement(); //Add one element to array;
  356.     void removeElement(); //Remove one element from array;
  357.     bool checkOccurenceOfElement(string) const; //Check if element is in array;
  358.     Array& operator=(const Array&);
  359.     friend Array operator +(const Array&, const Array&);
  360.     friend Array operator *(const Array&, const Array&);
  361.     friend Array operator -(const Array&, const Array&);
  362.     friend istream& operator>>(istream& s, Array& a);
  363.     friend ostream& operator<<(ostream& s, const Array& a);
  364.     ~Array(){ //destructor
  365.         delete[] element;
  366.         cout << "\n\n\t\tSYSINFO: Object deleted.\n\n";
  367.     }
  368.    
  369. };
  370.  
  371. main.cpp:
  372. #include <iostream>
  373. #include <string>
  374. #include <cstdio>
  375. #include "Array.h"
  376.  
  377. int main()
  378. {
  379.     Array a1;
  380.     cin >> a1;
  381.     cout << a1;
  382.     return 0;
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement