High School Mathematics Extensions/Mathematical Programming/Processing Commands

From Wikibooks, open books for an open world
< High School Mathematics Extensions‎ | Mathematical Programming
Jump to: navigation, search


Explanation[edit]

Two new functions were added: char retrieve_keypress() and char validate(char check_me) and two functions were changed: void input_message(), char input().

Additions

char retrieve_keypress()

This function make sure program input and output are displayed on different lines on the console. This makes program error messages clearer, and will make the program output clearer when we print it.
  • reads a key from the console.
  • outputs a newline character to the console after it reads a key. This makes the program error message clearer, and will make the program output clearer when we print it.

char validate (char checkme)

Returns true if the character passed to it is in this set: {'=' '+' '-' 'd' 'D' 'x' 'X'} and false if it is not.

Changes

void input_message()

Changed to prompt for the commands this program will process.

char input()

Changed to
  • call retrieve_keypress to get a command
  • to validate the command
  • to print an error message and instructions if the command is not valid.

Code to change[edit]

Crystal Clear action apply.png Add and replace the following code:
 //function prototypes
 //...
 char retrieve_keypress();
 char validate(char check_me);

 //function definitions
 //...
 void input_message()
 {
    // Prompt for Commands
    cprintf("Key      Action.\n");
    cprintf("=        Display f(x) = result.\n");       
    cprintf("d or D   Change delta.\n");
    cprintf("+        Add delta to x.\n");
    cprintf("-        Subtract delta from x.\n");
    cprintf("x or X   End program.\n");
 }
 char input()
 {
     // Input a character at a time
     read=retrieve_keypress();
     // Validate command
     while (!validate(read))
        {
            cprintf("\nInvalid Command.\n");
            input_message();
            read=retrieve_keypress();
        }
     return read;
 }
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 'd':
        case 'D':
        case 'x':
        case 'X':   ret_val++;
                    break;
    }
    return ret_val;
}


Next Step[edit]