#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();
else
return 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--//----程序入口文件