Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Лабораторная №4:
- 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);
- }
- //Creating array.
- element = new Element[size];
- for(int i = 0; i < size; i++){
- element[i].setValue("0");
- }
- }
- //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() const {
- 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) const {
- for(int i = 0; i < size; i++){
- if(tmp == element[i].getValue()) return true;
- }
- return false;
- }
- 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) {
- 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++)
- element[i].setValue(element[i+1].getValue());
- Element *temp = new Element[size-1];
- for(int i = 0; i < size-1; i++)
- temp[i].setValue(element[i].getValue());
- delete[] element;
- element = temp;
- size--;
- temp = 0;
- 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;
- cin >> inStr;
- if(!this->checkOccurenceOfElement(inStr)) {
- cout << "\nElement not found." << endl;
- break;
- }
- int tInd;
- for(int i = 0; i < size; i++){
- if(element[i].getValue() == inStr)
- tInd = i;
- }
- for(int i = tInd; i < size-1; i++)
- element[i].setValue(element[i+1].getValue());
- Element *temp = new Element[size-1];
- 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: {
- cout << "\nError #I4: Your answer should be 1 or 2.\n";
- exit(14);
- break;
- }
- }
- }
- Array operator+(const Array& a1, const Array& a2){
- Array a3;
- Array temp;
- temp.size = a1.size + a2.size;
- temp.element = new Element[temp.size];
- int j = 0;
- for(int i = 0; i < a1.size; i++){
- if(!a2.checkOccurenceOfElement(a1.element[i].getValue())){
- temp.element[j].setValue(a1.element[i].getValue());
- j++;
- }
- }
- a3.size = j + a2.size;
- a3.element = new Element[a3.size];
- for(int i = 0; i < j; i++){
- a3.element[i].setValue(temp.element[i].getValue());
- }
- for(int i = 0; i < a2.size; i++){
- a3.element[j].setValue(a2.element[i].getValue());
- j++;
- }
- return a3;
- }
- Array& Array::operator =(const Array& a){
- this->size = a.size;
- this->element = new Element[this->size];
- for(int i = 0; i < this->size; i++)
- this->element[i].setValue(a.element[i].getValue());
- return *this;
- }
- Array operator *(const Array& a1, const Array& a2){
- Array a3;
- Array temp;
- temp.size = a1.size + a2.size;
- temp.element = new Element[temp.size];
- int j = 0;
- for(int i = 0; i < a1.size; i++){
- if(a2.checkOccurenceOfElement(a1.element[i].getValue())){
- temp.element[j].setValue(a1.element[i].getValue());
- j++;
- }
- }
- a3.size = j;
- a3.element = new Element[a3.size];
- for(int i = 0; i < j; i++){
- a3.element[i].setValue(temp.element[i].getValue());
- }
- return a3;
- }
- Array operator -(const Array& a1, const Array& a2){
- Array a3;
- Array temp;
- temp.size = a1.size + a2.size;
- temp.element = new Element[temp.size];
- int j = 0;
- for(int i = 0; i < a1.size; i++){
- if(!a2.checkOccurenceOfElement(a1.element[i].getValue())){
- temp.element[j].setValue(a1.element[i].getValue());
- j++;
- }
- }
- for(int i = 0; i < a2.size; i++){
- if(!a1.checkOccurenceOfElement(a2.element[i].getValue())){
- temp.element[j].setValue(a2.element[i].getValue());
- j++;
- }
- }
- a3.size = j;
- a3.element = new Element[a3.size];
- for(int i = 0; i < j; i++){
- a3.element[i].setValue(temp.element[i].getValue());
- }
- return a3;
- }
- ostream& operator << (ostream& s, const Array& a){
- s << "[Size: " << a.size << "]" << endl;
- s << "Array:\n{\n" << endl;
- for(int i = 0; i < a.size; i++){
- s << "\tElement [" << i+1 << "] = " << a.element[i].getValue() << ";\n";
- }
- s << "}" << endl;
- return s;
- }
- istream& operator >> (istream& s, Array& a){
- cout << "\n\tStarting creating of array. Enter size (should be >= 0):" << endl << "-> ";
- s >> a.size;
- if(a.size < 0){ //Checking size.
- cout << "\n\n\t\aError #I1: Size of array shouldn't be less, than 0! And you entered: " << a.size << "." << endl;
- exit(11);
- }
- //Creating array.
- a.element = new Element[a.size];
- for(int i = 0; i < a.size; i++){
- a.element[i].setValue("0");
- }
- cout << "Now enter values for each element of array: " << endl;
- string tmp = "";
- for(int i = 0; i < a.size; i++){ //Cycle for entering array of elements.
- retry1: //***LABEL RETRY1***This label is using if the user entered an empty string.
- a.element[i].setValue(""); //Just to make right retry.
- cout << "Element [" << i+1 << "] = "; //Beautiful printing.
- s >> tmp;
- a.element[i].setValue(tmp);//Entering values.
- if(a.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): ";
- s >> 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".
- }
- }
- }
- return s;
- }
- 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() const {
- return value;
- }
- void setValue(string val){
- 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 = 1; }
- 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() const; //Printing all data about array;
- void addElement(); //Add one element to array;
- void removeElement(); //Remove one element from array;
- bool checkOccurenceOfElement(string) const; //Check if element is in array;
- Array& operator=(const Array&);
- friend Array operator +(const Array&, const Array&);
- friend Array operator *(const Array&, const Array&);
- friend Array operator -(const Array&, const Array&);
- friend istream& operator>>(istream& s, Array& a);
- friend ostream& operator<<(ostream& s, const Array& a);
- ~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 a1;
- cin >> a1;
- cout << a1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement