| | |
Creating Calulator in C
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I am starting to develop a calculator in C++ which accepts the expression in the form:
2+3 or
2/3 ,etc. ( for the time being, only two operands and one operator) Once i get that done, I will begin the process of continuely expanding it
The problem I am having is, that I read the book by Stroustrup, and there was an entire chap on it on Low level input. And since I have read that, my brain has gotten trapped and I continuely am adopting the same procedure to make my own program.
Here is the code:
Any help would be greatly appreciated
2+3 or
2/3 ,etc. ( for the time being, only two operands and one operator) Once i get that done, I will begin the process of continuely expanding it

The problem I am having is, that I read the book by Stroustrup, and there was an entire chap on it on Low level input. And since I have read that, my brain has gotten trapped and I continuely am adopting the same procedure to make my own program.
Here is the code:
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; enum Type_exp{ PRINT,NUM, PLUS='+',MINUS='-',DIV='/',MUL='*',SPACE,END}; Type_exp curr_exp=END; double num; Type_exp getexp(); double oper(); int main() { double ans; cout<<"Enter the expression to evaluate"; ans=oper(); cout<<endl<<endl<<ans; return 0; } Type_exp getexp() { char ch=0; cin>>ch; switch(ch) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 0: cin.putback(ch); cin>>num; return curr_exp=NUM; case '+': return curr_exp=PLUS; case '-': return curr_exp=MINUS; case '/': return curr_exp=DIV; case '*': return curr_exp=MUL; case '\n': case ';': return curr_exp=PRINT; } } double oper() { getexp(); double left; while(curr_exp!=PRINT) { switch(curr_exp) { case PRINT: return left; case NUM: left=num; getexp(); break; case PLUS: getexp(); while(curr_exp==SPACE) getexp(); left+=num; getexp(); break; case MINUS: getexp(); while(curr_exp==SPACE) getexp(); left-=num; getexp(); break; case DIV: getexp(); while(curr_exp==SPACE) getexp(); left/=num; getexp(); break; case MUL: getexp(); while(curr_exp==SPACE) getexp(); left*=num; getexp(); break; } } }
" Life is like a box of chocolates, you never know what you are going to get " - Forrest Gump
•
•
•
•
I am starting to develop a calculator in C++ which accepts the expression in the form:
2+3 or
2/3 ,etc. ( for the time being, only two operands and one operator) Once i get that done, I will begin the process of continuely expanding it
•
•
•
•
The problem I am having is, that I read the book by Stroustrup, and there was an entire chap on it on Low level input. And since I have read that, my brain has gotten trapped and I continuely am adopting the same procedure to make my own program.
Are you another one that didn't read the post titled Read Me: Read This Before Posting? Don't leave it to us to figure out what's wrong. You need to tell us.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
Good idea.
What the heck does that mean?
Are you another one that didn't read the post titled Read Me: Read This Before Posting? Don't leave it to us to figure out what's wrong. You need to tell us.
I hope this clears things up.
" Life is like a box of chocolates, you never know what you are going to get " - Forrest Gump
•
•
Join Date: Jul 2005
Posts: 1,758
Reputation:
Solved Threads: 283
Input the whole expression as a string using the getline() method appropriate for the style of string you are using. Then parse the input expression into tokens using a stringstream. First create the stringstream by using the input string as a parameter to the stringstream constructor. Then use the >> operator to separate the tokens.
•
•
•
•
I guess i was in a hurry while writing this post, so i did mess up. Heading should have been Calculater in C++. Anyways, the code that i posted is not working and I can't seem to figure out whats going wrong. the same approach was used in the book, thats what i actually meant. I just twisted it a little to suit my needs.
I hope this clears things up.
•
•
•
•
The main problem is inputting the expression...I need help on how to input the expression without the need to press enter after each operand or operator...Any help would be gr8!
getline() . If you can't use stringstream, look at each individual character and process it as you need (digits will become operands, nondigits should be operators) The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Feb 2008
Posts: 14
Reputation:
Solved Threads: 2
Please note that case 1: case 2: case 3: case 4: case 5: don't do what you intend.
You want to use case '1':case '2': '1' '2' being chars.
Ok, I stand corrected. cin does parse numbers. case 1: will work. However it parses the entire number that will fit into a char.
input: 123
cin >> ch;
ch == 123, not 1
Also you are better off using an if/else if condition block rather than a switch() in the tokenizer. The primary reason for if/else if is the flexibility it offers when you need to read with peek ahead for more complex expressions. Consider "-1**2" and "2 - -1", etc.
You want to use case '1':case '2': '1' '2' being chars.
Ok, I stand corrected. cin does parse numbers. case 1: will work. However it parses the entire number that will fit into a char.
input: 123
cin >> ch;
ch == 123, not 1
Also you are better off using an if/else if condition block rather than a switch() in the tokenizer. The primary reason for if/else if is the flexibility it offers when you need to read with peek ahead for more complex expressions. Consider "-1**2" and "2 - -1", etc.
Last edited by TimeFractal; Feb 11th, 2008 at 6:02 pm. Reason: cin parses numbers
![]() |
Other Threads in the C++ Forum
- Previous Thread: Classes
- Next Thread: c++ fullscreen in vista
Views: 1428 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






