Changeset 2512 in Sophya for trunk/SophyaLib/SysTools/rpneval.cc
- Timestamp:
- Mar 16, 2004, 12:49:33 AM (22 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/SysTools/rpneval.cc
r2510 r2512 22 22 FillVStringFrString(sex, exe, ' '); 23 23 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 } 25 32 } 26 33 … … 28 35 { 29 36 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 } 31 45 } 32 46 … … 49 63 { 50 64 51 if (args.size() <= off) return 0;65 if (args.size() <= off) return 1; 52 66 double x,y; 53 67 x = y = 0.; 54 stack<double> rpnstack_; // Stack des operations en RPN 68 55 69 for(int k=off; k<args.size(); k++) { 56 70 // Les 4 operations de base + - * / 57 71 if (args[k] == "+") { 58 if ( CheckStack( x, y) ) return (1);72 if ( CheckStack( x, y) ) return k; 59 73 rpnstack_.top() = y+x; 60 74 } 61 75 else if (args[k] == "-") { 62 if ( CheckStack( x, y) ) return (1);76 if ( CheckStack( x, y) ) return k; 63 77 rpnstack_.top() = y-x; 64 78 } 65 79 else if (args[k] == "*") { 66 if ( CheckStack( x, y) ) return (1);80 if ( CheckStack( x, y) ) return k; 67 81 rpnstack_.top() = y*x; 68 82 } 69 83 else if (args[k] == "/") { 70 if ( CheckStack( x, y) ) return (1);84 if ( CheckStack( x, y) ) return k; 71 85 rpnstack_.top() = y/x; 72 86 } 73 87 else if (args[k] == "%") { 74 if ( CheckStack( x, y) ) return (1);88 if ( CheckStack( x, y) ) return k; 75 89 rpnstack_.top() = (int)y % (int)x; 76 90 } … … 84 98 // Les fonctions usuelles a 1 argument f(x) 85 99 else if (args[k] == "cos") { 86 if ( CheckStack( x) ) return (1);100 if ( CheckStack( x) ) return k; 87 101 rpnstack_.top() = cos(x); 88 102 } 89 103 else if (args[k] == "sin") { 90 if ( CheckStack( x) ) return (1);104 if ( CheckStack( x) ) return k; 91 105 rpnstack_.top() = sin(x); 92 106 } 93 107 else if (args[k] == "tan") { 94 if ( CheckStack( x) ) return (1);108 if ( CheckStack( x) ) return k; 95 109 rpnstack_.top() = tan(x); 96 110 } 97 111 else if (args[k] == "acos") { 98 if ( CheckStack( x) ) return (1);112 if ( CheckStack( x) ) return k; 99 113 rpnstack_.top() = acos(x); 100 114 } 101 115 else if (args[k] == "asin") { 102 if ( CheckStack( x) ) return (1);116 if ( CheckStack( x) ) return k; 103 117 rpnstack_.top() = asin(x); 104 118 } 105 119 else if (args[k] == "atan") { 106 if ( CheckStack( x) ) return (1);120 if ( CheckStack( x) ) return k; 107 121 rpnstack_.top() = atan(x); 108 122 } 109 123 else if (args[k] == "chs") { 110 if ( CheckStack( x) ) return (1);124 if ( CheckStack( x) ) return k; 111 125 rpnstack_.top() = -x; 112 126 } 113 127 else if (args[k] == "sqrt") { 114 if ( CheckStack( x) ) return (1);128 if ( CheckStack( x) ) return k; 115 129 rpnstack_.top() = sqrt(x); 116 130 } 117 131 else if (args[k] == "sq") { // x^2 118 if ( CheckStack( x) ) return (1);132 if ( CheckStack( x) ) return k; 119 133 rpnstack_.top() = x*x; 120 134 } 121 135 else if (args[k] == "log") { 122 if ( CheckStack( x) ) return (1);136 if ( CheckStack( x) ) return k; 123 137 rpnstack_.top() = log(x); 124 138 } 125 139 else if (args[k] == "log10") { 126 if ( CheckStack( x) ) return (1);140 if ( CheckStack( x) ) return k; 127 141 rpnstack_.top() = log10(x); 128 142 } 129 143 else if (args[k] == "exp") { 130 if ( CheckStack( x) ) return (1);144 if ( CheckStack( x) ) return k; 131 145 rpnstack_.top() = exp(x); 132 146 } 133 147 else if (args[k] == "fabs") { 134 if ( CheckStack( x) ) return (1);148 if ( CheckStack( x) ) return k; 135 149 rpnstack_.top() = fabs(x); 136 150 } 137 151 else if (args[k] == "floor") { 138 if ( CheckStack( x) ) return (1);152 if ( CheckStack( x) ) return k; 139 153 rpnstack_.top() = floor(x); 140 154 } 141 155 else if (args[k] == "ceil") { 142 if ( CheckStack( x) ) return (1);156 if ( CheckStack( x) ) return k; 143 157 rpnstack_.top() = ceil(x); 144 158 } 145 159 // trunc et nint vire - ca ne compile pas sous linux - Reza 01/2003 146 160 else if (args[k] == "deg2rad") { 147 if ( CheckStack( x) ) return (1);161 if ( CheckStack( x) ) return k; 148 162 rpnstack_.top() = x*M_PI/180.; 149 163 } 150 164 else if (args[k] == "rad2deg") { 151 if ( CheckStack( x) ) return (1);165 if ( CheckStack( x) ) return k; 152 166 rpnstack_.top() = x*180./M_PI; 153 167 } 154 168 // Les fonctions usuelles a 2 argument f(x,y) 155 169 else if (args[k] == "pow") { 156 if ( CheckStack( x, y) ) return (1);170 if ( CheckStack( x, y) ) return k; 157 171 rpnstack_.top() = pow(y,x); 158 172 } 159 173 else if (args[k] == "atan2") { 160 if ( CheckStack( x, y) ) return (1);174 if ( CheckStack( x, y) ) return k; 161 175 rpnstack_.top() = atan2(x,y); 162 176 } … … 188 202 double px; 189 203 int nn = ProductStack( px); 190 if (nn == 0) return (1);204 if (nn == 0) return k; 191 205 rpnstack_.push(px); 192 206 } … … 196 210 } 197 211 else if (args[k] == "x<>y") { 198 if ( CheckStack( x, y) ) return (1);212 if ( CheckStack( x, y) ) return k; 199 213 rpnstack_.top() = x; rpnstack_.push(y); 200 214 } … … 211 225 x = strtod(args[k].c_str(), &esptr); 212 226 // 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; 214 228 rpnstack_.push(x); 215 229 } 216 230 217 231 } 218 return( 0);232 return(args.size()+1); 219 233 } 220 234
Note:
See TracChangeset
for help on using the changeset viewer.