C++结合QT实现带有优先级的计算器功能

 更新时间:2021年01月05日 17:13:30   作者:ruokey_1  
这篇文章主要介绍了C++结合QT实现带有优先级的计算器,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

代码

MyCalculator.h

#pragma once

#include <QtWidgets/QMainWindow>
#include <QStack>
#include <QString>
#include "ui_MyCalculator.h"

class MyCalculator : public QMainWindow
{
  Q_OBJECT

public:
  MyCalculator(QWidget *parent = Q_NULLPTR);
	
/*	void setnum1(int num);
	void setnum2(int num);
	void setflag(int f);
	int calculartor();
*/


private slots:
	void on_number_Button_clicked();
	void on_action_Button_clicked();
	void on_comma_Button_clicked();
	void on_action_Button_c_clicked();
	void on_action_Button_d_clicked();
	void on_action_Button_e_clicked();

/*
	void on_action_Button_equal_clicked();
	void on_number_Button_clicked();
	void on_action_Button_clicked();
	void on_action_Button_c_clicked();
*/

private:
  Ui::MyCalculatorClass ui;

};

MyCalculator.main

#include "MyCalculator.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  MyCalculator w;
  w.show();
  return a.exec();
}

MyCalculator.cpp

#include "MyCalculator.h"

#include<iostream>
using namespace std;
#include<stack>
#include<vector>
#include<cstdlib>
#include<limits.h>

