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. :)