c++ - 我无法让我的字符串方程解析器识别和合并负数
问题描述
我又回来了,并试图为此简化我的代码。本质上,我需要编写一个计算器程序来读取并输出答案。不需要 Pemdas。字符串输入的语法总是“x + y * z”。我目前的问题是我的程序不能正确处理负操作数。例如使用“4 * -5”给我 20。这是我的代码。
#include <iostream>
#include <string>
#include <list>
#include <cstdlib>
using namespace std;
float process_func(string x);
bool isPartOfNum(char x);
void std_calc();
int main() {
std_calc();
return 0;
}
/******************************************************
** Function: std_calc
** Description: loops through standard calculations
** Parameters: n/a
** Pre-Conditions:
** Post-Conditions:
******************************************************/
void std_calc() {
string x;
float f;
int y;
cout << "input a string" << endl;
cin.ignore();
getline(cin, x);
f = process_func(x);
cout << f << endl;
cout << "Go again? 1 for yes, 0 for no" << endl;
cin >> y;
if (y == 1)
std_calc();
}
/******************************************************
** Function: process_func
** Description: Takes an equation and splits into two lists
** Parameters: string
** Pre-Conditions: There is a single space between every number and operand
** Post-Conditions:
******************************************************/
float process_func(string x) {
int end_of_num = 0, start_of_num = -1;// used to find last index from num // used to hold start of each num
list <float> numList;
list <char> operList;
if (x.length() == 0) {
cout << "Empty string provided" << endl;
exit(0);
}
if (x.find_first_not_of("0123456789+*-/. ") != string::npos) {
cout << "Invalid input string" << endl;
exit(0);
}
if ((x.at(0) < 48 || x.at(0) > 57) && x.at(0) != '-') { //check if start of string doesnt have a number or negative symbol
cerr << "Improper input, use syntax: A + B" << endl;
exit(0);
}
x.append(" ");
for (int i = 0; i < x.length(); i++) {
if (isPartOfNum(x.at(i))) {
if (start_of_num == -1) {
start_of_num = i;
end_of_num = i;
}
end_of_num++;
}
else if (x.at(i) == ' ') {
numList.push_back(stof(x.substr(start_of_num, end_of_num)));
start_of_num = -1;
}
else if ((x.at(i) == '+' || x.at(i) == '-' || x.at(i) == '*' || x.at(i) == '/') && !isPartOfNum(x.at(i+1))) { //need to i++ in here to skip space
operList.push_back(x.at(i));
i++;
}
} //at this point both lists should be full of all needed pieces of info
return process_lists(numList, operList);
}
/******************************************************
** Function: isPartOfNum
** Description: checks if the character is a part of a number
** Parameters: char
** Pre-Conditions:
** Post-Conditions:
******************************************************/
bool isPartOfNum(char x) {
if ((x >= '0' && x <= '9') || ( x == '.'))
return true;
return false;
}
/*bool isPartOfNum(char x) {
if ((x >= '0' && x <= '9') || (x == '.') || (x == '-'))
return true;
return false;
}
*/
这里的问题与 isPartOfNum 以及我的数字如何读入列表有关。我已经检查过了,我的程序的数学部分不是问题,所以我从这里省略了它。如果 isPartOfNum 包含“-”,则负数有效,只要它们是数字的一部分。例如,“-5 * 4”给了我 20。但是,“5 - 4”使程序崩溃,因为它试图在“”上使用 stof(string x),这会导致无效的参数异常。如您所知,我将包含“-”的 isPartOfNum 注释掉了。
相反,如果 isPartOfNum 不包含'-',那么程序总是在数字开始时开始读取,即使它前面有一个负数。
解决方案
推荐阅读
- wkwebview - Swift WkWebView createWebArchiveData 对返回类型感到困惑
- python - 如何从一列中选择相似(相同或接近)的值?
- python - 我怎样才能只写文件名而不是写整个路径?
- c# - 统一的Inventorysystem得到Nullreference错误
- python - 将字典列表合并到一个列表中
- javascript - 如何将 html 上的 firebase 查询结果显示为列表?
- javascript - 错误 找不到绑定文件。VS 代码上的 JS/TS
- swagger - 如何在 OpenAPI / Swagger 中指定两个路径模板部分的示例
- javascript - 在 DOM 中添加新元素后,FabricJS 画布停止工作
- c# - 通过 Angular 11 应用程序和 .Net 4.7.2 Web API 正确使用 CAS 身份验证