vs集成qt实现简单的计算器

可能没有考虑到所有情况,但基本的计算可以做到!

Calculator.h

#pragma once

#include 
#include "ui_Calculator.h"
#include "calculation.h"
#include 
#include 
#include 
#include 

const int TOTALNUM = 10; // 数字键总数
const int OPERATORNUM = 6; // 操作符总数
const int WIDTH = 300; // 默认窗口宽度
const int HEIGHT = 450; // 默认窗口高度
const int ZOOMINWIDTH = 600; // 显示历史记录栏时的窗口宽度

const int BUTTONWIDTH = 60; // 按钮的宽度
const int BUTTONHEIGHT = 40; // 按钮的高度

const int SMALLBUTTONWIDTH = 30; // 小图标的宽度
const int SMALLBUTTONHEIGHT = 30; // 小图标的高度

class Calculator : public QDialog
{
	Q_OBJECT

public:
	Calculator(QWidget *parent = Q_NULLPTR);

private:
	Ui::CalculatorClass ui;

private:
	calculation c;

	QPushButton *histotyButton;

	QPushButton *numButton[TOTALNUM];
	QPushButton *operatorButton[OPERATORNUM];
	QPushButton *pointButton;
	QPushButton *leftParenthesesButton;
	QPushButton *rightParenthesesButton;
	QPushButton *sinButton;
	QPushButton *cosButton;
	QPushButton *tanButton;
	QPushButton *sqrtButton;
	QPushButton *squareButton;
	QPushButton *logButton;
	QPushButton *expButton;
	QPushButton *none1;
	QPushButton *none2;
	QPushButton *miciButton;
	QPushButton *tenMiciButton;
	QPushButton *factorialButton;
	QPushButton *positiveAndNegativeButton;
	QPushButton *piButton;
	QPushButton *clearButton;
	QPushButton *backspaceButton;

	QLineEdit *expressionLineEdit;
	QPlainTextEdit *historyResultTextEdit;

private:
	void initButton();
	void setButton(QPushButton * button, const QString & filePath, const int & width, const int & height, const int & buttonWidth = BUTTONWIDTH, const int & buttonHeight = BUTTONHEIGHT);

	void initLineEdit();
	char* getLastNum();

private slots:
	void onNumButtonClicked(const qreal & num);
	void onOperatorButtonClicked(const char & operatorName);
	void backspaceButtonClicked();
	void clearButtonClicked();
	void triFunButtonClicked(const QString & tri);
	void squareButtonClicked();
	void sqrtButtonClicked();
	void powButtonClicked();
	void tenPowButtonClicked();
	void positiveAndNegativeButtonClicked();
	void eFunButtonClicked(const QString & e);
	void histotyButtonClicked();
};

Calculaor.cpp

#include "Calculator.h"

bool zoomin = false; // 是否放大(显示历史栏)

const char OPERATOR[] = "%/*-+=";

Calculator::Calculator(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	// 初始化
	initButton();
	initLineEdit();
}

// 简单计算阶乘
double factorial(int n) {
	double res = 1;
	while (n > 0) {
		res *= n;
		n--;
	}
	return res;
}

