C++實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
本文實(shí)例為大家分享了C++實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能的具體代碼,供大家參考,具體內(nèi)容如下
介紹
介紹:僅支持自然數(shù)間的+ - * /操作,并沒有括號(hào)。
實(shí)現(xiàn):利用棧實(shí)現(xiàn)存儲(chǔ)運(yùn)算數(shù)以及運(yùn)算符。
流程
1、輸入:string exp
2、對(duì)EXP進(jìn)行處理:數(shù)字入數(shù)棧,運(yùn)算符入字符棧。
1)??眨址霔?br /> 2)棧非空
棧頂運(yùn)算級(jí)別> =當(dāng)前字符運(yùn)算級(jí),取棧頂運(yùn)算符并出棧兩個(gè)數(shù),計(jì)算,結(jié)果入數(shù)棧
3)字符入棧
3、對(duì)字符棧檢測(cè),非空時(shí)進(jìn)行計(jì)算
4、輸出:結(jié)果。
實(shí)現(xiàn)
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;//運(yùn)算符
?? ?
?? ?bool GetTwoOperands(int& left,int& right);
?? ?void Compute();
?? ?void Deal(const string& exp);
public:
?? ?Calculator() {}
?? ?void Run();
?? ?void Clear();//清空數(shù)棧以及字符棧
};
void Calculator::Run() {
?? ?string exp;
?? ?cin >> exp;
?? ?Deal(exp);
?? ?while ( !ch.IsEmpty()) {
?? ??? ?Compute();
?? ?}
?? ?cout << "結(jié)果:" << 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;
?? ?}
}計(jì)算:
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);
?? ?}
}
// 將運(yùn)算符優(yōu)先級(jí)轉(zhuǎn)為整數(shù),便于比較
int f(const char& c) {
?? ?if (c == '+' || c == '-') return 1;
?? ?else return 2;
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- c++編寫簡(jiǎn)單的計(jì)算器程序
- 簡(jiǎn)單實(shí)現(xiàn)C++復(fù)數(shù)計(jì)算器
- C/C++實(shí)現(xiàn)日期計(jì)算器的示例代碼
- C++有限狀態(tài)機(jī)實(shí)現(xiàn)計(jì)算器小程序
- C/C++經(jīng)典實(shí)例之模擬計(jì)算器示例代碼
- C++實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- C++實(shí)現(xiàn)四則混合運(yùn)算計(jì)算器
- C++實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- C++實(shí)現(xiàn)分?jǐn)?shù)計(jì)算器
- 基于C++實(shí)現(xiàn)簡(jiǎn)單日期計(jì)算器
相關(guān)文章
應(yīng)用程序操作NorFlash示例代碼分享(norflash接口使用方法)
相對(duì)于操作NandFlash,操作NorFlash相對(duì)簡(jiǎn)單,因?yàn)榛静恍枰紤]壞塊,NorFlash也沒有OOB區(qū)域,也跟ECC沒有關(guān)系。讀寫擦除相對(duì)容易,下面看個(gè)例子吧2013-12-12
C語(yǔ)言實(shí)現(xiàn)通訊管理系統(tǒng)設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)通訊管理系統(tǒng)設(shè)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
C語(yǔ)言簡(jiǎn)易實(shí)現(xiàn)掃雷小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言簡(jiǎn)易實(shí)現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
C語(yǔ)言中程序環(huán)境和預(yù)處理的詳細(xì)圖文講解
這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中程序環(huán)境和預(yù)處理的相關(guān)資料,我們寫的C語(yǔ)言代碼,從運(yùn)行,到在屏幕上生成結(jié)果,經(jīng)歷了比較復(fù)雜的過程,需要的朋友可以參考下2023-02-02
解析設(shè)計(jì)模式中的Prototype原型模式及在C++中的使用
這篇文章主要介紹了設(shè)計(jì)模式中的Prototype原型模式及在C++中的使用,需要的朋友可以參考下2016-03-03
Linux下使用C/C++進(jìn)行UDP網(wǎng)絡(luò)編程詳解
UDP 是User Datagram Protocol 的簡(jiǎn)稱,中文名是用戶數(shù)據(jù)報(bào)協(xié)議,是一種無連接、不可靠的協(xié)議,本文主要介紹了如何在Linux下使用C/C++進(jìn)行UDP網(wǎng)絡(luò)編程,有需要的可以了解下2024-10-10

