-6

what does return 4 return 2 return 6 in this code really returning is not making sense to me may someone explain to me what they return,i saw this code on stack flow someone wanted an explanation on infix and prefix convertion

#include<stdio.h> 
#include<conio.h> 
#include<string.h> 
#define MAX 20 

char stack[MAX]; 
int top = -1; 
char pop(); 
void push(char item); 

int prcd(char symbol) 
{ 
    switch(symbol) 
    { 
    case '+': 
    case '-': 
        return 2; 
    case '*': 
    case '/': 
        return 4; 
    case '^': 
    case '$': 
        return 6; 
    case '(': 
    case ')': 
    case '#': 
        return 1; 
    } 
} 

int isoperator(char symbol) 
{ 
    switch(symbol) 
    {
    case '+': 
    case '-': 
    case '*': 
    case '/': 
    case '^': 
    case '$': 
    case '(': 
    case ')': 
        return 1; 
    default: 
        return 0; 
    } 
} 

void convertip(char infix[],char prefix[]) 
{ 
    int i,symbol,j=0; 
    char test[MAX]; 

    infix=strrev(infix); 
    stack[++top]='#'; 

    for(i=0;i<strlen(infix);i++) 
    { 
        symbol=infix[i]; 
        if(isoperator(symbol)==0) 
        { 
            prefix[j]=symbol; 
            j++; 
        }
        else 
        { 
            if(symbol==')') 
            { 
                push(symbol); 
            } 
            else if(symbol=='(') 
            {    
                while(stack[top]!=')') 
                { 
                    prefix[j]=pop(); 
                    j++; 
                }    

                pop();//pop out (. 
            } 
            else 
            { 
                if(prcd(symbol)>prcd(stack[top])) 
                { 
                    push(symbol); 
                }
                else 
                { 
                    while(prcd(symbol)<=prcd(stack[top])) 
                    { 
                        prefix[j]=pop(); 
                        j++; 
                    } 
                    push(symbol); 
                }//end of else. 
            }//end of else. 
        }//end of else. 
    }//end of for. 

    while(stack[top]!='#') 
    { 
        prefix[j]=pop(); 
        j++; 
    } 

    prefix[j]='\0';//null terminate string. 
    prefix=strrev(prefix); 

} 

int main() 
{ 
    char infix[20],prefix[20]; 
    //clrscr(); 
    printf("Enter the valid infix string:\n"); 
    gets(infix); 
    convertip(infix,prefix); 
    printf("The corresponding prefix string is:\n"); 
    puts(prefix); 
    getch(); 

    return 0; 
} 

void push(char item)
{ 
    top++; 
    stack[top]=item; 
} 

char pop() 
{ 
    char a; 
    a=stack[top]; 
    top--; 
    return a; 
} 
Bobby
  • 11,419
  • 5
  • 44
  • 69
Mildred Shimz
  • 607
  • 4
  • 11
  • 20
  • 1
    Try to put only the interesting part. People here are not going to read all of that. – enrico.bacis Oct 30 '12 at 09:25
  • Could you separate the question into sentences? Short answer: it does make sense. [Switch statement](http://en.cppreference.com/w/cpp/language/switch) – Andriy Oct 30 '12 at 09:25
  • This question has been linked to a bug on meta http://meta.stackexchange.com/questions/153048/code-indenting-just-appears-to-be-broken-here due to some issues with formatting the actual code which several of us seemed to be attempting to fix. – CashCow Oct 30 '12 at 11:12

1 Answers1

1

This code might interpret numerical terms, like. 17 + 3 * 8. To calculate this properly, the code must determine to first take the * and then the +. The order of evaluation is set by the precedence rules: * and / come before + and -.

The return statements look like some precedence code.

  • (, ), #: return 1
  • +, -: return 2
  • *, /: return 4
  • ^, $: return 6

(, ) and # have lowest precedence. After that + and - have next lowest precedence. Then follows * and /. Highest precedence is for ^ and $.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • im trying to get some sense out of it when u write code for example a sum of two number some times i can write return b+c i know that there is b and c in the code and the return type is the sum but in this code im confused – Mildred Shimz Oct 30 '12 at 09:29
  • @MildredShimz: [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Andriy Oct 30 '12 at 09:34
  • thanks i got the meaning i was confused though – Mildred Shimz Oct 30 '12 at 09:48
  • strange because parentheses have the highest precedence, however ( and ) are not operators, they are just there to parse expressions. You could say that operator precedence is used where ( and ) are not present to show where they should be inserted. – CashCow Oct 30 '12 at 11:16
  • @CashCow That's why I modified my answer to "*might* interpret numerical terms". It seems to be some sort of precedence as the function name suggests. – Olaf Dietsche Oct 30 '12 at 11:26