void Calculator::initButton()
{
	setMinimumSize(WIDTH, HEIGHT);
	setMaximumSize(WIDTH, HEIGHT);

	QString iconPath;
	int j = 1;
	int i;
	// 初始化数字按钮
	for (i = 0; i < TOTALNUM; ++i) {
		numButton[i] = new QPushButton(this);
		iconPath = "./img/num" + QString::number(i) + ".png";
		if (i == 0) {
			setButton(numButton[i], iconPath, (WIDTH - BUTTONWIDTH) >> 1, HEIGHT - BUTTONHEIGHT);
		}
		else {
			if (i % 3 == 1) {
				j++;
			}
			setButton(numButton[i], iconPath, BUTTONWIDTH * ((i - 1) % 3 + 1), HEIGHT - j * BUTTONHEIGHT);
		}
		// 通过lamda表达式进行传参
		connect(numButton[i], &QPushButton::clicked, this, [=] {onNumButtonClicked(i); });
	}

	// 初始化加 减 乘 除 取余 等于
	for (i = 0; i < OPERATORNUM; ++i) {
		operatorButton[i] = new QPushButton(this);
		iconPath = "./img/operator" + QString::number(i + 1) + ".png";
		setButton(operatorButton[i], iconPath, WIDTH - BUTTONWIDTH, HEIGHT - BUTTONHEIGHT * (OPERATORNUM - i));
		connect(operatorButton[i], &QPushButton::clicked, this, [=] {onOperatorButtonClicked(OPERATOR[i]); });
	}

	// 初始化小数点按钮
	pointButton = new QPushButton(this);
	setButton(pointButton, "./img/point.png", WIDTH - BUTTONWIDTH * 2, HEIGHT - BUTTONHEIGHT);
	connect(pointButton, &QPushButton::clicked, this, [=] {onOperatorButtonClicked('.'); });

	// 初始化左括号按钮
	leftParenthesesButton = new QPushButton(this);
	setButton(leftParenthesesButton, "./img/leftParentheses.png", 0, HEIGHT - BUTTONHEIGHT);
	connect(leftParenthesesButton, &QPushButton::clicked, this, [=] {onOperatorButtonClicked('('); });


	// 初始化右括号按钮
	rightParenthesesButton = new QPushButton(this);
	setButton(rightParenthesesButton, "./img/rightParentheses.png", BUTTONWIDTH, HEIGHT - BUTTONHEIGHT);
	connect(rightParenthesesButton, &QPushButton::clicked, this, [=] {onOperatorButtonClicked(')'); });


	// 初始化三角函数按钮
	sinButton = new QPushButton(this);
	cosButton = new QPushButton(this);
	tanButton = new QPushButton(this);
	setButton(sinButton, "./img/sin.png", WIDTH - 3 * BUTTONWIDTH, HEIGHT - 7 * BUTTONHEIGHT);
	setButton(cosButton, "./img/cos.png", WIDTH - 2 * BUTTONWIDTH, HEIGHT - 7 * BUTTONHEIGHT);
	setButton(tanButton, "./img/tan.png", WIDTH - BUTTONWIDTH, HEIGHT - 7 * BUTTONHEIGHT);
	connect(sinButton, &QPushButton::clicked, this, [=] {triFunButtonClicked("sin"); });
	connect(cosButton, &QPushButton::clicked, this, [=] {triFunButtonClicked("cos"); });
	connect(tanButton, &QPushButton::clicked, this, [=] {triFunButtonClicked("tan"); });

	// 初始化开方与开根号
	sqrtButton = new QPushButton(this);
	squareButton = new QPushButton(this);
	setButton(sqrtButton, "./img/sqrt.png", 0, HEIGHT - 6 * BUTTONHEIGHT);
	setButton(squareButton, "./img/square.png", 0, HEIGHT - 7 * BUTTONHEIGHT);
	connect(sqrtButton, SIGNAL(clicked()), this, SLOT(sqrtButtonClicked()));
	connect(squareButton, SIGNAL(clicked()), this, SLOT(squareButtonClicked()));

	// 初始化对数函数与指数函数按钮
	logButton = new QPushButton(this);
	expButton = new QPushButton(this);
	setButton(logButton, "./img/log.png", WIDTH - 3 * BUTTONWIDTH, HEIGHT - 6 * BUTTONHEIGHT);
	setButton(expButton, "./img/exp.png", WIDTH - 2 * BUTTONWIDTH, HEIGHT - 6 * BUTTONHEIGHT);
	connect(logButton, &QPushButton::clicked, this, [=] {eFunButtonClicked("log"); });
	connect(expButton, &QPushButton::clicked, this, [=] {eFunButtonClicked("exp"); });

	// 初始化暂无用处的按钮
	none1 = new QPushButton(this);
	none2 = new QPushButton(this);
	setButton(none1, "./img/none1.png", 0, HEIGHT - 5 * BUTTONHEIGHT);
	setButton(none2, "./img/none2.png", BUTTONWIDTH, HEIGHT - 5 * BUTTONHEIGHT);

	// 初始化幂次按钮
	miciButton = new QPushButton(this);
	tenMiciButton = new QPushButton(this);
	setButton(miciButton, "./img/mici.png", BUTTONWIDTH, HEIGHT - 7 * BUTTONHEIGHT);
	setButton(tenMiciButton, "./img/tenMici.png", BUTTONWIDTH, HEIGHT - 6 * BUTTONHEIGHT);
	connect(miciButton, SIGNAL(clicked()), this, SLOT(powButtonClicked()));
	connect(tenMiciButton, SIGNAL(clicked()), this, SLOT(tenPowButtonClicked()));

	// 初始化阶乘按钮
	factorialButton = new QPushButton(this);
	setButton(factorialButton, "./img/factorial.png", 0, HEIGHT - 3 * BUTTONHEIGHT);
	connect(factorialButton, &QPushButton::clicked, this, [=] {onOperatorButtonClicked('!'); });

	// 初始化切换正负号的按钮
	positiveAndNegativeButton = new QPushButton(this);
	setButton(positiveAndNegativeButton, "./img/positiveAndNegative.png", 0, HEIGHT - 2 * BUTTONHEIGHT);
	connect(positiveAndNegativeButton, SIGNAL(clicked()), this, SLOT(positiveAndNegativeButtonClicked()));

	// 初始化PI按钮
	piButton = new QPushButton(this);
	setButton(piButton, "./img/pi.png", 0, HEIGHT - 4 * BUTTONHEIGHT);
	connect(piButton, &QPushButton::clicked, this, [=] {onNumButtonClicked(3.14); });

	// 初始化清屏按钮
	clearButton = new QPushButton(this);
	setButton(clearButton, "./img/clear.png", WIDTH - 3 * BUTTONWIDTH, HEIGHT - 5 * BUTTONHEIGHT);
	connect(clearButton, SIGNAL(clicked()), this, SLOT(clearButtonClicked()));

	// 初始化退格按钮
	backspaceButton = new QPushButton(this);
	setButton(backspaceButton, "./img/backspace.png", WIDTH - 2 * BUTTONWIDTH, HEIGHT - 5 * BUTTONHEIGHT);
	connect(backspaceButton, SIGNAL(clicked()), this, SLOT(backspaceButtonClicked()));

	// 初始化显示历史记录按钮
	histotyButton = new QPushButton(this);
	setButton(histotyButton, "./img/history.png", 0, 0, SMALLBUTTONWIDTH, SMALLBUTTONHEIGHT);
	connect(histotyButton, SIGNAL(clicked()), this, SLOT(histotyButtonClicked()));

}

