Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Лабораторная №2:
- Array.cpp:
- #include <iostream>
- #include <string>
- #include <cstdio>
- #include "Array.h"
- //Methods for class "Array:
- //Creating Array.
- void Array::createArray(){
- cout << "\n\tStarting creating of array. Enter size (should be >= 0):" << endl << "-> ";
- cin >> size;
- if(size < 0){ //Checking size.
- cout << "\n\n\t\aError #I1: Size of array shouldn't be less, than 0! And you entered: " << size << "." << endl;
- exit(11);
- }
- element = new Element[size]; //Creating array.
- }
- //Entering Array.
- void Array::enterArray(){
- cout << "\nStarted entering of array.\n" << endl;
- cout << "Array size is: " << size << ".\n";
- cout << "Now enter values for each element of array: " << endl;
- string tmp = "";
- for(int i = 0; i < size; i++){ //Cycle for entering array of elements.
- retry1: //***LABEL RETRY1***This label is using if the user entered an empty string.
- element[i].setValue(""); //Just to make right retry.
- cout << "Element [" << i+1 << "] = "; //Beautiful printing.
- cin >> tmp;
- element[i].setValue(tmp);//Entering values.
- if(element[i].getValue() == ""){ //If entered value is null .
- retry2: //***LABEL RETRY2***This label is using if the user entered something other than Y or N.
- tmp = ""; //Just to have right checking.
- cout << "\nAre You shure, that this element is empty? (Y/N): ";
- cin >> tmp;
- if(tmp == "Y") continue; //If Y: Continuing to next iteration.
- else if(tmp == "N") goto retry1; //If N: Allow the user to enter the value again by going to label "retry1".
- else {
- cout << "Error #I2: You should answer with Y or N. (Your answer is: " << tmp << "). Try again." << endl; //If not Y or N:
- goto retry2; //Moving user to label "retry2".
- }
- }
- }
- cout << "\nNice. Array was successfully created." << endl;
- }
- //Printing array.
- void Array::showArray(){
- cout << "Size: " << size << endl;
- cout << "Array:\n{\n" << endl;
- for(int i = 0; i < size; i++){
- cout << "\tElement [" << i+1 << "] = " << element[i].getValue() << ";\n";
- }
- cout << "}" << endl;
- }
- //Add element to array.
- void Array::addElement(){
- cout << "\nStarted adding element." << endl;
- Element *temp = new Element[size + 1]; //Creating temporary array to add element.
- for(int i = 0; i < size; i++){
- temp[i].setValue(element[i].getValue()); //Copying all elements from Array.
- }
- string v;
- size++; //Increasing size.
- delete[] element; //Deleting pointer.
- element = temp; //Now element is pointer for bigger array.
- temp = 0;
- retry1: //***LABEL RETRY1***This label is using if the user entered an empty string.
- cout << "\nNow enter value of element, which you want to add:" << endl << "-> ";
- cin >> v;
- element[size-1].setValue(v);
- if(element[size-1].getValue() == ""){ //If entered value is null .
- retry2: //***LABEL RETRY2***This label is using if the user entered something other than Y or N.
- string tmp = ""; //Just to have right checking.
- cout << "\nAre You shure, that this element is empty? (Y/N): ";
- cin >> tmp;
- if(tmp == "N") goto retry1; //If N: Allow the user to enter the value again by going to label "retry1".
- else if(tmp != "Y"){
- cout << "Error #I2: You should answer with Y or N. (Your answer is: " << tmp << "). Try again." << endl; //If not Y or N:
- goto retry2; //Moving user to label "retry2".
- }
- }
- cout << "\nAdding new element ended successfully.\n";
- }
- //Checking occurence of element in array.
- bool Array::checkOccurenceOfElement(string tmp){
- for(int i = 0; i < size; i++){
- if(tmp == element[i].getValue()) return true;
- }
- return false;
- }
- //Removing one element.
- void Array::removeElement(){
- cout << "\nStarted removing of element." << endl;
- this->showArray();
- cout << "If you want to delete element by index, enter 1. \nIf you want to remove element by value, enter 2." << endl;
- cout << "Your answer -> ";
- int check = 0;
- Element *temp;
- cin >> check;
- switch(check){
- case 1: //If user decided to delete element by index in array.
- {
- int index = 0;
- cout << "\nDeleting element by index." << endl;
- cout << "Now enter index of element, you want to delete: ";
- cin >> index;
- if(index <= 0 || index > size) { //Checking index to be right.
- cout << "\nError #I3: Index should be less, than " << size << ", and bigger, than 0." << endl;
- exit(13);
- }
- for(int i = index - 1; i < size-1; i++) //If checked successfull, deleting:
- element[i].setValue(element[i+1].getValue()); //In result: last element will be not needed.
- Element *temp = new Element[size-1]; //creating temporary object to decreese size of array
- for(int i = 0; i < size-1; i++)
- temp[i].setValue(element[i].getValue()); //Copying to temp
- delete[] element; //Clearing original
- element = temp; //Pointing original to temp
- size--; //Minus size;
- temp = 0; //"Destroying temp"
- cout << "\nSuccessfully removed.\n";
- break;
- }
- case 2:
- {
- cout << "\Deleting element by value." << endl;
- cout << "Enter value of element, which you need to remove: ";
- string inStr; //Creating buffer string
- cin >> inStr;
- if(!this->checkOccurenceOfElement(inStr)) { //checking for occurence
- cout << "\nElement not found." << endl;
- break;
- }
- int tInd; //if element found getting
- for(int i = 0; i < size; i++){
- if(element[i].getValue() == inStr) //if element equals to needed
- tInd = i; //remember it's index
- }
- for(int i = tInd; i < size-1; i++)
- element[i].setValue(element[i+1].getValue()); //moving elements for 1 to left (with destroying needed element)
- Element *temp = new Element[size-1]; //same as with indexes
- for(int i = 0; i < size-1; i++)
- temp[i].setValue(element[i].getValue());
- delete[] element;
- size--;
- element = temp;
- temp = 0;
- cout << "\nSuccessfully removed.\n";
- break;
- }
- default: { //If user's answer != 1 or 2
- cout << "\nError #I4: Your answer should be 1 or 2.\n";
- exit(14);
- break;
- }
- }
- }
- Array.h:
- #pragma once
- #include <iostream>
- #include <string>
- #include <cstdio>
- using namespace std;
- class Element
- {
- private:
- string value; //Value of element;
- public:
- Element() { value = ""; } //Default constructor;
- Element(string inValue) { value = inValue; } //Condstructor with parameter;
- Element(const Element& el) { value = el.value; } //Copy constructor;
- string getValue() { // get value of element
- return value;
- }
- void setValue(string val){ //set value of element
- value = val;
- }
- ~Element() { //Destructor
- value = "";//Just making this empty
- }
- };
- class Array
- {
- private:
- long int size; //Size of array
- Element *element; //pointer to create array of objects || example of agregation.
- public:
- Array() { size = 0; } //Default constructor
- Array(long int inSize){//Constuctor with params;
- if(inSize < 0){ //Checking size.
- cout << "\n\n\tError #I1: Size of array shouldn't be less, than 0! And you entered: " << inSize << "." << endl;
- exit(11);
- }
- size = inSize;
- }
- Array(const Array& a){ //Copy constructor;
- size = a.size;
- element = a.element;
- }
- void createArray(); //Create array of Elements;
- void enterArray(); //Entering array of Elements;
- void showArray(); //Printing all data about array;
- void addElement(); //Add one element to array;
- void removeElement(); //Remove one element from array;
- bool checkOccurenceOfElement(string); //Check if element is in array;
- ~Array(){ //destructor
- delete[] element;
- cout << "\n\n\t\tSYSINFO: Object deleted.\n\n";
- }
- };
- main.cpp:
- #include <iostream>
- #include <string>
- #include <cstdio>
- #include "Array.h"
- int main()
- {
- Array ar1;
- ar1.createArray();
- ar1.enterArray();
- ar1.showArray();
- ar1.addElement();
- ar1.showArray();
- ar1.checkOccurenceOfElement("abcde") ? cout << "Found" << endl : cout << "Not Found" << endl;
- ar1.removeElement();
- ar1.showArray();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement