The Calculator Program

Tom Kelliher, CS23

Feb. 28, 1997

Outline:

  1. Reasonable extension(s).

  2. A header file.

  3. Pseudo-code for reading a number.

  4. Lab time.

Header File

#ifndef __MISC_H

#define __MISC_H


const int MAX_LEN = 100;


struct Num
{
   char digits[MAX_LEN];
   int len;
};


enum Operator {INVALID, ADD, MULTIPLY};


int getNum(char num[], int &digits, int max);
Operator getOp();
void reverse(char *num, int n);
void print(Num a);
void die(char *s);
int add(Num &c, Num a, Num b);
int multiply(Num &c, Num a, Num b);

#endif

Reading a Number

My problems in debugging this & the print function:

  1. Starting at wrong end of array on printing.

  2. Handling the input 2+2. ( putback).

  3. Handling an input value of 0.

  4. Keeping straight the ASCII and numeric representations of digits.

None of this overwhelmed me. Why?

What are the ``boundary cases?''

  1. An unexpected character.

  2. EOF.

Only really one good way to do this: one character at a time.

Pseudocode:

Skip over whitespace;
Error check: EOF or non-digit;
Skip over leading zeroes;
Check if number is zero;
Error check: EOF or non-digit;
Read number & record number of digits;
Error check: too many digits.
Reverse number;



Thomas P. Kelliher
Thu Feb 27 16:51:16 EST 1997
Tom Kelliher