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

Monday, July 2, 2007

C++ Sample Calculator Program version 2


// calcltr-02.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.
//
// Mods
// 01 Change command short cuts to alpha characters so that they are simple to
// key. Trivial change.
// 02 Add extra functions again trivial.
// Planned mods
// 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.
//


#include
#include
#include
#include
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

display_menu();
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"
<< " (A)add, (S)ubtract X(mult), (D)ivide , (N)egative,\n"
<< " (R)eciprocal, (I)nitialize, (P)ower, Q)uit,\n"
<< " (H)elp show this menu again.\n\n";
}

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

/*A typical run:

Calculator functions:\n"
(A)add, (S)ubtract X(mult), (D)ivide , (N)egative,\n"
(R)eciprocal, (I)nitialize, (P)ower, Q)uit,\n"
(H)elp show this menu again.\n\n";

I 2.5
2.5
P 3
15.625
R
0.064
x 100
6.4
n
-6.4
a 10
3.6
s 1.12
2.48
d 2.5
0.992
i 4
4
p 0.5
2
n
-2
p 0.5
UNPREDICTABLE SEE LATER VERSION
q

------------------------

Note you do not need to leave space,
e.g. you can enter "d 2.5" or "d2.5"

*/

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.