// 设置按钮
// 后面两个参数默认是 BUTTONWIDTH(60) 和 BUTTONHEIGHT(40)
void Calculator::setButton(QPushButton * button, const QString & filePath
	, const int & width, const int & height
	, const int & buttonWidth, const int & buttonHeight)
{
	button->setGeometry(width, height, buttonWidth, buttonHeight);
	button->setIcon(QIcon(filePath));
	button->setIconSize(QSize(buttonWidth, buttonHeight));
}

// 初始化答案栏和历史记录栏
void Calculator::initLineEdit()
{
	expressionLineEdit = new QLineEdit(this);
	expressionLineEdit->setGeometry(0, HEIGHT - 9 * BUTTONHEIGHT, WIDTH, BUTTONHEIGHT);
	expressionLineEdit->setFont(QFont("微软雅黑", 24)); // 设置字体
	expressionLineEdit->setReadOnly(true); // 无法用键盘修改

	historyResultTextEdit = new QPlainTextEdit(this);
	historyResultTextEdit->setGeometry(WIDTH, 0, ZOOMINWIDTH - WIDTH, HEIGHT);
	historyResultTextEdit->setFont(QFont("微软雅黑", 24)); // 设置字体
	historyResultTextEdit->setReadOnly(true); // 无法用键盘修改
}

