C++實(shí)現(xiàn)簡單計算器
更新時間:2020年05月18日 09:50:56 作者:時光丨荏苒
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡單計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C++實(shí)現(xiàn)簡單計算器的具體代碼,供大家參考,具體內(nèi)容如下
工具
- stack
- map
步驟
- 初始化
- 讀取字符串
- 去空格
- 負(fù)號處理
- 判斷為空
- 檢查格式
- 計算
示例
代碼
#include <iostream> #include <string> #include <stdio.h> #include <stack> #include <map> #include <math.h> #include <stdlib.h> #include <sstream> using namespace std; char op[8][8]; map<char, int> m; void init() { m['+'] = 1, m['-'] = 2, m['*'] = 3, m['/'] = 4, m['('] = 5, m[')'] = 6, m['#'] = 7; op[1][1] = '>', op[1][2] = '>', op[1][3] = '<', op[1][4] = '<', op[1][5] = '<', op[1][6] = '>', op[1][7] = '>'; op[2][1] = '>', op[2][2] = '>', op[2][3] = '<', op[2][4] = '<', op[2][5] = '<', op[2][6] = '>', op[2][7] = '>'; op[3][1] = '>', op[3][2] = '>', op[3][3] = '>', op[3][4] = '>', op[3][5] = '<', op[3][6] = '>', op[3][7] = '>'; op[4][1] = '>', op[4][2] = '>', op[4][3] = '>', op[4][4] = '>', op[4][5] = '<', op[4][6] = '>', op[4][7] = '>'; op[5][1] = '<', op[5][2] = '<', op[5][3] = '<', op[5][4] = '<', op[5][5] = '<', op[5][6] = '='; op[6][1] = '>', op[6][2] = '>', op[6][3] = '>', op[6][4] = '>', op[6][6] = '>', op[6][7] = '>'; op[7][1] = '<', op[7][2] = '<', op[7][3] = '<', op[7][4] = '<', op[7][5] = '<', op[7][7] = '='; } double operate(double num1, char oper, double num2) { if(oper == '+') return num1 + num2; if(oper == '-') return num1 - num2; if(oper == '*') return num1 * num2; if(oper =='/') return num1 / num2; } string trim(string str) { index = 0; if(!str.empty()){ while((index = str.find(' ',index) != string::nops) str.erase(index,1); } return str; } string change(string str) { int start; start += "#"; for(int i = 0; i < str.length(); i++){ if(str[i] == '-'){ if(i == 0 || i != 0 && (m[str[i-1]] >= 1 && m[str[i-1]] <= 5 && str[i+1] >= '0' && str[i+1] <= '9'){ str += " "; int j = i+1; start = j; while(m[str[j]] == 0) j++; for(int k = str.length() - 1; k >= j; k--) str[k] = str[k-3]; str[i] = '(', str[i+1] = '0', str[i+2] = '-'; int l = i+3; string s = str.substr(start, j - start); for(int k = 0; k < s.length(); k++) str[l+k] = s[k]; str[l+s.length()] = ')'; } } } str.erase(str.length()-1, 1); return str; } bool test(string str) { bool ifOK = true; int flag = 0; stack<char> s; int start, e; str += "#"; for(int i = 0; i < str.length(); i++){ //判斷非法字符 if((str[i] < '0' || str[i] > '9') && m[str[i]] == 0 && str[i] != '.') return false; if(str[i] == '#' && i!= str.length()-1) return false; //判斷小數(shù)點(diǎn) if(m[str[i]] == 0){ if(flag == 0){ start = i; flag = 1; } } if(m[str[i]] != 0){ if(flag == 1){ e = i; string st = str.substr(start, e - start); int pointNum = 0; if(st[0] == '.') return false; for(int j = 0; j < st.length(); j++){ if(st[j] == '.') pointNum++; } if(pointNum > 1) return false; } flag = 0; } //判斷運(yùn)算符 if(str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/'){ if(i == 0) return false; if((str[i-1] < '0' || str[i-1] > '9') && str[i-1] != ')') return false; if((str[i+1] < '0' || str[i+1] > '9') && str[i+1] != '(') return false; } //判斷括號 if(str[i] == '(') s.push(str[i]); if(str[i] == ')'){ if(s.empty()) return false; char c = s.top(); if(c == '(') s.pop(); else return false; } } //判斷括號 if(!s.empty()) ifOK = false; return ifOK; } void caculate(string str) { str += "#"; int flag = 0; int start, e; stack<double> numStack;//操作數(shù)棧 stack<char> operStack;//運(yùn)算符棧 operStack.push('#'); int i = 0; while(str[i] != '#' || operStack.top() != '#'){ if(m[str[i]] == 0){ if(flag == 0){ start = i; flag = 1; } } else{ if(flag == 1){ e = i; //string 轉(zhuǎn) double stringstream s (str.substr(start, e - start)); double num; s >> num; numStack.push(num); flag = 0; } char c = operStack.top; if(op[m[c]][m[str[i]]] == '<'){ operStack.push(str[i]); i++; } else if(op[m[c]][m[str[i]]] == '='){ operStac.pop(); i++; } else{ char oper = operStack.top(); operStac.pop(); double num2 = numStack.top(); numStack.pop(); double num1 = numStack.top(); numStack.pop(); double result = operate(num1, oper, num2); numStack.push(result); } } } printf("\t\t\t= %f\n",numStack.top()); } int main() { init();//初始化 printf("\n\n\n\t\t\t請輸入表達(dá)式, 退出請輸入end:\n"); string s; getline(cin, s, "\n");//讀取字符串 s = trim(s);//去空格 s = change(s);//處理負(fù)號 if(s == "end"){ break; } else if(s.length() > 0){//判斷為空 if(!test(s))//檢查格式 printf("\t\t\t格式錯誤\n"); else caculate(s);//計算 } else{ printf("\t\t\t輸入不能為空\n"); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++ 關(guān)于 CMFCPropertyGridCtrl 的使用方法
這篇文章主要介紹了C++ 關(guān)于 CMFCPropertyGridCtrl 的使用方法的相關(guān)資料,需要的朋友可以參考下2015-06-06c++基礎(chǔ)算法動態(tài)DP解決CoinChange問題
這篇文章主要為大家介紹了c++基礎(chǔ)算法如何利用動態(tài)DP來解決Coin Change的問題示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10sqlserver,sqlite,access數(shù)據(jù)庫鏈接字符串整理
本節(jié)主要整理sqlserver,sqlite,access數(shù)據(jù)庫鏈接字符串,有需要的朋友可以參考下2014-07-07C語言位段(位域)機(jī)制結(jié)構(gòu)體的特殊實(shí)現(xiàn)及解析
這篇文章主要為大家介紹了C語言位段位域機(jī)制結(jié)構(gòu)體的特殊實(shí)現(xiàn)講解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-02-02