我正在创建一个评估数学表达式的程序。它需要两个输入:一个整数和一个空间分隔的数学表达式的字符串。它应该返回与整数输入匹配的表达式的索引。
样本输入:
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;
}发布于 2022-06-06 01:01:36
"//检查其他子操作符“,这应该给您一个提示。您已经迭代了所有的输入,但似乎有"-“之后,您需要再次这样做。但你可以计算出"-“无序。
问题是,即使在找到一个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也是如此。
https://stackoverflow.com/questions/72511021
复制相似问题