布尔函数始终返回true C ++

时间:2019-07-13 23:34:44

标签: c++ function boolean return-value

我有一个公共函数validInfixCheck(),该函数调用私有函数isBalanced()

如果字符串中的字符不是+-* /或数字,则isBalanced()函数应返回false。我遇到的问题是isBalanced()总是返回true。如果我将所有代码都放在同一个文件中,则逻辑工作正常,但是当我具有.h和.cpp时,逻辑就没有问题。

例如,如果我输入2 ^(3 + 4),该函数应返回false,因为^无效,但现在它返回true。

g ++ CalculatorMain.cpp CalculatorExp.cpp

Enter infix expression to evaluate:
2^(3+4)
This is what you entered: 2^(3+4)
expression is well formed

calculatorMain.cpp

#include "CalculatorExp.h"
#include<iostream>
#include <string>

using namespace std;

//prototype declarations
string getInfixExpression();

int main()
{
    CalculatorExp calc; 
    string inputExpression;
    inputExpression = getInfixExpression();

    cout<<"This is what you entered: "<< inputExpression <<endl;

    calc.validInfixCheck(inputExpression);

    return 0;
}

string getInfixExpression()
{
    string exp;
    cout<<"Enter infix expression to evaluate: "<<endl;
    cin>>exp;

    return exp;
}

CalculatorExp.cpp

#include "CalculatorExp.h"
#include<iostream>
#include <string>
#include <stack> 

using namespace std;

CalculatorExp::CalculatorExp()
{
  //default constructor 
}
// public //

// valid input
bool CalculatorExp::validInfixCheck(string inputExpression)
{
    // Checks string is well formed
    if(isBalanced(exp))
    {
    cout << "expression is well formed" << endl;
    }
    else
    {
        cout << "expression is NOT well formed" << endl;
    }

    // Check string has balanced ( )
    //...

    return 0;
}

// private //
bool CalculatorExp::isBalanced(string exp)
{
        int expLength = exp.length();
        bool balancedSoFar = true;
        int i = 0; // Tracks character position in string

        while(balancedSoFar && i < expLength)
        {
            char ch = exp[i];
            i++;
            if(!isdigit(ch))
            {

            switch (ch)
            {

                case '+':
                balancedSoFar = true;
                break;

                case '-':
                balancedSoFar = true;
                break;

                case '*':
                balancedSoFar = true;
                break;

                case '/':
                balancedSoFar = true;
                break;

                case '(':
                balancedSoFar = true;
                break;

                case ')':
                balancedSoFar = true;
                break;

                default:
                balancedSoFar = false;
                break;
            }
            }
        }
        if(balancedSoFar)
        {
            return 1;
            // cout << "exp is balanced" << endl;
            // Probably run the check braces function here
        }
        else 
        {
            return 0;
            // cout << "exp is not balanced" << endl;
            // Probably run the program to enter a new exp again
        }
}  // end isBalanced

1 个答案:

答案 0 :(得分:0)

考虑代码

// valid input
bool CalculatorExp::validInfixCheck(string inputExpression)
{
    // Checks string is well formed
    if(isBalanced(exp))
    {

在这里,您的方法有一个参数inputExpression,但是您随后测试了(不相关的)字符串exp,以查看其是否平衡。因此,除非您有一个成员exp是一个空字符串,否则这可能不会编译–在这种情况下,它将测试该空字符串,该字符串将返回true ...