about c++ convert infix to postfix when it contains two power signs (^^)












-3















My goal is to convert infix expression to postfix expression.



I'm facing issue on line:



while(precedence(stack[top])>=precedence(symbol)) 


Condition is (and should be) satisfied when it is ++ or ** or / or -- because it checks from left, but in case of power(^) it begins from the right. I don't want to enter this loop if it is doubled power(^^).



What can I do? Please help me



in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop I will remove this case because it is wrong but I did not know how



#include<iostream>
#include<stdio.h>
using namespace std;
#define size 100
int temp,length=0,inx=0,pos=0,top=-1;
char symbol,infix[size],postfix[size],stack[size];

void push(char);
char pop();
int precedence(char);
void infix_to_postfix(char);


void push(char symbol)
{
if(top>=size-1)
cout<<"stack is over flow push not possible"<<endl;
else{
top=top+1;
stack[top]=symbol;
} }

char pop()
{
temp=stack[top];
top=top-1;
return temp;
}
int precedence(char symbol)
{
int priority=0;
switch(symbol)
{
case '+':
case '-':
priority=1;
break;

case '*':
case '/':
priority=2;
break;
case '^':
priority=3;
break;
}//end of switch()
return priority;
}//end of precedence()
void infix_to_postfix(char infix)
{


while(infix[inx]!='')
{
symbol=infix[inx++];
switch(symbol)
{
case '(':
push(symbol);
break;
case ')':
temp=pop();
while(temp!='(')
{
postfix[pos++]=temp;
temp=pop();
}
break;
case '-':
case '+':
case '*':
case '/':
case '^':
while(precedence(stack[top])>=precedence(symbol))
{
temp=pop();
postfix[pos++]=temp;
}
push(symbol);
break;
default:
postfix[pos++]=symbol;
break;
}
}

while(top>-1)
{
temp=pop();
postfix[pos++]=temp;
postfix[pos]='';

}
}




int main()
{
cout<<"nEnter an infix expression:n";
cin>>infix;
infix_to_postfix(infix);
cout<<"nThe equivalent postfix expression:n";;
cout<<postfix<<endl;;
return 0;
}









share|improve this question

























  • Your question is unclear, and hard to understand. Please rewrite your question in plain, short sentences, that are at least mostly grammatically correct. If English is not your first language, find someone to help you translate your question properly. Nobody can give you an answer if nobody can understand exactly what you're asking.

    – Sam Varshavchik
    Nov 20 '18 at 11:33











  • I think his question is how to detect two consecutive '^' characters and act accordingly

    – Hristijan Gjorshevski
    Nov 20 '18 at 11:35











  • I will explain when convert form infix to postfix when you scan 1st operator you add it in the stack and then scan other when you reach to 2nd operator if 1st operator is()and 2nd operator ()this mean the 1st operator will make it pop and the 2nd push to stack this happen in (*,+,-,/)because when scan priority from left to right when there is same operator but in case of power (^)when 1st operator is (^)but it in stack and 2nd operator (^)we can not pop()1st operator because the priority from the right to left when there is same two (^^)I want not enter the while loop when there is two power

    – how
    Nov 20 '18 at 11:50











  • sorry yes English is not my first language

    – how
    Nov 20 '18 at 11:51











  • in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop I will remove this case because it is wrong but I did not know how

    – how
    Nov 20 '18 at 11:57


















-3















My goal is to convert infix expression to postfix expression.



I'm facing issue on line:



while(precedence(stack[top])>=precedence(symbol)) 


Condition is (and should be) satisfied when it is ++ or ** or / or -- because it checks from left, but in case of power(^) it begins from the right. I don't want to enter this loop if it is doubled power(^^).



What can I do? Please help me



in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop I will remove this case because it is wrong but I did not know how



#include<iostream>
#include<stdio.h>
using namespace std;
#define size 100
int temp,length=0,inx=0,pos=0,top=-1;
char symbol,infix[size],postfix[size],stack[size];

void push(char);
char pop();
int precedence(char);
void infix_to_postfix(char);


void push(char symbol)
{
if(top>=size-1)
cout<<"stack is over flow push not possible"<<endl;
else{
top=top+1;
stack[top]=symbol;
} }

