Error in lexar a mathematical equation

3

I am doing an equation interpreter in C ++ and am trying to show the type of the symbol. But I'm in trouble. I do not know what's wrong.

Any constructive tip is welcome.

main.cpp :

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

//disables any deprecation warning
#pragma warning(disable : 4996)

//usings
using std::vector;
using std::string;
using std::cout;
using std::endl;
using std::stringstream;

bool try_parse(const std::string& s)
{
    char* end = 0;
    double val = strtod(s.c_str(), &end);
    return end != s.c_str() && val != HUGE_VAL;
}

char first_char(string str) {
    return *str.c_str();
}


vector<string> tok_type(vector<string> vec) {
    for (int i = 0; i < vec.size(); i++) {
        string &s = vec[i];
        if (!try_parse(s) || first_char(s) != '&') {
            s = "<unknown> " + s;
            continue;
        }
        else if(!try_parse(s) || first_char(s) == '&'){
            s = "<operator> " + s;
            continue;
        }
        else if (try_parse(s) || first_char(s) != '&') {
            s = "<double> " + s;
            continue;
        }
    }
    return vec;
}

long double parse(string str) {
    return std::stold(str);
}

vector<string> split(string str, string token = " ") {
    vector<string>result;
    while (str.size()) {
        int index = str.find(token);
        if (index != string::npos) {
            result.push_back(str.substr(0, index));
            str = str.substr(index + token.size());
            if (str.size() == 0)result.push_back(str);
        }
        else {
            result.push_back(str);
            str = "";
        }
    }
    return result;
}

string simplify(string expr) {
    string iexpr = expr;
    for (int i = 0; i < iexpr.length(); i++) {

        char& c = iexpr[i];

        if (c == '+')
            iexpr.replace(i, 1, " &ad ");
        else if (c == '-')
            iexpr.replace(i, 1, " &sb ");
        else if (c == '*') 
            iexpr.replace(i, 1, " &mp ");
        else if (c == '/')
            iexpr.replace(i, 1, " &dv ");

    }
    return iexpr;
}

int main() {
    vector<string> sep_rep = tok_type(split(simplify("21+32-3*2")));
    for (auto str : sep_rep) {
        cout << str << endl;
    }

    std::cin.get();
    return 0;
}

The output I want is:

<double>21
<operator>&ad
<double>32
<operator>&sb
<double>3
<operator>&mp
<double>2 

But this is what I get ...

<unknown>21
<unknown>&ad
<unknown>32
<unknown>&sb
<unknown>3
<unknown>&mp
<unknown>2
    
asked by anonymous 19.06.2018 / 23:01

1 answer

3

Your mistake is silly. In parts where you have try_parse(s) and first_char(s) , you used || when you should use && .

I also recommend storing the results of try_parse(s) and first_char(s) in a variable of type bool so that you do not call those methods up to three times for the same string.

In my case, I also had to add a #include <cmath> to compile.

See here working on ideone.

    
20.06.2018 / 00:06