MyCalculator::MyCalculator(QWidget *parent)
  : QMainWindow(parent)
{
  ui.setupUi(this);
	setWindowTitle(QStringLiteral("计算器"));
	ui.textBrowser->setFontPointSize(28);

	connect(ui.pushButton0, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton1, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton2, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton3, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton4, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton5, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton6, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton7, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton8, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton9, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));

	connect(ui.pushButton_add, SIGNAL(clicked()), this, SLOT(on_action_Button_clicked()));
	connect(ui.pushButton_div, SIGNAL(clicked()), this, SLOT(on_action_Button_clicked()));
	connect(ui.pushButton_mul, SIGNAL(clicked()), this, SLOT(on_action_Button_clicked()));
	connect(ui.pushButton_sub, SIGNAL(clicked()), this, SLOT(on_action_Button_clicked()));
	connect(ui.pushButton_right, SIGNAL(clicked()), this, SLOT(on_action_Button_clicked()));
	connect(ui.pushButton_left, SIGNAL(clicked()), this, SLOT(on_action_Button_clicked()));
	connect(ui.pushButton_dian, SIGNAL(clicked()), this, SLOT(on_comma_Button_clicked()));


	connect(ui.pushButton_del, SIGNAL(clicked()), this, SLOT(on_action_Button_d_clicked()));
	connect(ui.pushButton_re, SIGNAL(clicked()), this, SLOT(on_action_Button_c_clicked()));
	connect(ui.pushButton_equ, SIGNAL(clicked()), this, SLOT(on_action_Button_e_clicked()));
	/*
	//没有优先级的计算器
	connect(ui.pushButton0, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton1, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton2, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton3, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton4, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton5, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton6, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton7, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton8, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));
	connect(ui.pushButton9, SIGNAL(clicked()), this, SLOT(on_number_Button_clicked()));

	connect(ui.pushButton_add, SIGNAL(clicked()), this, SLOT(on_action_Button_clicked()));
	connect(ui.pushButton_div, SIGNAL(clicked()), this, SLOT(on_action_Button_clicked()));
	connect(ui.pushButton_mul, SIGNAL(clicked()), this, SLOT(on_action_Button_clicked()));
	connect(ui.pushButton_sub, SIGNAL(clicked()), this, SLOT(on_action_Button_clicked()));

	connect(ui.pushButton_re, SIGNAL(clicked()), this, SLOT(on_action_Button_c_clicked()));

	connect(ui.pushButton_equ, SIGNAL(clicked()), this, SLOT(on_action_Button_equal_clicked()));
	*/

}
void MyCalculator::on_number_Button_clicked()
{
	QPushButton *btn = qobject_cast<QPushButton*>(sender());
	QString number = btn->text();
	QString ss = ui.textBrowser->toPlainText();
	ui.textBrowser->clear();
	ui.textBrowser->append(ss + number);
}
void MyCalculator::on_action_Button_clicked()//操作符
{
	QPushButton *btn = qobject_cast<QPushButton*>(sender());
	QString action = btn->text();
	QString ss = ui.textBrowser->toPlainText();
	ui.textBrowser->clear();
	ui.textBrowser->append(ss + action);
}
void MyCalculator::on_comma_Button_clicked()//小数点
{
	QPushButton *btn = qobject_cast<QPushButton*>(sender());
	QString action = btn->text();
	QString ss = ui.textBrowser->toPlainText();
	ui.textBrowser->clear();
	ui.textBrowser->append(ss + action);
}
void MyCalculator::on_action_Button_c_clicked()//重置输入框里的内容
{
	ui.textBrowser->clear();
}
void MyCalculator::on_action_Button_d_clicked()//删除表达式中最后一个符号
{
	QString ss = ui.textBrowser->toPlainText();//在一行
	ss = ss.left(ss.length() - 1);
	ui.textBrowser->clear();
	ui.textBrowser->setText(ss);
}
bool isNum(QString ch)
{
	if (ch >= '0' && ch <= '9' || ch == '.')
		return true;
	else
		return false;
}
bool isOperate(QString ch)
{
	if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')' )
		return true;
	else
		return false;
}
int level(QString ch) {//优先级设置
	if(ch == '(')
		return 5;
	else if (ch == '*' || ch== '/') 
		return 4;
	else if (ch == '+' || ch == '-')
		return 3;
	else if (ch == ')')
		return 2;
}
double calcu(double a ,double b, QString c)
{
	double result = 0;
	if (c == '+')
		result = b + a;
	else if (c == '-')
		result = b - a;
	else if (c == '*')
		result = b * a;
	else if (c == '/')
		result = b / a;
	else
		result = INT_MAX;
	return result;
}
double getjieguo(QString input)
{
	QStack<double> Num;
	QStack<QString> Act;
	int a = input.length();
	for(int i=0;i< a;i++)
	{
		int flag = 0;//用来判断是否是数字
		int xiaoshu = 1;//用来判断是否是小数部分
		double number = 0;//暂存数字
		QString frist = input.left(1);
		while (isNum(frist))				//连续的数字转化为一个整数
		{
			if (frist == '.' || xiaoshu == 2)
			{
				number = frist.toDouble() / 10 + number;
				xiaoshu = 2;
			}
			else
			{
				number = number * 10 + frist.toInt();
			}
			input = input.right(input.length() - 1);
			frist = input.left(1);
			flag = 1;
		}
		if (flag == 1)
			Num.push(number);

		if (isOperate(frist))
		{
			if (!Act.empty() && (level(Act.top()) > level(frist)))
			{
					double a = Num.pop();
					double b = Num.pop();
					QString c = Act.pop();
					double result = calcu(a, b, c);
					Num.push(result);
					if (frist != ')')
						Act.push(frist);
					input = input.right(input.length() - 1);
					frist = input.left(1);
			}
			else if (!Act.empty() && (level(Act.top()) <= level(frist)))
			{
				if (frist != '(')
					Act.push(frist);
				input = input.right(input.length() - 1);
				frist = input.left(1);
			}
			else if(Act.empty())				//操作符第一次入符号栈
			{
				Act.push(frist);
				input = input.right(input.length() - 1);
				frist = input.left(1);
			}
		}
		if (frist == '=')//支持得到结果后仍可以继续运算
		{
			Num.clear();
			Act.clear();
			input = input.right(input.length() - 1);
			frist = input.left(1);
		}

		if (i + 1 >= a)				//当表达式都进栈后,只要符号栈不为空就继续执行
		{
			while (!Act.empty())
			{
				double a1 = Num.pop();
				double b1 = Num.pop();
				QString c1 = Act.pop();
				double result = calcu(a1, b1, c1);
				Num.push(result);
			}
		}
	}

	return Num.top();

}
void MyCalculator::on_action_Button_e_clicked()
{
	QString input = ui.textBrowser->toPlainText();//将输入框里的内容给input
	double value = getjieguo(input);//将表达式传给getjieguo来将数字和操作符分别入对应的栈
	ui.textBrowser->clear();
	ui.textBrowser->append(input + "=" + QString::number(value));//将结果的类型由数字转化为QString

}
/*
void MyCalculator::setnum1(int num)
{
	num1 = num;
}
void MyCalculator::setnum2(int num)
{
	num2 = num;
}
void MyCalculator::setflag(int f)
{
	flag = f;
}
int MyCalculator::calculartor()
{
	int result = 0;
	if (flag == 1)
		result = num1 + num2;
	else if (flag == 2)
		result = num1 - num2;
	else if (flag == 3)
		result = num1 * num2;
	else if (flag == 4)
	{
		if (num2 == 0)
			setflag(5);
		else
			result = num1 / num2;
	}
	else
		result = 0;

	return result;
}

void MyCalculator::on_action_Button_c_clicked()
{
	ui.textBrowser->clear();
	setnum1(0);
	setnum2(0);
	setflag(0);
}

void MyCalculator::on_number_Button_clicked()
{
	QPushButton *btn = qobject_cast<QPushButton*>(sender());
	QString number = btn->text();
	QString ss = ui.textBrowser->toPlainText();
	ui.textBrowser->clear();
	ui.textBrowser->append(ss + number);
}

void MyCalculator::on_action_Button_clicked()
{
	int number = ui.textBrowser->toPlainText().toInt();
	setnum1(number);
	QPushButton *btn = qobject_cast<QPushButton*>(sender());
	QString action = btn->text();
	ui.textBrowser->clear();
	if (action == "+")
		setflag(1);
	else if (action == "-")
		setflag(2);
	else if (action == "*")
		setflag(3);
	else
		setflag(4);

}

void MyCalculator::on_action_Button_equal_clicked() {
	int number = ui.textBrowser->toPlainText().toInt();
	setnum2(number);
	int res = calculartor();
	ui.textBrowser->clear();
	if (flag == 5)
		ui.textBrowser->append(QStringLiteral("不能除于0,请重新输入"));
	else
		ui.textBrowser->append(QString::number(res));
}
*/

