網站首頁 編程語言 正文
本文實例為大家分享了C++實現簡易計算器功能的具體代碼,供大家參考,具體內容如下
介紹
介紹:僅支持自然數間的+ - * /操作,并沒有括號。
實現:利用棧實現存儲運算數以及運算符。
流程
1、輸入:string exp
2、對EXP進行處理:數字入數棧,運算符入字符棧。
?1)棧空,字符入棧
?2)棧非空
? ? ? 棧頂運算級別> =當前字符運算級,取棧頂運算符并出棧兩個數,計算,結果入數棧
?3)字符入棧
3、對字符棧檢測,非空時進行計算
4、輸出:結果。
實現
const int MAXSIZE = 100;//棧的最大大小 template<typename T> class Stack { private: ?? ?T data[MAXSIZE]; ?? ?int top; public: ?? ?Stack(); ?? ?void Push(const T& val); ?? ?T Top()const; ?? ?void Pop(); ?? ?void Clear(); ?? ?bool IsFull()const; ?? ?bool IsEmpty()const; }; template<typename T> Stack<T>::Stack() { ?? ?top = -1; } template<typename T> void Stack<T>::Push(const T& val) { ?? ?if (IsFull()) exit(1); ?? ?//未滿 ?? ?data[++top] = val; } template<typename T> T Stack<T>::Top()const { ?? ?if (IsEmpty()) exit(1); ?? ?//非空 ?? ?return data[top]; } template<typename T> void Stack<T>::Pop() { ?? ?if (IsEmpty()) exit(1); ?? ?--top; } template<typename T> void Stack<T>::Clear() { ?? ?top = -1; } template<typename T> bool Stack<T>::IsFull()const { ?? ?return top == MAXSIZE - 1; } template<typename T> bool Stack<T>::IsEmpty()const { ?? ?return top == -1; } ? class Calculator { private: ?? ?Stack<int> num; ?? ?Stack<char> ch;//運算符 ?? ? ?? ?bool GetTwoOperands(int& left,int& right); ?? ?void Compute(); ?? ?void Deal(const string& exp); public: ?? ?Calculator() {} ?? ?void Run(); ?? ?void Clear();//清空數棧以及字符棧 }; void Calculator::Run() { ?? ?string exp; ?? ?cin >> exp; ?? ?Deal(exp); ?? ?while ( !ch.IsEmpty()) { ?? ??? ?Compute(); ?? ?} ?? ?cout << "結果:" << num.Top() << endl; ?? ?Clear(); } void Calculator::Deal(const string& exp) { ?? ?int i(0), n(exp.length()); ?? ?bool last = false; ?? ?while (i < n&&exp[i] != '=') { ?? ??? ?if (exp[i] >= '0'&&exp[i] <= '9') { ?? ??? ??? ?if (last) { ?? ??? ??? ??? ?int x = num.Top() * 10 + (exp[i] - '0'); ?? ??? ??? ??? ?num.Pop(); ?? ??? ??? ??? ?num.Push(x); ?? ??? ??? ?} ?? ??? ??? ?else { ?? ??? ??? ??? ?num.Push(exp[i] - '0'); ?? ??? ??? ??? ?last = true; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?else { ?? ??? ??? ?last = false; ?? ??? ??? ?while (!ch.IsEmpty()) { ?? ??? ??? ??? ?int i1 = f(ch.Top()); ?? ??? ??? ??? ?int i2 = f(exp[i]); ?? ??? ??? ??? ?if (i1 >= i2) ?? ??? ??? ??? ??? ?Compute(); ?? ??? ??? ??? ?else ?? ??? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?ch.Push(exp[i]); ?? ??? ?} ?? ??? ?++i; ?? ?} }
計算:
void Calculator::Compute() ?{ ?? ?bool b; ?? ?int left, right; ?? ?b = GetTwoOperands(left, right); ?? ?if (!b) { ?? ??? ?cout << "Error\n"; ?? ??? ?Clear(); ?? ?} ?? ?else { ?? ??? ?char op = ch.Top(); ?? ??? ?ch.Pop(); ?? ??? ?switch (op) { ?? ??? ?case '+': ?? ??? ??? ?left += right; ?? ??? ??? ?break; ?? ??? ?case '-': ?? ??? ??? ?left -= right; ?? ??? ??? ?break; ?? ??? ?case '*': ?? ??? ??? ?left *= right; ?? ??? ??? ?break; ?? ??? ?case '/': ?? ??? ??? ?if (right == 0) { ?? ??? ??? ??? ?cout << "Error\n"; ?? ??? ??? ??? ?Clear(); ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ??? ?left /= right; ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?num.Push(left); ?? ?} } // 將運算符優先級轉為整數,便于比較 int f(const char& c) { ?? ?if (c == '+' || c == '-') return 1; ?? ?else return 2; }
原文鏈接:https://blog.csdn.net/qq_31854267/article/details/109086232
相關推薦
- 2023-07-16 spring boot 多環境配置
- 2022-12-27 教你使用Psycopg2連接openGauss的方法_python
- 2022-10-07 Unity游戲開發實現場景切換示例_C#教程
- 2023-01-29 HTTP與HTTPS超文本傳輸協議的區別是什么_經驗交流
- 2023-08-01 webpack 的熱更新及其原理
- 2022-11-19 Linux下定時自動備份Docker中所有SqlServer數據庫的腳本_docker
- 2022-09-15 React手寫一個手風琴組件示例_React
- 2023-01-31 golang定時任務cron項目實操指南_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支