Convert string to equation ADT

I'm writing a function to convert a string to an equation ADT which has got two members: "grado" that is the grade of the equation and "coefficienti" that is a vector of int containing the coefficients of the equation.

For example: "9x^3-2x" ---> grado = 3, coefficienti = {0, 2, 0, 3}.

This is my code. It works correctly till the last for-loop where it only does one loop and then gives me a "SIGABRT" error.

Does anybody know how to fix it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
equazione convEquaz(string str) {
    equazione e;
    
    if (str.find_first_of("xX") == str.npos) {
        e.grado = 0;
        
        e.coefficienti.push_back(atof(str.c_str()));
    } else {
        if (str.find('^') == str.npos) {
            e.grado = 1;
            
            if (str.find_first_of("+-") == str.npos) {
                e.coefficienti.push_back(0);
                
                str.erase(str.find_first_of("xX"));
                
                e.coefficienti.push_back(atof(str.c_str()));
            } else {
                string str2 = str.substr(str.find_first_of("+-")+1);
                
                e.coefficienti.push_back(atof(str2.c_str()));
                
                str.erase(str.find_first_of("xX"));
                
                e.coefficienti.push_back(atof(str.c_str()));
            }
        } else {
            string str2(str.begin()+str.find('^')+1, str.begin()+str.find_first_of("+-"));
            
            e.grado = atof(str2.c_str());
            
            e.coefficienti.resize(e.grado+1);
            
            for (int i = 0; i < e.grado+1; i++) {
                string monomioFinale = str.substr(str.find_last_of("+-"));
                
                if (monomioFinale.find_first_of("xX") == monomioFinale.npos) {
                    e.coefficienti[0] = atof(monomioFinale.c_str());
                } else if (monomioFinale.find('^') == monomioFinale.npos) {
                    string str2 = monomioFinale.substr(0, monomioFinale.find_first_of("xX"));
                    
                    e.coefficienti[1] = atof(str2.c_str());
                } else {
                    string str2 = monomioFinale.substr(0, monomioFinale.find_first_of("xX"));
                    
                    string str3 = monomioFinale.substr(monomioFinale.find('^')+1);
                    
                    int index = atoi(str3.c_str());
                    
                    e.coefficienti[index] = atof(str2.c_str());
                }
                str.erase(str.find_last_of("+-"));
            }
        }
    }
    e.minimiTermini();
    return e;
}
You're calling:
 
string monomioFinale = str.substr(str.find_last_of("+-"));
where str is
9x^3
Thank you, kbw, you've been very helpful.
Topic archived. No new replies allowed.