Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <sstream>
- #include <algorithm>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- stack<char>digit_stack;
- stack<char>operator_stack;
- stack<int>addition_stack;
- int bitnost(char a)
- {
- int temp;
- if (a == '*' || a == '/')
- temp = 2;
- else if (a == '+' || a == '-')
- temp = 3;
- return temp;
- }
- int konvertaj_u_int(string broj)
- {
- int niz[10000];
- for(long long int i=0; i<broj.length(); i++)
- niz[i]=broj[i]-'0';
- int jedinice=0, desetice=0, stotice=0, hiljadice=0, desethiljadice=0, stohiljadice=0;
- int rezultat=0;
- if(broj.length()==1)
- jedinice=niz[0];
- if(broj.length()==2)
- {
- desetice=niz[0];
- jedinice=niz[1];
- }
- if(broj.length()==3)
- {
- stotice=niz[0];
- desetice=niz[1];
- jedinice=niz[2];
- }
- if(broj.length()==4)
- {
- hiljadice=niz[0];
- stotice=niz[1];
- desetice=niz[2];
- jedinice=niz[3];
- }
- if(broj.length()==5)
- {
- desethiljadice=niz[0];
- hiljadice=niz[1];
- stotice=niz[2];
- desetice=niz[3];
- jedinice=niz[4];
- }
- if(broj.length()==6)
- {
- stohiljadice=niz[0];
- desethiljadice=niz[1];
- hiljadice=niz[2];
- stotice=niz[3];
- desetice=niz[4];
- jedinice=niz[5];
- }
- jedinice*=1;
- desetice*=10;
- stotice*=100;
- hiljadice*=1000;
- desethiljadice*=10000;
- stohiljadice*=100000;
- rezultat=jedinice+desetice+stotice+hiljadice+desethiljadice+stohiljadice;
- return rezultat;
- }
- int izracunaj(const char *izraz)
- {
- string mandza;
- string infix_string;
- string postfix_string;
- for(long long int i=0; i<strlen(izraz); i++)
- infix_string+=izraz[i];
- for(long long int i=0; i<infix_string.length(); i++)
- {
- if(infix_string[i]=='+' || infix_string[i]=='-' || infix_string[i]=='*' || infix_string[i]=='/')
- {
- postfix_string+="|";
- while(!operator_stack.empty() && bitnost(operator_stack.top())<=bitnost(infix_string[i]))
- {
- postfix_string+=operator_stack.top();
- operator_stack.pop();
- }
- operator_stack.push(infix_string[i]);
- }
- else if (infix_string[i] == '(') operator_stack.push(infix_string[i]);
- else if (infix_string[i] == ')')
- {
- while (operator_stack.top() != '(')
- {
- postfix_string+= operator_stack.top();
- operator_stack.pop();
- }
- operator_stack.pop();
- }
- else postfix_string+=infix_string[i];
- if(isdigit(infix_string[i])) digit_stack.push(infix_string[i]);
- }
- while(!operator_stack.empty())
- {
- postfix_string+=operator_stack.top();
- operator_stack.pop();
- }
- string broj="";
- long long int brojic=0;
- for(long long int i=0; i<postfix_string.length(); i++)
- {
- if(isdigit(postfix_string[i])) broj+=postfix_string[i];
- if(!isdigit(postfix_string[i]) && konvertaj_u_int(broj)!=0)
- {
- brojic=konvertaj_u_int(broj);
- addition_stack.push(brojic);
- broj="";
- }
- if(postfix_string[i]=='*')
- {
- long long int prvi, drugi;
- prvi=addition_stack.top();
- addition_stack.pop();
- drugi=addition_stack.top();
- addition_stack.pop();
- addition_stack.push(prvi*drugi);
- }
- if(postfix_string[i]=='/')
- {
- long long int prvi, drugi;
- prvi=addition_stack.top();
- addition_stack.pop();
- drugi=addition_stack.top();
- addition_stack.pop();
- addition_stack.push(drugi/prvi);
- }
- if(postfix_string[i]=='+')
- {
- long long int prvi, drugi;
- prvi=addition_stack.top();
- addition_stack.pop();
- drugi=addition_stack.top();
- addition_stack.pop();
- addition_stack.push(prvi+drugi);
- }
- if(postfix_string[i]=='-')
- {
- long long int prvi, drugi;
- prvi=addition_stack.top();
- addition_stack.pop();
- drugi=addition_stack.top();
- addition_stack.pop();
- addition_stack.push(drugi-prvi);
- }
- }
- return addition_stack.top();
- }
Advertisement
Add Comment
Please, Sign In to add comment