Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
ccalc.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "parser.h"
00008 #include <ctype.h>
00009
00010 #undef USE_READLINE
00011 #ifdef USE_READLINE
00012 #include "readline.h"
00013 #endif
00014
00015 int main( int argc , char * argv[] )
00016 {
00017 PARSER_code * pcode ;
00018 char expr[900] , * cexp ;
00019 double atoz[26] , value ;
00020 int ii , kvar, kar, brk ;
00021 int DoOnce;
00022
00023 DoOnce = 0;
00024
00025 kar = 1;
00026 brk = 0;
00027 DoOnce = 0;
00028 expr[0] = '\0';
00029
00030 while (kar < argc) {
00031 if (strcmp(argv[1],"-help") == 0 ){
00032 printf("Usage: ccalc [-eval <expr>]\n"
00033 "With no command line parameters:\n"
00034 "Interactive numerical calculator, using the same\n"
00035 "expression syntax as 3dcalc. Mostly for playing.\n"
00036 "With -eval <expr> option:\n"
00037 "Calculates expr and quits. \n"
00038 "Do not use variables in expr.\n"
00039 "Example: ccalc -eval '3 + 5 * sin(22)' \n"
00040 "or: ccalc -eval 3 +5 '*' 'sin(22)'\n") ;
00041 exit(0) ;
00042 }
00043
00044 if( !brk ){
00045 if( strcmp(argv[1],"-eval") == 0 ){
00046 ++kar;
00047 if (kar >= argc) {
00048 fprintf (stderr, "need argument after -eval ");
00049 exit (1);
00050 }
00051 }
00052
00053
00054 while (kar < argc) {
00055 sprintf(expr,"%s %s", expr, argv[kar]);
00056 ++ kar;
00057 }
00058
00059 DoOnce = 1;
00060 brk = 1;
00061 }
00062
00063 if (!brk) {
00064 fprintf (stderr,"Error: Option %s not understood. Try -help for usage\n", argv[kar]);
00065 exit (1);
00066 } else {
00067 brk = 0;
00068 kar ++;
00069 }
00070
00071 }
00072
00073 for( ii=0 ; ii < 25 ; ii++ ) atoz[ii] = 0.0 ;
00074
00075 do{
00076 if (!DoOnce){
00077 #ifdef USE_READLINE
00078 { char *lin = readline("ccalc> ") ;
00079 if( lin == NULL ) continue ;
00080 if( *lin == '\0' ){ free(lin); continue; }
00081 add_history(lin) ;
00082 strncpy(expr,lin,899); expr[899]='\0'; free(lin);
00083 }
00084 #else
00085 printf("calc> ") ; fflush(stdout) ;
00086 fgets(expr,900,stdin) ;
00087 #endif
00088 }
00089
00090 if( strlen(expr) == 0 ) continue ;
00091 if( strstr(expr,"quit") != NULL ) exit(0) ;
00092
00093 if( strstr(expr,"=") != NULL ){
00094 kvar = toupper(expr[0]) - 'A' ;
00095 cexp = strstr(expr,"=") + 1 ;
00096 } else {
00097 kvar = -1 ;
00098 cexp = expr ;
00099 }
00100
00101 pcode = PARSER_generate_code( cexp ) ;
00102 if( pcode == NULL ){
00103 printf("parser error!\n") ; fflush(stdout) ;
00104 if (!DoOnce) continue ;
00105 else exit(1);
00106 }
00107
00108 #if 0
00109 if( PARSER_has_symbol( "I" , pcode ) )
00110 printf(" [contains symbol I]\n") ;
00111 #endif
00112
00113 value = PARSER_evaluate_one( pcode , atoz ) ; free(pcode) ;
00114
00115 if (!DoOnce) {
00116 if( kvar >= 0 && kvar < 26 ){
00117 printf("%c", kvar+'A' ) ;
00118 atoz[kvar] = value ;
00119 } else {
00120 printf(" ") ;
00121 }
00122 printf(" = %g\n",value) ; fflush(stdout) ;
00123 } else {
00124 if (0) printf("%g\n",value) ;
00125 else printf("%f\n",value) ;
00126 exit (0);
00127 }
00128 } while(1) ;
00129 }