Simple Calc: Problem 1

Hey guys,so I hav been learning c++ off time for 2 years,and in this time I suppose I hav learnt the basics of the language enough to start some projects of my own.

So I hav decided to start by making a calculator,called Simple Calc.
Heres how its supposed to work:
1)Take a string as a input
2) Pass it to Parser class,which has a string member
3) In Parser class,the class object parses through the elements of the string,giving appropriate results
4) Return the result as output

Hers my Parser.hpp
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

#ifndef PARSER_HPP_INCLUDED
#define PARSER_HPP_INCLUDED

#include<iostream>
#include<sstream>
#include<fstream>
#include"Global_Defines.hpp"

using namespace std;

class Parser
{
private:
    string Input;
    stringstream Sio;
    double dInput;
    signed long lInput;
    //functions here...
    string GetValBtwBrackts();
    int GetNumbrBtwBrackts();

public:
    Parser()
    {
     dInput=0.0;
     lInput=0;
    }
    ~Parser(){ delete this; }
    inline void getInput( string &s )
    {
     this->Input=s;
    }
    string putInput()
    { return this->Input; }

    double Parse();


};


#endif // PARSER_HPP_INCLUDED



Some of the functions are defined here :

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

#ifndef GLOBAL_DEFINES_HPP_INCLUDED
#define GLOBAL_DEFINES_HPP_INCLUDED

#include<ctype.h>
#include<sstream>

using std::string;
using std::stringstream;
using std::istringstream;
using std::ostringstream;

const char PlusOperator= '+';
const char MinusOperator = '-';
const char MultiplyOperator = '*';
const char DivideOperator = '/';

const char Bracket1L = '(';
const char Bracket1R = ')';

inline bool IsABracket1L(string &s,int pos)
{
  if(s[pos]==Bracket1L)
   { return true; }
  else
    return false;
return false;
}

inline bool IsABracket1R(string &s,int pos)
{
  if(s[pos]==Bracket1R)
   { return true; }
  else
    return false;
return false;
}

inline bool IsAPlus(string &s,int pos)
{
 if(s[pos]==PlusOperator)
   { return true; }
 else
  { return false; }
return false;
}

inline bool IsAMinus(string &s,int pos)
{
 if(s[pos]==MinusOperator)
   { return true; }
 else
  { return false; }
return false;
}

inline int IntGrtrOfThe2(int a,int b)
{
  if(a>b)
  {
    return a;
  }
  else if(b>a)
  {
    return b;
  }
  else
    { return a; }
 return a;
}

inline bool IsAMultiply(string &s,int pos)
{
 if(s[pos]==MultiplyOperator)
   { return true; }
 else
  { return false; }
return false;
}

inline bool IsADivider(string &s,int pos)
{
 if(s[pos]==DivideOperator)
   { return true; }
 else
  { return false; }
return false;
}

inline int Strng2Int(string &s)
{
  istringstream istr(s);
  int ret = 0;

  istr>>ret;
  if(istr.fail()||istr.eof())
  { return 0; }

  return ret;
}

inline float Strng2Float(string &s)
{
  istringstream istr(s);
  float ret = 0;
  istr>>ret;
  if(istr.fail()||istr.eof())
  { return 0; }

  return ret;
}

inline string Int2Strng(int &a)
{
  ostringstream ostr;
  ostr<<a;

  return ostr.str();
}

inline string Float2Strng(float &b)
{
  ostringstream ostr;
  ostr<<b;
  return ostr.str();
}

inline bool IsaInt(string &s,int pos)
{
  if(isdigit(s[pos]))
  { return true; }
  else
  { return false; }
  return false;
}

inline bool IsaFloat(string &s,int pos)
{
  //Note : This function needs improvement
  // Check if first postion is a digit AND Second position is a digit OR
  // is a dot ( '.' ) AND pos+2 is a digit

  //Stress test
  // 4.56
  // First is true,second is true in the OR part
 if( (isdigit(s[pos])) && ( isdigit(s[pos+1]) || (s[pos+1]=='.' && isdigit(s[pos+2]) ) ) )
  {
   return true;
  }
  else
   return false; //Needs improvement here
  return false;
}

inline int FindNxtInt(string &s)
{
  unsigned int l = s.size();
  unsigned int pos,i;
  for( i=0; i<s.size();i++)
    {
      if(IsaInt((string&)s,i))
       { pos = i; break;}
       if( i == l )
        { break; }
    }
  for( i = pos ; i<s.size() ; i++)
    {
      if(i == s.size() )
       {  pos = 9876; break; }

      if(pos == i)
       { pos = 5432; break; }

      if( IsaInt((string&)s[i],i) )
      {
        pos=i;
        break;
      }
    }
  return pos;
}

inline char FindNxtOps(string &s)
{
  char ret=0;
  unsigned int i;
  for( i=0; i<s.size(); i++ )
  {
    if(IsAPlus((string&)s,i))
     {
       ret = '+';
       return PlusOperator;
     }
    else if(IsAMinus((string&)s,i))
    {
      ret = '-';
      return MinusOperator;
    }
    else if(IsAMultiply((string&)s,i))
    {
      ret = '*';
      return MultiplyOperator;
    }
    else if(IsADivider((string&)s,i))
    {
      ret ='/';
      return DivideOperator;
    }
  }
 return ret;
}
#endif // GLOBAL_DEFINES_HPP_INCLUDED


And heres my main.cpp :

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

#include<iostream>
#include"Global_Defines.hpp"
#include"Parser.hpp"

using namespace std;

