#include <iostream>
#include <stack.>
#include <string>
#include <sstream>
using namespace std;
bool czyOperator(string wejscie);
void wykonajOperacje(string wejscie, stack<double>& stos);
int main(){
stack<double> stos;
string wejscie;
cout<<"witaj"<<endl;
//sprawdz czy liczba
while(true){
cout<<">>";
double liczba;
cin>>wejscie;
if (istringstream(wejscie)>>liczba){
stos.push(liczba);
}
//sprawdz czy znak
else if (czyOperator(wejscie)){
wykonajOperacje(wejscie,stos);
}
else if (wejscie=="q"){
return 0;
}
else {
cout<<"Niepoprawna wartosc"<<endl;
}
}
}
bool czyOperator(string wejscie){
string operatory []={"-","+","*","/"};
for (int i=0;i<4;i++){
if (operatory[i]==wejscie){
return true;
}
}
return false;
}
void wykonajOperacje(string wejscie, stack<double>& stos){
double lewy,prawy,wynik;
prawy=stos.top();
stos.pop();
lewy=stos.top();
stos.pop();
if (wejscie=="-"){ wynik=lewy-prawy;}
if (wejscie=="+"){ wynik=lewy+prawy;}
if (wejscie=="*"){ wynik=lewy*prawy;}
if (wejscie=="/"){ wynik=lewy/prawy;}
cout<<wynik<<endl;
stos.push(wynik);
}
Comments