Changeset 2593 in Sophya for trunk/SophyaLib/SysTools/cexpre.cc
- Timestamp:
- Aug 9, 2004, 11:40:00 AM (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/SysTools/cexpre.cc
r2550 r2593 6 6 #include <stack> 7 7 #include <math.h> 8 #include "srandgen.h" 8 9 9 10 namespace SOPHYA { … … 154 155 virtual double Evaluate() const ; 155 156 inline void AddArg(CExprBase * e); 157 inline int GetMaxNArg() const { return _maxne; } 156 158 virtual bool CheckE(string& errmsg) const; 157 159 virtual void Print(ostream& os) const; … … 166 168 }; 167 169 170 // Les fonctions de generation de nombre aleatoire 171 double _CE_rand01() { return drand01(); } 172 double _CE_randpm1() { return drandpm1(); } 173 double _CE_gaurand() { return GauRnd(0., 1.); } 168 174 //--------------------------------------------------------- 169 175 CE_FuncExp::CE_FuncExp(string const & func) … … 193 199 else if (func == "cosh") _f1 = cosh; 194 200 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; 195 205 else { 196 206 string errmsg = "CE_FuncExp::CE_FuncExp() - Uknown function " ; … … 219 229 double CE_FuncExp::Evaluate() const 220 230 { 221 if ((_ne != _maxne) || (_maxne < 1) ) {231 if ((_ne != _maxne) || (_maxne < 0) ) { 222 232 throw CExprException("CE_FuncExp::Evaluate() - Wrong argument number "); 223 233 } 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()); 225 236 else if (_ne == 2) return _f2(_e[0]->Evaluate(), _e[1]->Evaluate()); 226 237 else if (_ne == 3) return _f3(_e[0]->Evaluate(), _e[1]->Evaluate(), … … 327 338 bool fgcont = true; 328 339 bool checkok = true; 340 bool apperrmsg = true; 329 341 // cout << " DBG-ParseString off= " << off << " sex[off]= " << sex[off] 330 342 // << " extype= " << extype << endl; 331 343 p = stop = off; 332 344 while ((p < len) && (fgcont) && (checkok) ) { 333 // 345 // cout << " DBG-2-ParseString p=" << p << " q=" << q << endl; 334 346 335 347 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 336 362 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 337 372 if (q < len) { // operateur trouve 338 373 opc = sex[q]; // signe operateur courant … … 344 379 } 345 380 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; } 347 382 else { 348 383 if (osn == 1) { … … 387 422 else cx = rx; 388 423 389 if (!cx) { 424 if (!cx && ( !fx || ( opc != ')' ) || 425 ( (fx && (fx->GetMaxNArg() > 0) && (opc == ')' ) ) ) ) ) { 390 426 checkok = false; p = q; fgcont = false; 391 427 errmsg = "CExpressionEvaluator::ParseString()/ Syntax Error - farg=NULL (BB)"; 392 428 } 393 429 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 } 399 436 if (opc == ')') { // Signe de fin de traitement d'une portion d'expression 400 437 if (extype == 2) { rx = fx; fx = NULL; } … … 475 512 if (!rx) errmsg += " !rx " ; 476 513 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 } 480 523 if (rx) delete rx; 481 524 rx = NULL;
Note:
See TracChangeset
for help on using the changeset viewer.