@Sopelek nie przywykłem do używania polskich nazw zmiennych i już samo to wywoływało dziwne uczucie podczas pisania tego kodu ^^
nobody__ sorki żę cie tak mecze ale już chyba wiem o co chodziło mojemu nauczycielowi zeby można było zapisywać do stosu rożne zmienne nie tylko inty, np jak chce zapisać litere to mnie wywala a powinno zapisać dał byś rade jeszcze przerobić ??
To właśnie zrobiłem... tyle, że kod się nie kompiluje, bo nie dokopiowałem części kodu i brakuje #include<string>, napisałem, że nie testowane, tyle co mi intelisense podkreślało na bieżąco
#include <iostream>
#include <string>
using namespace std;
#define SIZE 10
enum Typ { // dozwolone typy
tNienzany = 0, // nieznany
// integery
tShort, tInt, tLong,
// zmiennoprzecinkowe
tFloat, tDouble,
// litery
tChar,
};
struct Element {
Typ typ; // typ zmiennej
union {
short _short;
int _int;
long _long;
float _float;
double _double;
char _char;
};
};
Element stos[SIZE];
int w=0;
bool push3t(Element liczba);
bool pop3t(Element &liczba);
int main(){
int i=0;
Element el;
string typ; // string na nazwę typu
bool nie_wlasciwy_typ; // czy podany typ jest właściwy
cout<<"ile liczb odlozyc na stos: ";
cin>>i;
for (int f=0; f<i; f++){
do {
// najpierw wybierasz typ do umieszczenia
cout << "Podaj kolejny typ i jego wartosc: ";
cin >> typ;
nie_wlasciwy_typ = false; // zakładamy ze jest właściwy
if(typ == "short")
{
el.typ = tShort;
cin >> el._short;
}
else if(typ == "int")
{
el.typ = tInt;
cin >> el._int;
}
else if(typ == "long")
{
el.typ = tLong;
cin >> el._long;
}
else if(typ == "float")
{
el.typ = tFloat;
cin >> el._float;
}
else if(typ == "double")
{
el.typ = tDouble;
cin >> el._double;
}
else if(typ == "char")
{
el.typ = tChar;
cin >> el._char;
}
else
{
cout << "Nieznany typ, podaj jeszcze raz" << endl;
nie_wlasciwy_typ = true; // powtarzamy petlę
}
}
while(nie_wlasciwy_typ);
if (push3t(el)==false){cout<<"przekroczono pojemnosc stosu!"<<endl; break;}
cout<<"wierzcholek stosu:"<<w<<endl;
}
cout<<endl<<endl<<"Aktualna zawartosc stosu: "<<endl;
for (int f=0; f<i; f++){
cout<<"stos["<<f<<"] = ";
if(stos[f].typ == tShort) cout << stos[f]._short;
else if(stos[f].typ == tInt) cout << stos[f]._int;
else if(stos[f].typ == tLong) cout << stos[f]._long;
else if(stos[f].typ == tFloat) cout << stos[f]._float;
else if(stos[f].typ == tDouble) cout << stos[f]._double;
else if(stos[f].typ == tChar) cout << stos[f]._char;
cout << endl;
}
cout<<"ile liczb chcesz zdjac ze stosu?: ";
cin>>i;
for (int f=0; f<i; f++){
pop3t(el);
cout<<"Ostatnio zdjeto ze stosu: ";
if(el.typ == tShort) cout << el._short;
else if(el.typ == tInt) cout << el._int;
else if(el.typ == tLong) cout << el._long;
else if(el.typ == tFloat) cout << el._float;
else if(el.typ == tDouble) cout << el._double;
else if(el.typ == tChar) cout << el._char;
cout << endl;
cout<<"wierzcholek stosu: "<<w<<endl;
cout<<endl<<endl<<"aktualna zawartosc stosu: "<<endl;
for(int f=0; f<w; f++){
cout<<"stos["<<f<<"] = ";
if(stos[f].typ == tShort) cout << stos[f]._short;
else if(stos[f].typ == tInt) cout << stos[f]._int;
else if(stos[f].typ == tLong) cout << stos[f]._long;
else if(stos[f].typ == tFloat) cout << stos[f]._float;
else if(stos[f].typ == tDouble) cout << stos[f]._double;
else if(stos[f].typ == tChar) cout << stos[f]._char;
cout << endl;
}
}
return 0;
}
bool push3t(Element element){
if (w==(SIZE)) return false;
else
{
stos[w] = element;
w++;
return true;
}
}
bool pop3t(Element &element){
if(w==0) return false;
else{
element = stos[w-1];
w--;
return true;
}
}
Na początku wpisujesz typ zmiennej, dopiero potem wartość
Jeśli będzie przeszkadzać Ci, że program się sam zamyka dopisujesz
system("PAUSE");
przed
return 0;
przy końcu programu