-3

My goal is to convert infix expression to postfix expression.

I've got a problem with this 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(^^).

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 didn't know how.

C++:

#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] != '\0') {
        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] = '\0';
    }
}

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;
}
karel
  • 5,489
  • 46
  • 45
  • 50
how
  • 3
  • 4
  • 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
  • Could you [edit] your question and add there your comments? IMO some `.` would improve your text, I can't tell where the sentence starts and where it ends – barbsan Nov 20 '18 at 12:18

1 Answers1

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;
        }

    // ...
TrebledJ
  • 8,713
  • 7
  • 26
  • 48