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
설명 하 다.
여기 서 C++QT 와 결합 하여 우선 순위 가 있 는 계산 기 를 실현 하 는 것 에 관 한 글 을 소개 합 니 다.더 많은 관련 C++계산기 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Visual Studio에서 파일 폴더 구분 (포함 경로 설정)Visual Studio에서 c, cpp, h, hpp 파일을 폴더로 나누고 싶었습니까? 어쩌면 대부분의 사람들이 있다고 생각합니다. 처음에 파일이 만들어지는 장소는 프로젝트 파일 등과 같은 장소에 있기 때문에 파일...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.