char pop()
{
temp=stack[top];
top=top-1;
return temp;
}
int precedence(char symbol)
{
int priority=0;
switch(symbol)
{
case '+':
case '-':
priority=1;
break;

case '*':
case '/':
priority=2;
break;
case '^':
priority=3;
break;
}//end of switch()
return priority;
}//end of precedence()
void infix_to_postfix(char infix)
{


while(infix[inx]!='')
{
symbol=infix[inx++];
switch(symbol)
{
case '(':
push(symbol);
break;
case ')':
temp=pop();
while(temp!='(')
{
postfix[pos++]=temp;
temp=pop();
}
break;
case '-':
case '+':
case '*':
case '/':
case '^':
while(precedence(stack[top])>=precedence(symbol))
{
temp=pop();
postfix[pos++]=temp;
}
push(symbol);
break;
default:
postfix[pos++]=symbol;
break;
}
}

while(top>-1)
{
temp=pop();
postfix[pos++]=temp;
postfix[pos]='';

}
}




int main()
{
cout<<"nEnter an infix expression:n";
cin>>infix;
infix_to_postfix(infix);
cout<<"nThe equivalent postfix expression:n";;
cout<<postfix<<endl;;
return 0;
}









share|improve this question

























  • Your question is unclear, and hard to understand. Please rewrite your question in plain, short sentences, that are at least mostly grammatically correct. If English is not your first language, find someone to help you translate your question properly. Nobody can give you an answer if nobody can understand exactly what you're asking.

    – Sam Varshavchik
    Nov 20 '18 at 11:33











  • I think his question is how to detect two consecutive '^' characters and act accordingly

    – Hristijan Gjorshevski
    Nov 20 '18 at 11:35











  • I will explain when convert form infix to postfix when you scan 1st operator you add it in the stack and then scan other when you reach to 2nd operator if 1st operator is()and 2nd operator ()this mean the 1st operator will make it pop and the 2nd push to stack this happen in (*,+,-,/)because when scan priority from left to right when there is same operator but in case of power (^)when 1st operator is (^)but it in stack and 2nd operator (^)we can not pop()1st operator because the priority from the right to left when there is same two (^^)I want not enter the while loop when there is two power

    – how
    Nov 20 '18 at 11:50











  • sorry yes English is not my first language

    – how
    Nov 20 '18 at 11:51











  • in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop I will remove this case because it is wrong but I did not know how

    – how
    Nov 20 '18 at 11:57
















-3












-3








-3


0






My goal is to convert infix expression to postfix expression.



I'm facing issue on line:



while(precedence(stack[top])>=precedence(symbol)) 


Condition is (and should be) satisfied when it is ++ or ** or / or -- because it checks from left, but in case of power(^) it begins from the right. I don't want to enter this loop if it is doubled power(^^).



What can I do? Please help me



in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop I will remove this case because it is wrong but I did not know how



#include<iostream>
#include<stdio.h>
using namespace std;
#define size 100
int temp,length=0,inx=0,pos=0,top=-1;
char symbol,infix[size],postfix[size],stack[size];

void push(char);
char pop();
int precedence(char);
void infix_to_postfix(char);


void push(char symbol)
{
if(top>=size-1)
cout<<"stack is over flow push not possible"<<endl;
else{
top=top+1;
stack[top]=symbol;
} }

char pop()
{
temp=stack[top];
top=top-1;
return temp;
}
int precedence(char symbol)
{
int priority=0;
switch(symbol)
{
case '+':
case '-':
priority=1;
break;

case '*':
case '/':
priority=2;
break;
case '^':
priority=3;
break;
}//end of switch()
return priority;
}//end of precedence()
void infix_to_postfix(char infix)
{


while(infix[inx]!='')
{
symbol=infix[inx++];
switch(symbol)
{
case '(':
push(symbol);
break;
case ')':
temp=pop();
while(temp!='(')
{
postfix[pos++]=temp;
temp=pop();
}
break;
case '-':
case '+':
case '*':
case '/':
case '^':
while(precedence(stack[top])>=precedence(symbol))
{
temp=pop();
postfix[pos++]=temp;
}
push(symbol);
break;
default:
postfix[pos++]=symbol;
break;
}
}

while(top>-1)
{
temp=pop();
postfix[pos++]=temp;
postfix[pos]='';

}
}




int main()
{
cout<<"nEnter an infix expression:n";
cin>>infix;
infix_to_postfix(infix);
cout<<"nThe equivalent postfix expression:n";;
cout<<postfix<<endl;;
return 0;
}









share|improve this question
















My goal is to convert infix expression to postfix expression.



I'm facing issue on line:



while(precedence(stack[top])>=precedence(symbol)) 


Condition is (and should be) satisfied when it is ++ or ** or / or -- because it checks from left, but in case of power(^) it begins from the right. I don't want to enter this loop if it is doubled power(^^).



What can I do? Please help me



in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop I will remove this case because it is wrong but I did not know how



#include<iostream>
#include<stdio.h>
using namespace std;
#define size 100
int temp,length=0,inx=0,pos=0,top=-1;
char symbol,infix[size],postfix[size],stack[size];

