#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--//----程序入口文件