逆波兰运算器浮点求值(c++版本)

来源:互联网 发布:linux入门教程 推荐 编辑:程序博客网 时间:2024/06/07 06:24

//--calculator_of_double.h   //----头文件

/*----------浮点求值运算器-----------*//*-------框架--------*//*1.判断匹配a.搞个临时栈tmpS存所有()b.判断tmpS的匹配遇到(进遇到)若空,则假若非空,即是(,则弹(;ch++返回tmpS空即是匹配真2.总函数1.若数字则s++,并后续,转double,push2.若字符switch优先级,分别处理A.<,pushB.=,pop,s++C,>,双目运算,push*/#ifndef Calculator_of_double_#define Calculator_of_double_#include<stack>using namespace std;class CalculatorOD{const char * ExpArray;//----输入的表达式s.assign(cp)用c指针串替换stack<double> TmpDoubS;//----double临时变动栈stack<char> TmpSymbolS;//----临时运算符栈;public:CalculatorOD(const char*);bool JudgeMarray();//----检测括号配对情况void Calculat();char OrderBetween(char, char);<span style="white-space:pre"></span>//----求优先级double GetResult(double, char, double);<span style="white-space:pre"></span>//----双目求值int factorial(int);//----单目阶乘};#endif // !Calculator_of_double_


//----calculator_of_double.cpp  //----定义文件

#include"calculator_of_double.h"#include<iostream>#include<stack>#include<ctype.h>#include<stdlib.h>#include<math.h>#include<stdio.h>CalculatorOD::CalculatorOD(const char *chPtr){ExpArray = chPtr;printf("\n输入的是:%s\n", ExpArray);Calculat();}bool CalculatorOD::JudgeMarray(){stack<char> TmpS;for (int i = 0; ExpArray[i] != '\0'; i++){if (ExpArray[i] == '(')TmpS.push(ExpArray[i]);else if (ExpArray[i] == ')'){if (!TmpS.empty())TmpS.pop();elsereturn false;}}return TmpS.empty();}void CalculatorOD::Calculat(){if (!JudgeMarray()){cout << "输入不配对!\n"<<endl;return;}TmpSymbolS.push('\0');for (int i = 0, j = 0; !TmpSymbolS.empty();){char TmpStr[100/*sizeof(ExpArray) / sizeof(ExpArray[0])*/];//----存储浮点字符串;if (!isdigit(ExpArray[i])){//----跳过非浮点switch (OrderBetween(TmpSymbolS.top(), ExpArray[i])){case '<':TmpSymbolS.push(ExpArray[i++]); break;case '=':TmpSymbolS.pop();i++;break;case '>':double TemD1 = TmpDoubS.top(); TmpDoubS.pop();char TemSbl = TmpSymbolS.top(); TmpSymbolS.pop();if (TemSbl == '!')TmpDoubS.push(factorial(static_cast<int>(TemD1)));else{double TemD2 = TmpDoubS.top(); TmpDoubS.pop();TmpDoubS.push(GetResult(TemD1, TemSbl, TemD2));}break;}}while (isdigit(ExpArray[i]) || ExpArray[i] == '.'){TmpStr[j++] = ExpArray[i++];<span style="white-space:pre"></span>//----复制TmpStr[j] = '\0';//----每个字符后添加'\0'if (!isdigit(ExpArray[i]) && ExpArray[i] != '.'){TmpDoubS.push(atof(TmpStr));<span style="white-space:pre"></span>//----转化为浮点存栈里,atof(char*p以空NULL结尾)j = 0;//----继续没串头0索引开始//printf("%15.5lf", TmpDoubS.top());}}}int ival = 5;printf("求得的结果是:%15.8lf\n\n", TmpDoubS.top());}char CalculatorOD::OrderBetween(char chStack, char chNew){const char SymbolSheet[10][10] =   // 运算符优先等级 [栈顶][ 当前]//  |--------------- 当前运算符--------------|{ '+', '-', '*', '/', '^', '!', '(', ')', '\0', '0', '>', '>', '<', '<', '<', '<', '<', '>', '>', '+'// + -, '>', '>', '<', '<', '<', '<', '<', '>', '>', '-'// - |, '>', '>', '>', '>', '<', '<', '<', '>', '>', '*'// * 栈, '>', '>', '>', '>', '<', '<', '<', '>', '>', '/'// / 顶, '>', '>', '>', '>', '>', '<', '<', '>', '>', '^'// ^ 运, '>', '>', '>', '>', '>', '>', '<', '>', '>', '!'// ! 算, '<', '<', '<', '<', '<', '<', '<', '=', ' ', '('// ( 符, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ')'// ) |, '<', '<', '<', '<', '<', '<', '<', ' ', '=', '\0'// \0--};char chS = chStack;//'(';//----栈char chCur = chNew;// ')';//----当前int X, Y;for (int i = 0; i <= 9; i++)if (SymbolSheet[i][9] == chS){X = i; break;}for (int i = 0; i <= 9; i++)if (SymbolSheet[0][i] == chCur){Y = i; break;}/*cout << endl << X << ',' << Y << endl;cout << SymbolSheet[X][Y] << endl;*///----测试用return SymbolSheet[X][Y];}double CalculatorOD::GetResult(double D2, char Syb, double D1){switch (Syb){case'+':return D1 + D2;case'-':return D1 - D2;case'/':return D1 / D2;case'*':return D1 * D2;case'^':return pow(D1, D2);/*default:return 0;*/}return 0;//----无意义的,防止编译警告无全部return}int CalculatorOD::factorial(int ival){if (ival == 0)return 1;return ival*factorial(ival - 1);}

//----main.cpp--//----程序入口文件

#include<conio.h>#include"calculator_of_double.h"#include<iostream>using namespace std;int main(){CalculatorOD calA("32^(1/5)^(1/2)");CalculatorOD calB("(1*(3+2)-3)/2)");CalculatorOD calC("32.1^(1/5.09)^(1.19/2.13)");CalculatorOD calD("(1+2^3!-4)*(5!-(6-(7-(89-0!))))");return _getch();}/*--------以下可实现--------- "32^(1/5)^(1/2)"得到1.41 "(1+2^3!-4)*(5!-(6-(7-(89-0!))))"  <span style="white-space:pre"></span>得到2013 "32.1^(1/5.09)^(1.19/2.13)"得到1.46338 ------以下风格不可实现------- 32^(-5) -3+1 */




0 0