bf算法思想解析

表达式求值

#include //输入的表达式要以'#'结尾,如‘5+6*3/(3-1)#’ #include #include #include #include using namespace std;

stack opter; //运算符栈 stack opval; //操作数栈

int getIndex(char theta) //获取theta所对应的索引 {

int index = 0; switch (theta) { case '+': index = 0; break; case '-': index = 1;

break; case '*': index = 2; break; case '/': index = 3; break; case '(': index = 4; break; case ')': index = 5; break; case '#': index = 6; default:break; }

return index; }

char getPriority(char theta1, char theta2) //theta2之间的优先级

获取theta1与{

const char priority[][7] = //算符间的优先级关系 {

{ '>','>','<','<','<','>','>' }, { '>','>','<','<','<','>','>' }, { '>','>','>','>','<','>','>' }, { '>','>','>','>','<','>','>' }, { '<','<','<','<','<','=','0' }, { '>','>','>','>','0','>','>' }, { '<','<','<','<','<','0','=' }, };

int index1 = getIndex(theta1); int index2 = getIndex(theta2); return priority[index1][index2]; }

double calculate(double b, char theta, double a) //theta a {

switch (theta) {

计算b

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4