Created
August 27, 2017 04:57
-
-
Save karthikkondagalla/12156d0f98dfc3114499722b3af8d09f to your computer and use it in GitHub Desktop.
Calculator Program in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "calculator.h" | |
int main() | |
{ | |
string token; | |
stack<double> S; | |
while(getline(cin, token)) | |
{ | |
process_token(token, S);//function call for the process_token function | |
} | |
return(0); | |
} | |
/****************************************************** | |
Function: process_token | |
Arguments: const string& token, stack<double>& S | |
Description: Reads and processes tokens | |
Return: None | |
*******************************************************/ | |
void process_token(const string& token, stack<double>& S) | |
{ | |
char c; | |
istringstream iss(token); | |
string itoken; | |
bool flag = false; | |
while(iss>>itoken) | |
{ | |
for(unsigned i=0;i<itoken.length();i++) | |
{ | |
c = itoken[i]; | |
flag = floatPoint(c, itoken, i);//function call for the floatPoint function | |
if(c == '=') | |
{ | |
printResult(S);//function call for the printResult function. | |
} | |
else if(c == 'c') | |
{ | |
emptyStack(S);//function call for emptyStack function | |
} | |
else if((isdigit(c)) || (unarySign(c, itoken, i)) || (flag)) | |
{ | |
i++; | |
S.push(getNumber(c, itoken, i, flag));//function call for the getNumber function | |
} | |
else if(isValidOperator(c)) | |
{ | |
S.push(operation(c, popStack(S), popStack(S))); | |
} | |
else | |
{ | |
cerr<<"Error : "<<c<<" is invalid"<<endl; | |
} | |
} | |
} | |
} | |
/************************************************* | |
Function: isValidOperator | |
Arguments: const char& c | |
Description: Returns true if char c is valid operator | |
Return: True if char c is valid operator | |
**************************************************/ | |
bool isValidOperator(const char& c) | |
{ | |
switch(c) | |
{ | |
case '+': | |
case '-': | |
case '*': | |
case '/': return(true); | |
} | |
return(false); | |
} | |
/*************************************************** | |
Function: printResult | |
Arguments: const stack<double>& S | |
Description: Prints top number in S | |
Return: None | |
****************************************************/ | |
void printResult(const stack<double>& S) | |
{ | |
bool st = false; | |
st = S.empty(); | |
if(st) | |
{ | |
cerr<<"Error : Stack is Empty"<<endl; | |
} | |
else | |
{ | |
cout<<setw(10)<<fixed<<setprecision(2)<<S.top()<<endl; | |
} | |
} | |
/*************************************************** | |
Function: popStack | |
Arguments: stack<double>& S | |
Description: Checks if stack is empty and returns number at top of S | |
Return: Number at top of stack | |
*****************************************************/ | |
double popStack(stack<double>& S) | |
{ | |
double t = 0; | |
bool st = false; | |
st = S.empty(); | |
if(st) | |
{ | |
cerr<<"Error: Stack is empty"<<endl; | |
} | |
else | |
{ | |
t = S.top(); | |
S.pop(); | |
} | |
return(t); | |
} | |
/*************************************************** | |
Function: emptyStack | |
Arguments: stack<double>& S | |
Description: Empties the stack by popping all the elements | |
Return: None | |
****************************************************/ | |
void emptyStack(stack<double>& S) | |
{ | |
bool st = false; | |
st = S.empty(); | |
while(false == st) | |
{ | |
S.pop(); | |
st = S.empty(); | |
} | |
} | |
/*************************************************** | |
Function: operation | |
Arguments: const char& c, const double& x, const double& y | |
Description: Returns result after applying operator c on x and y | |
Return: double | |
****************************************************/ | |
double operation(const char& c, const double& x, const double& y) | |
{ | |
if(c == '+') | |
{ | |
return(x+y); | |
} | |
if(c == '-') | |
{ | |
return(x-y); | |
} | |
if(c == '*') | |
{ | |
return(x*y); | |
} | |
if(c == '/') | |
{ | |
return(x/y); | |
} | |
return(0); | |
} | |
/********************************************************* | |
Function: unarySign | |
Arguments: const char& c, const string& token, const unsigned& i | |
Description: Returns true if char is valid unary operator | |
Return: bool | |
***********************************************************/ | |
bool unarySign(const char& c, const string& token, const unsigned& i) | |
{ | |
if((c == '+') || (c == '-')) | |
{ | |
if((i < token.length()-1) && ((isdigit(token.at(i+1))) || (token.at(i+1) == '.'))) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
/*********************************************************** | |
Function: floatPoint | |
Arguments: const char& c, consttring& token, const unsigned& i | |
Description: Returns true if char is valid floating point | |
Return: bool value | |
*************************************************************/ | |
bool floatPoint(const char& c, const string& token, const unsigned& i) | |
{ | |
if(c == '.') | |
{ | |
if((i < token.length()-1) && (isdigit(token.at(i+1)))) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
/************************************************************** | |
Function: getNumber | |
Arguments: const char& c, const string& token, unsigned& i, const bool& flag | |
Description: Returns the processed substring after validation | |
Return: double | |
****************************************************************/ | |
double getNumber(const char& c, const string& token, unsigned& i, const bool& flag) | |
{ | |
int f=0; | |
unsigned index=0,j=0; | |
char word[token.length()]; | |
memset(word, 0, token.length()); | |
word[index++] = c; | |
if(flag == true) | |
{ | |
f++; | |
} | |
for(j = i;j<token.length();j++) | |
{ | |
if((token[j] == '.')) | |
{ | |
if(false == floatPoint(token[j], token, j)) | |
{ | |
f = FLOAT_ERROR; | |
break; | |
} | |
else | |
{ | |
f++; | |
} | |
} | |
else if(false == isdigit(token[j])) break; | |
} | |
if(f >= FLOAT_ERROR) | |
{ | |
cerr<<"Error : Invalid Floating point number "<<token<<endl; | |
return(0); | |
} | |
else | |
{ | |
for(unsigned int x=i;x<=j;x++) | |
{ | |
word[index++] = token[x]; | |
} | |
word[index] = '\0'; | |
i = j-1; | |
string str(word); | |
return(stringToDouble(str)); | |
} | |
} | |
/************************************************************** | |
Function: stringToDouble | |
Arguments: string& str | |
Description: Converts the string to double | |
Return: double | |
****************************************************************/ | |
double stringToDouble(string& str)//I used this function because when i directly used stod function I did not get the double number with floating precision....if i converted 3 I needed 3.00 but it gave 3 back in number..so I used this where I used another double variable to catch that and I got the correct output. | |
{ | |
double d=stod(str); | |
return(d); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef H_TEST1 | |
#define H_TEST1 | |
#include "/home/cs689/common/689.h" | |
#endif | |
#define FLOAT_ERROR 2 | |
double popStack(stack<double>& S); | |
double operation(const char& c, const double& x, const double& y); | |
double getNumber(const char& c, const string& token, unsigned& i, const bool& floatPointFlag); | |
double stringToDouble(string& str); | |
void process_token(const string& token, stack<double>& S); | |
void emptyStack(stack<double>& S); | |
void printResult(const stack<double>& S); | |
bool isValidOperator(const char& c); | |
bool unarySign(const char& c, const string& token, const unsigned& i); | |
bool floatPoint(const char& c, const string& token, const unsigned& i); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment