/**********************************************************
*                                                         *
*  This program tests the functions Convert and           *
*  Evaluate in funceval.c.  It will allow the user to     *
*  type in a function (no spaces please) and then         *
*  enter values for X and Y.  The value of the function   *
*  with these values of X and Y substituted will be       *
*  printed.  If an error is found in the function, the    *
*  function will be printed to the standard output, an    *
*  up arrow will be printed beneath the offending         *
*  character and an appropriate error message will be     *
*  printed.                                               *
*                                                         *
*  This program may be freely used for non-profit         *
*  purposes as long as the copyright notice remains       *
*  in the code.                                           *
*                                                         *
***********************************************************
*                                                         *
*  testfevl.c -- Copyright 1988  Randy C. Finch           *
*                                                         *
**********************************************************/

/*----------------  INCLUDES  ------------------*/

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include "syntxerr.h"


/*---------------  DEFINES  ------------------*/

#define NULL 0


/*---------------  EXTERNAL VARIABLES  -------------*/

extern double Evaluate();
extern unsigned char *Convert();
extern unsigned char SyntaxErr;


/*---------------  FUNCTIONS  ---------------*/

void main()
{
  unsigned char function[256];
  unsigned char *expression;
  unsigned long spaces, i;
  char answer1[10], answer2[10];

  do {

     printf("\nEnter a function:\n\n");
     scanf("%s",function);

     expression = Convert(function);

     if (SyntaxErr) {
        if (SyntaxErr == STACKUNDERFLOW)
           printf("Stack underflow\n");
        else if (SyntaxErr == STACKOVERFLOW)
           printf("Stack overflow\n");
        else if (SyntaxErr == TOOMANYCONST)
           printf("Too many constants in function\n");
        else {
           printf("\n%s\n",function);
           spaces = expression - function;
           for(i=spaces; i>0; --i) printf(" ");
           printf("^\n");

           switch (SyntaxErr) {
              case MISPLACEDOP:
                 printf("Misplaced operator\n");
                 break;
              case ILLEGALCHAR:
                 printf("Illegal character\n");
                 break;
              case ILLEGALEXP:
                printf("Illegal exponent\n");
                break;
              case ILLEGALFUNC:
                 printf("Illegal function\n");
                 break;
              case MISSINGOP:
                 printf("Missing operator\n");
                 break;
              case MISSINGOPRP:
                 printf("Missing operator or right parenthesis\n");
                 break;
              case MISSINGLP:
                 printf("Missing left parenthesis\n");
                 break;
              case MISSINGRP:
                 printf("Missing right parenthesis\n");
                 break;
              case MISSINGPARM:
                 printf("Missing parameter\n");
                 break;
              case LONEDECIMAL:
                 printf("Lone decimal point\n");
                 break;
              case EXTRADECIMAL:
                 printf("Extra decimal\n");
                 break;
              case EXTRAE:
                 printf("Extra E\n");
                 break;

           } /* switch */

        } /* else */

     } /* if */

     else {
        double x, y, result;

        do {
           printf("\nInput X: ");
           scanf("%lf",&x);
           printf("Input Y: ");
           scanf("%lf",&y);

           result = Evaluate(x,y);

           printf("\nX = %f\n",x);
           printf("Y = %f\n",y);
           printf("Result of %s: %f\n\n", function, result);

           printf("Another calculation? (Y or N) ");
           scanf("%s",answer1);

        } while( (*answer1 == 'y') || (*answer1 == 'Y') );

     } /* else */

     printf("\nAnother function? (Y or N) ");
     scanf("%s",answer2);

  } while( (*answer2 == 'y') || (*answer2 == 'Y') );

} /* main */