// 找到最后一个数字(对于某些特殊函数需要这个操作)
char* Calculator::getLastNum()
{
	std::string s = expressionLineEdit->text().toStdString();
	size_t length = strlen(s.c_str());
	char res[NUMSIZE];
	memset(res, '\0', NUMSIZE);
	/*
	 *判断条件:
	 *当前位不是操作符
	 *或者 当前位为操作符(+,-)且前面为e
	 *或者 当前操作符是负号并且负号前面也是操作符
	 */
	for (int i = length - 1; i >= 0
		&& (!c.isOperator(s[i])
			|| (c.isOperator(s[i]) && (0 == i || 'e' == s[i - 1]))
			|| ('-' == s[i] && (i == 0 || c.isOperator(s[i - 1])))); --i) {
		strcpy(res, strcat(ctocstar(s[i]), res));
		expressionLineEdit->backspace();
	}
	return res;
}

// 设置按钮槽函数
void Calculator::onNumButtonClicked(const qreal & num)
{
	std::string tempStr = expressionLineEdit->text().toStdString();
	// PI进行特殊处理
	// 如果不为空,且PI前面是操作符就加个乘号
	if (3.14 == num && "" != tempStr
		&& (!c.isOperator(tempStr.back())
			|| ')' == tempStr.back())) {
		expressionLineEdit->setText(expressionLineEdit->text() + '*' + QString::number(num));
	}
	else {
		expressionLineEdit->setText(expressionLineEdit->text() + QString::number(num));
	}
}

void Calculator::onOperatorButtonClicked(const char & operatorName)
{
	std::string tempStr = expressionLineEdit->text().toStdString();
	/*
	 *等于号需要单独判断
	 *如果为空则不进行计算
	 *如果最后一位是操作符也不进行计算
	 *如果计算有误则清空
	 */
	if ('=' == operatorName) {
		if ("" == expressionLineEdit->text()) {
			QMessageBox::information(NULL, "warning!", "you can't do this!");
			return;
		}
		if (c.isOperator(tempStr.back())) {
			if (')' != tempStr.back()) {
				QMessageBox::information(NULL, "warning!", "you can't put a single operator at the end!");
				return;
			}
		}
		double res = c.calculate(tempStr.c_str());
		if (WRONGTAG == res) {
			expressionLineEdit->clear();
		}
		else {
			historyResultTextEdit->setPlainText(expressionLineEdit->text() 
				+ '=' 
				+ QString::number(res) 
				+ '\n' 
				+ historyResultTextEdit->toPlainText());
			expressionLineEdit->setText(QString::number(res));
		}
	}
	// 单独处理阶乘符号
	else if ('!' == operatorName) {
		char* res = new char[NUMSIZE];
		strcpy(res, getLastNum());
		double d = c.stod(res);
		expressionLineEdit->setText(expressionLineEdit->text() + QString::number(factorial(d)));
	}
	// 单独处理小数点符号
	else if ('.' == operatorName) {
		if ("" != tempStr && !c.isOperator(tempStr.back()) && '.' != tempStr.back()) {
			expressionLineEdit->setText(expressionLineEdit->text() + '.');
		}
	}
	/*
	 *为空只能放左括号或者负号
	 *左括号前面是数字就补乘号
	 *前面不能是操作符(除了')')
	 */
	else if ('(' == operatorName) {
		if ("" != expressionLineEdit->text() && !c.isOperator(tempStr.back())) {
			expressionLineEdit->setText(expressionLineEdit->text() + '*');
		}
		expressionLineEdit->setText(expressionLineEdit->text() + operatorName);
	}
	else if ("" == expressionLineEdit->text() && '-' == operatorName) {
		expressionLineEdit->setText(expressionLineEdit->text() + operatorName);
	}
	else if(("" != expressionLineEdit->text() 
			&& (!c.isOperator(tempStr.back()) && '.' != tempStr.back() || ')' == tempStr.back()))){
		expressionLineEdit->setText(expressionLineEdit->text() + operatorName);
	}
}

