- #include "stdafx.h"
- #include "stdafx.h"
- #include <iostream>
- #include <conio.h>
- #include <string.h>
- #define SIZE 500
- using namespace std;
- int str_in() {
- char text[1024];
- int length, i, k;
- int s;
- gets(text);
- length = strlen(text);
- while (1) {
- k = 0;
- for (i = 0; i <= length; i++)
- if ((text[i] >= '\x30') & (text[i] <= '\x39')) k++;
- else break;
- if (k != length) puts("Incorrect Enter, try one more time");
- else break;
- gets(text);
- length = strlen(text);
- }
- s = atoi(text);
- if (strlen(text) == 0) {
- puts("Incorrect Enter, try one more time");
- gets(text);
- }
- return s;
- }
- class Stroka {
- public:
- char *pChar;
- int length;
- Stroka (void);
- Stroka (char* source);
- Stroka (char schar);
- Stroka (Stroka &source);
- void down_reg ();
- void add (char* source);
- void show ();
- ~Stroka ();
- };
- Stroka::Stroka (void) {
- length = 0;
- pChar = new char[SIZE];
- }
- Stroka::Stroka(char schar) {
- pChar = new char[SIZE];
- char *p = &schar;
- strcpy (pChar, p);
- length = 1;
- }
- Stroka::Stroka(char* source) {
- pChar = new char[SIZE];
- length = strlen(source);
- strcpy (pChar, source);
- }
- Stroka::Stroka (Stroka &source) {
- pChar = new char[SIZE];
- strcpy (pChar, source.pChar);
- length = source.length;
- }
- void Stroka::down_reg () {
- int i, asci;
- for (i = 0; i <= length; i++) {
- asci = (int) pChar[i];
- if (((asci > 64)&&(asci < 91))||((asci > -129)&&(asci < -112))) {
- asci = asci + 32;
- pChar[i] = (char) asci;
- }
- if ((asci > -113)&&(asci < -96)) {
- asci = asci + 80;
- pChar[i] = (char) asci;
- }
- if (asci == -16) {
- asci = asci + 1;
- pChar[i] = (char) asci;
- }
- }
- }
- void Stroka::add (char* source) {
- strcat (pChar, source);
- length = length + strlen (source);
- }
- void Stroka::show () {
- if (length == 1)
- printf("%c",pChar[0]);
- else cout << pChar;
- }
- Stroka::~Stroka() {
- delete [] pChar;
- }
- class Hex_Stroka : public Stroka {
- public:
- Hex_Stroka (void);
- Hex_Stroka (char* source);
- Hex_Stroka (Hex_Stroka &source);
- int hex_to_dec (void);
- ~Hex_Stroka ();
- bool operator == (Hex_Stroka &source) {
- if (length != source.length)
- return false;
- else {
- for (int i=0; i<length; i++)
- if (pChar[i] != source.pChar[i]) return false;
- }
- return true;
- }
- Hex_Stroka& operator = (Hex_Stroka &source) {
- length = source.length;
- if (source.pChar == 0) {
- pChar = 0;
- return *this;
- }
- pChar = new char[SIZE];
- strcpy (pChar, source.pChar);
- return *this;
- }
- Hex_Stroka operator & (Hex_Stroka &source) {
- char tChar[SIZE];
- memset(tChar, 0, sizeof(tChar));
- int k;
- if (length!=source.length) {
- if (length < source.length) {
- k = source.length - length;
- for (int i=0; i < k; i++)
- tChar[i] = '0';
- for (int i=k; i < source.length; i++)
- tChar[i] = pChar[i-k] & source.pChar[i];
- }
- else {
- k = length - source.length;
- for (int i=0; i < k; i++)
- tChar[i] = '0';
- for (int i=k; i < length; i++)
- tChar[i] = pChar[i] & source.pChar[i - k];
- }
- }
- Hex_Stroka res(tChar);
- return res;
- }
- };
- Hex_Stroka::Hex_Stroka (void) {
- length = 0;
- pChar = new char[SIZE];
- }
- Hex_Stroka::Hex_Stroka(char* source) {
- pChar = new char[SIZE];
- length = strlen(source);
- int i, asci;
- strcpy (pChar, source);
- for (i = 0; i < length; i++) {
- asci = (int) source[i];
- if (((asci >= 48)&&(asci <= 57))||((asci >= 65)&&(asci <= 70)))
- pChar[i] = source[i];
- else pChar[i] = '0';
- }
- }
- Hex_Stroka::Hex_Stroka (Hex_Stroka &source) {
- pChar = new char[SIZE];
- strcpy (pChar, source.pChar);
- length = source.length;
- }
- int Hex_Stroka::hex_to_dec (void)
- {
- int i, asci, res, k;
- k = 1;
- res = 0;
- for (i = length - 1; i >= 0; i--) {
- asci = (int) pChar[i];
- if ((asci>=48) && (asci<=57))
- res = res + (asci-48)*k;
- else if ((asci>=65) && (asci<=70))
- res = res + (asci-55)*k;
- k = k * 16;
- }
- return res;
- }
- Hex_Stroka::~Hex_Stroka() {
- // delete [] pChar;
- }
- void main() {
- char st1[SIZE];
- int choice=-1;
- Hex_Stroka *pointer[4];
- string** mas;
- mas = new string*[4];
- while (choice!=0) {
- puts("Welcome to main menu");
- puts("1 - Filling objects");
- puts("2 - Working with subclass");
- puts("3 - Testing overloaded operations");
- puts("4 - Show all objects");
- puts("0 - Exit");
- choice = str_in();
- if (choice==1) {
- while (choice!=0) {
- puts("Welcome to menu - Filling objects");
- puts("1 - All clean");
- puts("2 - All with strings");
- puts("3 - Copying objects");
- puts("4 - One clean");
- puts("5 - One with string");
- puts("0 - Exit in main menu");
- choice = str_in();
- switch (choice) {
- case 1 :
- {
- for (int i=0; i<4; i++) {
- mas[i] = new Hex_Stroka;
- pointer[i] = reinterpret_cast<Hex_Stroka*>(mas[i]);
- }
- system("cls");
- puts("4 pure object created");
- puts("-----------------------");
- }
- break;
- case 2 :
- {
- for (int i=0; i<4; i++) {
- cout << "Enter string " << i+1 << " to create hex_string object:\n";
- cin.getline (st1, SIZE);
- mas[i] = new Hex_Stroka(st1);
- pointer[i] = reinterpret_cast<Hex_Stroka*>(mas[i]);
- }
- system("cls");
- puts("4 filled object created");
- puts("-----------------------");
- }
- break;
- case 3 :
- {
- cout << "Enter number of object that will be source: ";
- int source, dest;
- source = str_in();
- cout << "Enter number of object that will be destination: ";
- dest = str_in();
- mas[dest] = new Hex_Stroka(*pointer[source]);
- pointer[dest] = reinterpret_cast<Hex_Stroka*>(mas[dest]);
- system("cls");
- puts("1 object copied");
- puts("-----------------------");
- }
- break;
- case 4 :
- {
- cout << "Enter number of object that will be created clean: ";
- int n;
- n = str_in();
- mas[n] = new Hex_Stroka;
- pointer[n] = reinterpret_cast<Hex_Stroka*>(mas[n]);
- system("cls");
- puts("1 pure object created");
- puts("-----------------------");
- }
- break;
- case 5 :
- {
- cout << "Enter number of object that will be created with string: ";
- int n;
- n = str_in();
- cout << "Enter string to create hex_string object:\n";
- cin.getline (st1, SIZE);
- mas[n] = new Hex_Stroka(st1);
- pointer[n] = reinterpret_cast<Hex_Stroka*>(mas[n]);
- system("cls");
- puts("1 filled object created");
- puts("-----------------------");
- }
- break;
- default:
- break;
- }
- }
- choice=-1;
- }
- if (choice==2) {
- while (choice!=0) {
- puts("Welcome to menu - working objects of subclass using pointers");
- puts("1 - Print one in decimal");
- puts("2 - Print all in decimal");
- puts("0 - Exit in main menu");
- choice = str_in();
- switch (choice) {
- case 1 :
- {
- cout << "Enter number of object that will be converted in dec: ";
- int n,d;
- n = str_in();
- d = pointer[n]->hex_to_dec();
- system("cls");
- puts("Source string:");
- pointer[n]->show();
- puts("");
- puts("In dec:");
- cout << d << "\n";
- puts("-----------------------");
- }
- break;
- case 2 :
- {
- system("cls");
- cout << "All objects converted in dec: \n";
- for (int i=0; i<4; i++) {
- int d;
- d = pointer[i]->hex_to_dec();
- pointer[i]->show();
- cout << "\t:\t" << d << "\n";
- }
- puts("-----------------------");
- }
- break;
- default:
- break;
- }
- }
- choice=-1;
- }
- if (choice==3) {
- while (choice!=0) {
- puts("Welcome to menu - Testing overloaded operations");
- puts("1 - 2 objects equivalence");
- puts("2 - Giving");
- puts("3 - Logic &");
- puts("0 - Exit in main menu");
- choice = str_in();
- switch (choice) {
- case 1 :
- {
- int n1, n2;
- system("cls");
- cout << "Enter number of object that will be 1 operand: ";
- n1 = str_in();
- cout << "Enter number of object that will be 2 operand: ";
- n2 = str_in();
- puts("Source strings:");
- pointer[n1]->show();
- cout << "\t";
- pointer[n2]->show();
- cout << "\n";
- if (*pointer[n1]==*pointer[n2])
- puts ("Objects are equivalents");
- else
- puts ("Objects are NOT equivalents");
- puts("-----------------------");
- }
- break;
- case 2 :
- {
- system("cls");
- int n1, n2 ,d;
- cout << "Enter number of object that will be 1 operand: ";
- n1 = str_in();
- cout << "Enter number of object that will be 2 operand: ";
- n2 = str_in();
- puts("Source strings:");
- pointer[n1]->show();
- cout << "\t";
- pointer[n2]->show();
- cout << "\n";
- *pointer[n1]=*pointer[n2];
- puts("Operator =");
- puts("After:");
- pointer[n1]->show();
- cout << "\t";
- pointer[n2]->show();
- cout << "\n";
- puts("-----------------------");
- }
- break;
- case 3 :
- {
- int n1, n2;
- system("cls");
- cout << "Enter number of object that will be 1 operand: ";
- n1 = str_in();
- cout << "Enter number of object that will be 2 operand: ";
- n2 = str_in();
- puts("Source strings:");
- pointer[n1]->show();
- cout << "\t";
- pointer[n2]->show();
- cout << "\n";
- puts("Operator &");
- puts("Result:");
- Hex_Stroka *tmp;
- tmp = new Hex_Stroka("tmp");
- *tmp = *pointer[n1] & *pointer[n2];
- tmp->show();
- puts("\n-----------------------");
- }
- break;
- default:
- break;
- }
- }
- choice=-1;
- }
- if (choice==4) {
- system("cls");
- for (int i=0; i<4; i++) {
- pointer[i]->show();
- puts("");
- }
- getch();
- }
- system("cls");
- }
- }