我无法获取字符串方程式解析器来识别和合并负数

时间:2018-11-03 20:30:02

标签: c++ string g++

我又回来了,试图为此简化我的代码。本质上,我需要编写一个计算器程序来读取并给出答案。不需要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不包含'-',则即使数字前面有一个负数,程序也总是在数字开始时开始读取。

0 个答案:

没有答案