可以計算階乘次方的大數計算器
阿新 • • 發佈:2018-12-30
#include<iostream> #include<vector> #include<string> #include<cstdio> #include<ctime> using namespace std; #define LEFT '(' #define RIGHT ')' #define THPOWER '^' #define FACTORIAL '!' #define MUL '*' #define DIV '/' #define ADD '+' #define SUB '-' #define MINUS '#' #define DIGIT 4 #define ARY 10000 //計算器類 class calculator { private: string import; //輸入 string outport; //輸出 vector<string> res_import; //轉換後的輸入 public: calculator(); ~calculator(); //輸入 bool PutIn(); //將輸入的公式轉換成逆波蘭形式儲存到vector bool Change(); //得到結果 void GetResult(); //輸出 void PutOut(); //計算 string DoCalculator(const string &a, const string &b, const char &fuhao); //優先順序判斷 是否低於前面 bool IsLow(const char &a, const char &b); //優先順序計算 int FirstCode(const char &a); //字串分成整數和小數部分 void DoCalBase(const string &a, const string &b, vector<int> &int_a_i, vector<int> &int_b_i, vector<int> &int_a_f, vector<int> &int_b_f); //字串對其補0 void DoToAlign(vector<int> &int_a_i, vector<int> &int_b_i, vector<int> &int_a_f, vector<int> &int_b_f); //字串去掉. void DoToDelPoint(int &n, const string &a, const string &b, vector<int> &int_a_i, vector<int> &int_b_i); //去掉前面的0 void DoDelFront(string &a); //去掉後面的0 void DoDelBack(string &a); //將兩個數字都化為整數(給除法用) void DoToBeInt(const string &a, const string &b, string &str_a_i, string &str_b_i); //加 string DoAdd(const string &a, const string &b); //減 string DoSub(const string &a, const string &b); //乘 string DoMul(const string &a, const string &b); //除 string DoDiv(const string &a, const string &b); //次方 string DoTHPOWER(const string &a, const string &b); //階乘 string DoFACTORIAL(const string &a); //負號 string DoNegativeNumber(const string &a); }; calculator::calculator() :import(""), outport(""), res_import(NULL) { } calculator::~calculator() { } bool calculator::PutIn() { import = ""; getline(cin, import); if (import == "") return false; else return true; } bool calculator::Change() { vector<string> temp; //用來臨時儲存操作符號的 string temp_str = ""; //用來臨時儲存數字 for (int i = 0; i < import.size(); i++) { string temp_minus = ""; //數字 if ((import[i] >= '0' && import[i] <= '9') || import[i] == '.') { temp_str.push_back(import[i]); } else if (import[i] == LEFT) { if (i > 0 && import[i - 1] != LEFT && import[i - 1] != THPOWER && import[i - 1] && FACTORIAL && import[i - 1] != MUL && import[i - 1] != DIV && import[i - 1] != ADD && import[i - 1] != SUB && import[i - 1] != MINUS) { if (temp_str != "") { res_import.push_back(temp_str); temp_str = ""; } temp.push_back("*"); } temp_minus = import[i]; temp.push_back(temp_minus); } else if (import[i] == RIGHT) { //把整個數字壓入 if (temp_str != "") { res_import.push_back(temp_str); temp_str = ""; } //彈出括號裡面的符號 if (temp.size() > 0) { string fuhao = temp.at(temp.size() - 1); while (fuhao[0] != LEFT) { temp.pop_back(); res_import.push_back(fuhao); if (temp.size() <= 0) break; fuhao = temp.at(temp.size() - 1); } //彈出左括號 temp.pop_back(); } } else if (import[i] == THPOWER || import[i] == FACTORIAL || import[i] == MUL || import[i] == DIV || import[i] == ADD || import[i] == SUB || import[i] == MINUS) { //把整個數字壓入 if (temp_str != "") { res_import.push_back(temp_str); temp_str = ""; } //如果是負數 則特殊處理 if (import[i] == SUB) { if (i == 0 || import[i - 1] == LEFT || import[i - 1] == ADD || import[i - 1] == SUB || import[i - 1] == MUL || import[i - 1] == DIV || import[i - 1] == THPOWER || import[i - 1] == FACTORIAL || import[i - 1] == MINUS ) { //壓入#代表負數符號 import[i] = MINUS; } } //判斷優先順序 如果比前面的低 則彈出前面一個 if (temp.size() > 0) { string fuhao = temp.at(temp.size() - 1); //前一個 while (IsLow(import[i], fuhao[0])) { temp.pop_back(); if (fuhao[0] == SUB && res_import.at(res_import.size() - 1)[0] == SUB) { res_import.at(res_import.size() - 1)[0] = '+'; } res_import.push_back(fuhao); if (temp.size() <= 0) break; fuhao = temp.at(temp.size() - 1); } } //壓入這個符號 temp_minus = import[i]; temp.push_back(temp_minus); } } //把整個數字壓入 用於最後一個字元是數字的情況 if (temp_str != "") { res_import.push_back(temp_str); temp_str = ""; } //將符號全部取出 for (; temp.size() > 0;) { string fuhao = temp.at(temp.size() - 1); temp.pop_back(); if (fuhao[0] == SUB && res_import.at(res_import.size() - 1)[0] == SUB) { res_import.at(res_import.size() - 1)[0] = '+'; } res_import.push_back(fuhao); if (temp.size() <= 0) break; fuhao = temp.at(temp.size() - 1); } //轉換成功與否 if (res_import.size() > 0) return true; else return false; } void calculator::PutOut() { cout << "計算結果為:" << outport.c_str() << endl; } bool calculator::IsLow(const char &a, const char &b) { if (FirstCode(a) < FirstCode(b)) return true; else return false; } int calculator::FirstCode(const char &a) { switch (a) { case LEFT: case RIGHT: return 0; case THPOWER: return 9; case FACTORIAL: return 8; case MUL: return 5; case DIV: return 6; case ADD: return 2; case SUB: return 3; case MINUS: return 9; } return 0; } void calculator::GetResult() { outport = ""; vector<string> temp; //用於臨時計算結果用的棧 string temp_str = "";//臨時儲存數字 for (int i = 0; i < res_import.size(); i++) { if (res_import[i][0] == FACTORIAL || res_import[i][0] == MINUS ) //如果是階乘或者符號 則只需要一元 { if (temp.size() < 1) return ; string res = DoCalculator(temp[temp.size() - 1], "", res_import[i][0]); if (res == "") return ; temp.pop_back(); //彈出上一個數字 temp.push_back(res); //壓入計算後的結果 } else if (res_import[i].size() == 1 && (res_import[i][0] == THPOWER || res_import[i][0] == MUL || res_import[i][0] == DIV || res_import[i][0] == ADD || res_import[i][0] == SUB)) { if (temp.size() < 2) return ; string res = DoCalculator(temp[temp.size() - 2], temp[temp.size() - 1], res_import[i][0]); if (res == "") return ; temp.pop_back(); //彈出上一個數字 temp.pop_back(); //彈出上一個數字 temp.push_back(res);//壓入計算後的結果 } else //數字 { //直接壓入臨時棧 temp.push_back(res_import[i]); } } outport = temp[0]; return ; } string calculator::DoCalculator(const string &a, const string &b, const char &fuhao) { string the_res = ""; switch (fuhao) { case MINUS: the_res = DoNegativeNumber(a); break; case FACTORIAL: the_res = DoFACTORIAL(a); break; case THPOWER: the_res = DoTHPOWER(a,b); break; case MUL: the_res = DoMul(a, b); break; case DIV: the_res = DoDiv(a, b); break; case ADD: the_res = DoAdd(a, b); break; case SUB: the_res = DoSub(a, b); break; } return the_res; } void calculator::DoCalBase(const string &a, const string &b, vector<int> &int_a_i, vector<int> &int_b_i, vector<int> &int_a_f, vector<int> &int_b_f) { string str_a_i = ""; string str_b_i = ""; string str_a_f = ""; string str_b_f = ""; int_a_i.clear(); int_b_i.clear(); int_a_f.clear(); int_b_f.clear(); //把數字的整數和小數部分分別分割 bool is_i = true; for (int i = 0; i < a.size(); i++) { if (a[i] == '-') continue; if (a[i] == '.') { is_i = false; continue; } if (is_i) { str_a_i += a[i]; } else { str_a_f += a[i]; } } is_i = true; for (int i = 0; i < b.size(); i++) { if (b[i] == '-') continue; if (b[i] == '.') { is_i = false; continue; } if (is_i) { str_b_i += b[i]; } else { str_b_f += b[i]; } } //將小數部分對齊 DoDelBack(str_a_f); DoDelBack(str_b_f); int f_max = str_a_f.size() - str_b_f.size(); if (f_max > 0) { for (int i = 0; i < f_max; i++) { str_b_f = str_b_f + "0"; } } else if (f_max < 0) { f_max = -f_max; for (int i = 0; i < f_max; i++) { str_a_f = str_a_f + "0"; } } int bunumber = str_a_f.size() % DIGIT; if (bunumber > 0) { for (int i = 0; i < DIGIT - bunumber; i++) { str_a_f = str_a_f + "0"; str_b_f = str_b_f + "0"; } } //每N位分割 int number = str_a_i.size() / DIGIT + 1; //多少個元素 int count = str_a_i.size() % DIGIT; //第一個元素有多少個字元 string str_temp = ""; int now_count = 0; for (; now_count < count; now_count++) { str_temp = str_temp + str_a_i[now_count]; } if (str_temp != "") { int_a_i.push_back(atoi(str_temp.c_str())); str_temp = ""; } for (int j = 1; j < number; j++) { bool is_push = false; for (int i = 0; i < DIGIT; i++, now_count++) { if (str_a_i[now_count] != '0') is_push = true; if (is_push) str_temp = str_temp + str_a_i[now_count]; } if (str_temp == "") str_temp = "0"; int_a_i.push_back(atoi(str_temp.c_str())); str_temp = ""; } number = str_b_i.size() / DIGIT + 1; //多少個元素 count = str_b_i.size() % DIGIT; //第一個元素有多少個字元 str_temp = ""; now_count = 0; for (; now_count < count; now_count++) { str_temp = str_temp + str_b_i[now_count]; } if (str_temp != "") { int_b_i.push_back(atoi(str_temp.c_str())); str_temp = ""; } for (int j = 1; j < number; j++) { bool is_push = false; for (int i = 0; i < DIGIT; i++, now_count++) { if (str_b_i[now_count] != '0') is_push = true; if (is_push) str_temp = str_temp + str_b_i[now_count]; } if (str_temp == "") str_temp = "0"; int_b_i.push_back(atoi(str_temp.c_str())); str_temp = ""; } //f number = str_a_f.size() / DIGIT + 1; //多少個元素 count = str_a_f.size() % DIGIT; //第一個元素有多少個字元 str_temp = ""; now_count = 0; for (; now_count < count; now_count++) { str_temp = str_temp + str_a_f[now_count]; } if (str_temp != "") { int_a_f.push_back(atoi(str_temp.c_str())); str_temp = ""; } for (int j = 1; j < number; j++) { bool is_push = false; for (int i = 0; i < DIGIT; i++, now_count++) { if (str_a_f[now_count] != '0') is_push = true; if (is_push) str_temp = str_temp + str_a_f[now_count]; } if (str_temp == "") str_temp = "0"; int_a_f.push_back(atoi(str_temp.c_str())); str_temp = ""; } number = str_b_f.size() / DIGIT + 1; //多少個元素 count = str_b_f.size() % DIGIT; //第一個元素有多少個字元 str_temp = ""; now_count = 0; for (; now_count < count; now_count++) { str_temp = str_temp + str_b_f[now_count]; } if (str_temp != "") { int_b_f.push_back(atoi(str_temp.c_str())); str_temp = ""; } for (int j = 1; j < number; j++) { bool is_push = false; for (int i = 0; i < DIGIT; i++, now_count++) { if (str_b_f[now_count] != '0') is_push = true; if (is_push) str_temp = str_temp + str_b_f[now_count]; } if (str_temp == "") str_temp = "0"; int_b_f.push_back(atoi(str_temp.c_str())); str_temp = ""; } } //字串對其補0 void calculator::DoToAlign(vector<int> &int_a_i, vector<int> &int_b_i, vector<int> &int_a_f, vector<int> &int_b_f) { int i_max = int_a_i.size() - int_b_i.size(); if (i_max > 0) { for (int i = 0; i < i_max; i++) { int_b_i.insert(int_b_i.begin(), 0); } } else if (i_max < 0) { i_max = -i_max; for (int i = 0; i < i_max; i++) { int_a_i.insert(int_a_i.begin(), 0); } } int f_max = int_a_f.size() - int_b_f.size(); if (f_max > 0) { for (int i = 0; i < f_max; i++) { int_b_f.push_back(0); } } else if (f_max < 0) { f_max = -f_max; for (int i = 0; i < f_max; i++) { int_a_f.push_back(0); } } } //去掉小數點 記錄以後小數點要在倒數第幾個 void calculator::DoToDelPoint(int &n, const string &a, const string &b, vector<int> &int_a_i, vector<int> &int_b_i) { n = 0; string str_a_i = ""; string str_b_i = ""; int_a_i.clear(); int_b_i.clear(); bool is_dian = false; for (int i = 0; i < a.size(); i++) { if (a[i] == '.') { is_dian = true; continue; } if (is_dian) n++; str_a_i += a[i]; } is_dian = false; for (int i = 0; i < b.size(); i++) { if (b[i] == '.') { is_dian = true; continue; } if (is_dian) n++; str_b_i += b[i]; } DoDelFront(str_a_i); DoDelFront(str_b_i); //每N位分割 int number = str_a_i.size() / DIGIT + 1; //多少個元素 int count = str_a_i.size() % DIGIT; //第一個元素有多少個字元 string str_temp = ""; int now_count = 0; for (; now_count < count; now_count++) { str_temp = str_temp + str_a_i[now_count]; } if (str_temp != "") { int_a_i.push_back(atoi(str_temp.c_str())); str_temp = ""; } for (int j = 1; j < number; j++) { bool is_push = false; for (int i = 0; i < DIGIT; i++, now_count++) { if (str_a_i[now_count] != '0') is_push = true; if (is_push) str_temp = str_temp + str_a_i[now_count]; } if (str_temp == "") str_temp = "0"; int_a_i.push_back(atoi(str_temp.c_str())); str_temp = ""; } number = str_b_i.size() / DIGIT + 1; //多少個元素 count = str_b_i.size() % DIGIT; //第一個元素有多少個字元 str_temp = ""; now_count = 0; for (; now_count < count; now_count++) { str_temp = str_temp + str_b_i[now_count]; } if (str_temp != "") { int_b_i.push_back(atoi(str_temp.c_str())); str_temp = ""; } for (int j = 1; j < number; j++) { bool is_push = false; for (int i = 0; i < DIGIT; i++, now_count++) { if (str_b_i[now_count] != '0') is_push = true; if (is_push) str_temp = str_temp + str_b_i[now_count]; } if (str_temp == "") str_temp = "0"; int_b_i.push_back(atoi(str_temp.c_str())); str_temp = ""; } } //給除法用 void calculator::DoToBeInt(const string &a, const string &b, string &str_a_i, string &str_b_i) { int n1 = 0; int n2 = 0; str_a_i = ""; str_b_i = ""; bool is_dian = false; for (int i = 0; i < a.size(); i++) { if (a[i] == '.') { is_dian = true; continue; } if (is_dian) n1++; str_a_i += a[i]; } is_dian = false; for (int i = 0; i < b.size(); i++) { if (b[i] == '.') { is_dian = true; continue; } if (is_dian) n2++; str_b_i += b[i]; } DoDelFront(str_a_i); DoDelFront(str_b_i); // int n = n1 - n2; if(n > 0) { for(int i = 0; i < n; i++) { str_b_i.push_back('0'); } } else if(n < 0) { for(int i = 0; i < -n; i++) { str_a_i.push_back('0'); } } if( str_a_i == "") str_a_i = "0"; if( str_b_i == "") str_b_i = "0"; } //去掉前面的0 void calculator::DoDelFront(string &a) { if (a.size() <= 0) return; while (a[0] == '0') { a.erase(a.begin()); if (a.size() <= 0) return; } } //去掉後面的0 void calculator::DoDelBack(string &a) { if (a.size() <= 0) return; while (a[a.size()-1] == '0') { a.erase(a.end() - 1); if (a.size() <= 0) return; } } //加法 string calculator::DoAdd(const string &a, const string &b) { string f_str = ""; string i_str = ""; string is_fushu = ""; vector<int> int_a_i; //a整數部分 vector<int> int_a_f; //a小數部分 vector<int> int_b_i; //b整數部分 vector<int> int_b_f; //b小數部分 string aa = a; string bb = b; if (a[0] == '-') aa.erase(aa.begin()); if (b[0] == '-') bb.erase(bb.begin()); if (a[0] == '-') { if (b[0] == '-') { is_fushu = "-"; } else { return DoSub(bb, aa); } } else { if (b[0] == '-') { return DoSub(aa, bb); } } DoCalBase(aa, bb, int_a_i, int_b_i, int_a_f, int_b_f); DoToAlign(int_a_i, int_b_i, int_a_f, int_b_f); //每N位分別相加 int temp = 0; //進位的數 for (int i = int_a_f.size() - 1; i >= 0; i--) { int res = int_a_f[i] + int_b_f[i] + temp; temp = int(res / ARY); char buffer[1024]; sprintf_s(buffer, "%d\0", res % ARY); string str_temp(buffer); for (int x = str_temp.size(); x < DIGIT; x++) { str_temp = "0" + str_temp; } f_str = str_temp + f_str; } for (int i = int_a_i.size() - 1; i >= 0; i--) { int res = int_a_i[i] + int_b_i[i] + temp; temp = int(res / ARY); char buffer[1024]; sprintf_s(buffer, "%d\0", res % ARY); string str_temp(buffer); for (int x = str_temp.size(); x < DIGIT; x++) { str_temp = "0" + str_temp; } i_str = str_temp + i_str; } if (temp > 0) { char buffer[1024]; sprintf_s(buffer, "%d\0", temp % ARY); string str_temp(buffer); i_str = str_temp + i_str; temp = 0; } DoDelFront(i_str); DoDelBack(f_str); if (i_str == "") i_str = "0"; if (f_str == "") f_str = "0"; string res_str = ""; if (f_str == "0") res_str = i_str; else res_str = i_str + "." + f_str; if (is_fushu == "-") res_str = is_fushu + res_str; return res_str; } //減 string calculator::DoSub(const string &a, const string &b) { string is_fushu = ""; string f_str = ""; string i_str = ""; string aa = a; string bb = b; if (a[0] == '-') aa.erase(aa.begin()); if (b[0] == '-') bb.erase(bb.begin()); vector<int> int_a_i; //a整數部分 vector<int> int_a_f; //a小數部分 vector<int> int_b_i; //b整數部分 vector<int> int_b_f; //b小數部分 if (a[0] == '-') { if (b[0] == '-') { return DoSub(bb, aa); } else { return DoAdd(aa, bb); } } else { if (b[0] == '-') { return DoAdd(aa, bb); } } DoCalBase(aa, bb, int_a_i, int_b_i, int_a_f, int_b_f); DoToAlign(int_a_i, int_b_i, int_a_f, int_b_f); if (is_fushu == "") { for (int i = 0; i < int_a_i.size(); i++) { if (int_a_i[i] < int_b_i[i]) { is_fushu = "-"; break; } else if (int_a_i[i] > int_b_i[i]) { is_fushu = "+"; break; } } } if (is_fushu == "") { for (int i = int_a_f.size() - 1; i >= 0; i--) { if (int_a_f[i] < int_b_f[i]) { is_fushu = "-"; break; } else if (int_a_f[i] > int_b_f[i]) { is_fushu = "+"; break; } } } int temp = 0; if (is_fushu == "+") { for (int i = int_a_f.size() - 1; i >= 0; i--) { int res = int_a_f[i] - temp - int_b_f[i]; if (res < 0) { res = res + ARY; temp = 1; } else { temp = 0; } char buffer[1024]; sprintf_s(buffer, "%d\0", res); string str_temp(buffer); for (int x = str_temp.size(); x < DIGIT; x++) { str_temp = "0" + str_temp; } f_str = str_temp + f_str; } for (int i = int_a_i.size() - 1; i >= 0; i--) { int res = int_a_i[i] - temp - int_b_i[i]; if (res < 0) { res = res + ARY; temp = 1; } else { temp = 0; } char buffer[1024]; sprintf_s(buffer, "%d\0", res); string str_temp(buffer); for (int x = str_temp.size(); x < DIGIT; x++) { str_temp = "0" + str_temp; } i_str = str_temp + i_str; } } else if(is_fushu == "-") { for (int i = int_a_f.size() - 1; i >= 0; i--) { int res = int_b_f[i] - temp - int_a_f[i]; if (res < 0) { res = res + ARY; temp = 1; } else { temp = 0; } char buffer[1024]; sprintf_s(buffer, "%d\0", res); string str_temp(buffer); for (int x = str_temp.size(); x < DIGIT; x++) { str_temp = "0" + str_temp; } f_str = str_temp + f_str; } for (int i = int_a_i.size() - 1; i >= 0; i--) { int res = int_b_i[i] - temp - int_a_i[i]; if (res < 0) { res = res + ARY; temp = 1; } else { temp = 0; } char buffer[1024]; sprintf_s(buffer, "%d\0", res); string str_temp(buffer); for (int x = str_temp.size(); x < DIGIT; x++) { str_temp = "0" + str_temp; } i_str = str_temp + i_str; } } else { i_str = "0"; f_str = "0"; } DoDelFront(i_str); DoDelBack(f_str); if (i_str == "") i_str = "0"; if (f_str == "") f_str = "0"; string res_str = ""; if (f_str == "0") res_str = i_str; else res_str = i_str + "." + f_str; if (is_fushu == "-") res_str = is_fushu + res_str; return res_str; } //乘 string calculator::DoMul(const string &a, const string &b) { string f_str = ""; string i_str = ""; string is_fushu = ""; string aa = a; string bb = b; if (a[0] == '-') aa.erase(aa.begin()); if (b[0] == '-') bb.erase(bb.begin()); if (a[0] == '-' && b[0] != '-') is_fushu = "-"; if (b[0] == '-' && a[0] != '-') is_fushu = "-"; vector<int> int_a_i; //a整數部分 vector<int> int_b_i; //b整數部分 int n = 0; DoToDelPoint(n, aa, bb, int_a_i, int_b_i); for (int j = int_b_i.size() - 1; j >= 0; j--) { string res_temp = ""; int temp = 0; for (int i = int_a_i.size() - 1; i >= 0; i--) { int res = int_a_i[i] * int_b_i[j] + temp; temp = int(res / ARY); char buffer[1024]; sprintf_s(buffer, "%d\0", res % ARY); string str_temp(buffer); for (int x = str_temp.size(); x < DIGIT; x++) { str_temp = "0" + str_temp; } res_temp = str_temp + res_temp; } if (temp > 0) { char buffer[1024]; sprintf_s(buffer, "%d\0", temp % ARY); string str_temp(buffer); res_temp = str_temp + res_temp; temp = 0; } DoDelFront(res_temp); if (res_temp != "") { for (int k = 0; k < int_b_i.size() - 1 - j; k++) { string stemp = ""; for (int x = 0; x < DIGIT; x++) stemp = stemp + "0"; res_temp = res_temp + stemp; } i_str = DoAdd(i_str, res_temp); } } int i_str_size = i_str.size(); if (i_str_size <= n) { for (int i = i_str_size; i <= n; i++) i_str = "0" + i_str; } for (int i = 0; i < n; i++) { f_str = i_str[i_str.size() - 1] + f_str; i_str.erase(i_str.end()-1); } DoDelFront(i_str); DoDelBack(f_str); if (i_str == "") i_str = "0"; if (f_str == "") f_str = "0"; string res_str = ""; if (f_str == "0") res_str = i_str; else res_str = i_str + "." + f_str; if (is_fushu == "-") res_str = is_fushu + res_str; return res_str; } //除 string calculator::DoDiv(const string &a, const string &b) { string f_str = ""; string i_str = ""; string is_fushu = ""; string res_str = ""; string aa = a; string bb = b; if (a[0] == '-') aa.erase(aa.begin()); if (b[0] == '-') bb.erase(bb.begin()); if (a[0] == '-' && b[0] != '-') is_fushu = "-"; if (b[0] == '-' && a[0] != '-') is_fushu = "-"; string str_a_i; //a整數部分 string str_b_i; //b整數部分 DoToBeInt(aa, bb, str_a_i, str_b_i); { if(str_b_i.size() <= 9) //被除數如果在int範圍內 則可以用除法去算. { int b_i = atoi(str_b_i.c_str()); if(b_i == 0) return "0"; string x = "1000000000"; vector<int> int_a_i; //每9位分割 //每9位分割 int number = str_a_i.size() / 9 + 1; //多少個元素 int count = str_a_i.size() % 9; //第一個元素有多少個字元 string str_temp = ""; int now_count = 0; for (; now_count < count; now_count++) { str_temp = str_temp + str_a_i[now_count]; } if (str_temp != "") { int_a_i.push_back(atoi(str_temp.c_str())); str_temp = ""; } for (int j = 1; j < number; j++) { bool is_push = false; for (int i = 0; i < 9; i++, now_count++) { if (str_a_i[now_count] != '0') is_push = true; if (is_push) str_temp = str_temp + str_a_i[now_count]; } if (str_temp == "") str_temp = "0"; int_a_i.push_back(atoi(str_temp.c_str())); str_temp = ""; } //計算 for(int i = 0;i < int_a_i.size();i++) { double res = double(int_a_i[i])/double(b_i); char buffer1[1024]; sprintf_s(buffer1, "%lf\0", res); string str(buffer1); char buffer2[1024]; sprintf_s(buffer2, "%d\0", int_a_i.size() - 1 - i); string cifang(buffer2); string thpower = DoTHPOWER(x, cifang); str = DoMul(str, thpower); res_str = DoAdd(res_str, str); } } else { string temp_a = str_a_i; int res = 0; int max_point = 100; // 最多小數點後100位 int point = 0; while(1) { if(temp_a == "0") break; if(point >= max_point) { point--; break; } string temp = DoSub(temp_a, str_b_i); if(temp[0] == '-') { point++; char buffer[1024]; sprintf_s(buffer, "%d\0", res); string str_temp(buffer); i_str = i_str + str_temp; res = 0; temp_a.push_back('0'); } else { temp_a = temp; res++; } } if(res != 0) { char buffer[1024]; sprintf_s(buffer, "%d\0", res); string str_temp(buffer); i_str = i_str + str_temp; res = 0; } int i_str_size = i_str.size(); if (i_str_size < point) { for (int i = i_str_size; i <= point; i++) i_str = "0" + i_str; } for (int i = 0; i < point; i++) { f_str = i_str[i_str.size() - 1] + f_str; i_str.erase(i_str.end()-1); } DoDelFront(i_str); DoDelBack(f_str); if (i_str == "") i_str = "0"; if (f_str == "") f_str = "0"; if (f_str == "0") res_str = i_str; else res_str = i_str + "." + f_str; if (is_fushu == "-") res_str = is_fushu + res_str; } } return res_str; } //次方 string calculator::DoTHPOWER(const string &a, const string &b) { string str = ""; string aa = a; string bb = ""; if(b[0] == '-') aa = DoDiv("1",aa); for(int i = 0; i < b.size();i++) { if(b[i] == '.') break; if(b[i] >= '0' && b[i] <= '9') bb.push_back(b[i]); } if(bb == "") return str; if(bb == "0") return "1"; int b_i = atoi(bb.c_str()); for (int i = 0; i < b_i; i++) { if (str == "") str = "1"; str = DoMul(str, aa); } return str; } //階乘 string calculator::DoFACTORIAL(const string &aa) { string a = aa; string is_fushu = ""; if(a[0] == '-') { a.erase(a.begin()); is_fushu = "-"; } string i_str = "1"; int int_temp = 1; int int_end = atoi(a.c_str()); if (int_end == 0) { i_str = ""; return i_str; } for (int i = 1; i < int_end;) { i++; char buffer[1024]; sprintf_s(buffer, "%d\0", i); string str_temp(buffer); i_str = DoMul(i_str, str_temp); } i_str = is_fushu + i_str; return i_str; } //負號 string calculator::DoNegativeNumber(const string &a) { string str = "-"; if(a[0] == '-') { str = a; str.erase(str.begin()); } else { str = str + a; } return str; } int main() { calculator cal; cout << "請輸入公式:" << endl; while (!cal.PutIn() || !cal.Change()) { cout << "輸入有誤,請重新輸入" << endl; } __int64 t1 = 0; __int64 t2 = 0; t1 = clock(); cal.GetResult(); t2 = clock(); cal.PutOut(); cout << endl << "用了 " << t2 - t1 << "ms" << endl; system("pause"); return 0; }