欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++結(jié)合QT實(shí)現(xiàn)帶有優(yōu)先級(jí)的計(jì)算器功能

 更新時(shí)間:2021年01月05日 17:13:30   作者:ruokey_1  
這篇文章主要介紹了C++結(jié)合QT實(shí)現(xiàn)帶有優(yōu)先級(jí)的計(jì)算器,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

代碼

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("計(jì)算器"));
	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()));
	/*
	//沒有優(yōu)先級(jí)的計(jì)算器
	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()//小數(shù)點(diǎn)
{
	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()//重置輸入框里的內(nèi)容
{
	ui.textBrowser->clear();
}
void MyCalculator::on_action_Button_d_clicked()//刪除表達(dá)式中最后一個(gè)符號(hào)
{
	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) {//優(yōu)先級(jí)設(shè)置
	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;//用來(lái)判斷是否是數(shù)字
		int xiaoshu = 1;//用來(lái)判斷是否是小數(shù)部分
		double number = 0;//暫存數(shù)字
		QString frist = input.left(1);
		while (isNum(frist))				//連續(xù)的數(shù)字轉(zhuǎn)化為一個(gè)整數(shù)
		{
			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())				//操作符第一次入符號(hào)棧
			{
				Act.push(frist);
				input = input.right(input.length() - 1);
				frist = input.left(1);
			}
		}
		if (frist == '=')//支持得到結(jié)果后仍可以繼續(xù)運(yùn)算
		{
			Num.clear();
			Act.clear();
			input = input.right(input.length() - 1);
			frist = input.left(1);
		}

		if (i + 1 >= a)				//當(dāng)表達(dá)式都進(jìn)棧后,只要符號(hào)棧不為空就繼續(xù)執(zhí)行
		{
			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();//將輸入框里的內(nèi)容給input
	double value = getjieguo(input);//將表達(dá)式傳給getjieguo來(lái)將數(shù)字和操作符分別入對(duì)應(yīng)的棧
	ui.textBrowser->clear();
	ui.textBrowser->append(input + "=" + QString::number(value));//將結(jié)果的類型由數(shù)字轉(zhuǎn)化為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,請(qǐng)重新輸入"));
	else
		ui.textBrowser->append(QString::number(res));
}
*/

測(cè)試

表達(dá)式 結(jié)果
2*3+6-(1+3) 8
2+3*6-(1+3) 16
2+3*6-(1.3+5/2) 16.2

說明

  1. 自己的學(xué)習(xí)筆記 ,還有一些bug沒有解決;
  2. 部分代碼需要優(yōu)化,重構(gòu);
  3. 沒有實(shí)現(xiàn)輸入錯(cuò)誤表達(dá)式報(bào)錯(cuò)功能,需要輸入正確的表達(dá)式。
  4. 不支持負(fù)數(shù)計(jì)算。
  5. 支持小數(shù),加,減,乘,除,括號(hào)運(yùn)算。

在這里插入圖片描述

到此這篇關(guān)于C++結(jié)合QT實(shí)現(xiàn)帶有優(yōu)先級(jí)的計(jì)算器的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)計(jì)算器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于C++實(shí)現(xiàn)的線程休眠代碼

    基于C++實(shí)現(xiàn)的線程休眠代碼

    這篇文章主要介紹了基于C++實(shí)現(xiàn)的線程休眠代碼,包括了Linux平臺(tái)及基于boost庫(kù)的兩種實(shí)現(xiàn)方法,有不錯(cuò)的參考借鑒價(jià)值,需要的朋友可以參考下
    2014-10-10
  • Qt的Qss用法小結(jié)

    Qt的Qss用法小結(jié)

    Qt的Qss是一種用于定義用戶界面的樣式表語(yǔ)言,本文主要介紹了Qt的Qss用法小結(jié),非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2023-06-06
  • C++ float、double判斷是否等于0問題

    C++ float、double判斷是否等于0問題

    這篇文章主要介紹了C++ float、double判斷是否等于0問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • C/C++?QT實(shí)現(xiàn)自定義對(duì)話框的示例代碼

    C/C++?QT實(shí)現(xiàn)自定義對(duì)話框的示例代碼

    對(duì)話框分為多種,常見的有通用對(duì)話框,自定義對(duì)話框,模態(tài)對(duì)話框,非模態(tài)對(duì)話框等,本文主要介紹了QT自定義對(duì)話框,感興趣的可以了解一下
    2021-11-11
  • Qt使用QCustomPlot的實(shí)現(xiàn)示例

    Qt使用QCustomPlot的實(shí)現(xiàn)示例

    QCustomPlot是一個(gè)基于Qt C++的圖形庫(kù),用于繪制和數(shù)據(jù)可視化,并為實(shí)時(shí)可視化應(yīng)用程序提供高性能服務(wù),本文主要介紹了Qt使用QCustomPlot的實(shí)現(xiàn)示例,感興趣的可以了解一下
    2024-01-01
  • C++11中bind綁定器和function函數(shù)對(duì)象介紹

    C++11中bind綁定器和function函數(shù)對(duì)象介紹

    這篇文章主要介紹了C++11中bind綁定器和function函數(shù)對(duì)象介紹,綁定器,函數(shù)對(duì)象和lambda表達(dá)式只能使用在一條語(yǔ)句中,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-07-07
  • C++中日期類的常見題目合集分享

    C++中日期類的常見題目合集分享

    這篇文章主要為大家詳細(xì)介紹了一些C++中日期類的常見題目,文中的示例代碼講解詳細(xì),對(duì)我們掌握C++有一定的幫助,感興趣的小伙伴可以了解一下
    2023-06-06
  • 基于C++實(shí)現(xiàn)kinect+opencv 獲取深度及彩色數(shù)據(jù)

    基于C++實(shí)現(xiàn)kinect+opencv 獲取深度及彩色數(shù)據(jù)

    本文的主要思想是Kinect SDK 讀取彩色、深度、骨骼信息并用OpenCV顯示,非常的實(shí)用,有需要的小伙伴可以參考下
    2015-12-12
  • 基于C語(yǔ)言字符串函數(shù)的一些使用心得

    基于C語(yǔ)言字符串函數(shù)的一些使用心得

    以下是對(duì)C語(yǔ)言中字符串函數(shù)的一些使用心得進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來(lái)參考下
    2013-08-08
  • C++深入刨析muduo中的抽象類Poller

    C++深入刨析muduo中的抽象類Poller

    muduo網(wǎng)絡(luò)庫(kù)中Poller類是一個(gè)抽象類,用戶使用PollPoller或者EPollPoller類,下面跟隨小編一起來(lái)詳細(xì)了解一下
    2022-04-04

最新評(píng)論