void push(char);
char pop();
int precedence(char);
void infix_to_postfix(char);


void push(char symbol)
{
if(top>=size-1)
cout<<"stack is over flow push not possible"<<endl;
else{
top=top+1;
stack[top]=symbol;
} }

char pop()
{
temp=stack[top];
top=top-1;
return temp;
}
int precedence(char symbol)
{
int priority=0;
switch(symbol)
{
case '+':
case '-':
priority=1;
break;

case '*':
case '/':
priority=2;
break;
case '^':
priority=3;
break;
}//end of switch()
return priority;
}//end of precedence()
void infix_to_postfix(char infix)
{


while(infix[inx]!='')
{
symbol=infix[inx++];
switch(symbol)
{
case '(':
push(symbol);
break;
case ')':
temp=pop();
while(temp!='(')
{
postfix[pos++]=temp;
temp=pop();
}
break;
case '-':
case '+':
case '*':
case '/':
case '^':
while(precedence(stack[top])>=precedence(symbol))
{
temp=pop();
postfix[pos++]=temp;
}
push(symbol);
break;
default:
postfix[pos++]=symbol;
break;
}
}

while(top>-1)
{
temp=pop();
postfix[pos++]=temp;
postfix[pos]='';

}
}




int main()
{
cout<<"nEnter an infix expression:n";
cin>>infix;
infix_to_postfix(infix);
cout<<"nThe equivalent postfix expression:n";;
cout<<postfix<<endl;;
return 0;
}






c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 14:59







how

















asked Nov 20 '18 at 11:29









howhow

33




33













  • Your question is unclear, and hard to understand. Please rewrite your question in plain, short sentences, that are at least mostly grammatically correct. If English is not your first language, find someone to help you translate your question properly. Nobody can give you an answer if nobody can understand exactly what you're asking.

    – Sam Varshavchik
    Nov 20 '18 at 11:33











  • I think his question is how to detect two consecutive '^' characters and act accordingly

    – Hristijan Gjorshevski
    Nov 20 '18 at 11:35











  • I will explain when convert form infix to postfix when you scan 1st operator you add it in the stack and then scan other when you reach to 2nd operator if 1st operator is()and 2nd operator ()this mean the 1st operator will make it pop and the 2nd push to stack this happen in (*,+,-,/)because when scan priority from left to right when there is same operator but in case of power (^)when 1st operator is (^)but it in stack and 2nd operator (^)we can not pop()1st operator because the priority from the right to left when there is same two (^^)I want not enter the while loop when there is two power

    – how
    Nov 20 '18 at 11:50











  • sorry yes English is not my first language

    – how
    Nov 20 '18 at 11:51











  • in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop I will remove this case because it is wrong but I did not know how

    – how
    Nov 20 '18 at 11:57





















  • Your question is unclear, and hard to understand. Please rewrite your question in plain, short sentences, that are at least mostly grammatically correct. If English is not your first language, find someone to help you translate your question properly. Nobody can give you an answer if nobody can understand exactly what you're asking.

    – Sam Varshavchik
    Nov 20 '18 at 11:33











  • I think his question is how to detect two consecutive '^' characters and act accordingly

    – Hristijan Gjorshevski
    Nov 20 '18 at 11:35











  • I will explain when convert form infix to postfix when you scan 1st operator you add it in the stack and then scan other when you reach to 2nd operator if 1st operator is()and 2nd operator ()this mean the 1st operator will make it pop and the 2nd push to stack this happen in (*,+,-,/)because when scan priority from left to right when there is same operator but in case of power (^)when 1st operator is (^)but it in stack and 2nd operator (^)we can not pop()1st operator because the priority from the right to left when there is same two (^^)I want not enter the while loop when there is two power

    – how
    Nov 20 '18 at 11:50











  • sorry yes English is not my first language

    – how
    Nov 20 '18 at 11:51











  • in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop I will remove this case because it is wrong but I did not know how

    – how
    Nov 20 '18 at 11:57



















Your question is unclear, and hard to understand. Please rewrite your question in plain, short sentences, that are at least mostly grammatically correct. If English is not your first language, find someone to help you translate your question properly. Nobody can give you an answer if nobody can understand exactly what you're asking.

– Sam Varshavchik
Nov 20 '18 at 11:33





Your question is unclear, and hard to understand. Please rewrite your question in plain, short sentences, that are at least mostly grammatically correct. If English is not your first language, find someone to help you translate your question properly. Nobody can give you an answer if nobody can understand exactly what you're asking.

– Sam Varshavchik
Nov 20 '18 at 11:33













