Created
June 17, 2011 16:26
-
-
Save resure/1031745 to your computer and use it in GitHub Desktop.
АЯП, тридцатая лаба первого усложненного
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <locale.h> | |
#include <ctype.h> | |
#define V true | |
#define CAPACITY 64 | |
using namespace std; | |
class TElement | |
{ | |
public: | |
TElement() {} | |
virtual ~TElement() {} | |
virtual void print() = 0; | |
}; | |
template <typename T> | |
class TValue:public TElement | |
{ | |
public: | |
T value; | |
TValue(T n):TElement(),value(n) {} | |
virtual ~TValue() {} | |
virtual void print() { cout << value << " "; } | |
}; | |
typedef struct stack { stack* last; TElement* Obj; }; | |
class xstack | |
{ | |
protected: | |
int size; | |
public: | |
stack* last; | |
xstack() | |
{ | |
last = new stack; | |
last -> last = NULL; | |
} | |
//destructor | |
~xstack() | |
{ | |
stack* tmp; | |
while (last -> last != NULL) | |
{ | |
tmp = last -> last; | |
delete last; | |
last = tmp; | |
} | |
} | |
void push() | |
{ | |
char str[12]; | |
int size, k; | |
TElement* p; | |
stack* tmp; | |
scanf("%s",str); | |
k = atoi(str); | |
if (k||(strlen(str)==1 && str[0]=='0')) | |
p = new TValue<int>(k); | |
else p = new TValue<char>(str[0]); | |
tmp = new stack; | |
tmp -> Obj = p; | |
tmp -> last = last; | |
last = tmp; | |
size++; | |
} | |
TElement* pop(bool print = true) | |
{ | |
if (print) (last -> Obj) -> print(); | |
return last -> Obj; | |
} | |
void rm() | |
{ | |
stack* tmp = last; | |
last = last -> last; | |
delete tmp; | |
} | |
void print() | |
{ | |
stack* tlast = last; | |
while (tlast -> last != NULL) | |
{ | |
TElement* p = tlast -> Obj; | |
if (TValue<int> *q = dynamic_cast<TValue<int> *>(p)) q -> print(); | |
else if (TValue<char> *q = dynamic_cast<TValue<char> *>(p)) q -> print(); | |
tlast = tlast -> last; | |
} | |
cout << endl; | |
} | |
}; | |
class exstack: public xstack | |
{ | |
public: | |
void sort() | |
{ | |
TElement *tmp; | |
stack *f, *s; | |
int z1, z2; | |
f = last; | |
while ((f -> last) -> last != NULL) | |
{ | |
s = f -> last; | |
while (s -> last != NULL) | |
{ | |
TElement* p1 = f -> Obj; | |
if (TValue<int> *q1 = dynamic_cast<TValue<int> *>(p1)) z1 = q1 -> value; | |
else if (TValue<char> *q1 = dynamic_cast<TValue<char> *>(p1)) z1 = q1 -> value; | |
TElement* p2 = s -> Obj; | |
if (TValue<int> *q2 = dynamic_cast<TValue<int> *>(p2)) z2 = q2 -> value; | |
else if (TValue<char> *q2 = dynamic_cast<TValue<char> *>(p2)) z2 = q2 -> value; | |
if (z1 > z2) | |
{ | |
tmp = f -> Obj; | |
f -> Obj = s -> Obj; | |
s -> Obj = tmp; | |
} | |
s = s -> last; | |
} | |
f = f -> last; | |
} | |
} | |
}; | |
int main() | |
{ | |
// Создаем класс | |
exstack* SX = new exstack(); | |
int code = -1; | |
while (code != 0) | |
{ | |
cout << "Enter 1 to add new element,\n\ | |
2 to remove last element,\n\ | |
3 to print all elements,\n\ | |
4 to sort and 0 to exit.\n"; | |
cin >> code; | |
switch(code) | |
{ | |
case 1: | |
cout << "Enter new element: "; | |
SX -> push(); | |
cout << endl; | |
break; | |
case 2: | |
SX -> rm(); | |
cout << "Last element deleted." << endl; | |
break; | |
case 3: | |
cout << "All elements: " << endl; | |
SX -> print(); | |
break; | |
case 4: | |
SX -> sort(); | |
cout << "Stack was sorted.\n"; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment