计算数学表达式失败并显示"-"符号



我正在创建一个计算数学表达式的程序。它需要两个输入:一个整数和一个由空格分隔的数学表达式字符串。它应该返回与整数输入匹配的表达式的索引。

样本输入:

20
(2+2) (10+10) (3*5*6)

样本输出:

index 1

当使用减法运算符时,我的逻辑似乎失败了;减法评价";部分。所有其他运营商似乎都取得了成功。我想弄清楚我是否错过了什么。感谢您的帮助-谢谢。

失败的输入示例是:

-8
(2-2-2-2-2-2)

这是我的代码:

#include <iostream>
#include <algorithm>
#include <regex>
using namespace std;
int main() {

int input;
string w;

cin >> input;

// ws ignore spaces or "whitespace"
getline(cin >> ws, w);

int w_len = w.length();

// count total expressions in string
int exp = 1;
for (int i = 0; i <= w_len; i++) {
if (isspace(w[i])) {
exp++;
}
}

// create array to hold expressions
string w_arr[exp];

// populate s_arr elements with the expressions
int wcount = 0;
for (int i = 0; i <= w_len; i++) {
if (w[i] == ' ') {
wcount++;
i++; 
}
w_arr[wcount] += w[i];
}

int w_size = sizeof(w_arr) / sizeof(w_arr[1]);

string s;
int index = -1;

// start of the evaluation loop
for (int k = 0; k < w_size; k++) {
index++;
s = w_arr[k];


s.erase(remove(s.begin(),s.end(),'('), s.end());
s.erase(remove(s.begin(),s.end(),')'), s.end());

// count total punctuation
int count = 0;
for (size_t i = 0; i < s.length(); i++) {
if (ispunct(s[i])) {
count++;
}
}

// add spaces around operators
s = regex_replace(s, regex("\+"), " + ");
s = regex_replace(s, regex("\*"), " * ");
s = regex_replace(s, regex("\/"), " / ");
s = regex_replace(s, regex("\-"), " - ");

int count2 = 0;
int size = count + count + 1;
string z[size];

// create array z[] from input string
for (size_t i = 0; i < s.length(); i++) {
if (s[i] == ' ') {
count2++;
i++;
}
z[count2] += s[i];
}

// multiplication evaluation
string res;
mulStart:
for (int j = 0; j <= size; j++) {
if (z[j] == "*") {
int mul1 = stoi(z[j-1]);
int mul2 = stoi(z[j+1]);
int mul = mul1 * mul2;
res = to_string(mul);

// overwrite expression with result
z[j-1] = res;
for (int i = j; i<=size-3; i++) {
z[i] = z[i+2];
}

// replace end of array with blanks
for (int i = size-2; i<size; i++) {
z[i] = " ";
}
}
}

// check for other mul operators
size = sizeof(z) / sizeof(z[0]);
int mulCount = 0;
for (int i = 0; i <= size; i++) {
if (z[i] == "*") {
mulCount++;
}
}
if (mulCount > 0) {
goto mulStart;
}

// division evaluation
divStart:
for (int j = 0; j <= size; j++) {
if (z[j] == "/") {
int div1 = stoi(z[j-1]);
int div2 = stoi(z[j+1]);
int div = div1 / div2;
res = to_string(div);

// overwrite expression with result
z[j-1] = res;
for (int i = j; i<=size-3; i++) {
z[i] = z[i+2];
}

// replace end of array with blanks
for (int i = size-2; i<size; i++) {
z[i] = " ";
}
}
}

// check for other div operators
size = sizeof(z) / sizeof(z[0]);
int divCount = 0;
for (int i = 0; i <= size; i++) {
if (z[i] == "/") {
divCount++;
}
}
if (divCount > 0) {
goto divStart;
}

// addition evaluation
addStart:
for (int j = 0; j <= size; j++) {
if (z[j] == "+") {
int add1 = stoi(z[j-1]);
int add2 = stoi(z[j+1]);
int add = add1 + add2;
res = to_string(add);

// overwrite expression with result
z[j-1] = res;
for (int i = j; i<=size-3; i++) {
z[i] = z[i+2];
}


// replace end of array with blanks
for (int i = size-2; i<size; i++) {
z[i] = " ";
}
}
}

// check for other add operators
size = sizeof(z) / sizeof(z[0]);
int addCount = 0;
for (int i = 0; i <= size; i++) {
if (z[i] == "+") {
addCount++;
}
}
if (addCount > 0) {
goto addStart;
}

// subtraction evaluation
subStart:
for (int j = 0; j <= size; j++) {
if (z[j] == "-") {
int sub1 = stoi(z[j-1]);
int sub2 = stoi(z[j+1]);
int sub = sub1 - sub2;
res = to_string(sub);

// overwrite expression with result
z[j-1] = res;
for (int i = j; i<=size-3; i++) {
z[i] = z[i+2];
}

// replace end of array with blanks
for (int i = size-2; i<size; i++) {
z[i] = " ";
}
}
}

// check for other sub operators
size = sizeof(z) / sizeof(z[0]);
int subCount = 0;
for (int i = 0; i <= size; i++) {
if (z[i] == "-") {
subCount++;
}
}
if (subCount > 0) {
goto subStart;
}


// print out index result
if (stoi(z[0]) == input) {
cout << "index " << index << endl;
break;
}

// print "none" otherwise
if (k == w_size - 1) {
cout << "none" << endl;
}

}

return 0;
}

"//检查其它子运算符";这应该给你一个线索。您已经对所有输入进行了迭代,但不知何故-"剩下的,你需要再做一次。但是你可以计算"-"发生故障的

问题是,即使找到-,也要提前索引j。通过删除一个-,下一个-将取代您删除的那个。然后你就跳过了。

举个例子,我对-进行了编号,以显示它们在循环时是如何移动的:

"2-2-2-2-2-2" becomes:
"2" "-"1 "2" "-"2 "2" "-"3 "2" "-"4 "2" "-"5 "2"
^
"2" "-"1 "2" "-"2 "2" "-"3 "2" "-"4 "2" "-"5 "2"
^
"0" "-"2 "2" "-"3 "2" "-"4 "2" "-"5 "2"
^
"0" "-"2 "2" "-"3 "2" "-"4 "2" "-"5 "2"
^ 
"0" "-"2 "0" "-"4 "2" "-"5 "2"
^ 
"0" "-"2 "0" "-"4 "2" "-"5 "2"
^ 
"0" "-"2 "0" "-"4 "0"

然后进入subStart。

/也应该存在同样的问题。

更糟糕的是,你在/之前做*。那么4 * 9 / 2 * 3会发生什么呢?与+-:1 + 2 - 3 + 4相同。

最新更新