CLHEP 2.0.4.7 Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // -*- C++ -*- 00002 // $Id: testEvaluator.cc,v 1.1 2003/07/16 21:47:28 garren Exp $ 00003 // --------------------------------------------------------------------------- 00004 // 00005 // This file is a part of the CLHEP - a Class Library for High Energy Physics. 00006 // 00007 // This is a test for the Evaluator class. 00008 // It can be also used as a scientific caculator. 00009 // 00010 00011 #include "CLHEP/Evaluator/Evaluator.h" 00012 #include <iostream> 00013 #include <string.h> 00014 #include <assert.h> 00015 00016 int main() { 00017 char exp[240]; 00018 HepTool::Evaluator eval; 00019 00020 eval.setVariable(" dummy ", 0.); // these three lines have been 00021 assert( eval.findVariable(" dummy ") ); // included just for test 00022 eval.clear(); // =========================== 00023 00024 eval.setStdMath(); // set standard constants and functions 00025 eval.setSystemOfUnits(); // set SI units 00026 std::cout.precision(15); 00027 for(;;) { 00028 std::cout << "CALC> "; 00029 00030 // In principle, next two lines could be replaced with just 00031 // std::cin.getline(exp, 240); but that does not work on DEC 00032 // because of bug in the cxx V6.1-027 compiler. 00033 std::cin.get(exp, 240); 00034 std::cin.ignore(); 00035 if ( !std::cin || strcmp(exp,"exit") == 0 || strcmp(exp,"quit") == 0) { 00036 std::cout << std::endl; 00037 break; // exit 00038 } 00039 double value = eval.evaluate(exp); 00040 switch(eval.status()) { 00041 case HepTool::Evaluator::OK: 00042 std::cout << value << std::endl; 00043 break; 00044 case HepTool::Evaluator::WARNING_BLANK_STRING: 00045 continue; 00046 default: 00047 std::cout << "------"; 00048 for (int i=0; i<eval.error_position(); i++) std::cout << "-"; 00049 std::cout << "^" << std::endl; 00050 eval.print_error(); 00051 } 00052 std::cout << std::endl; 00053 } 00054 return 0; 00055 }