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

Sunday, July 15, 2007

C++ Calculator Prog Mod 5 with input checking for valid number






// kalk-05.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.
// 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.
// 03a Added code to change negative current value to positive when
// Power function AND exponent is less than 1 (e.g. 0.5 for square root)
// as even roots of negative numbers are imaginary
// 04 Add capability 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.
//
// Planned mods
// 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 <iostream>
#include <iomanip>
#include <cmath>
#include <cctype>
#include <string>
using namespace std;

void display_menu();
// int is_menu_choice(char choice);
void process_choice (char choice, // IN
double& num, // IN-OUT
double& mem); // IN-OUT
void cin_number(double& num, // IN-OUT
double& mem); // IN-OUT
char RT_Num_Check(string& digit);

int main()
{
char choice; // menu choice
double curr_val; // current value of calculator
double mem; // Calculator Mem function

display_menu();
cout << setprecision(15);
curr_val = 0;
mem = 0;

do {
cin >> choice;
choice = toupper(choice);
process_choice (choice, curr_val, mem);
}
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 [positive only], Q)uit,\n"
<< " (M)em in, mem p(L)us, mem (O)ut, mem s(W)itch \n"
<< " use mem to refer to memory in calculations"
<< " (H)elp show this menu again.\n\n";
}

void process_choice (char choice, // IN
double& curr_val, // IN-OUT
double& mem) // IN-OUT

{
// double mem;
double work; // required for memory sWitch function
double num; // numeric value entered by user
num = 0;
// mem=0;
switch (choice)
{
case 'A': cin_number(num, mem);
curr_val += num;
break;
case 'D': cin_number(num, mem);
curr_val = curr_val/num;
break;
case 'H': break;
case 'I': cin_number(num, mem);
curr_val = num;
break;
case 'L': mem += curr_val;
break;
case 'M': mem = curr_val;
break;
case 'N': curr_val *= (-1);
break;
case 'O': curr_val = mem;
break;
case 'P': cin_number(num, mem);
if (num < 1) curr_val = fabs(curr_val);
//stops possibilty roots of negative values causing errors
curr_val = pow(curr_val, num);
break;
case 'Q': break;
case 'R': curr_val = 1/curr_val;
break;
case 'S': cin_number(num, mem);
curr_val -= num;
break;
case 'W': work = curr_val;
curr_val = mem;
mem = work;
break;
case 'X': cin_number(num, mem);
curr_val *= num;
break;
default: cout << "\tInvalid operation!\n";
} // end switch
if (choice=='H') display_menu();
if (choice != ('Q' || 'H' ))
cout << setw(16) << ' ' << curr_val
<< endl;
}

void cin_number(double& num,
double& mem) // IN-OUT
{
string input;
char ValidNumber = 'N';
while (ValidNumber == 'N')
{
cin >> input;
if (input == "mem")
{
num = mem;
ValidNumber = 'Y';
}
else
{
ValidNumber=RT_Num_Check(input);
if (ValidNumber=='Y')
num = strtod(input.c_str(), NULL);
else
{
cout << input <<" Not a proper number\n Re-enter number\n";
}
}
}
return;

}
char RT_Num_Check(string& digit)
{
char NumOK; // result
int p; // digit position
char decimal = 'N'; // used to make sure not more than 1 decimal point
string sign = "0"; // used to check for negative
// cout << "The length of str is " << digit.length() << " characters.\n";
if (digit.substr(0,1) == "-") // sign -1
{
digit=digit.substr (1,(digit.length()-1)); // strip sign
sign= "-"; // to check so that neg restored at end
}
for (p=0;p<digit.length();p++)
{

if ((digit.substr(p,1) >="0") && (digit.substr(p,1) <="9"))
NumOK='Y';
else
{
if ((digit.substr(p,1) ==".") && (decimal=='N'))
{
NumOK='Y';
decimal='Y'; // so that later positions checked for erraneous 2nd decimal point
}
else
{
NumOK='N'; // if we reach here must be an incorrect number
break;
}
}
// cout <<"p is " <<p<< " number is "<< digit.substr(p,1)<<endl;
}

if (sign == "-")
digit = sign+digit; // put back negative if necessary
return (NumOK);

}


/*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
H
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";

2.48
d 2.5
0.992
i 4
4
p 0.5
2
n
-2
p 0.5
1.4142135623731
i10
10
m
10
r
0.1
w
10
o
0.1
x1000
100
l
100
l
100
o
200.1
i 2
2
m
2
i5
5
amem
7
p mem
49
d mem
24.5
a 5..2
5..2 Not a proper number
Re-enter number
5.2
29.7
q

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

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

*/




