如何通过将Infix转换为Postfix,然后将其评估为答案来解决结构问题?

时间:2019-04-07 19:59:28

标签: c++

我正在尝试将多行中缀分别转换为后缀(RPN),然后评估每行以获取其值,但是后缀未按正确的顺序打印表达式,因此未读取行单独,并且代码不会继续进行评估

Stack *s = initStack();
Stack *s_double = initStack();
Queue * q = initQueue();
char symbol; 
//char rpn_storage[1000];
int operand;
char rator;
char temp, temp1;
double carry, carry1;
int priority;
int priority_temp;
in>> symbol;
//Converting Infix to Postfix
while(symbol != '$'){
    cout << "Converting Infix to Postfix" << endl;
    out << "Converting Infix to Postfix" << endl;
    while(symbol != '\n'){
        if (symbol >= '0' && symbol <= '9'){
            out<< symbol;
            cout<< symbol;
            enqueue(q, symbol); 
        }
        if (symbol == '('){
            push(s, symbol);
        }
        if (symbol == ')'){
            temp = peek(s);
            while (temp != '(' && temp != '\0'){
                temp = pop(s);
                out << temp;
                cout << temp;
                enqueue(q, temp);
                if (temp == '\0'){
                    cout << "\n Error: Stack was found to be empty with no left parenthesis found" << endl;
                    //insert escape to outside the function so one can start over - possibly a return here
                }
                temp = peek(s);
            }
        }
        if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/' || symbol == '^'){
            temp = peek(s);

            if (symbol == '+' || symbol == '-'){
                priority = 1;
            }
            if (symbol == '*' || symbol == '/' || symbol == '%'){
                priority = 2;
            }
            if (symbol == '^'){
                priority = 3;
            }
            if (temp == '(' || temp == '\0'){
                priority_temp = 0;
            }
            if (temp == '+' || temp == '-'){
                priority_temp = 1;
            }
            if (temp == '*' || temp == '/'){
                priority_temp = 2;
            }
            if (temp == '^'){
                priority_temp = 3;
            }               
            if (priority >= priority_temp){
                push(s, symbol);
            }
            else{
                temp = pop(s);
                out << temp;
                cout << temp;
                enqueue(q, temp);
                temp = peek(s);
                while (temp != '\0'){
                    if (symbol == '+' || symbol == '-'){
                        priority = 1;
                    }
                    if (symbol == '*' || symbol == '/'){
                        priority = 2;
                    }
                    if (symbol == '^'){
                        priority = 3;
                    }
                    if (temp == '(' || temp == '\0'){
                        priority_temp = 0;
                    }
                    if (temp == '+' || temp == '-'){
                        priority_temp = 1;
                    }
                    if (temp == '*' || temp == '/'){
                        priority_temp = 2;
                    }
                    if (temp == '^'){
                        priority_temp = 3;
                    }
                    if (priority >= priority_temp){
                        push(s, symbol);
                    }
                    else{
                        temp = pop(s);
                        out << temp;
                        cout << temp;
                        enqueue(q, temp);
                        temp = peek(s);                     
                    }
                }   
            }
        }
        in>> symbol;    
    }
    if (symbol == '\n'){
        temp = peek(s);
        while (temp != '\0'){
            temp = pop(s);
            out << temp;
            cout << temp;
            enqueue(q, temp);
        }
        out << endl;
        cout << endl;
        enqueue(q, '\n');
    }
    in >> symbol;
}   
enqueue(q, '$');
//Evaluating Postfix
cout << endl;
cout << endl;
cout << "Evaluating Postfix:" << endl;
out << endl;
out << endl;
out << "Evaluating Postfix:" << endl;
symbol = peek_q(q);                 
while(symbol != '$'){

    while(symbol != '\n'){
        if (symbol >= '0' && symbol <= '9'){
            symbol = dequeue(q);
            symbol = symbol - '0';
            push_double(s_double, symbol);  
        }
        if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/' || symbol == '^'){
            carry = pop_double(s_double);
            carry1 = pop_double(s_double);
            if (carry == -999 || carry1 == -999){
                cout<< "\n Error: Malformed RPN expression" << endl;
                //Terminate evaluation here - possibly a return function                
            }
            if(symbol == '+'){
                carry = carry + carry1;
                carry1 = -999;
                push_double(s_double, carry);
            }
            if(symbol == '-'){
                carry = carry - carry1;
                carry1 = -999;
                push_double(s_double, carry);
            }
            if(symbol == '*'){
                carry = carry * carry1;
                carry1 = -999;
                push_double(s_double, carry);
            }
            if(symbol == '/'){
                carry = carry / carry1 * 1.0;
                carry1 = -999;
                push_double(s_double, carry);
            }
            if(symbol == '^'){
                carry = pow(carry1, carry);
                carry1 = -999;
                push_double(s_double, carry);
            }
        }   
    }
    carry = pop_double(s_double);
    out << carry << endl;
    cout << carry << endl;
}

该代码应该将RPN表达式打印到屏幕上并输入到文本文件中,然后分别遍历RPN表达式以打印相应的值

0 个答案:

没有答案