📜  以字符串形式给出的方程式的验证

📅  最后修改于: 2021-05-31 19:50:13             🧑  作者: Mango

给定方程式形式的字符串,即A + B + C – D = E ,其中A,B,C,D和E是整数,-,+和=是运算符。任务是,如果方程式有效,则打印Valid ,否则打印Invalid
注意:字符串仅包含{0、1、2、3、4、5、6、7、8、9,+,-,=}中的字符。

例子:

方法:

  • 遍历字符串和阵列中的操作数的所有操作数[]和所有运算符存储在数组中运算符[]。
  • 现在,对操作数[0]操作数[1]执行存储在运算符[0]中的算术运算,并将其存储在ans中
  • 然后执行秒算术运算,即ans上的运算运算符[1]运算符[2] ,依此类推。
  • 最后,将计算出的ans与最后一个操作数(即操作数[4])进行比较。如果它们相等,则打印Valid,否则打印Invalid

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if the equation is valid
bool isValid(string str)
{
    int k = 0;
    string operands[5] = "";
    char operators[4];
    long ans = 0, ans1 = 0, ans2 = 0;
    for (int i = 0; i < str.length(); i++) {
  
        // If it is an integer then add it to another string array
        if (str[i] != '+' && str[i] != '=' && str[i] != '-')
            operands[k] += str[i];
        else {
            operators[k] = str[i];
  
            // Evaluation of 1st operator
            if (k == 1) {
                if (operators[k - 1] == '+')
                    ans += stol(operands[k - 1]) + stol(operands[k]);
  
                if (operators[k - 1] == '-')
                    ans += stol(operands[k - 1]) - stol(operands[k]);
            }
  
            // Evaluation of 2nd operator
            if (k == 2) {
                if (operators[k - 1] == '+')
                    ans1 += ans + stol(operands[k]);
  
                if (operators[k - 1] == '-')
                    ans1 -= ans - stol(operands[k]);
            }
  
            // Evaluation of 3rd operator
            if (k == 3) {
                if (operators[k - 1] == '+')
                    ans2 += ans1 + stol(operands[k]);
  
                if (operators[k - 1] == '-')
                    ans2 -= ans1 - stol(operands[k]);
            }
            k++;
        }
    }
  
    // If the LHS result is equal to the RHS
    if (ans2 == stol(operands[4]))
        return true;
    else
        return false;
}
  
// Driver code
int main()
{
    string str = "2+5+3+1=11";
    if (isValid(str))
        cout << "Valid";
    else
        cout << "Invalid";
  
    return 0;
}


Python3
# Python3 implementation of the approach 
  
# Function that returns true if 
# the equation is valid 
def isValid(string) :
      
    k = 0; 
    operands = [""] * 5 ; 
    operators = [""] * 4 ; 
    ans = 0 ; ans1 = 0; ans2 = 0; 
    for i in range(len(string)) : 
  
        # If it is an integer then add 
        # it to another string array 
        if (string[i] != '+' and 
            string[i] != '=' and 
                string[i] != '-') :
            operands[k] += string[i]; 
        else : 
            operators[k] = string[i]; 
  
            # Evaluation of 1st operator 
            if (k == 1) : 
                if (operators[k - 1] == '+') : 
                    ans += int(operands[k - 1]) + int(operands[k]); 
  
                if (operators[k - 1] == '-') :
                    ans += int(operands[k - 1]) - int(operands[k]); 
  
            # Evaluation of 2nd operator 
            if (k == 2) :
                if (operators[k - 1] == '+') :
                    ans1 += ans + int(operands[k]); 
  
                if (operators[k - 1] == '-') :
                    ans1 -= ans - int(operands[k]); 
              
  
            # Evaluation of 3rd operator 
            if (k == 3) : 
                if (operators[k - 1] == '+') :
                    ans2 += ans1 + int(operands[k]); 
  
                if (operators[k - 1] == '-') :
                    ans2 -= ans1 - int(operands[k]); 
            k += 1
  
    # If the LHS result is equal to the RHS 
    if (ans2 == int(operands[4])) :
        return True; 
    else :
        return False; 
  
  
# Driver code 
if __name__ == "__main__" : 
  
    string = "2 + 5 + 3 + 1 = 11"; 
    if (isValid(string)) :
        print("Valid"); 
    else :
        print("Invalid"); 
          
# This code is contributed by Ryuga


输出:
Valid
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”