widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include<QMessageBox>
#include <QWidget>
#include<QStack>
#include<string.h>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_num1_clicked();
void on_num0_clicked();
void on_point_clicked();
void on_deng_clicked();
void on_jia_clicked();
void on_num2_clicked();
void on_num3_clicked();
void on_jian_clicked();
void on_num4_clicked();
void on_num5_clicked();
void on_num6_clicked();
void on_cheng_clicked();
void on_num7_clicked();
void on_num8_clicked();
void on_num9_clicked();
void on_chu_clicked();
void on_sign1_clicked();
void on_sign2_clicked();
void on_pushButton_11_clicked();
void on_signdel_clicked();
void on_sign2_3_clicked();
private:
Ui::Widget *ui;
QString expression;
int Priority(char ch);
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QIcon con("C:\\Users\\MALING\\Pictures\\Saved Pictures\\OIP-C (1).jpg");
ui->sign2_3->setIcon(con);
ui->deng->setStyleSheet("background:rgb(254,246,113)");
ui->num0->setStyleSheet("background:rgb(254,246,113)");
ui->num1->setStyleSheet("background:rgb(254,246,113)"
"border: 1px solid gray"/*设置边框的粗细,以及颜色*/
"border-radius: 10px"/*设置圆角的大小*/
" padding: 0 8px"/*如果没有内容光标在开始往后移动0.8像素点*/);
ui->sign2_3->setStyleSheet("background:rgb(225,140,143)");
ui->lineEdit->setStyleSheet("background:rgb(172,216,201)");
ui->history->setStyleSheet("background:rgb(172,216,201)");
ui->num2->setStyleSheet("background:rgb(254,246,113)");
ui->num3->setStyleSheet("background:rgb(254,246,113)");
ui->num4->setStyleSheet("background:rgb(254,246,113)");
ui->num5->setStyleSheet("background:rgb(254,246,113)");
ui->num6->setStyleSheet("background:rgb(254,246,113)");
ui->num7->setStyleSheet("background:rgb(254,246,113)");
ui->num8->setStyleSheet("background:rgb(254,246,113)");
ui->num9->setStyleSheet("background:rgb(254,246,113)");
ui->sign1->setStyleSheet("background:rgb(254,246,113)");
ui->sign2->setStyleSheet("background:rgb(254,246,113)");
ui->signdel->setStyleSheet("background:rgb(254,246,113)");
ui->jia->setStyleSheet("background:rgb(254,246,113)");
ui->jian->setStyleSheet("background:rgb(254,246,113)");
ui->cheng->setStyleSheet("background:rgb(254,246,113)");
ui->chu->setStyleSheet("background:rgb(254,246,113)");
ui->num1->setStyleSheet("background:rgb(254,246,113)");
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_num1_clicked()
{
expression += "1";
ui->lineEdit->setText(expression);
}
void Widget::on_num2_clicked()
{
expression += "2";
ui->lineEdit->setText(expression);
}
void Widget::on_num3_clicked()
{
expression += "3";
ui->lineEdit->setText(expression);
}
void Widget::on_num4_clicked()
{
expression += "4";
ui->lineEdit->setText(expression);
}
void Widget::on_num5_clicked()
{
expression += "5";
ui->lineEdit->setText(expression);
}
void Widget::on_num6_clicked()
{
expression += "6";
ui->lineEdit->setText(expression);
}
void Widget::on_num7_clicked()
{
expression += "7";
ui->lineEdit->setText(expression);
}
void Widget::on_num8_clicked()
{
expression += "8";
ui->lineEdit->setText(expression);
}
void Widget::on_num9_clicked()
{
expression += "9";
ui->lineEdit->setText(expression);
}
void Widget::on_num0_clicked()
{
expression += "0";
ui->lineEdit->setText(expression);
}
void Widget::on_sign1_clicked()
{
expression += "(";
ui->lineEdit->setText(expression);
}
void Widget::on_sign2_clicked()
{
expression += ")";
ui->lineEdit->setText(expression); if(ui->lineEdit->text() == "0")
{
ui->lineEdit->clear();
}
}
void Widget::on_jia_clicked()
{
expression += "+";
ui->lineEdit->setText(expression);
}
void Widget::on_jian_clicked()
{
expression += "-";
ui->lineEdit->setText(expression);
}
void Widget::on_chu_clicked()
{
expression += "/";
ui->lineEdit->setText(expression);
}
void Widget::on_cheng_clicked()
{
expression += "*";
ui->lineEdit->setText(expression);
}
void Widget::on_signdel_clicked()
{
expression.clear();
ui->lineEdit->clear(); if(ui->lineEdit->text() == "0")
{
ui->lineEdit->clear();
}
}
void Widget::on_sign2_3_clicked()
{
expression.chop(1);
ui->lineEdit->setText(expression); if(ui->lineEdit->text() == "0")
{
ui->lineEdit->clear();
}
}
bool isArithmeticExpressionValid(const QString &expression) {
QStack<QChar> stack;
for (const QChar &ch : expression) {
if (ch == '(') {
stack.push(ch);
} else if (ch == ')') {
if (stack.isEmpty() || stack.pop() != '(') {
return false;
}
}
}
return stack.isEmpty();
}
void Widget::on_deng_clicked()
{
QString temp = ui->lineEdit->text();if(!isArithmeticExpressionValid(temp))
{
QMessageBox::warning(0, "", "错误!请重新输入!");
return;
}
QStack<int> s_num, s_opt;
char opt[128] = {0};
int i = 0, tmp = 0, num1, num2;
//把QString转换成char *
QByteArray ba;
ba.append(expression); //把QString转换成QByteArray
strcpy(opt, ba.data());
//data可以把QByteArray转换成const char *
while (opt[i] != '\0' || s_opt.empty() != true)
{
if (opt[i] >= '0' && opt[i] <= '9')
{
tmp = tmp * 10 + opt[i] - '0';
i++;
if (opt[i] < '0' || opt[i] > '9')
{
s_num.push(tmp);
tmp = 0;
}
}
else
{
if (s_opt.empty() == true || Priority(opt[i]) > Priority(s_opt.top()) ||
(s_opt.top() == '(' && opt[i] != ')'))
{
s_opt.push(opt[i]);
i++;
continue;
}
if (s_opt.top() == '(' && opt[i] == ')')
{
s_opt.pop();
i++;
continue;
}
if (Priority(opt[i]) <= Priority(s_opt.top()) || (opt[i] == ')' && s_opt.top() != '(') ||
(opt[i] == '\0' && s_opt.empty() != true))
{
char ch = s_opt.top();
s_opt.pop();
switch(ch)
{
case '+':
num1 = s_num.top();
s_num.pop();
num2 = s_num.top();
s_num.pop();
s_num.push(num1 + num2);
break;
case '-':
num1 = s_num.top();
s_num.pop();
num2 = s_num.top();
s_num.pop();
s_num.push(num2 - num1);
break;
case '*':
num1 = s_num.top();
s_num.pop();
num2 = s_num.top();
s_num.pop();
s_num.push(num1 * num2);
break;
case '/':
num1 = s_num.top();
s_num.pop();
num2 = s_num.top();
s_num.pop();
s_num.push(num2 / num1);
break;
}
}
}
}
ui->lineEdit->setText(QString::number(s_num.top()));
expression.clear();
if(ui->lineEdit->text() == "0")
{
ui->lineEdit->clear();
}
int a = s_num.top();
QString m = QString::number(a,10);
ui->lineEdit->setText(m);
expression=m;
ui->lineEdit->setText(ui->lineEdit->text());
ui->history->setText(ui->history->toPlainText() + "\n" + temp + " = " +m);
}
int Widget::Priority(char ch)
{
switch(ch)
{
case '(':
return 3;
case '*':
case '/':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}