#include<stdio.h>
#include<string.h>
#include<ctype.h>
typedef struct{
char arg1,arg2,op,result;
char LHS,RHS[20];
}three_add;
three_add q[10];
int x=0,asc=65;
char OP[20],OD[20],t1,t2;//OP and OD represent operator and data stack
char substring[20];
char* substr(char* string,int start,int end){
int k,l;
memset(substring,0,sizeof(substring));
substring[0]='\0';
for(l=start,k=0;l<=end;l++,k++){
substring[k]=string[l];
substring[k+1]='\0';
}
return substring;
}
int priority(char ele){
int pr;
if(ele=='^')pr=3;
else if(ele=='*'||ele=='/'||ele=='%')pr=2;
else if(ele=='+'||ele=='-')pr=1;
else pr=0;
return pr;
}
void pop(char* op,char* od){
char op1,od1,od2;
op1=op[strlen(op)-1];//pop the operator at stack top from operator stack
od2=od[strlen(od)-1];//topmost is stored in od2 not od1
od1=od[strlen(od)-2];
//Form the three address
q[x].result=(char)asc;
q[x].arg1=od1;
q[x].op=op1;
q[x].arg2=od2;
//push the result into the data stack
od[strlen(od)-2]=(char)asc;
od[strlen(od)-1]='\0';
op[strlen(op)-1]='\0';
asc++;
x++;
}
int main(){
char string[20],str[20];
int i=0,j,len1,len2;
memset(str,0,sizeof(str));
memset(string,0,sizeof(string));
string[0]='\0';
printf("Enter the infix expression:");
scanf("%c=%s",&q[0].LHS,q[0].RHS);
strcpy(string,q[0].RHS);
len1=strlen(string);
for(i=0;i<strlen(string);i++){
str[0]='\0';
if(string[0]=='-'){
q[x].arg1=string[1];/*if infix=-b+a+7+(b+c)-9-s*w then after this step, it is A+a+7+(b+c)-9-s*w where A=-b*/
q[x].op='\0';
q[x].arg2=string[0];//string[0] is arg2 and not arg1 because we are printing arg2 first
q[x].result=(char)asc;
str[0]=q[x].result;//write A to str
str[1]='\0';
strcat(str,substr(string,2,len1));//copy from + to -9
asc++;
x++;
strcpy(string,str);
}
else if(string[i]=='('){
memset(str,0,sizeof(str));
q[x].arg1=string[i+1];//to replace (b+c) with B
q[x].op=string[i+2];
q[x].arg2=string[i+3];
q[x].result=(char)asc;
strcpy(str,substr(string,0,i-1));//copy from A+ to 7+ in str
len2=strlen(str);
str[len2]=q[x].result;//write B to str
str[len2+1]='\0';
strcat(str,substr(string,i+5,len1));
asc++;
x++;
strcpy(string,str);//hence the string in string is A+a+7+B-9-s*w
}
}
for(i=0;i<strlen(string);i++){
t1=string[i];//t1 is the current string
if(priority(t1)==0){//it is data ie number and not operator
OD[strlen(OD)]=t1;
}
else{
for(j=(strlen(OP)-1);j>=0;j--){//traverse the stack
t2=OP[j];//t2 is the stack top
if(priority(t1)>priority(t2))
break;
else
pop(OP,OD);//pop until the priority of the symbol at stack top is less than the priority of the given symbol
}
OP[strlen(OP)]=t1;//push the current operator into the stack
}
}
//the three addresses are formed in sequence
for(i=strlen(OP)-1;i>=0;i--)//pop the remaining operators from the stack
pop(OP,OD);
if(q[0].LHS!=' '){//The last line of three address For eg:S=c+a*b, A=a*b,B=c+A,S=B. It is for S=B line
q[x].arg1=q[x-1].result;
q[x].result=q[0].LHS;
}
//print the three address
for(i=0;i<=x;i++){
if(q[i].result!='\0'){
if(i==x)//last one
printf("\n\n%c:=%c",q[i].result,q[i].arg1);//done above
else
printf("\n\n%c:=%c %c %c",q[i].result,q[i].arg2,q[i].op,q[i].arg1);//arg2 printed first
}
}
return 0;
}
#include<string.h>
#include<ctype.h>
typedef struct{
char arg1,arg2,op,result;
char LHS,RHS[20];
}three_add;
three_add q[10];
int x=0,asc=65;
char OP[20],OD[20],t1,t2;//OP and OD represent operator and data stack
char substring[20];
char* substr(char* string,int start,int end){
int k,l;
memset(substring,0,sizeof(substring));
substring[0]='\0';
for(l=start,k=0;l<=end;l++,k++){
substring[k]=string[l];
substring[k+1]='\0';
}
return substring;
}
int priority(char ele){
int pr;
if(ele=='^')pr=3;
else if(ele=='*'||ele=='/'||ele=='%')pr=2;
else if(ele=='+'||ele=='-')pr=1;
else pr=0;
return pr;
}
void pop(char* op,char* od){
char op1,od1,od2;
op1=op[strlen(op)-1];//pop the operator at stack top from operator stack
od2=od[strlen(od)-1];//topmost is stored in od2 not od1
od1=od[strlen(od)-2];
//Form the three address
q[x].result=(char)asc;
q[x].arg1=od1;
q[x].op=op1;
q[x].arg2=od2;
//push the result into the data stack
od[strlen(od)-2]=(char)asc;
od[strlen(od)-1]='\0';
op[strlen(op)-1]='\0';
asc++;
x++;
}
int main(){
char string[20],str[20];
int i=0,j,len1,len2;
memset(str,0,sizeof(str));
memset(string,0,sizeof(string));
string[0]='\0';
printf("Enter the infix expression:");
scanf("%c=%s",&q[0].LHS,q[0].RHS);
strcpy(string,q[0].RHS);
len1=strlen(string);
for(i=0;i<strlen(string);i++){
str[0]='\0';
if(string[0]=='-'){
q[x].arg1=string[1];/*if infix=-b+a+7+(b+c)-9-s*w then after this step, it is A+a+7+(b+c)-9-s*w where A=-b*/
q[x].op='\0';
q[x].arg2=string[0];//string[0] is arg2 and not arg1 because we are printing arg2 first
q[x].result=(char)asc;
str[0]=q[x].result;//write A to str
str[1]='\0';
strcat(str,substr(string,2,len1));//copy from + to -9
asc++;
x++;
strcpy(string,str);
}
else if(string[i]=='('){
memset(str,0,sizeof(str));
q[x].arg1=string[i+1];//to replace (b+c) with B
q[x].op=string[i+2];
q[x].arg2=string[i+3];
q[x].result=(char)asc;
strcpy(str,substr(string,0,i-1));//copy from A+ to 7+ in str
len2=strlen(str);
str[len2]=q[x].result;//write B to str
str[len2+1]='\0';
strcat(str,substr(string,i+5,len1));
asc++;
x++;
strcpy(string,str);//hence the string in string is A+a+7+B-9-s*w
}
}
for(i=0;i<strlen(string);i++){
t1=string[i];//t1 is the current string
if(priority(t1)==0){//it is data ie number and not operator
OD[strlen(OD)]=t1;
}
else{
for(j=(strlen(OP)-1);j>=0;j--){//traverse the stack
t2=OP[j];//t2 is the stack top
if(priority(t1)>priority(t2))
break;
else
pop(OP,OD);//pop until the priority of the symbol at stack top is less than the priority of the given symbol
}
OP[strlen(OP)]=t1;//push the current operator into the stack
}
}
//the three addresses are formed in sequence
for(i=strlen(OP)-1;i>=0;i--)//pop the remaining operators from the stack
pop(OP,OD);
if(q[0].LHS!=' '){//The last line of three address For eg:S=c+a*b, A=a*b,B=c+A,S=B. It is for S=B line
q[x].arg1=q[x-1].result;
q[x].result=q[0].LHS;
}
//print the three address
for(i=0;i<=x;i++){
if(q[i].result!='\0'){
if(i==x)//last one
printf("\n\n%c:=%c",q[i].result,q[i].arg1);//done above
else
printf("\n\n%c:=%c %c %c",q[i].result,q[i].arg2,q[i].op,q[i].arg1);//arg2 printed first
}
}
return 0;
}
0 comments:
Post a Comment