Changeset 2512 in Sophya for trunk/SophyaLib/SysTools/rpneval.cc


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/rpneval.cc

    r2510 r2512  
    2222  FillVStringFrString(sex, exe, ' ');
    2323  int rc = EvalRPNExpr(exe, 0);
    24   if (rc != 0) throw RPNExprException("RPNExpressionEvaluator() - syntax error"); 
     24  if (rc < exe.size()) {
     25    string msg = "RPNExpressionEvaluator() - syntax error near ";
     26    msg += exe[rc];
     27    char buff[32];
     28    sprintf(buff," (word %d)",rc);
     29    msg += buff;
     30    throw RPNExprException(msg);
     31  }
    2532}
    2633
     
    2835{
    2936  int rc = EvalRPNExpr(exe, off);
    30   if (rc != 0) throw RPNExprException("RPNExpressionEvaluator() - syntax error");
     37  if (rc < exe.size()) {
     38    string msg = "RPNExpressionEvaluator() - syntax error near ";
     39    msg += exe[rc];
     40    char buff[32];
     41    sprintf(buff," (word %d)",rc);
     42    msg += buff;
     43    throw RPNExprException(msg);
     44  }
    3145}
    3246
     
    4963{
    5064 
    51   if (args.size() <= off)  return 0;
     65  if (args.size() <= off)  return 1;
    5266  double x,y;
    5367  x = y = 0.;
    54   stack<double> rpnstack_;  // Stack des operations en RPN
     68
    5569  for(int k=off; k<args.size(); k++) {
    5670    // Les 4 operations de base + - * /
    5771    if (args[k] == "+") {
    58       if ( CheckStack( x, y) ) return(1);
     72      if ( CheckStack( x, y) ) return k;
    5973      rpnstack_.top() = y+x;
    6074    }
    6175    else if (args[k] == "-") {
    62       if ( CheckStack( x, y) ) return(1);
     76      if ( CheckStack( x, y) ) return k;
    6377      rpnstack_.top() = y-x;
    6478    }
    6579    else if (args[k] == "*") {
    66       if ( CheckStack( x, y) ) return(1);
     80      if ( CheckStack( x, y) ) return k;
    6781      rpnstack_.top() = y*x;
    6882    }
    6983    else if (args[k] == "/") {
    70       if ( CheckStack( x, y) ) return(1);
     84      if ( CheckStack( x, y) ) return k;
    7185      rpnstack_.top() = y/x;
    7286    }
    7387    else if (args[k] == "%") {
    74       if ( CheckStack( x, y) ) return(1);
     88      if ( CheckStack( x, y) ) return k;
    7589      rpnstack_.top() = (int)y % (int)x;
    7690    }
     
    8498    // Les fonctions usuelles a 1 argument f(x)
    8599    else if (args[k] == "cos") {
    86       if ( CheckStack( x) ) return(1);
     100      if ( CheckStack( x) ) return k;
    87101      rpnstack_.top() = cos(x);
    88102    }
    89103    else if (args[k] == "sin") {
    90       if ( CheckStack( x) ) return(1);
     104      if ( CheckStack( x) ) return k;
    91105      rpnstack_.top() = sin(x);
    92106    }
    93107    else if (args[k] == "tan") {
    94       if ( CheckStack( x) ) return(1);
     108      if ( CheckStack( x) ) return k;
    95109      rpnstack_.top() = tan(x);
    96110    }
    97111    else if (args[k] == "acos") {
    98       if ( CheckStack( x) ) return(1);
     112      if ( CheckStack( x) ) return k;
    99113      rpnstack_.top() = acos(x);
    100114    }
    101115    else if (args[k] == "asin") {
    102       if ( CheckStack( x) ) return(1);
     116      if ( CheckStack( x) ) return k;
    103117      rpnstack_.top() = asin(x);
    104118    }
    105119    else if (args[k] == "atan") {
    106       if ( CheckStack( x) ) return(1);
     120      if ( CheckStack( x) ) return k;
    107121      rpnstack_.top() = atan(x);
    108122    }
    109123    else if (args[k] == "chs") {
    110       if ( CheckStack( x) ) return(1);
     124      if ( CheckStack( x) ) return k;
    111125      rpnstack_.top() = -x;
    112126    }
    113127    else if (args[k] == "sqrt") {
    114       if ( CheckStack( x) ) return(1);
     128      if ( CheckStack( x) ) return k;
    115129      rpnstack_.top() = sqrt(x);
    116130    }
    117131    else if (args[k] == "sq") {  // x^2
    118       if ( CheckStack( x) ) return(1);
     132      if ( CheckStack( x) ) return k;
    119133      rpnstack_.top() = x*x;
    120134    }
    121135    else if (args[k] == "log") {
    122       if ( CheckStack( x) ) return(1);
     136      if ( CheckStack( x) ) return k;
    123137      rpnstack_.top() = log(x);
    124138    }
    125139    else if (args[k] == "log10") {
    126       if ( CheckStack( x) ) return(1);
     140      if ( CheckStack( x) ) return k;
    127141      rpnstack_.top() = log10(x);
    128142    }
    129143    else if (args[k] == "exp") {
    130       if ( CheckStack( x) ) return(1);
     144      if ( CheckStack( x) ) return k;
    131145      rpnstack_.top() = exp(x);
    132146    }
    133147    else if (args[k] == "fabs") {
    134       if ( CheckStack( x) ) return(1);
     148      if ( CheckStack( x) ) return k;
    135149      rpnstack_.top() = fabs(x);
    136150    }
    137151    else if (args[k] == "floor") {
    138       if ( CheckStack( x) ) return(1);
     152      if ( CheckStack( x) ) return k;
    139153      rpnstack_.top() = floor(x);
    140154    }
    141155    else if (args[k] == "ceil") {
    142       if ( CheckStack( x) ) return(1);
     156      if ( CheckStack( x) ) return k;
    143157      rpnstack_.top() = ceil(x);
    144158    }
    145159    // trunc et nint vire - ca ne compile pas sous linux - Reza 01/2003
    146160    else if (args[k] == "deg2rad") {
    147       if ( CheckStack( x) ) return(1);
     161      if ( CheckStack( x) ) return k;
    148162      rpnstack_.top() = x*M_PI/180.;
    149163    }
    150164    else if (args[k] == "rad2deg") {
    151       if ( CheckStack( x) ) return(1);
     165      if ( CheckStack( x) ) return k;
    152166      rpnstack_.top() = x*180./M_PI;
    153167    }
    154168    // Les fonctions usuelles a 2 argument f(x,y)
    155169    else if (args[k] == "pow") {
    156       if ( CheckStack( x, y) ) return(1);
     170      if ( CheckStack( x, y) ) return k;
    157171      rpnstack_.top() = pow(y,x);
    158172    }
    159173    else if (args[k] == "atan2") {
    160       if ( CheckStack( x, y) ) return(1);
     174      if ( CheckStack( x, y) ) return k;
    161175      rpnstack_.top() = atan2(x,y);
    162176    }
     
    188202      double px;
    189203      int nn = ProductStack( px);
    190       if (nn == 0) return(1);
     204      if (nn == 0) return k;
    191205      rpnstack_.push(px);
    192206    }
     
    196210    }
    197211    else if (args[k] == "x<>y") {
    198       if ( CheckStack( x, y) ) return(1);
     212      if ( CheckStack( x, y) ) return k;
    199213      rpnstack_.top() = x;  rpnstack_.push(y);
    200214    }
     
    211225      x = strtod(args[k].c_str(), &esptr);
    212226      //      if (ctof(args[k].c_str(),&x) < 0) {
    213       if (esptr == args[k].c_str()) return 2;
     227      if (esptr == args[k].c_str()) return k;
    214228      rpnstack_.push(x);
    215229    }
    216230
    217231  }
    218   return(0);
     232  return(args.size()+1);
    219233}
    220234
Note: See TracChangeset for help on using the changeset viewer.