High School Mathematics Extensions/Mathematical Programming/Exercise 1: Processing Commands
Explanation
editIncludes
stdio.h
conio.h
Type Definitions
Values for TRUE and FALSE to get away from the standard C syntax of just testing for zero / not zero.
Function Prototypes
Prototype so we don't need to worry about forward references.
Global Variables
done = loop control in main.
r = denominator(on the right hand side of the operators).
l = numerator (on the left hand side of the operators).
Function Definitions
init - set global variables.
input_message = prompt for a command.
input = read command.
get_integer = read an integer.
retrieve_keypress = look for a keypress.
validate = make sure the command is one the program can handle.
execute_command = map the command on to the routines to implement it.
output_divide = implement divide '/'.
output_modulus = implement modulus '%'.
output_values = print out l and r.
Code to change
edit//includes #include <stdio.h> #include <conio.h> //type definitions #define TRUE 1 #define FALSE 0 //function prototypes void init(); void input_message(); char input(); char retrieve_keypress(); char validate(char check_me); void execute_command(char command); void output_divide(); void output_modulus(); void output_values(); int get_integer(); //global variables unsigned char done; int r; int l; //function definitions void init() { done=FALSE; r=5; l=2; } void input_message() { cprintf("Key Action.\n"); cprintf("/ Display r/l = result.\n"); cprintf("%% Display r%%l = result.\n"); cprintf("= Display r=value, l=value.\n"); cprintf("r or R Set the value for r.\n"); cprintf("l or L Set the value for l.\n"); cprintf("x or X End program.\n"); } char input() { char read; read=retrieve_keypress(); while (!validate(read)) { cprintf("\nInvalid Command.\n"); input_message(); read=retrieve_keypress(); } return read; } int get_integer() { int ret_val; char lastpress; cprintf("Enter integer: "); cscanf("%i",&ret_val); lastpress=getch(); cprintf("\n"); return ret_val; } char retrieve_keypress() { char ret_val; ret_val=getche(); cprintf("\n"); return ret_val; } char validate(char check_me) { char ret_val=0; switch (check_me) { case '/': case '%': case '=': case 'r': case 'R': case 'l': case 'L': case 'x': case 'X': ret_val++; break; } return ret_val; } void execute_command(char command) { switch (command) { case '/': output_divide(); break; case '%': output_modulus(); break; case '=': output_values(); break; case 'r': case 'R': r=get_integer(); output_values(); break; case 'l': case 'L': l=get_integer(); output_values(); break; case 'x': case 'X': done++; break; } } void output_divide() { if (l > 0) { cprintf("%d / %d = %d.\n",r,l,r/l); } else { cprintf("r = %d, l = %d. Please set l to a value other than 0.\n",r,l); } } void output_modulus() { if (l > 0) { cprintf("%d %% %d = %d.\n",r,l,r%l); } else { cprintf("r = %d, l = %d. Please set l to a value other than 0.\n",r,l); } } void output_values() { cprintf("r = %d, l = %d.\n",r,l); } void main() { char command; init(); input_message(); while(!done) { command=input(); execute_command(command); } }