Journal of computing related issues for Web Authoring, and general topics using Mac, Windows and Linux platforms.

Monday, July 2, 2007

C++ Calculator Sample Program

// calcltr.cpp
// Calculator program that runs in console.
// Type M for menu.
// This is the original from PSW book Using C++
// I have used it as a basis to learn C++
// I have kept is at the following modifications will
// be easier to understand as the program becomes more sophisticated.
// Planned mods
// 01 Change command short cuts to alpha characters so that they are simple to
// key. Trivial change.
// 02 Add extra functions again trivial.
// 03 Add Memory functions
// MEM IN .. loads memory with current value
// MEM PLUS .. adds current value to memory
// Note: Mem Minus achieved by typing n (negative) to make current
// value negative and MEM PLUS. If required redo n to make current
// value positive.
// MEM SWITCH switches current value and memory.
// 04 Add cacpability to use mem value in function, e.g. to subtract value in
// in memory from current value, but keep memory value.
// This was a bit complicated and required writing function to replace CIN
// and use of STRING to DOUBLE function
// 05 Improve by checking that values entered are numbers and not garbage.
// 06 Capability to undo up to last 5 commands. Code with Vector?
// 07 Log comands to file so that they can be audited.
// Have default log KALKLOG.DAT in C:|MyDATA which is backed up to
// KALKLOG.BAK start of new program, overwriting previous version.
// 08 Allow user to change log file name and locaton.
// 09 Take data file in LOG format and run as batch program.
// Also plan to use as test for compiling in Linux and with other systems.
// May try to convert to Java and Ruby??
// When I have learned widows programming may try to convert to windows type.
// I know it is all old hat but it helps me to learn and may even help others.

using namespace std;

void display_menu();
int is_menu_choice(char choice);
void process_choice (char choice, // IN
double& curr_val); // IN-OUT
int main()
char choice; // menu choice
double curr_val; // current value of calculator

cout << setprecision(15);
curr_val = 0;
do {
cin >> choice;
choice = toupper(choice);
process_choice (choice, curr_val);
while (choice != 'Q');
return 0;

void display_menu()
cout << "Calculator functions:\n"
<< " +(add), *(mult), ^(expo),\n"
<< " (I)nvert, (S)et, (Q)uit,\n"
<< " show this (M)enu again.\n\n";

int is_menu_choice(char choice)
switch (choice)
case '+': case '*': case '^':
case 'I': case 'S': case 'Q':
case 'M':
return 1;
return 0;
} // end switch

void process_choice (char choice, // IN
double& curr_val) // IN-OUT
double num;
switch (choice)
case '+': cin >> num;
curr_val += num;
case '*': cin >> num;
curr_val *= num;
case '^': cin >> num;
curr_val = pow(curr_val, num);
case 'I': curr_val = 1/curr_val;
case 'M': break;
case 'Q': break;
case 'S': cin >> curr_val;
default: cout << "\tInvalid operation!\n";
} // end switch
if (choice=='M') display_menu();
if (choice != ('Q' || 'M' ))
cout << setw(16) << ' ' << curr_val
<< endl;

/* A typical run:
Calculator functions:
+(add), *(mult), ^(expo),
(I)nvert, (S)et, (Q)uit,
show this (M)enu again.

S 2.5
^ 3
* 100

No comments:

About Me

Husband, dad and grandad. Physics graduate from London University in late 60s. Retired from IBM company in 2000 after 27 years as both Sytems Engineer and Salesman. Interests include photography, nature, science, maths, walking, travel. I like facts as a basis of opinion and not opinion that is assumed to be fact.