| home | contents | previous | next page | send comment | send link | add bookmark |

Calculator.h

/*---------------------------------------------------------------------------------*
 *  "calculator.h"
 *
 *  Declarations for the calculator class.  These perform operations on
 *  spreadsheet data and provide access to the data through calls to
 *  "document" class public methods.
 *
 *  Stephen R. Schmitt
 *
 *  January 1999
 */

#ifndef CALCULATOR_H
#define CALCULATOR_H

#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "document.h"
#include "scalc.h"

// enumerated token codes for lexical analysis of expressions
enum TOKEN
{
    EOLTOK, ERRTOK, EQUALS, NUMBER, CELL_ID,
    ADD, SUBTRACT, MULTIPLY, DIVIDE, FMOD, RAISE,
    LEFT_PN, RIGHT_PN, COMMA,
    ABS, ACOS, ASIN, ATAN, ATAN2,
    CEIL, COS, COSH,
    EXP, FLOOR, HYPOT,
    LN, LOG,
    MAX, MIN,
    SIN, SINH, SQRT,
    TAN, TANH
};

// structure for mapping strings to function tokens
typedef struct
{
    char *name;
    TOKEN code;
}
FUNCTION;

/*---------------------------------------------------------------------------------*
 *  "calculator" class declarations
 *
 *  Inherits document methods and uses TOKEN enumeration and
 *  FUNCTION struct
 */
class calculator : public document
{
public:
    void evaluate( int, int );
    void recalculate();

private:
    bool expression();
    bool term();
    bool factor();
    bool atom();
    void get_token();
    void get_word( char );
    bool get_function( char * );
    void get_cell( char * );
    void get_number( char );
    void get_symbol( char );
    bool call_function( TOKEN );
    bool call_function2( TOKEN );
    void exc_function( TOKEN );

    char    cell_contents[MAX_CONTENTS];            // formula or some text
    int     Icc;                                    // index into cell_contents
    double  Stack[MAX_CONTENTS];                    // expression evaluation stack
    int     Tos;                                    // top of stack

    struct                                          // tokens
    {
        TOKEN  code;                                // enumerated code
        double value;                               // used when token is a number
                                                    // used when token is a cell:
        int    cx;                                  //   cell column number
        int    cy;                                  //   cell row number
    }
    Token;
};

#endif

| home | contents | previous | next page | send comment | send link | add bookmark |

Copyright © 2004, Stephen R. Schmitt