void Calculator::backspaceButtonClicked()
{
	expressionLineEdit->backspace();
}

void Calculator::clearButtonClicked()
{
	expressionLineEdit->clear();
}

void Calculator::triFunButtonClicked(const QString & tri)
{
	char* res = new char[NUMSIZE];
	strcpy(res, getLastNum());
	double d = c.stod(res);
	if ("sin" == tri) {
		expressionLineEdit->setText(expressionLineEdit->text() + QString::number(sin(d)));
	}
	else if ("cos" == tri) {
		expressionLineEdit->setText(expressionLineEdit->text() + QString::number(cos(d)));
	}
	else if ("tan" == tri) {
		expressionLineEdit->setText(expressionLineEdit->text() + QString::number(tan(d)));
	}
}

void Calculator::squareButtonClicked()
{
	char* res = new char[NUMSIZE];
	strcpy(res, getLastNum());
	double d = c.stod(res);
	expressionLineEdit->setText(expressionLineEdit->text() + QString::number(pow(d, 2)));
}

void Calculator::sqrtButtonClicked()
{
	char* res = new char[NUMSIZE];
	strcpy(res, getLastNum());
	double d = c.stod(res);
	expressionLineEdit->setText(expressionLineEdit->text() + QString::number(pow(d, 0.5)));
}

void Calculator::powButtonClicked()
{
	expressionLineEdit->setText(expressionLineEdit->text() + "^");
}

void Calculator::tenPowButtonClicked()
{
	std::string tempStr = expressionLineEdit->text().toStdString();
	// 处理方式同PI
	if ("" != tempStr && (!c.isOperator(tempStr.back())
			|| ')' == tempStr.back())) {
		expressionLineEdit->setText(expressionLineEdit->text() + '*');
	}
	expressionLineEdit->setText(expressionLineEdit->text() + "10^");
}

void Calculator::positiveAndNegativeButtonClicked()
{
	// do nothing
}

void Calculator::eFunButtonClicked(const QString & e)
{
	char* res = new char[NUMSIZE];
	strcpy(res, getLastNum());
	double d = c.stod(res);
	if ("exp" == e) {
		expressionLineEdit->setText(expressionLineEdit->text() + QString::number(exp(d)));
	}
	else if ("log" == e) {
		if (d <= 0) {
			QMessageBox::information(NULL, "warning!", "you can't use this number as the base");
			expressionLineEdit->clear();
		}
		else {
			expressionLineEdit->setText(expressionLineEdit->text() + QString::number(log(d)));
		}
	}
}

void Calculator::histotyButtonClicked()
{
	if (false == zoomin) {
		setMinimumSize(ZOOMINWIDTH, HEIGHT);
		setMaximumSize(ZOOMINWIDTH, HEIGHT);
		zoomin = true;
	}
	else if (true == zoomin) {
		setMinimumSize(WIDTH, HEIGHT);
		setMaximumSize(WIDTH, HEIGHT);
		zoomin = false;
	}
}

calculation.h

#pragma once
#include "vector.h"
#include "stack.h"
#include 


const int NUMSIZE = 20; // 设置数组的缓冲长度
const int WRONGTAG = -INT_MAX; // 错误标记


class calculation {
public:
	double stod(char* s);
	bool isOperator(const char c);
	double calculate(const char* expressions);
};

char* ctocstar(char c);

calculation.cpp

#pragma warning(disable:4996)
#include "calculation.h"

const char* operators = "+-*/%()^";


