Calculator/Parsing/Tokens

Hello. I am start coding a calculator in the console now. It will be need for me later in my GUI app. I am stuck in method which convert tokens to RPN stack.
I have error: "No match for 'operator=='".


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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>

using namespace std;

class Token
{
    friend class MathFunctions;
  private:
   string TokenName;
   int priority;
   bool Operator;
   bool Number;
   bool Symbol;
  public:
   int GetPriority(string ch){return priority;};
   bool GetAssociative(string ch);
   bool IsOperator(string ch);
   bool IsNumber(string ch);
   bool IsVariable(string ch);
   bool IsFunction(string ch);
   bool IsSymbol(string ch);
   Token(){};
   Token(string Name,bool Ope, bool Num, bool Sym){TokenName=Name,Operator=Ope,Number=Num,Symbol=Sym;};
};

bool Token::IsNumber(string ch)
{
    string numbers="0123456789";
    for(int i=0; i<10; i++)
    {
        if(ch==numbers[i])
           return true;
    }
    return false;
}

bool Token::IsOperator(string ch)
{
   string operators="+-*/";
   for(int i=0; i<4; i++)
   {
       if(ch==operators[i])
        return true;
   }
   return false;
}

class MathFunctions
{
   private:
      vector<Token> ToRPN(vector <Token>);
   public:
      MAathFunctions();
      int Parse(string exp);
      bool Tokenization(string exp);
      bool Calculator();
};

bool MathFunctions::Tokenization(string exp)
{
    int j;
    string tmp;
    vector <Token> tokens;
  for(int i=0; i<exp.size(); i++)
  {
      if(isdigit(exp[i]))
      {
         tmp = exp[i];
         j=i+1;
         while(isdigit(exp[j]))
         {
            tmp += exp[j];
            j++;
         }
         cout<<tmp<<endl;
         Token tok(tmp,false,true,false);
         tokens.push_back(tok);
         i = j-1;
      }
      else if(exp[i]=='+'||exp[i]=='-'||exp[i]=='*'||exp[i]=='/')
      {
          cout<<exp[i]<<endl;
          tmp = exp[i];
         Token tok(tmp,true,false,false);
         tokens.push_back(tok);
      }
      else if(exp[i]=='('||exp[i]==')')
      {
         tmp = exp[i];
         Token tok(tmp,false,false,true);
         tokens.push_back(tok);
      }
  }
  for (vector<Token>::iterator it = tokens.begin() ; it != tokens.end(); ++it)
       cout <<" "<< (*it).TokenName;

}


MathFunctions::MathFunctions()
{}

vector<Token> MathFunctions::ToRPN(vector <Token> tok)
{
    vector <Token> RPN;
    vector <Token> Stack;

   for(vector<Token>::iterator it = tok.begin(); it != tok.end(); ++it)
      {
         if((*it).IsNumber((*it).TokenName))
         {

         }
      }
}


int main(int argc, char * argv[])
{
    string exp;
    getline(cin,exp);

    return 0;
}

Here's a few compiler errors fixed so it runs.
BTW if you are comparing 2 strings you can do it without comparing them character by character.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>

using namespace std;

class Token
{
    friend class MathFunctions;
private:
    string TokenName;
    int priority;
    bool Operator;
    bool Number;
    bool Symbol;
public:
    int GetPriority(string ch){return priority;};
    bool GetAssociative(string ch);
    bool IsOperator(string ch);
    bool IsNumber(string ch);
    bool IsVariable(string ch);
    bool IsFunction(string ch);
    bool IsSymbol(string ch);
    Token(){};
    Token(string Name,bool Ope, bool Num, bool Sym){TokenName=Name,Operator=Ope,Number=Num,Symbol=Sym;};
};

bool Token::IsNumber(string ch)
{
    string numbers="0123456789";
    for(int i=0; i<10; i++)
    {
        if(ch[i]==numbers[i]) // <--
            return true;
    }
    return false;
}

bool Token::IsOperator(string ch)
{
    string operators="+-*/";
    for(int i=0; i<4; i++)
    {
        if(ch[i]==operators[i]) // <--
            return true;
    }
    return false;
}

class MathFunctions
{
private:
    vector<Token> ToRPN(vector <Token>);
public:
    MathFunctions(); // <--
    int Parse(string exp);
    bool Tokenization(string exp);
    bool Calculator();
};

bool MathFunctions::Tokenization(string exp)
{
    int j;
    string tmp;
    vector <Token> tokens;
    for(int i=0; i<exp.size(); i++)
    {
        if(isdigit(exp[i]))
        {
            tmp = exp[i];
            j=i+1;
            while(isdigit(exp[j]))
            {
                tmp += exp[j];
                j++;
            }
            cout<<tmp<<endl;
            Token tok(tmp,false,true,false);
            tokens.push_back(tok);
            i = j-1;
        }
        else if(exp[i]=='+'||exp[i]=='-'||exp[i]=='*'||exp[i]=='/')
        {
            cout<<exp[i]<<endl;
            tmp = exp[i];
            Token tok(tmp,true,false,false);
            tokens.push_back(tok);
        }
        else if(exp[i]=='('||exp[i]==')')
        {
            tmp = exp[i];
            Token tok(tmp,false,false,true);
            tokens.push_back(tok);
        }
    }
    for (vector<Token>::iterator it = tokens.begin() ; it != tokens.end(); ++it)
    cout <<" "<< (*it).TokenName;
    
    
    return false; // <-- or whatever
}


MathFunctions::MathFunctions()
{}

vector<Token> MathFunctions::ToRPN(vector <Token> tok)
{
    vector <Token> RPN;
    vector <Token> Stack;
    
    for(vector<Token>::iterator it = tok.begin(); it != tok.end(); ++it)
    {
        if((*it).IsNumber((*it).TokenName))
        {
            
        }
    }
    
    return RPN; // <-- OR WHATEVER
}


int main(int argc, char * argv[])
{
    string exp;
    getline(cin,exp);
    
    return 0;
}
On line 36/47 you try to compare a string with a char. That's not allowed. Use one of find...() function for this. E.g.

http://www.cplusplus.com/reference/string/string/find_first_not_of/

1
2
3
4
bool Token::IsNumber(string ch)
{
    return (ch.find_first_not_of("0123456789") == std::string::npos);
}
And your isNumber function might be better like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

bool isNumber(std::string number_string)
{
    for(int i = 0; i < number_string.length(); i++)
    {
        if( !isdigit(number_string[i]) )
            return false;
    }
    return true;
}

int main()
{
    std::string str_1 {"12c"};
    std::cout << isNumber(str_1) << '\n';
    
    std::string str_2 {"123"};
    std::cout << isNumber(str_2) << '\n';
    
    return 0;
}



0
1
Program ended with exit code: 0
Topic archived. No new replies allowed.