Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Polish.h
- class polish{
- double eval;
- char* infix;
- char* postfix;
- int priority(char ch);
- int isOperator(char ch);
- int operate(int a, int b, char op);
- int caracter(char c);
- public:
- polish(char *ch);
- void ReversePolishNotation();
- void ReversePolishEvaluate();
- };
- Polish.cpp
- #include<stdio.h>
- #include<string.h>
- #include<iostream.h>
- polish::polish(char *ch)
- {
- strcat(infix,ch);
- }
- int polish::priority(char ch)
- { if (ch=='*') return 3;
- if (ch=='/') return 4;
- if (ch=='+') return 1;
- if (ch=='-‘) return 2;
- return 0;
- }
- int polish::isOperator(char c)
- { if(priority(c)>0) return 1;
- else return 0;
- }
- int polish::caracter(char c)
- { int ret;
- if (c== '0') ret=0;
- if (c== '1') ret=1;
- if (c== '2') ret=2;
- if (c== '3') ret=3;
- if (c== '4') ret=4;
- if (c== '5') ret=5;
- if (c== '6') ret=6;
- if (c== '7') ret=7;
- if (c== '8') ret=8;
- if (c== '9') ret=9;
- return ret;
- }
- void polish::ReversePolishNotation()
- { int i;
- char *ret;
- ret="";
- char c;
- char st[10];
- int top=-1;
- i=0;
- while(i<strlen(infix))
- { if(!isOperator(*(infix+i)))
- ret=ret+(*(infix+i));
- else{
- while((top>=0)&&(priority(st[top])>=priority(*(infix+i))))
- { c=st[top];
- top--;
- ret+=c;
- }
- top++;
- st[top]=*(infix+i);
- }
- i++;
- }
- while(top>=0)
- { c=st[top];
- top--;
- ret+=c;
- }
- postfix=ret;
- cout<<ret;
- }
- int polish::operate(int a, int b, char op)
- { int rez;
- if (op=='+') rez=a+b;
- if (op=='-') rez=a-b;
- if (op=='*') rez=a*b;
- if (op=='/') rez=a/b;
- return rez;
- }
- void polish::ReversePolishEvaluate()
- { int st[10];
- int a,b;
- int i,top=-1;
- i=0;
- while(i<strlen(postfix))
- { if(isOperator(*(postfix+i)))
- { b=st[top];
- top--;
- a=st[top];
- st[top]=operate(a,b,*(postfix+i));
- }
- else
- {
- top++;
- st[top]=caracter(*(postfix+i));
- }
- i++;
- }
- eval=st[top];
- cin>>eval;
- }
- exPolish.cpp
- void main(){
- char *sir;
- cout<<"Introduceti expresia de evaluat"<<endl;
- cin>>sir;
- polish p(sir);
- p.ReversePolishNotation();
- p.ReversePolishEvaluate();
- }
Add Comment
Please, Sign In to add comment