Ignore:
Timestamp:
Mar 16, 2004, 12:49:33 AM (22 years ago)
Author:
ansari
Message:

Correction bugs ds CExpression/RPNExpression et adaptation classe Commander avec utilisation classe CExpressionEvaluator - Reza 16 Mars 2004

File:
1 edited

Legend:

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

    r2483 r2512  
    77#include "strutil.h"
    88#include "strutilxx.h"
     9#include "cexpre.h"
     10#include "rpneval.h"
    911#include "srandgen.h"
    1012
     
    831833}
    832834else if ((tokens.size() > 0) && (tokens[0] == "=")) { 
    833   // x = RPNExpression (Evaluation d'expression RPN)
    834   tokens[0] = kw;
    835   int rcev = EvalRPNExpr(tokens, s);
    836   if (rcev) {
    837     cerr << "Commander::Interpret() evaluation (RPN) syntax Error ! " << "line: " << s << endl;
     835  // x = Expression
     836  try {
     837    CExpressionEvaluator cex(tokens[1]);
     838    double res = cex.Value();
     839    char cbuff[64];
     840    sprintf(cbuff,"%g",res);
     841    string vv = cbuff;
     842    SetVariable(kw, vv);
     843  }
     844  catch (CExprException& cexerr) {
     845    cerr << "Commander::Interpret() evaluation Error : \n " << "line: " << s
     846         << " \n Msg=" << cexerr.Msg()  << endl;
    838847    _xstatus = 98;
    839848    return(98);
    840849  }
    841   return(0);
    842850}
    843851else if (kw == "defscript") {  // definition de script
     
    12361244  else {
    12371245    if (idx >= (*it).second.size())
    1238       for(int j=(*it).second.size(); j<idx; j++)  (*it).second.push_back("");
    1239     (*it).second.push_back(val);
     1246      for(int j=(*it).second.size(); j<=idx; j++)  (*it).second.push_back("");
     1247    (*it).second[idx] = val;
    12401248    fg = true;
    12411249  }
     
    14041412}
    14051413
    1406 /* Operations sur le stack RPN */
    1407 inline bool Check_myRPNStack_(stack<double>& s, double& x, string& line)
    1408 {
    1409   if (s.empty()) {
    1410     cerr << "Commander::EvalRPNExpr: syntax error / empty RPN stack " << line << endl;
    1411     return true;
    1412   }
    1413   else x = s.top();
    1414   return false;
    1415 }
    1416 inline bool Check_myRPNStack_(stack<double>& s, double& x, double& y, string& line)
    1417 {
    1418   if (s.size() < 2) {
    1419     cerr << "Commander::EvalRPNExpr: syntax error / RPN stack size < 2  " << line << endl;
    1420     return true;
    1421   } 
    1422  else {
    1423    x = s.top(); s.pop(); y = s.top();
    1424  }
    1425   return false;
    1426 }
    1427 
    1428 inline void Print_myRPNStack_(stack<double> s)
    1429 {
    1430   if (s.empty())
    1431     cout << "Commander::EvalRPNExpr/PrintStack: Empty stack " << endl;
    1432   else {
    1433     int k = 0;
    1434     cout << "Commander::EvalRPNExpr/PrintStack: Size()= " << s.size() << endl;
    1435     while( !s.empty() ) {
    1436       cout << "    " << k << ":  " << s.top() << "  ";
    1437       if (k == 0)  cout << " (x) " << endl;
    1438       else if (k == 1) cout << " (y) " << endl;
    1439       else if (k == 2) cout << " (z) " << endl;
    1440       else cout << endl;
    1441       s.pop(); k++;
    1442     }
    1443   }
    14441414 
    1445 }
    1446 
    1447 int Sum_RPNStack_(stack<double>& s, double& sx, double& sx2, string& line)
    1448 {
    1449   sx = sx2 = 0.;
    1450   int nn = 0;
    1451   double x = 0.;
    1452   while( !s.empty() ) {
    1453     x = s.top(); s.pop();
    1454     sx += x; sx2 += x*x;
    1455     nn++;
    1456   }
    1457   return(nn); 
    1458 }
    1459 
    1460 int Product_RPNStack_(stack<double>& s, double& px, string& line)
    1461 {
    1462   px = 1.;
    1463   int nn = 0;
    1464   double x = 0.;
    1465   while( !s.empty() ) {
    1466     x = s.top(); s.pop();
    1467     px *= x;  nn++;
    1468   }
    1469   return(nn); 
    1470 }
    1471  
    14721415/* --Methode-- */
    14731416int Commander::EvalRPNExpr(vector<string> & args, string & line)
    14741417{
    1475  
    1476   if (args.size() < 2) {
    1477     cerr << "Commander::EvalRPNExpr: syntax error / missing arguments " << line << endl;
    1478     return(1);
    1479   }
    1480   else if (args.size() == 2) {
    1481     SetVariable(args[0], args[1]);
    1482     return(0);
    1483   }
    1484 
    1485   double x,y;
    1486   x = y = 0.;
    1487   stack<double> rpnstack;  // Stack des operations en RPN
    1488   for(int k=1; k<args.size(); k++) {
    1489     // Les 4 operations de base + - * /
    1490     if (args[k] == "+") {
    1491       if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
    1492       rpnstack.top() = y+x;
    1493     }
    1494     else if (args[k] == "-") {
    1495       if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
    1496       rpnstack.top() = y-x;
    1497     }
    1498     else if (args[k] == "*") {
    1499       if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
    1500       rpnstack.top() = y*x;
    1501     }
    1502     else if (args[k] == "/") {
    1503       if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
    1504       rpnstack.top() = y/x;
    1505     }
    1506     else if (args[k] == "%") {
    1507       if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
    1508       rpnstack.top() = (int)y % (int)x;
    1509     }
    1510     // Les constantes : e , pi
    1511     else if (args[k] == "e") {
    1512       rpnstack.push(M_E);
    1513     }
    1514     else if (args[k] == "pi") {
    1515       rpnstack.push(M_PI);
    1516     }
    1517     // Les fonctions usuelles a 1 argument f(x)
    1518     else if (args[k] == "cos") {
    1519       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1520       rpnstack.top() = cos(x);
    1521     }
    1522     else if (args[k] == "sin") {
    1523       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1524       rpnstack.top() = sin(x);
    1525     }
    1526     else if (args[k] == "tan") {
    1527       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1528       rpnstack.top() = tan(x);
    1529     }
    1530     else if (args[k] == "acos") {
    1531       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1532       rpnstack.top() = acos(x);
    1533     }
    1534     else if (args[k] == "asin") {
    1535       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1536       rpnstack.top() = asin(x);
    1537     }
    1538     else if (args[k] == "atan") {
    1539       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1540       rpnstack.top() = atan(x);
    1541     }
    1542     else if (args[k] == "chs") {
    1543       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1544       rpnstack.top() = -x;
    1545     }
    1546     else if (args[k] == "sqrt") {
    1547       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1548       rpnstack.top() = sqrt(x);
    1549     }
    1550     else if (args[k] == "sq") {  // x^2
    1551       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1552       rpnstack.top() = x*x;
    1553     }
    1554     else if (args[k] == "log") {
    1555       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1556       rpnstack.top() = log(x);
    1557     }
    1558     else if (args[k] == "log10") {
    1559       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1560       rpnstack.top() = log10(x);
    1561     }
    1562     else if (args[k] == "exp") {
    1563       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1564       rpnstack.top() = exp(x);
    1565     }
    1566     else if (args[k] == "fabs") {
    1567       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1568       rpnstack.top() = fabs(x);
    1569     }
    1570     else if (args[k] == "floor") {
    1571       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1572       rpnstack.top() = floor(x);
    1573     }
    1574     else if (args[k] == "ceil") {
    1575       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1576       rpnstack.top() = ceil(x);
    1577     }
    1578     // trunc et nint vire - ca ne compile pas sous linux - Reza 01/2003
    1579     else if (args[k] == "deg2rad") {
    1580       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1581       rpnstack.top() = x*M_PI/180.;
    1582     }
    1583     else if (args[k] == "rad2deg") {
    1584       if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1585       rpnstack.top() = x*180./M_PI;
    1586     }
    1587     // Les fonctions usuelles a 2 argument f(x,y)
    1588     else if (args[k] == "pow") {
    1589       if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
    1590       rpnstack.top() = pow(y,x);
    1591     }
    1592     else if (args[k] == "atan2") {
    1593       if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
    1594       rpnstack.top() = atan2(x,y);
    1595     }
    1596     // generateur aleatoire
    1597     else if (args[k] == "rand") {
    1598       double rnd = drand01();
    1599       rpnstack.push(rnd);
    1600     }
    1601     else if (args[k] == "norand") {
    1602       double rnd = GauRnd(0., 1.);
    1603       rpnstack.push(rnd);
    1604     }
    1605     // Fonction a N arguments  - Somme, produit, etc ...
    1606     else if ((args[k] == "sum") || (args[k] == "mean") || (args[k] == "sigmean") ||
    1607              (args[k] == "sigma") || (args[k] == "sigma2") ) {
    1608       double sx, sx2;
    1609       int nn = Sum_RPNStack_(rpnstack, sx, sx2, line);
    1610       if (args[k] == "sum") rpnstack.push(sx);
    1611       else {
    1612         if (nn == 0) {
    1613           cerr << "Commander::EvalRPNExpr: mean/sigma error- RPN stack empty: "
    1614                << line << endl;
    1615           return(1);
    1616         }
    1617         double fnn = nn;
    1618         if ((args[k] == "sigma") || (args[k] == "sigmean"))
    1619           rpnstack.push(sqrt(sx2/fnn-(x*x/(fnn*fnn))));
    1620         else if ((args[k] == "mean") || (args[k] == "sigmean"))  rpnstack.push(sx/fnn);
    1621         else rpnstack.push(sx2/fnn-(x*x/(fnn*fnn)));
    1622       }
    1623     }
    1624     else if (args[k] == "product") {
    1625       double px;
    1626       int nn = Product_RPNStack_(rpnstack, px, line);
    1627       if (nn == 0) {
    1628         cerr << "Commander::EvalRPNExpr: product error- RPN stack empty: "
    1629              << line << endl;
    1630         return(1);
    1631       }
    1632       rpnstack.push(px);
    1633     }
    1634     // Fonctions de manipulation de stack
    1635     else if (args[k] == "print") {
    1636       Print_myRPNStack_(rpnstack);
    1637     }
    1638     else if (args[k] == "x<>y") {
    1639       if ( Check_myRPNStack_(rpnstack, x, y, line) ) return(1);
    1640       rpnstack.top() = x;  rpnstack.push(y);
    1641     }
    1642     else if (args[k] == "pop") {
    1643       rpnstack.pop();
    1644     }
    1645     else if (args[k] == "push") {
    1646       if (rpnstack.empty()) rpnstack.push(0.);
    1647       else rpnstack.push(rpnstack.top());
    1648     }
    1649     // On met un nombre sur le stack
    1650     else {
    1651       char * esptr;
    1652       x = strtod(args[k].c_str(), &esptr);
    1653       //      if (ctof(args[k].c_str(),&x) < 0) {
    1654       if (esptr == args[k].c_str()) {
    1655         cerr << "Commander::EvalRPNExpr: syntax error near " << args[k]
    1656              << " in expression: \n" << line << endl;
    1657         return(2);
    1658       }
    1659       rpnstack.push(x);
    1660     }
    1661 
    1662   }
    1663 
    1664   if ( Check_myRPNStack_(rpnstack, x, line) ) return(1);
    1665   char buff[64];
    1666   sprintf(buff, "%g", x);
    1667   string res = buff;
    1668   SetVariable(args[0], res);
     1418  // A virer - Reza 15/03/2004
    16691419  return(0);
    16701420}
     
    17551505}
    17561506// Evaluation d'expression en notation polonaise inverse
    1757 else if (kw == "rpneval") { 
    1758   return(EvalRPNExpr(tokens, toks));
     1507else if (kw == "rpneval") {
     1508  try {
     1509    RPNExpressionEvaluator rpn(tokens, 1);
     1510    double res = rpn.Value();
     1511    char cbuff[64];
     1512    sprintf(cbuff,"%g",res);
     1513    string vv = cbuff;
     1514    SetVariable(tokens[0],vv);
     1515    return 0;
     1516  }
     1517  catch (RPNExprException& rpnerr) {
     1518    cerr << " rpneval: Syntax error - Msg=" << rpnerr.Msg()
     1519         << " \n Line=" << toks << endl;
     1520    return 98;
     1521  }
    17591522}
    17601523else if (kw == "echo") {
Note: See TracChangeset for help on using the changeset viewer.