C++结合QT实现带有优先级的计算器功能
作者:ruokey_1 发布时间:2022-07-05 08:12:49
标签: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 |
说明
自己的学习笔记 ,还有一些bug没有解决;
部分代码需要优化,重构;
没有实现输入错误表达式报错功能,需要输入正确的表达式。
不支持负数计算。
支持小数,加,减,乘,除,括号运算。
来源:https://blog.csdn.net/qq_44052058/article/details/112170541
0
投稿
猜你喜欢
- C#中的string是比特殊的类,说引用类型,但不存在堆里面,而且String str=new String("HelloWorl
- 一. SpringBoot中实现Session共享1. 创建web项目我们按照之前的经验,创建一个web程序,并将之改造成Spring Bo
- 简介Exchanger是一个用于线程间数据交换的工具类,它提供一个公共点,在这个公共点,两个线程可以交换彼此的数据。当一个线程调用excha
- 本文主要介绍了C# 泛型List排序的实现,分享给大家,具体如下:代码using System;using System.Collectio
- 在《Spring Boot Hello World》中介绍了一个简单的spring boot例子,体验了spring boot中的诸多特性,
- 刚刚开始学习C#,想自己做一个网页游戏的挂。游戏里面有收钱的动作,一个建筑物一个建筑物的点,很累啊。于是想用C#模拟鼠标操作替我收钱,想着学
- 代码中已经加入了注释,需要的朋友可以直接参考代码中的注释。下面直接上功能实现的主要代码:import java.io.File;import
- 这篇文章主要介绍了shiro多验证登录代码实例及问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 前言分页是我们在开发中绕不过去的一个坎!当你的数据量大了的时候,一次性将所有数据查出来不现实,所以我们一般都是分页查询的,减轻服务端的压力,
- 1.使用的是maven项目,添加依赖<!-- mybatis-plus begin --> <depend
- spring针对Bean之间的循环依赖,有自己的处理方案。关键点就是 * 缓存。当然这种方案不能解决所有的问题,他只能解决Bean单例模式下非
- 继承反应了类和类之间的关系。世界上很多事物都是有共性的,共性的那一部分我们就抽象为基类,用于派生其它类,这样提高了代码的复用性,使得代码的结
- 本文实例为大家分享了Unity实现攻击范围检测并绘制检测区域的具体代码,供大家参考,具体内容如下一、圆形检测using System.Col
- C#编写一个简易计算器,供大家参考,具体内容如下界面代码using System;using System.Collections.Gene
- 本文实例讲述了C#使用oledb操作excel文件的方法。分享给大家供大家参考。具体分析如下:不管什么编程语言都会提供操作Excel文件的方
- 一、程序运行环境编译环境:IntelliJ IDEA所需测试文件:PDF、.pfx数字证书及密钥、PDF Jar包(Free Spire.P
- 看到某些App里面有读取联系人的功能,然后自己尝试了一下。发现这个挺简单的。然后自己就做了一个demo给大家,希望借这个demo可以让大家学
- java Mybatis存进时间戳封装了一个实体类,里面有个字段 Integer createTime。要利用这个实体类将一个时间戳存进数据
- 本文实例实现C#以一个收银付费的小程序演示switch case语法如何使用,读入用户选择,把用户的选择赋值给变量n,再根据用户的输入提示付
- 简介ThreadPoolExecutor是一个实现ExecutorService接口的线程池,ExecutorService是主要用来处理多