int calculate(string s) { stack<char> optr; stack<int> opnd; optr.push('#'); s.push_back('#'); int i = 0; while (i < s.length()) { if (s[i] == ' ') { i++; continue; } if (isdigit(s[i])) { int num = 0; while (isdigit(s[i])) num = 10 * num + s[i++] - '0'; opnd.push(num); continue; } switch(precede(optr.top(), s[i])) { case '<': optr.push(s[i++]); break; case '=': optr.pop(); i++; break; case '>': char theta = optr.top(); optr.pop(); int b = opnd.top(); opnd.pop(); int a = opnd.top(); opnd.pop(); int c = operate(a, theta, b); opnd.push(c); break; } } return opnd.top(); }
float EvaluateExpression(char* MyExpression) { // 算法3.4 // 算术表达式求值的算符优先算法。 // 设OPTR和OPND分别为运算符栈和运算数栈,OP为运算符集合。 StackChar OPTR; // 运算符栈,字符元素 StackFloat OPND; // 运算数栈,实数元素 char TempData[20]; float Data,a,b; char theta,*c,x,Dr[2]; InitStack (OPTR); Push (OPTR, '#'); InitStack (OPND); c = MyExpression; strcpy(TempData,"\0"); while (*c!= '#' || GetTop(OPTR)!= '#') { if (!In(*c, OPSET)) { Dr[0]=*c; Dr[1]='\0'; strcat(TempData,Dr); c++; if(In(*c,OPSET)) { Data=(float)atof(TempData); Push(OPND, Data); strcpy(TempData,"\0"); } } else { // 不是运算符则进栈 switch (precede(GetTop(OPTR), *c)) { case '<': // 栈顶元素优先权低 Push(OPTR, *c); c++; break; case '=': // 脱括号并接收下一字符 Pop(OPTR, x); c++; break; case '>': // 退栈并将运算结果入栈 Pop(OPTR, theta); Pop(OPND, b); Pop(OPND, a); Push(OPND, Operate(a, theta, b)); break; } // switch } } // while return GetTop(OPND); } // EvaluateExpression
float EvaluateExpression(char* MyExpression) { // 算术表达式求值的算符优先算法 // 设OPTR和OPND分别为运算符栈和运算数栈,OP为运算符集合 SC *OPTR = NULL; // 运算符栈,字符元素 SF *OPND = NULL; // 运算数栈,实数元素 char TempData[20]; float Data, a, b; char theta, *c, Dr[] = { '#', '\0' }; OPTR = Push(OPTR, '#'); c = strcat(MyExpression, Dr); strcpy(TempData, "\0"); //字符串拷贝函数 while (*c != '#' || OPTR->c != '#') { if (!In(*c, OPSET)) { Dr[0] = *c; strcat(TempData, Dr); //字符串连接函数 c++; if (In(*c, OPSET)) { Data = atof(TempData); //字符串转换函数(double) OPND = Push(OPND, Data); strcpy(TempData, "\0"); } } else // 不是运算符则进栈 { switch (precede(OPTR->c, *c)) { case '<': // 栈顶元素优先级低 OPTR = Push(OPTR, *c); c++; break; case '=': // 脱括号并接收下一字符 OPTR = Pop(OPTR); c++; break; case '>': // 退栈并将运算结果入栈 theta = OPTR->c; OPTR = Pop(OPTR); b = OPND->f; OPND = Pop(OPND); a = OPND->f; OPND = Pop(OPND); OPND = Push(OPND, Operate(a, theta, b)); break; } //switch } } //while return OPND->f; } //EvaluateExpression
void Expression::toPostfix() { List<Token> operators; //if(tokens.size()<3) throw(parse); for(int x = 0;x<tokens.size();x++) { if(tokens[x].type==VALUE) { // if token is numeric value //if(x<tokens.size()-1 and tokens[x+1].type!=OPERATOR) throw parse; postfix.push_back(tokens[x]); } else if(tokens[x].type==OPERATOR) { // if the token is operator if(x==tokens.size()-1) throw(parse); if(operators.isEmpty())operators.append(tokens[x]); else if(strcmp(tokens[x].str, "#")==0){operators.append(tokens[x]);} else if(!operators.isEmpty() and strcmp(operators.topVal().str, "#")==0){ postfix.push_back(operators.pop()); operators.append(tokens[x]); } else if (!operators.isEmpty() and operators.topVal().type==FUNCTION) { postfix.push_back(operators.pop()); if(operators.topVal().type==OPERATOR and precede(operators.topVal().str[0], tokens[x].str[0])){ postfix.push_back(operators.pop()); } operators.append(tokens[x]); } else { while(!operators.isEmpty() and operators.topVal().type==OPERATOR and precede(operators.topVal().str[0], tokens[x].str[0])) { postfix.push_back(operators.pop()); } operators.append(tokens[x]); } } else if(tokens[x].type==LEFT_PARENTHESIS) operators.append(tokens[x]); // if token is left parenthesis else if(tokens[x].type==RIGHT_PARENTHESIS) { // if token is right parenthesis while(operators.topVal().type!=LEFT_PARENTHESIS) { // pop until left parenthesis occurs postfix.push_back(operators.pop()); if(operators.isEmpty())throw(parenthesis); // error in parenthesis } operators.pop(); // popping the last '(' which is not needed in postfix if(!operators.isEmpty() and operators.topVal().type==FUNCTION) postfix.push_back(operators.pop()); } else if(tokens[x].type==VARIABLE) { // if token is a variable if(tokens[x-1].type==VARIABLE or tokens[x+1].type==VARIABLE)throw(parse); // error, two variables at same place // if(x<tokens.size()-1 and tokens[x+1].type!=OPERATOR) throw parse; postfix.push_back(tokens[x]); } else if(tokens[x].type==CONSTANT) { // if token is a constant if(tokens[x-1].type==CONSTANT or tokens[x+1].type==CONSTANT)throw(parse); // error, two constants at same place postfix.push_back(tokens[x]); } else if(tokens[x].type==FUNCTION) { // if token is a function operators.append(tokens[x]); } else ; } // end of for while(!operators.isEmpty()) { // if(operators.topVal().type == LEFT_PARENTHESIS)throw(parenthesis); // error in parenthesis else postfix.push_back(operators.pop()); } /*for(int x = 0; x<postfix.size(); x++) { if(postfix[x].type==VALUE) { cout << postfix[x].numVal<< " "; continue; } cout << postfix[x].str<< " "; } cout << endl; */ }