idzik

Kółko olimpijskie/ONP.cpp

Oct 5th, 2016
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack.>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7. bool czyOperator(string wejscie);
  8. void wykonajOperacje(string wejscie, stack<double>& stos);
  9.  
  10. int main(){
  11.  
  12. stack<double> stos;
  13. string wejscie;
  14. cout<<"witaj"<<endl;
  15.  
  16. //sprawdz czy liczba
  17.  
  18. while(true){
  19. cout<<">>";
  20. double liczba;
  21. cin>>wejscie;
  22. if (istringstream(wejscie)>>liczba){
  23.         stos.push(liczba);
  24. }
  25. //sprawdz czy znak
  26. else if (czyOperator(wejscie)){
  27.     wykonajOperacje(wejscie,stos);
  28. }
  29. else if (wejscie=="q"){
  30.     return 0;
  31. }
  32. else {
  33.     cout<<"Niepoprawna wartosc"<<endl;
  34. }
  35. }
  36. }
  37. bool czyOperator(string wejscie){
  38.     string operatory []={"-","+","*","/"};
  39.     for (int i=0;i<4;i++){
  40.         if (operatory[i]==wejscie){
  41.             return true;
  42.         }
  43.     }
  44.     return false;
  45. }
  46.  
  47. void wykonajOperacje(string wejscie, stack<double>& stos){
  48.     double lewy,prawy,wynik;
  49.     prawy=stos.top();
  50.     stos.pop();
  51.     lewy=stos.top();
  52.     stos.pop();
  53.     if (wejscie=="-"){ wynik=lewy-prawy;}
  54.     if (wejscie=="+"){ wynik=lewy+prawy;}
  55.     if (wejscie=="*"){ wynik=lewy*prawy;}
  56.     if (wejscie=="/"){ wynik=lewy/prawy;}
  57.     cout<<wynik<<endl;
  58.     stos.push(wynik);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment