Changeset 2593 in Sophya for trunk/SophyaLib/SysTools/cexpre.cc


Ignore:
Timestamp:
Aug 9, 2004, 11:40:00 AM (21 years ago)
Author:
ansari
Message:

Correction bug decodage cst xxx.e-yy , ajout fonctions 0 argument rand01(), randpm1() gaurand() - Reza 9 Aout 2004

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/SysTools/cexpre.cc

    r2550 r2593  
    66#include <stack>
    77#include <math.h>
     8#include "srandgen.h"
    89
    910namespace SOPHYA {
     
    154155  virtual double Evaluate() const ;
    155156  inline void AddArg(CExprBase * e);
     157  inline int  GetMaxNArg() const { return _maxne; }
    156158  virtual bool CheckE(string& errmsg) const;
    157159  virtual void Print(ostream& os) const;
     
    166168};
    167169
     170// Les fonctions de generation de nombre aleatoire
     171double _CE_rand01() { return drand01(); }
     172double _CE_randpm1() { return drandpm1(); }
     173double _CE_gaurand() { return GauRnd(0., 1.); }
    168174//---------------------------------------------------------
    169175CE_FuncExp::CE_FuncExp(string const & func)
     
    193199  else if (func == "cosh") _f1 = cosh;
    194200  else if (func == "tanh") _f1 = tanh;
     201// Les fonctions aleatoires declaree plus haut
     202  else if (func == "rand01") _f0 = _CE_rand01; 
     203  else if (func == "randpm1") _f0 = _CE_randpm1;       
     204  else if (func == "gaurand") _f0 = _CE_gaurand;       
    195205  else {
    196206    string errmsg = "CE_FuncExp::CE_FuncExp() - Uknown function " ;
     
    219229double CE_FuncExp::Evaluate() const
    220230{
    221   if ((_ne != _maxne) || (_maxne < 1) ) {
     231  if ((_ne != _maxne) || (_maxne < 0) ) {
    222232    throw CExprException("CE_FuncExp::Evaluate() - Wrong argument number ");
    223233  }
    224   if (_ne == 1) return _f1(_e[0]->Evaluate());
     234  if (_ne == 0) return _f0();
     235  else if (_ne == 1) return _f1(_e[0]->Evaluate());
    225236  else if (_ne == 2) return _f2(_e[0]->Evaluate(), _e[1]->Evaluate());
    226237  else if (_ne == 3) return _f3(_e[0]->Evaluate(), _e[1]->Evaluate(),
     
    327338  bool fgcont = true;
    328339  bool checkok = true;
     340  bool apperrmsg = true;
    329341  //  cout << " DBG-ParseString off= " << off << " sex[off]= " << sex[off]
    330342  //    << " extype= " << extype << endl;
    331343  p = stop = off; 
    332344  while ((p < len) && (fgcont) && (checkok) ) {
    333     //   cout << " DBG-2-ParseString p=" << p << " q=" << q << endl;
     345    // cout << " DBG-2-ParseString p=" << p << " q=" << q << endl;
    334346
    335347    cx = NULL;
     348    // ------------------------------- Bretelle ------
     349    // Il faut sauter les signes +/- qui se trouverait dans les
     350    // exposants des nombres xxxe-yy xxxe+y : 0.4e-2 ...
     351    // Les qques lignes sont une bretelle qui saute ces signes +/- (Reza, Aout 2004)
     352    // On verifie d'abord s'il y a un nombre valide au debut de la positions courante
     353    char* lcc;
     354    size_t pfn=0,psn;
     355    bool numberok = false;
     356    strtod(sex.c_str()+p, &lcc);
     357    if (lcc != sex.c_str()+p) {  // Expression valide de double au debut
     358      numberok = true;           // de la position courante
     359      pfn = lcc-sex.c_str();
     360    }
     361    // On cherche le 1er signe d'operateur - apres le nombre de debut
    336362    q = sex.find_first_of("+-*/(),",p);
     363
     364    while ( numberok && (q < len) && (q < pfn) && (q > p) &&
     365            ((sex[q] == '+') || (sex[q] == '-')) ) {
     366      psn = q+1;
     367      if (psn < len) q = sex.find_first_of("+-*/(),",psn);
     368      else q = len+1;
     369    }
     370    //---- Fin de la Bretelle ---- pour sauter les signes +/- de l'exposant des nombres
     371
    337372    if (q < len) {  // operateur trouve
    338373      opc = sex[q];  // signe operateur courant
     
    344379          }
    345380          else cx = ParseString(2, sex.substr(p,q-p), sex, q+1, stop, errmsg);
    346           if (!cx)  { checkok = false; p = stop+1; }
     381          if (!cx)  { checkok = false; p = stop+1; apperrmsg = false; }
    347382          else {
    348383            if (osn == 1) {
     
    387422          else cx = rx;
    388423         
    389           if (!cx) {
     424          if (!cx && ( !fx || ( opc != ')' ) ||
     425                       ( (fx && (fx->GetMaxNArg() > 0) && (opc == ')' ) ) ) ) ) {
    390426            checkok = false; p = q;  fgcont = false;
    391427            errmsg = "CExpressionEvaluator::ParseString()/ Syntax Error - farg=NULL (BB)";
    392428          }
    393429          else {
    394             rx = Arrange_CE_BinExpStack(sbx, cx);
    395             if (extype == 2) {
    396               //            cout << " DBG-ParseString-AddArg " << *(cx) << endl;
    397               fx->AddArg(rx);  rx = NULL;
    398             }
     430            if (cx) { rx = Arrange_CE_BinExpStack(sbx, cx);
     431              if (extype == 2) {
     432                //          cout << " DBG-ParseString-AddArg " << *(cx) << endl;
     433                fx->AddArg(rx);  rx = NULL;
     434              }
     435            }
    399436            if (opc == ')') { // Signe de fin de traitement d'une portion d'expression
    400437              if (extype == 2) { rx = fx; fx = NULL; }
     
    475512      if (!rx)  errmsg += " !rx " ;
    476513      errmsg += " (F)";
    477     }
    478     sprintf(buff,"\n CExprError... S=[%s]  (p=%ld , len=%ld)", sex.c_str(), (long)p,(long)len);
    479     errmsg += buff;
     514    }   
     515
     516    if (apperrmsg) {
     517      if (p<len)
     518        sprintf(buff,"\n CExprError... %s <> %s ", sex.substr(0,p).c_str(),
     519                sex.substr(p).c_str());
     520      else  sprintf(buff,"\n CExprError... %s <<", sex.c_str());
     521      errmsg += buff;
     522    }
    480523    if (rx) delete rx;
    481524    rx = NULL;
Note: See TracChangeset for help on using the changeset viewer.