Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <ctype.h>
- #include <cmath>
- #define MAX 100
- #define EMPTY -1
- using namespace std;
- struct stack
- {
- char data[MAX];
- long long int top;
- };
- int isempty(struct stack *s)
- {
- return (s->top==EMPTY)?1:0;
- }
- void emptystack(struct stack* s)
- {
- s->top=EMPTY;
- }
- void push(struct stack* s,int item)
- {
- if(s->top==(MAX-1))
- {
- printf("\nSTACK FULL");
- }
- else
- {
- ++s->top;
- s->data[s->top]=item;
- }
- }
- char pop(struct stack* s)
- {
- char ret=(char)EMPTY;
- if(!isempty(s))
- {
- ret=s->data[s->top];
- --s->top;
- }
- return ret;
- }
- void display(struct stack s)
- {
- while(s.top != EMPTY)
- {
- printf("\n%d",s.data[s.top]);
- s.top--;
- }
- }
- int isoperator(char e)
- {
- if(e=='+'||e=='-'||e=='*'||e=='^')
- return 1;
- else
- return 0;
- }
- int priority(char e)
- {
- int pri=0;
- if(e=='^')
- pri=3;
- else
- if(e=='*')
- pri=2;
- else
- if(e=='+'||e=='-')
- pri=1;
- return pri;
- }
- void infix2postfix(char* infix, char * postfix, int insertspace)
- {
- char *i,*p;
- struct stack X;
- char n1;
- emptystack(&X);
- i=&infix[0];
- p=&postfix[0];
- while(*i)
- {
- while(*i==' '||*i=='\t')
- {
- i++;
- }
- if(isdigit(*i)||isalpha(*i))
- {
- while(isdigit(*i)||isalpha(*i))
- {
- *p=*i;
- p++;
- i++;
- }
- /*SPACE CODE*/
- if(insertspace)
- {
- *p=' ';
- p++;
- }
- /*END SPACE CODE*/
- }
- if(*i=='(' )
- {
- push(&X,*i);
- i++;
- }
- if(*i==')')
- {
- n1=pop(&X);
- while(n1!='(' )
- {
- *p=n1;
- p++;
- /*SPACE CODE*/
- if(insertspace)
- {
- *p=' ';
- p++;
- }
- /*END SPACE CODE*/
- n1=pop(&X);
- }
- i++;
- }
- if(isoperator(*i))
- {
- if(isempty(&X))
- push(&X,*i);
- else
- {
- n1=pop(&X);
- while(priority(n1)>=priority(*i))
- {
- *p=n1;
- p++;
- /*SPACE CODE*/
- if(insertspace)
- {
- *p =' ';
- p++;
- }
- /*END SPACE CODE*/
- n1=pop(&X);
- }
- push(&X,n1);
- push(&X,*i);
- }
- i++;
- }
- }
- while(!isempty(&X))
- {
- n1=pop(&X);
- *p=n1;
- p++;
- /*SPACE CODE*/
- if(insertspace)
- {
- *p=' ';
- p++;
- }
- /// END SPACE CODE
- }
- *p ='\0';
- }
- long long int evaluate(char *postfix)
- {
- char *p;
- long long int S[100];
- int varf=0;
- long long int nr=0,op1,op2,result;
- p=&postfix[0]; /// In p avem expresia in forma postfixata
- ///cout<<p<<"I"<<endl;
- while(*p!='\0') /// Cat timp n-am ajuns la sfarsitul expresiei
- {
- /// removes tabs and spaces
- if(*p==' '||*p=='\t')
- {
- p++;
- }
- /// if is digit
- if(isdigit(*p))
- {
- nr=(long long int)(*p-48);
- p++;
- while(isdigit(*p))
- nr=nr*10+(*p-48),++p;
- S[++varf]=nr;
- }
- else
- {
- /// it is an operator
- if(varf)
- op1=S[varf--];
- if(varf)
- op2=S[varf--];
- ///cout<<"OPERATIA: "<<op1<<' '<<*p<<' '<<op2<<endl;
- ///if(*p!='+'||*p!='-'||*p!='*'||*p!='^')
- switch(*p)
- {
- case '+':
- result=op2+op1;
- break;
- case '-':
- result=op2-op1;
- break;
- case '^':
- result=(long long int)pow(op2,op1);
- break;
- case '*':
- result=op2*op1;
- break;
- default:
- printf("\nInvalid Operator");
- return 0;
- }
- S[++varf]=result;
- }
- p++;
- }
- result=S[varf];
- return result;
- }
- int main()
- {
- char in[50]={0},post[100]={0};
- FILE *f=fopen("evaluareexpresie.in","r");
- fscanf(f,"%s",in);
- fclose(f);
- ///cout<<"S= "<<in<<endl;
- infix2postfix(in,post,1);
- FILE *g=fopen("evaluareexpresie.out","w");
- unsigned int L=strlen(post);
- while(post[L-1]!='+'&&post[L-1]!='-'&&post[L-1]!='*'&&post[L-1]!='^')
- --L;
- post[L]='\0';
- fprintf(g,"%s\n",post);
- fprintf(f,"%lld\n",evaluate(post));
- fclose(g);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement