473,394 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to solve infix expression

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAX 10

struct opndstack
{
int top;
double items[MAX];
};

struct optrstack
{
int top;
char items[MAX];
};

void pushopnd(struct opndstack *s,double val)
{
if(s->top==MAX-1)
{
printf(“nStack Overflow.”);
exit(1);
}
else
{s->items[++(s->top)]=val;
}
}

double popopnd(struct opndstack *s)
{
if(s->top==-1)
{
printf(“nStack Underflow.”);
exit(1);
}
else
{
return(s->items[(s->top)--]);
}
}

void pushoptr(struct optrstack *s,char ch)
{
if(s->top==MAX-1)
{
printf(“nStack Overflow.”);
exit(1);
}
else
{s->items[++(s->top)]=ch;
}
}

char popoptr(struct optrstack *s)
{
if(s->top==-1)
{
printf(“nStack Underflow.”);
exit(1);
}
else
{
return(s->items[(s->top)--]);
}
}

int isdigit(char ch)
{return(ch>=’0′ && ch<=’9′);
}

int isoperator(char ch)
{
switch(ch)
{
case ‘+’:
case ‘-’:
case ‘*’:
case ‘/’:
case ‘^’:
return 1;
default:
return 0;
}
}

double eval(char ch,double opnd1,double opnd2)
{
switch(ch)
{
case ‘+’:return(opnd1+opnd2);
case ‘-’:return(opnd1-opnd2);
case ‘*’:return(opnd1*opnd2);
case ‘/’:return(opnd1/opnd2);
case ‘^’:return(pow(opnd1,opnd2));
default:printf(“nInvalid operator.”);
exit(1);
}
}

int precedence(char ch)
{
switch(ch)
{
case ‘#’: return 0;
case ‘+’:
case ‘-’: return 1;
case ‘*’:
case ‘/’:return 2;
case ‘^’:return 3;
case ‘(‘:return 4;
default :printf(“nInvalid operator.”);
exit(1);
}
}

double infix(char str[])
{
double opnd1,opnd2,value;
char ch;
opndstack opndstk;
optrstack optrstk;
opndstk.top=-1;
optrstk.top=-1;
pushoptr(&optrstk,’#');
int i=0;
char optr2;
for(i=0;str[i]!=’�’;i++)
{
if(isdigit(str[i]))
pushopnd(&opndstk,(double)(str[i]-’0′));
else if(isoperator(str[i]))
{optr2=popoptr(&optrstk);
if(precedence(str[i])>precedence(optr2))
{pushoptr(&optrstk,optr2);
pushoptr(&optrstk,str[i]);
}
else
{
while(precedence(str[i])<=precedence(optr2))
{ opnd2=popopnd(&opndstk);
opnd1=popopnd(&opndstk);
value = eval(optr2,opnd1,opnd2);
pushopnd(&opndstk,value);
optr2=popoptr(&optrstk);
}
pushoptr(&optrstk,optr2);
pushoptr(&optrstk,str[i]);
}
}
}
while((ch=popoptr(&optrstk))!=’#')
{
opnd2=popopnd(&opndstk);
opnd1=popopnd(&opndstk);
value=eval(ch,opnd1,opnd2);
pushopnd(&opndstk,value);
}
return(popopnd(&opndstk));
}

int main() {
char str[80];
int i;
printf(“nEnter an infix string?”);
for(i=0;(str[i]=getchar())!=’n';i++)
str[i]=’�’;
printf(“nInfix String = %s”,str);
printf(“nEvaluation of infix = %f”,infix(str));
return 0;
}
Sep 8 '14 #1
3 1602
weaknessforcats
9,208 Expert Mod 8TB
Exactly what help do you need?

I see a ton of code with no comments. Are you expecting me to debug this program?
Sep 8 '14 #2
donbock
2,426 Expert 2GB
  1. Please enclose your code in CODE tags. That will make it easier to read and it provides line numbers. By the way, fix the spacing & indentation after you add the CODE tags.
  2. Cut & paste your compilation error into your post so we don't have to guess what went wrong.
  3. Typically, the line numbers in your source file don't match the line numbers in the snippet you post. Tell us which line of the snippet corresponds to the line number provided by the compiler error.
Sep 8 '14 #3
donbock
2,426 Expert 2GB
One thing that jumped out at me is a line near the bottom of your snippet:
Expand|Select|Wrap|Line Numbers
  1. str[i] = ' ';
Where the character between the single quotes is a square. I doubt that's a legal character constant in C.
Sep 8 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: KidLogik | last post by:
Hello! I am converting an infix expression string into a postfix so that I will be able to evaluate it easier -> (5*(((9+8)*(4*6))+7)) == 598+46**7+* I believe the rule is "Replace all...
19
by: caramel | last post by:
i've been working on this program forever! now i'm stuck and going insane because i keep getting a syntax error msg and i just can't see what the compiler is signaling to! #include <stdio.h>...
5
by: Thumbski | last post by:
Alright basically I just have one simple question. I can code perfectly fine and don't have any problems with the .cpp of the infix class but with my header I can't get anything to work really, any...
0
by: coolguyjas07 | last post by:
plzzzzzz help me out to run my source code ......... i hav written dis code to convert infix expression to postfix but not getting desired output.... plzzz help me to get correct output.. d source...
2
by: ostorat_elwafa2 | last post by:
program that uses a stack to convert a given infix expression to a postfix expression and then to evaluate it. program should first check if the infix expression entered has balanced brackets ( ...
4
by: mahalingsalimath | last post by:
Sir i want to evualate an infix expression without converting into an intermediate form of postfix, using two stacks as a datastructures using C programming.
1
by: aitia | last post by:
this the code. i used ECLIPSE to run this.. it has some codes smells that i can't seem to figure out.. can any one help? import java.io.*; import java.util.*; public class Postfix { private...
6
amarniit
by: amarniit | last post by:
sir i am submitting a C program code in which i am getting the diffrent diffrent answer for a same expression....... and i am not getting that in it how the work of pre and post increment in these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.