Click here to Skip to main content
14,970,309 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have to make a function which change the infix expression to postfix expression and then evaluate the postfix expression using one stack i tried to do the following but its not working the output of the expression is 0.0000.

What I have tried:

C++
#include <stdio.h>
#include <math.h>
#define N 100
typedef float element;
typedef struct {
    element data[N];
    int top;
}stack;

stack CreateStack(){
    stack p;
    p.top =-1;
    return p;
}

int isEmptyStack(stack p){
    return (p.top == -1);
}

int isFullStack(stack p){
    return (p.top == N-1);
}

int Push(stack *p,element e){
    if(isFullStack(*p)) return 0;
    p->data[++p->top]=e;
    return 1;
    
}

int Pop(stack* p){
    if(isEmptyStack(*p)) return 0;
    p->top--;
    return 1;
    
}

int Top(stack p,element *e){
    if(isEmptyStack(p)) return 0;
    *e=p.data[p.top];
    return 1;
}


void print(stack s){
    element e;
    while(Top(s,&e)){
        Pop(&s);
        printf("|%5lf|\n",e);
    }
    printf("|_____|\n");
    
}



int stackable(char o1,char o2){
    switch(o1){
        case '(': return 1;
        case '+':case '-': return(o2 == '(');
        case '#': return (o2 != '#');
        case '*': case '/': return ( o2 == '(' || o2== '+'|| o2=='-');
        case '^': return (o2 != '#' && o2 != '^');
        case ')': return (o2 == '(');
        
    }
    return 0;
}
int infix_evaluation(char *str){
    stack s=CreateStack();
    int i,j;
    float value,ch;
    element e,e1,e2;
    i=0;
    j=-1;
    for(;*(str+i);i++)
    {
        if(stackable(*(str+i),e)){
            Push(&s,*(str+i));
          
        }
        else{
          
               *(str+j+1)=*(str+i);
            
                 if(*(str+i)==')')
                {
                  while(Top(s,&e))
                    {
                        Pop(&s);
                        e = *(str+j+1); 
                         
                    }
               
                }
              
            
        }
    }
    
         for(;*(str+i);i++){
        if(!stackable(*(str+i),e)){
           
            Push(&s,*(str+i));
        }
        else
        {
            if(Top(s,&e1))
            Pop(&s);
            if(Top(s,&e2))
            Pop(&s);
            
            switch(*(str+i)){
                case '*':value=(e2*e1);
                break;
                case '+':value=(e2+e1);
                break;
                case '-':value=(e2-e1);
                break;
                case '/':value=e2/e1;
                break;
                case '^':value=pow(e2,e1);
                break;
            }
            
            Push(&s,value);
        }
    }
    
    if(Top(s,&e)){
        Pop(&s);
        e=value;
    }
    
    return e;
    
    
  
}
int main()

{ float i;
    
    
    char str[30]={((6-(2+3))*(3+8/2))^2+3};
  i= infix_evaluation(str); 
   
 printf("%lf",i);

    return 0;
}
Posted
Updated 27-Mar-21 14:23pm
v3
Comments
CHill60 27-Mar-21 17:52pm
   
What does "not working" actually mean? Describe the problem
nan1001 27-Mar-21 18:30pm
   
the output of the expression is 0.000 i tried to print the characters in the array but it prints nothing which means that its not working
Member 14732673 27-Mar-21 18:19pm
   
Does it compile clean no errors.
And if there are errors where are they happening and what are they.
be verbose.
nan1001 27-Mar-21 18:31pm
   
yeah, but the output of the expression is zero '

Your first problem is here:
C++
char str[30]={((6-(2+3))*(3+8/2))^2+3};

That creates a character array with a single 2 followed by 29 zeros. That's not '2' the character, that's 2 the numeric value.
Why? Because it's and expression, and they get evaluated by the compiler and converted to a number.

You could convert it to a string:
C++
char str[30]="((6-(2+3))*(3+8/2))^2+3";

But then your function will probably still not give the results you expect, because I can't see anything in there to "tokenize" the numbers to numeric values instead of single characters. And that also means that if you try to evaluate "666+1" you won;t get what you expect at all!

Start with the debugger to find out what you do have working - but I suspect it's not a lot. That looks like code that was typed in in a single go, had the compilation errors fixed, and was run. That's not a good idea, particularly when you are starting out. Write a small bit, test it very thoroughly, then write the next which uses the now-known-working code. Repeat.
This may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
   
Comments
nan1001 29-Mar-21 14:42pm
   
yes ,thank you I'm trying again.
Quote:
i tried to do the following but its not working the output of the expression is 0.0000.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Comments
nan1001 29-Mar-21 14:43pm
   
thank you, I'll see what I can do .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900