I think his question is how to detect two consecutive '^' characters and act accordingly

– Hristijan Gjorshevski
Nov 20 '18 at 11:35





I think his question is how to detect two consecutive '^' characters and act accordingly

– Hristijan Gjorshevski
Nov 20 '18 at 11:35













I will explain when convert form infix to postfix when you scan 1st operator you add it in the stack and then scan other when you reach to 2nd operator if 1st operator is()and 2nd operator ()this mean the 1st operator will make it pop and the 2nd push to stack this happen in (*,+,-,/)because when scan priority from left to right when there is same operator but in case of power (^)when 1st operator is (^)but it in stack and 2nd operator (^)we can not pop()1st operator because the priority from the right to left when there is same two (^^)I want not enter the while loop when there is two power

– how
Nov 20 '18 at 11:50





I will explain when convert form infix to postfix when you scan 1st operator you add it in the stack and then scan other when you reach to 2nd operator if 1st operator is()and 2nd operator ()this mean the 1st operator will make it pop and the 2nd push to stack this happen in (*,+,-,/)because when scan priority from left to right when there is same operator but in case of power (^)when 1st operator is (^)but it in stack and 2nd operator (^)we can not pop()1st operator because the priority from the right to left when there is same two (^^)I want not enter the while loop when there is two power

– how
Nov 20 '18 at 11:50













sorry yes English is not my first language

– how
Nov 20 '18 at 11:51





sorry yes English is not my first language

– how
Nov 20 '18 at 11:51













in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop I will remove this case because it is wrong but I did not know how

– how
Nov 20 '18 at 11:57







in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop I will remove this case because it is wrong but I did not know how

– how
Nov 20 '18 at 11:57














1 Answer
1






active

oldest

votes


















0















in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop




Use



while (precedence(stack[top]) >= precedence(symbol) && stack[top] != '^' && symbol != '^')


Adding an && to your condition will make it check that both the operator at the top of the stack and the symbol isn't '^'.



You could also do



// ...

case '^':
if (stack[top] != '^' && symbol != '^')
while(precedence(stack[top])>=precedence(symbol))
{
temp=pop();
postfix[pos++]=temp;
}

// ...





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392047%2fabout-c-convert-infix-to-postfix-when-it-contains-two-power-signs%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0















    in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop




    Use



    while (precedence(stack[top]) >= precedence(symbol) && stack[top] != '^' && symbol != '^')


    Adding an && to your condition will make it check that both the operator at the top of the stack and the symbol isn't '^'.



    You could also do



    // ...

    case '^':
    if (stack[top] != '^' && symbol != '^')
    while(precedence(stack[top])>=precedence(symbol))
    {
    temp=pop();
    postfix[pos++]=temp;
    }

    // ...





    share|improve this answer






























      0















      in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop




      Use



      while (precedence(stack[top]) >= precedence(symbol) && stack[top] != '^' && symbol != '^')


      Adding an && to your condition will make it check that both the operator at the top of the stack and the symbol isn't '^'.



      You could also do



      // ...

      case '^':
      if (stack[top] != '^' && symbol != '^')
      while(precedence(stack[top])>=precedence(symbol))
      {
      temp=pop();
      postfix[pos++]=temp;
      }

      // ...





      share|improve this answer




























        0












        0








        0








        in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop




        Use



        while (precedence(stack[top]) >= precedence(symbol) && stack[top] != '^' && symbol != '^')


        Adding an && to your condition will make it check that both the operator at the top of the stack and the symbol isn't '^'.



        You could also do



        // ...

        case '^':
        if (stack[top] != '^' && symbol != '^')
        while(precedence(stack[top])>=precedence(symbol))
        {
        temp=pop();
        postfix[pos++]=temp;
        }

        // ...





        share|improve this answer
















        in simple in while(precedence(stack[top])>=precedence(symbol)) if the operator in top is (^)and operator in symbol is (^)I do not want to enter while loop




        Use



        while (precedence(stack[top]) >= precedence(symbol) && stack[top] != '^' && symbol != '^')


        Adding an && to your condition will make it check that both the operator at the top of the stack and the symbol isn't '^'.



        You could also do



        // ...

        case '^':
        if (stack[top] != '^' && symbol != '^')
        while(precedence(stack[top])>=precedence(symbol))
        {
        temp=pop();
        postfix[pos++]=temp;
        }

        // ...






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 17:06

























        answered Nov 20 '18 at 14:27









        TrebuchetMSTrebuchetMS

        2,4021622




        2,4021622






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392047%2fabout-c-convert-infix-to-postfix-when-it-contains-two-power-signs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$