int main()
{
cout<<"This is a prototype for my calculator...."<<endl
    <<"Type a calculation to proceed..."<<endl;
string input;
signed int output;

Parser *p = new Parser;

cout<<"Type the commands : \n";
cin>>input;
p->getInput(input);

output = (int) p->Parse();

cout<<"The result is : "<<output<<endl;
cout<<"Thank you for trying this out !!! \n";

return 0;
}


When I try to compile the project, this is what i get

....
Type the commands :
9+1
Starting Parse()
In condition 2 of for loop
The result is : 0
....

I hav given couts in the code to check the flow of the program.Whatever I give as input ,the result is always zero.

I hav omitted some of the code which ill post later.

Pls help me in showing where/how its wrong in the code.
Thnx in advance. :)


Heres my Parser.cpp :

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

#include"Parser.hpp"
#include"Global_Defines.hpp"

using namespace std;


string Parser :: GetValBtwBrackts()
{
  string subs;
  string temps1;
  int temppos1,temppos2;
  temps1 = this->Input;
  for(unsigned int i;i<this->Input.size();i++)
   {
//First Check if theres a first bracket or not
     if(Input[i]==Bracket1L)
      {
       temppos1 =i;
      }
   }

    //This lines in brackets are for finding the closing first bracket
   for(unsigned int j=temppos1;j<temps1.size();j++)
    {
       if(IsABracket1R(temps1,j))
        {
          temppos2 = j;
        }
    }

          //Now substring the values between the brackets and operate on them

 subs=temps1.substr(temppos1,temppos2);
 return subs;
}

int Parser :: GetNumbrBtwBrackts()
{
  string workbench,intstr;
  char opsstr;
  unsigned int i,j;
  int tempos=0;
  int pos;
  signed int a,sum;

  workbench = this->GetValBtwBrackts();
  j=workbench.size();

  for(i=0;i<workbench.size();i++)
  {
   pos = i;
   string temp = workbench.substr(i,j);
   pos = FindNxtInt(temp);
   if((pos==9876) || (pos==5432) )
    { break; }
   else
     {
       intstr = workbench[pos];
       a = Strng2Int(intstr);
       opsstr=FindNxtOps(workbench);
       if(opsstr  == PlusOperator)
       { sum = sum + a; }
       else if(opsstr == MinusOperator)
         {
          int pos1 = FindNxtInt(workbench);
          if((pos==9876) || (pos==5432) )
           { break; }
          signed int b; b = Strng2Int((string&)workbench[pos1]);
          sum = sum + (a - b);
         }
       else if(opsstr == MultiplyOperator)
         {
          int pos1 = FindNxtInt(workbench);
          if((pos==9876) || (pos==5432) )
           { break; }
          signed int b; b=Strng2Int((string&)workbench[pos1]);
          sum = sum + (a * b);
         }
       else if(opsstr == DivideOperator)
         {
           int pos1 = FindNxtInt(workbench);
           if((pos==9876) || (pos==5432) )
            { break; }
           signed  int b; b=Strng2Int((string&)workbench[pos1]);
           if( b == 0 )
            { cout<<"Enormous Error; Aborting!!!"<<endl; return -1; }
           else
            {
              sum = sum + ( a / b );
            }
         }
     }

  }
  return sum;
}

double Parser :: Parse()
{
double Result=0.0, numBtwBrackts=0.0;
int length=this->Input.size();
int pos1;
string workbench;

cout<<"Starting Parse() \n";

for(int i = 0;i<length; i++)
 {
   if(Input[i]==Bracket1L)
    {
      cout<<"In condition 1 of for loop \n";
      Result = Result + this->GetNumbrBtwBrackts();
    }
   else if(Input[i]==PlusOperator)
    {
      cout<<"In condition 2 of for loop \n";
      workbench = Input.substr(i,length);
      pos1 = FindNxtInt(workbench);
      if((pos1==9876) || (pos1==5432) )
        { break; }
      signed int a; a = Strng2Int((string&)workbench[pos1]);
      Result = Result + a;
    }
    else if(Input[i]==MinusOperator)
    {
      cout<<"In condition 3 of for loop \n";
      workbench = Input.substr(i,length);
      pos1 = FindNxtInt(workbench);
      if((pos1==9876) || (pos1==5432) )
        { break; }
      signed int a; a = Strng2Int((string&)workbench[pos1]);
      Result = Result - a;
    }
    else if(Input[i]==MultiplyOperator)
    {
      cout<<"In condition 4 of for loop \n";
      workbench = Input.substr(i,length);
      pos1 = FindNxtInt(workbench);
      if((pos1==9876) || (pos1==5432) )
        { break; }
      signed int a; a = Strng2Int((string&)workbench[pos1]);
      Result =(Result * a );
    /*workbench = workbench.substr(pos1,length);
      int pos2; pos2 = FindNxtInt(workbench);
      if((pos==9876) || (pos==5432) )
        { break; }
      signed int b; b = Strng2Int(workbench[pos2]);
      Result = Result + (a*b); */
    }
    else if(Input[i]==DivideOperator)
    {
      cout<<"In condition 5 of for loop \n";
      workbench = Input.substr(i,length);
      pos1 = FindNxtInt(workbench);
      if((pos1==9876) || (pos1==5432) )
        { break; }
      signed int a; a = Strng2Int((string&)workbench[pos1]);
      Result = (Result / a);
    }

 }
 return Result;
}
Without looking at your code, there is a Bjarne Stroustrup book; I can't remember which one it is I hope another seasoned member can quote it. But one of the first programs you write is exactly what you are tring to accomplish, even more so because it allows you to declare varibales also. You may want to look that up, I am going to try and find which book now. :)


EDIT: Here is a link to the code to the chapter - http://www.stroustrup.com/Programming/Solutions/Ch7/e7-1.cpp

Focus your attention on the "Token" and "TokenStream" classes.
Last edited on
Topic archived. No new replies allowed.