// 栈外优先数
int icp(const char c)
{
	switch (c)
	{
	case '(':return 6;
	case '*':
	case '/':
	case '^':
	case '%':return 4;
	case '+':
	case '-':return 2;
	case ')':return 1;
	default:break;
	}
	return 0;
}

// 栈内优先数
int isp(const char c)
{
	switch (c)
	{
	case '(':return 1;
	case '*':
	case '/':
	case '^':
	case '%':return 5;
	case '+':
	case '-':return 3;
	case ')':return 6;
	default:break;
	}
	return 0;
}

// 将字符串转换成数字
double calculation::stod(char * s)
{
	double res = 0;
	bool isNegative = s[0] == '-' ? true : false; // 判断是否为负数
	int i = 0;
	int size = 0; // 数字长度(在e之前)
	int ePos = -1; // 找到e的位置,如果没有找到则是-1
	int pointPos = -1; // 找到小数点的位置,如果没有找到则是-1
	for (i = isNegative; s[i] != '\0'; ++i) {
		if (s[i] == '.') {
			pointPos = i;
			size++;
		}
		else if (s[i] == 'e') {
			ePos = i;
			break;
		}
		else {
			size++;
		}
	}
	// 计算结果
	for (i = isNegative; i < size + isNegative; ++i) {
		if (i == pointPos) {
			continue;
		}
		res = res * 10 + (s[i] - '0');
	}
	if (-1 != pointPos) {
		while (size != pointPos + 1) {
			res = res / 10;
			size--;
		}
	}
	// 处理指数部分
	if (-1 != ePos) {
		int exponential = 0; // 指数部分
		bool isNegativeExp = s[ePos + 1] == '-' ? true : false; // 判断指数部分是否为负数
		for (i = ePos + 2; s[i] != '\0'; ++i) {
			exponential = exponential * 10 + (s[i] - '0');
		}
		if (isNegativeExp) {
			while (exponential--) {
				res /= 10;
			}
		}
		else {
			while (exponential--) {
				res *= 10;
			}
		}
	}
	return isNegative ? -res : res;
}

bool calculation::isOperator(const char c)
{
	for (int i = 0; operators[i] != '\0'; ++i) {
		if (c == operators[i]) {
			return true;
		}
	}
	return false;
}

char* ctocstar(const char c) {
	char *ch = new char[2];
	ch[0] = c;
	ch[1] = '\0';
	return ch;
}

