--> Frank Masingill wrote to Cliff Rhodes <--
FM> Cliff, here is the program after I made the changes you
FM>suggested. I do hope to develop it into a full-fledged C++ with
FM>some object-oriented features and I'm studying all of the examples
FM>I can find in my books and disks. I started this as a project for
FM>the kids next door to play with when we keep them and wanted to get
FM>it to the point where they could play with it.
Frank, I think you are on the right track. I've looked at your updated
version and most of the changes look good. It is still a C program,
but that's OK at this point. I think you should continue on that line
until you get it in top notch shape. Then do a conversion to C++.
The reason I say that is because going to C++ will require a different
design approach.
FM>I also had a message from Jerry Coffin with some suggestions
FM>along that line.
Yes, I saw it. Jerry suggested getting on a more solid C++ design
basis. To me that might be a large jump from where you are. It might
be better to go a little slower and evolve what you have into an OOP
app. That's up to you.
I have taken your updated version and modified it. I reworked the
xxxCalc() routines into one. You need to look at that and see if you
can get the color exactly like you want it. I didn't do a good job on
the color consistency after the answer is given.
Notice there are now no globals--so things are getting more polished.
// MATHPLAY.CPP
// Edited 8/24/97
// Added Operation enum to help reduce all xxxCalc() routines into one
#include
#include
#include
// Add this to give us some easy names for the calculations
enum Operation { Add = 0, Subtract, Multiply, Divide, NoOp };
int menu(void);
int calc(Operation op);
void promptc(int x, int y, int color , const char *p);
int main(void)
{
while (menu())
;
clrscr();
promptc(1, 1, 11, "Thanks for playing!");
return 0;
}
int menu()
{
int i, col = 30;
clrscr();
promptc(col, 10, 14, "Would you like to practice a little");
col=35;
promptc(col, 12, 14, "1. Addition?");
promptc(col, 13, 10, "2. Subtraction?");
promptc(col, 14, 13, "3. Multiplication?");
promptc(col, 15, 11, "4. Division?");
promptc(col, 16, 12, "5. or just Quit?");
do
{
promptc(col, 20, 15, "Enter your choice: ");
scanf("%d", &i);
switch(i)
{
case 1: calc(Add); break;
case 2: calc(Subtract); break;
case 3: calc(Multiply); break;
case 4: calc(Divide); break;
case 5: break;
}
}while (i 5);
return (i == 5) ? 0 : 1;
}
int calc(Operation op)
{
/* Performs the calculation type specified by the Operation.
* Still lacks adequate input protection and checking!
*/
/* Several arrays of labels follow. These assume that the
* Operation Add has a value of 0, Subtract 1, etc.
*/
static const char *type[] = {
"ADDITION", "SUBTRACTION", "MULTIPLICATION", "DIVISION" };
static const char *arg1[] = {
"first number (Addend)",
"number to subract FROM (Minuend)",
"number to be multiplied (MULTIPLICAND)",
"number to be divided (DIVIDEND)"
};
static const char *arg2[] = {
"second number (Addend)",
"number to subract (Subtrahend)",
"number to multiply it by (MULTIPLIER)",
"number to divide it by (DIVISOR)"
};
static const char *ask[] = {
"(called the SUM)",
"(REMAINDER)",
"(PRODUCT)",
"(QUOTIENT)"
};
// A list of colors for each Operation type
static const int colors[] = { 14, 10, 13, 11 };
int num1, num2, guess, ans;
if(op >= NoOp) // Error, this should not happen
return 1;
do // In case the user wants to do another problem, loop here
{
int col = 20;
clrscr();
gotoxy(col, 10);
textcolor(colors[op]);
cprintf("%s PROBLEM:", type[op]);
gotoxy(col, 12);
cprintf("Type in the %s: ", arg1[op]);
scanf("%d", &num1);
do // Add a loop on num2 to make sure num2 != 0 for Divide
{
gotoxy(col, 13);
cprintf("Type in the %s: ", arg2[op]);
scanf("%d", &num2);
}while(op == Divide && num2 == 0); // Check for divide by 0
gotoxy(col, 14);
cprintf("What do you think is the answer %s? ", ask[op]);
scanf("%d", &guess);
switch(op)
{
case Add: ans = num1 + num2; break;
case Subtract: ans = num1 - num2; break;
case Multiply: ans = num1 * num2; break;
case Divide: ans = num1 / num2; break;
}
gotoxy(col, 15);
cprintf("The answer is ");
textcolor(15);
cprintf("%d", ans);
textcolor(2);
gotoxy(col, 16);
cprintf("You guessed %s", (ans == guess) ? "right!" : "wrong.");
gotoxy(col, 18);
cprintf("Would you like to try another (Y/N)? ");
do
{
guess = (0 == (guess = getch())) ? -getch() : guess;
guess = toupper(guess);
} while(guess != 'Y' && guess != 'N');
} while(guess == 'Y'); // If yes, loop through calc() again
return 0;
}
void promptc(int x, int y, int color, const char *p)
{
gotoxy(x,y);
textcolor(color);
cputs(p);
}
Cliff Rhodes
cliff.rhodes@juge.com
crhodes@flash.net
X CMPQwk 1.42 1692 X"When Love is suppressed Hate takes its place." -
Havelock Ellis
--- Maximus/2 3.01
---------------
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)
|