需要改进/改变的领域?

时间:2018-02-19 04:50:55

标签: c++ class

我不太确定stackoverflow上是否允许这类问题,但现在就这样了。我完成了这个任务,用一小组运算符来计算反向抛光表达式。所有测试都通过了,我已经提交了作业。我想知道你们是否有任何建议,我可以改进可以应用于我未来的任务/项目的代码。

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class TheStack
{
public:
    void push(double d1);
    double pop();
    int getNodeTracker();

private:
    struct Node
    {
        double data;
        Node* next;
    };
    Node* bottom = NULL;
    Node* top = NULL;
    Node* newP = NULL;
    int nodeTracker = 0;
};
void TheStack::push(double d1)
{
    newP = new Node;
    newP->data = d1;
    newP->next = NULL;

    if (bottom == NULL)
    {
        bottom = top = newP;
        top->next = NULL;
    }
    else
    {
        newP->next = top;
        top = newP;
    }

    nodeTracker += 1;
}
double TheStack::pop()
{
    Node* tempP;
    double tempD;

    if (top == NULL)
    {
        cout << "The stack is empty." << endl;
    }
    else
    {
        tempP = top;
        tempD = tempP->data;
        top = top->next;
        delete(tempP);
        nodeTracker -= 1;
        return(tempD);
    }
}
int TheStack::getNodeTracker()
{
    return nodeTracker;
}

bool isOperand(string s1);
bool isOperator(string s1);
double operate(string s1, double d1, double d2);
int processor(string str);
bool validString(string str);

int main()
{
    // getting user input
    string str;
    getline(cin, str);

    // input loop
    while (str != "0")
    {
        processor(str);
        cout << endl;
        getline(cin, str);
    }

    // end program
    return 0;
}

bool isOperand(string s1)
{
    try
    {
        stod(s1);
    }
    catch (...)
    {
        return false;
    }
    return true;

}

bool isOperator(string s1)
{
    string validOps[4] = { "+", "-", "*", "/" };
    for (int i = 0; i < 4; i++)
    {
        if (s1 == validOps[i])
        {
            return true;
        }
    }
    return false;
}

double operate(string s1, double d1, double d2)
{
    char op = s1[0];
    switch (op)
    {
        case '+': 
            return d1 + d2;
        case '-': 
            return d1 - d2;
        case '*': 
            return d1 * d2;
        case '/': 
            return d1 / d2;
    }
}

bool validString(string str)
{
    for (int i = str.size()-1; i >= 0; i--)
    {
        if (str[i] == '=')
        {
            return true;
        }
    }
    return false;
}

int processor(string str)
{
    TheStack stacklist;
    istringstream iss(str);
    string streamVar;
    iss >> streamVar;

    // checks if expression has = sign
    if (!validString(str))
    {
        cout << "Error: Please input valid expression." << endl;
        return -1;
    }

    // builds and operates upon stack
    while (streamVar != "=")
    {
        if (isOperand(streamVar))
        {
            stacklist.push(stod(streamVar));
        }
        else if (isOperator(streamVar))
        {
            if (stacklist.getNodeTracker() > 1)
            {
                double d2 = stacklist.pop();
                double d1 = stacklist.pop();
                if (streamVar == "/" && d2 == 0)
                {
                    cout << "Error: Division by 0." << endl;
                    return -1;
                }
                double result = operate(streamVar, d1, d2);
                stacklist.push(result);
            }
            else
            {
                cout << "Error: Too many operators." << endl;
                return -1;
            }
        }

        iss >> streamVar;
    }

    // operand error case
    if (stacklist.getNodeTracker() > 1)
    {
        cout << "Error: Too many operands." << endl;
        return -1;
    }

    // output
    cout << stacklist.pop() << endl;
    return 0;
}

0 个答案:

没有答案