Пример #1
0
void eval(LinkedStack& op, LinkedStack& num) {
    int numtop = num.top2();
    num.pop();
    if (op.top() == "+")
        numtop = add(num.top2(),numtop);
    else if (op.top() == "-")
        numtop = subtract(num.top2(),numtop);
    else if (op.top() == "*")
        numtop = multiply(num.top2(),numtop);
    else if (op.top() == "/")
        numtop = divide(num.top2(),numtop);
    op.pop(), num.pop();
    num.push(numtop);
}
Пример #2
0
void expressionMain() {
    LinkedStack expressionNum;
    LinkedStack expressionOp;
    int choice = 1, num;
    string op, expression = "", numS;
    stringstream ss;
    cout << "Please enter your expression one character at a time." << endl;
    cout << "num:";
    num = enterNum();
    ss << num;
    ss >> numS;
    expression += numS;
    expressionNum.push(num);
    while (choice != 3) {
        switch (choice) {
            case 1: cout << "op:";
                    op = enterOp();
                    cout << "num:";
                    num = enterNum();
                    ss.clear();
                    ss << num;
                    ss >> numS;
                    expression += op + numS;
                    if (!expressionOp.empty() && opSize(expressionOp.top()) >= opSize(op)) {
                        eval(expressionOp,expressionNum);
                        if (!expressionOp.empty() && opSize(expressionOp.top()) >= opSize(op)) {
                            eval(expressionOp,expressionNum);
                        }
                        expressionOp.push(op);
                        expressionNum.push(num);
                    }
                    else {
                        expressionOp.push(op);
                        expressionNum.push(num);
                    }
                    cout << "Expression: " << expression << endl;
                    cout << "More(1),Quit(2)?: ";
                    cin >> choice;
                break;
            case 2: while (!expressionOp.empty())
                        eval(expressionOp,expressionNum);
                    cout << "Answer: " << expressionNum.top2() << endl;
                    choice = 3;
        }
    }
    
}