Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
allenjiji authored Oct 29, 2019
1 parent 4a31513 commit 27afe3c
Show file tree
Hide file tree
Showing 2 changed files with 318 additions and 139 deletions.
137 changes: 137 additions & 0 deletions po.c
Original file line number Diff line number Diff line change
@@ -0,0 1,137 @@
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#define MAX 25
char a[MAX],b[MAX],stack[MAX];
int top=-1;

void push(char c){
if(top>MAX){
printf("An Error Occured! Please update MAX for better Perfomance.\n");
exit(0);
}
else{
top ;
stack[top]=c;
}
}

char pop(){
if(top==-1) {
printf("An Error Occured!\n");
exit(0);
}
else{
char a=stack[top];
top--;
return a;
}
}



void display(){
for(int i=0;b[i]!='\0';i ){
printf("%c",b[i]);
}
}

void insert(){
scanf("%s",a);
}

int priority(char c){
if(c==' ' ||c=='-') return 1;
else if(c=='*' ||c=='/') return 2;
else if(c=='^') return 3;
else if(c=='(') return 0;
}

int isoperator(char x){
if(x=='*'||x=='^'||x=='/'||x=='-'||x==' ') return 1;
else return 0;
}



void convert(){
push('(');
int i;
for (i=0;a[i]!='\0';i );
a[i ]=')';
a[i]='\0';
int j=0;
for (i=0;a[i]!='\0';i ){
if(isalpha(a[i])){
b[j ]=a[i];
}


else if(a[i]=='(') push(a[i]);

else if(a[i]==')'){
while(stack[top]!='('){
b[j ]=pop();
}
char z=pop();
}
else if(isoperator(a[i])){
char c=a[i];
char d=stack[top];
while(priority(c)<=priority(d)){
b[j ]=pop();
}
push(c);
}
}
b[j]='\0';
}

int calc(int v1,int v2,char c){
if (c==' ') return (v1 v2);
else if (c=='-') return (v1-v2);
else if (c=='*') return (v1*v2);
else if (c=='/') return (v1/v2);
else if (c=='^') return pow(v1,v2);
}

int evaluate(){
int alphabets[26];
for (int p=0;p<26;p ){
alphabets[p]=0;
}
for (int i=0;b[i]!='\0';i ){
char c=b[i];
if(isoperator(c)){
int v1=alphabets[(pop()-'a')];
int v2=alphabets[(pop()-'a')];
int val=calc(v2,v1,c);
printf("%d %c %d = %d\n",v1,c,v2,val);
push(val);
}
else {
printf("Enter the value of %c:",c);
scanf("%d",&alphabets[(c-'a')]);
push(c);

}
}
return pop();
}


void main(){
while(1){
printf("Enter the infix expression:");
insert();
convert();
printf("The postfix expression is:");
display();
printf("\n");
//top=-1;
int x=evaluate();
printf ("The value after evaluation is:%d\n",x);
printf("\n");
}
}
Loading

0 comments on commit 27afe3c

Please sign in to comment.