Friday, July 6, 2007

Linux Live Boot on Notebook with Wireless Cont

Having got wireless to work on PCLinuxOS 2007 on notebook, see previous post, I then set about trying to get USB to work.

After some Google activity I found reference to NOAPIC and ACPI=OFF commands that you ca add as Linux boot parameters by pressing Tab key when CD gives various boot options. APIC effects how IRQs are shared out and ACPI controls power management. Various entries had suggested that they can cause problems.

Sure enough if I add EITHER the USB works as it should, however I cannot then get the wireless to work. When PCLinuxOS starts it goes into its network routines and now does not find driver for my wireless inbuilt into Fujitsu Siemens Amilo L7310GW. This it correctly recognises as
Atheros Communications 802.11b/g Wireless PCI Adapter.

Thursday, July 5, 2007

Linux Live Boot on Notebook with Wireless

I have over recent months been looking for best method of running Linux on my Fujitsu Siemens Amilo L7310GW notebook without changing hard drive as I am not confident enough yet to risk damaging working XP system. So I want to build live boot ultimately off USB 2GB Pen Drive.

So I have tried various live CD boots and except Mepis I have not been able to get wireless to work. Mepis was OK but I could not get it to run on a high resolution screen.

Yesterday I bought August edition of Linux Format magazine and tried the PCLinuxOS on the DVD. Booted up fine and after wireless set to correct WPA key connected OK. (I had to switch off IPv6 in Firefox, as I have to do with all new distros to get Firefox to work see http://en.opensuse.org/Disable_IPv6_for_Firefox
One day I will find out what IPv6 is and why I have to disable it!

Changed resolution to 1280 x 1024 in Control Centre. However when I right click desktop and check here it says resolution is 1024x768 which looks more correct.

Only problem is that I do not see USB Pendrive when I insert it.

PCLinuxOS MiniMe
This has instructions on how to use USB Pendrive to live boot from

My PCLinuxOS
This well written log appears to be written by person travelling along same path as me so I will check it out in detail, especially hgow he/she made Pendrive boot.

Tuesday, July 3, 2007

C++ Calculator Program v4.0 with Mem as Parameter



// kalk-04.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.
// 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.
// 03a Added code to change negative current value to positive when
// Power function AND exponent is less than 1 (e.g. 0.5 for square root)
// as even roots of negative numbers are imaginary
// 04 Add capability 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
//
// Planned mods
// 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
#include
using namespace std;

void display_menu();
// int is_menu_choice(char choice);
void process_choice (char choice, // IN
double& num, // IN-OUT
double& mem); // IN-OUT
void cin_number(double& num, // IN-OUT
double& mem); // IN-OUT


int main()
{
char choice; // menu choice
double curr_val; // current value of calculator
double mem; // Calculator Mem function

display_menu();
cout << setprecision(15);
curr_val = 0;
mem = 0;

do {
cin >> choice;
choice = toupper(choice);
process_choice (choice, curr_val, mem);
}
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 [positive only], Q)uit,\n"
<< " (M)em in, mem p(L)us, mem (O)ut, mem s(W)itch \n"
<< " use mem to refer to memory in calculations"
<< " (H)elp show this menu again.\n\n";
}

void process_choice (char choice, // IN
double& curr_val, // IN-OUT
double& mem) // IN-OUT

{
// double mem;
double work; // required for memory sWitch function
double num; // numeric value entered by user
num = 0;
// mem=0;
switch (choice)
{
case 'A': cin_number(num, mem);
curr_val += num;
break;
case 'D': cin_number(num, mem);
curr_val = curr_val/num;
break;
case 'H': break;
case 'I': cin_number(num, mem);
curr_val = num;
break;
case 'L': mem += curr_val;
break;
case 'M': mem = curr_val;
break;
case 'N': curr_val *= (-1);
break;
case 'O': curr_val = mem;
break;
case 'P': cin_number(num, mem);
if (num < 1) curr_val = fabs(curr_val);
//stops possibilty roots of negative values causing errors
curr_val = pow(curr_val, num);
break;
case 'Q': break;
case 'R': curr_val = 1/curr_val;
break;
case 'S': cin_number(num, mem);
curr_val -= num;
break;
case 'W': work = curr_val;
curr_val = mem;
mem = work;
break;
case 'X': cin_number(num, mem);
curr_val *= num;
break;
default: cout << "\tInvalid operation!\n";
} // end switch
if (choice=='H') display_menu();
if (choice != ('Q' || 'H' ))
cout << setw(16) << ' ' << curr_val
<< endl;
}

