Skip to content
Snippets Groups Projects
calc.cpp 1.93 KiB
Newer Older
  • Learn to ignore specific revisions
  • //**************************************************************************//
    //* FRIDA: fast reliable interactive data analysis                         *//
    //* calc.cpp: the command-line pocket calculator                           *//
    //* (C) Joachim Wuttke 1990-, v2(C++) 2001-                                *//
    //* http://frida.sourceforge.net                                           *//
    //**************************************************************************//
    
    #include <math.h>
    #include <iostream>
    #include <stdlib.h> // for boost/shared_ptr
    #include <stdio.h>
    
    #include <ask_simple_value.h> // for calculator
    #include <readln.h>
    
    #include "mystd.h"
    
    #include "expr.h"
    #include "calc.h"
    
    #include "xax_yacc.h"
    #include "xax_lex.h"
    
    void NCalc::Calculator() 
    {
        string s;
        PTree T;
        double v;
    
        if ( NRead::stack_empty() ) {
            // interactive calculator
            while (1) {
                s = sask("calc> ");
                if (s[0]=='q')
                    return;
                if (s[0]=='h' || s[0]=='?') {
    
    Wuttke, Joachim's avatar
    Wuttke, Joachim committed
                    printf( "enter expression, or q to quit\n");
    
                    continue;
                }
                try {
                    user_xaxparse( s.c_str(), &T );
                    T->tree_point_val( &v );
                    printf("%.12g\n", v);
                } catch( string& s ) {
                    cerr << s << endl;
                }
            }
        } else {
            // evaluate one expression, with respect to given file selection
            s = NRead::readln("");
            user_xaxparse( s.c_str(), &T );
            NOlm::IterateO fiter;
            if( fiter.size()==0 ) {
                T->tree_point_val( &v, -1 );
                printf("%.12g\n", v);
            } else {
                while ( (fiter()) ) {
                    uint k = fiter.k();
                    T->tree_point_val( &v, k );
                    if ( fiter.size()>1 )
                        printf( "f%d: ", k );
                    printf("%.12g\n", v);
                }
            }
        }
    }