测试

表达式 结果
2*3+6-(1+3) 8
2+3*6-(1+3) 16
2+3*6-(1.3+5/2) 16.2

说明

  1. 自己的学习笔记 ,还有一些bug没有解决;
  2. 部分代码需要优化,重构;
  3. 没有实现输入错误表达式报错功能,需要输入正确的表达式。
  4. 不支持负数计算。
  5. 支持小数,加,减,乘,除,括号运算。

在这里插入图片描述

到此这篇关于C++结合QT实现带有优先级的计算器的文章就介绍到这了,更多相关C++实现计算器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C 语言进制之间的转换

    C 语言进制之间的转换

    本篇文章主要介绍了C语言进制之间的转换,举例说明并附图片,帮助大家理解,希望对大家有所帮助
    2016-07-07
  • C++实现LeetCode(149.共线点个数)

    C++实现LeetCode(149.共线点个数)

    这篇文章主要介绍了C++实现LeetCode(149.共线点个数),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • C++中用new创建二维数组和指针数组实例代码

    C++中用new创建二维数组和指针数组实例代码

    这篇文章主要介绍了C++中用new创建二维数组和指针数组实例代码,非常不错,具有参考借鉴价值,需要的朋友参考下
    2017-03-03
  • C语言指针详解及用法示例

    C语言指针详解及用法示例

    这篇文章主要介绍了C语言指针详解及用法示例,介绍了其相关概念,然后分享了几种用法,具有一定参考价值。需要的朋友可以了解下。
    2017-11-11
  • Qt 进度条的实现示例

    Qt 进度条的实现示例

    进度条在很多时候都可以用到,有时我们需要在表格,树状栏中直观显示任务进度或消耗百分比,本文就详细的介绍一下Qt 进度条的使用实例,感兴趣的可以了解一下
    2021-06-06
  • 解决scanf_s输入%d%c%d格式错误的问题

    解决scanf_s输入%d%c%d格式错误的问题

    这篇文章主要介绍了解决scanf_s输入%d%c%d格式错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • C++ 实现桶排序的示例代码

    C++ 实现桶排序的示例代码

    桶排序或所谓的箱排序,是一个排序算法,工作的原理是将数组分到有限数量的桶子,本文详细的介绍了如何实现,感兴趣的可以了解一下
    2021-07-07
  • 浅析操作系统中的虚拟地址与物理地址

    浅析操作系统中的虚拟地址与物理地址

    本文主要介绍了操作系统中的虚拟地址与物理地址。在早期的计算机中,要运行一个程序,会把这些程序全都装入内存,程序都是直接运行在内存上的,也就是说程序中访问的内存地址都是实际的物理内存地址。那当程序同时运行多个程序时,操作系统是如何为这些程序分配内存的呢
    2021-06-06
  • 详解C++ new-handler机制

    详解C++ new-handler机制

    这篇文章主要介绍了C++ new-handler机制的相关资料,帮助大家更好的理解和使用c++,感兴趣的朋友可以了解下
    2020-11-11
  • C++实现LeetCode(144.二叉树的先序遍历)

    C++实现LeetCode(144.二叉树的先序遍历)

    这篇文章主要介绍了C++实现LeetCode(144.二叉树的先序遍历),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07

最新评论