double calculation::calculate(const char * expressions)
{
	if ("" == expressions) {
		return WRONGTAG;
	}
	Stack numStack; // 储存数字的栈
	Stack symStack; // 储存符号的栈
	// 将中缀表达式转换成后缀表达式
	char num[NUMSIZE]; // 用于解析表达式中的数字
	sprintf_s(num, NUMSIZE, "%c", '\0');
	char temp = NULL;
	Vector sufExpressions;
	int i = NULL;
	for (i = 0; expressions[i] != '\0'; ++i) {
		temp = expressions[i];
		// 如果不是操作符就加到num内
		// 加减号前面是e要进行处理
		if (!isOperator(temp) || (isOperator(temp) && expressions[i - 1] == 'e')) {
			sprintf_s(num, NUMSIZE, "%s%c", num, temp);
		}
		// 如果是操作符
		else {
			char *tempch = new char[NUMSIZE];
			strcpy(tempch, num);
			if (num[0] != '\0') {
				sufExpressions.push_back(tempch); // 将数字输出到后缀表达式中
			}
			sprintf_s(num, NUMSIZE, "%c", '\0');
			/*
			 *如果栈为空则直接压栈
			 *如果不为空则比较栈外元素与栈内元素大小
			 *如果栈外操作符大于栈内操作符,入栈
			 *反之则输出到后缀表达式中,然后循环
			 */
			if (')' == temp) {
				// 如果是右括号则一直输出到后缀表达式中,直至碰到左括号
				while (!symStack.empty() && symStack.top() != '(') {
					sufExpressions.push_back(ctocstar(symStack.top()));
					symStack.pop();
				}
				// 如果为空则说明没有匹配的左括号
				if (symStack.empty()) {
					QMessageBox::information(NULL, "warning!", "lack of leftparentheses!");
					return WRONGTAG;
				}
				else {
					// 将左括号弹出
					symStack.pop();
				}
			}
			else {
				while (!symStack.empty() && (isp(symStack.top()) > icp(temp))) {
					sufExpressions.push_back(ctocstar(symStack.top()));
					symStack.pop();
				}
				symStack.push(temp);
			}
		}
	}
	// 处理最后一位数字
	if (num[0] != '\0') {
		sufExpressions.push_back(num);
	}
	// 将栈内剩余操作符输出
	while (!symStack.empty()) {
		sufExpressions.push_back(ctocstar(symStack.top()));
		symStack.pop();
	}
	double tempNum1 = NULL, tempNum2 = NULL;
	// 计算后缀表达式
	for (i = 0; i < sufExpressions.size(); ++i) {
		if (0 == strcmp(sufExpressions[i], "+")) {
			tempNum1 = numStack.top();
			numStack.pop();
			tempNum2 = numStack.top();
			numStack.pop();
			numStack.push(tempNum1 + tempNum2);
		}
		else if (0 == strcmp(sufExpressions[i], "-")) {
			tempNum1 = numStack.top();
			numStack.pop();
			// 如果数字栈为空或者负号在最后则直接取反(单目运算)
			if (numStack.empty() || (i < sufExpressions.size() - 1 && isOperator(sufExpressions[i + 1][0]))) {
				tempNum2 = 0;
			}
			else {
				tempNum2 = numStack.top();
				numStack.pop();
			}
			numStack.push(tempNum2 - tempNum1);
		}
		else if (0 == strcmp(sufExpressions[i], "*")) {
			tempNum1 = numStack.top();
			numStack.pop();
			tempNum2 = numStack.top();
			numStack.pop();
			numStack.push(tempNum1 * tempNum2);
		}
		else if (0 == strcmp(sufExpressions[i], "/")) {
			tempNum1 = numStack.top();
			numStack.pop();
			tempNum2 = numStack.top();
			numStack.pop();
			// 除数不能为0
			if (tempNum1 == 0) {
				QMessageBox::information(NULL, "warning!", "you can't divide zero!");
				return WRONGTAG;
			}
			else {
				numStack.push(tempNum2 / tempNum1);
			}
		}
		else if (0 == strcmp(sufExpressions[i], "%")) {
			tempNum1 = numStack.top();
			numStack.pop();
			tempNum2 = numStack.top();
			numStack.pop();
			// 如果被除数小于0要进行处理
			if (tempNum2 < 0) {
				numStack.push(tempNum1 + (tempNum2 - (int)(tempNum2 / tempNum1) * tempNum1));
			}
			else {
				numStack.push(tempNum2 - (int)(tempNum2 / tempNum1) * tempNum1);
			}
		}
		else if (0 == strcmp(sufExpressions[i], "^")) {
			tempNum1 = numStack.top();
			numStack.pop();
			tempNum2 = numStack.top();
			numStack.pop();
			numStack.push(pow(tempNum2, tempNum1));
		}
		else if (0 == strcmp(sufExpressions[i], "(")) {
			QMessageBox::information(NULL, "warning!", "lack of rightparenteses!");
			return WRONGTAG;
		}
		else {
			numStack.push(stod(sufExpressions[i]));
		}
	}
	// 如果结果栈为空则返回0
	if (numStack.empty()) {
		return 0;
	}
	return numStack.top();
}

链接:https://share.weiyun.com/5A45cgb

图片的素材在链接里面,是微软计算器的截图

你可能感兴趣的:(c++,算法,QT,c++,qt,算法,计算器)