void cin_number(double& num,
double& mem) // IN-OUT
{
string input;
cin >> input;
if (input == "mem")
num = mem;
else
num = strtod(input.c_str(), NULL);
return;

}


/*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
H
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";

2.48
d 2.5
0.992
i 4
4
p 0.5
2
n
-2
p 0.5
1.4142135623731
i10
10
m
10
r
0.1
w
10
o
0.1
x1000
100
l
100
l
100
o
200.1
i 2
2
m
2
i5
5
amem
7
p mem
49
d mem
24.5
q

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

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

*/

C++ String to Double (strtod) function notes

As part of my ongoing exercise to write calculator in C++ I wanted to add capability of using memory content as parameter, e.g. add mem to current value.

Although I quickly determined that one solution was strtod function I had problems getting it to work until I read GID Forum at http://www.gidforums.com/t-1131.html.

I have cut and pasted relevent section here.

Garth Farley Garth Farley is offline
Invalid Email Address
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
The problem is that strtod() is a function that has been around since the original C. C didn't have strings as we know it (a class which contains the characters), instead strings were character arrays, with the end being denoted by the a NULL terminator (\0).

So, in plain C, to get a string, you'd have

char[] word = {'h', 'e', 'l', 'l', 'o', '\0'};

These were pretty dodgy, since the only way to know the end of a string was to see the NULL character at the end. If the \0 is overwritten (and it can happen pretty easily), then the memory is continually read until a NULL character is found, getting a load of rubbish along the way. And writing to this would overwrite memory needed elsewhere, crashing all.

C++ improved this drastically, with the string class, which stores the length of the string along with a character array - so it doesn't rely on a NULL character.

However (getting back to the point), strtod() function only can accept C style strings, i.e. character arrays. You can convert a C++ string to a C string using a method of the string class, c_str().

word.c_str() - returns a character array with a NULL at the end.

So to answer your question, the syntax to get that working is:
CPP / C++ / C Code:
n1=strtod( t1.c_str() );

Monday, July 2, 2007

C++ Calculator Program v3.0 with Mem Functions


// kalk-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.
// 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.
// 03a Added code to change negative current value to positive when
// Power function AND exponent is less than 1 (e.g. 0.5 for square root)
// as even roots of negative numbers are imaginary
// Planned mods
// 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();
// h/H displays instuctions
// int is_menu_choice(char choice);
void process_choice (char choice, // OUT
double& curr_val, // IN-OUT
double& mem);
int main()
{
char choice; // menu choice
double curr_val; // current value of calculator
double mem; // Calculator Mem function

display_menu();
cout << setprecision(15);
curr_val = 0;
mem = 0;

do {
cin >> choice;
choice = toupper(choice);
process_choice (choice, curr_val, mem);
}
while (choice != 'Q'); // user selects quit
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 [positive only], Q)uit,\n"
<< " (M)em in, mem p(L)us, mem (O)ut, mem s(W)itch \n"
<< " (H)elp show this menu again.\n\n";
}

void process_choice (char choice, // OUT
double& curr_val, // IN-OUT
double& mem) // IN-OUT

{
double work; // required for memory sWtch function
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 'L': mem += curr_val;
break;
case 'M': mem = curr_val;
break;
case 'N': curr_val *= (-1);
break;
case 'O': curr_val = mem;
break;
case 'P': cin >> num;
if (num < 1) curr_val = fabs(curr_val); //stops possibilty roots of negative values causing errors
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 'W': work = curr_val; // switches current and memory values
curr_val = mem;
mem = work;
break;
case 'X': cin >> num;
curr_val *= num;
break;
default: cout << "\tInvalid operation!\n";
} // end switch
if (choice=='H') 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
H
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";

2.48
d 2.5
0.992
i 4
4
p 0.5
2
n
-2
p 0.5
1.4142135623731
i10
10
m
10
r
0.1
w
10
o
0.1
x1000
100
l
100
l
100
o
200.1

q

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

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

*/

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"

*/

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.
//

#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"
<< " +(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;
default:
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;
break;
case '*': cin >> num;
curr_val *= num;
break;
case '^': cin >> num;
curr_val = pow(curr_val, num);
break;
case 'I': curr_val = 1/curr_val;
break;
case 'M': break;
case 'Q': break;
case 'S': cin >> curr_val;
break;
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
2.5
^ 3
15.625
I
0.064
* 100
6.4
q
*/

C++ Program - Console Calculator

The following entries are my further exploration of basic C++ where I am building a calculator to run in Dos Window.

Further documentation is in the detailed comments embedded in the code.

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.