一、模块图
二、核心算法 —– 中缀表达式转化为后缀表达式
1、将中缀表达式转换为后缀表达式的算法思想:
1)计算机实现转换:
·开始扫描;
·数字时,加入后缀表达式;
·运算符:
a. 若为 ‘(‘,入栈;
b. 若为 ‘)’,则依次把栈中的的运算符加入后缀表达式中,直到出现’(‘,从栈中删除’(’ ;
c. 若为 除括号外的其他运算符, 当其优先级高于除’(‘以外的栈顶运算符时,直接入栈。否则从栈顶开始,依次弹出比当前处理的运算符优先级高和优先级相等的运算符,直到一个比它优先级低的或者遇到了一个左括号为止。
·当扫描的中缀表达式结束时,栈中的的所有运算符出栈;
2)人工实现转换
这里我给出一个中缀表达式:a+b*c-(d+e)
第一步:按照运算符的优先级对所有的运算单位加括号:式子变成了:((a+(b*c))-(d+e))
第二步:转换前缀与后缀表达式
前缀:把运算符号移动到对应的括号前面
则变成了:-( +(a *(bc)) +(de))
把括号去掉:-+a*bc+de 前缀式子出现
后缀:把运算符号移动到对应的括号后面
则变成了:((a(bc)* )+ (de)+ )-
把括号去掉:abc*+de+- 后缀式子出现
发现没有,前缀式,后缀式是不需要用括号来进行优先级的确定的。如表达式:3+(2-5)*6/3
后缀表达式 栈
3_________________+
3 ______+(
3 2 _____+(-
3 2 5 -___ +
3 2 5 - ___+*
3 2 5 - 6 * _+/
3 2 5 - 6 *3 ____+/
3 2 5 - 6 *3 /+__
(“_“用于隔开后缀表达式与栈)
另外一个人认为正确的转换方法:
遍历中缀表达式的每个节点,如果:
1、 该节点为操作数:
直接拷贝进入后缀表达式
2、 该节点是运算符,分以下几种情况:
A、 为“(”运算符:
压入临时堆栈中
B、 为“)”运算符:
不断地弹出临时堆栈顶部运算符直到顶部的运算符是“(”为止,从栈中删除’(‘。并把弹出的运算符都添加到后缀表达式中。
C、 为其他运算符,有以下步骤进行:
比较该运算符与临时栈栈顶指针的运算符的优先级,如果临时栈栈顶指针的优先级大于等于该运算符的优先级,弹出并添加到后缀表达式中,反复执行前面的比较工作,直到遇到一个栈顶指针的优先级低于该运算符的优先级,停止弹出添加并把该运算符压入栈中。
此时的比较过程如果出现栈顶的指针为‘(’,则停止循环并把该运算符压入栈中,注意:‘(’不要弹出来。
遍历完中缀表达式之后,检查临时栈,如果还有运算符,则全部弹出,并添加到后缀表达式中。
2)运用后缀表达式进行计算的具体做法:
建立一个栈S 。从左到右读表达式,如果读到操作数就将它压入栈S中,如果读到n元运算符(即需要参数个数为n的运算符)则取出由栈顶向下的n项按操作数运算,再将运算的结果代替原栈顶的n项,压入栈S中 。如果后缀表达式未读完,则重复上面过程,最后输出栈顶的数值则为结束。
**三、代码
**
mycalculator.h:
#ifndef MYCALCULATOR_H
#define MYCALCULATOR_H
#include <QWidget>
#include <QMainWindow>
#include <QPushButton>
#include <QLineEdit>
#include <QPalette>
#include <QStack>
#include <QPainter>
#include <QDateTime>
namespace Ui
{
class MyCalculator;
}
class MyCalculator : public QMainWindow
{
Q_OBJECT
private:
QLineEdit *lineEdit;//显示框
QString input; //输入框
void MYcalculate();
private slots://定义槽函数
void buttonClicked();
public:
explicit MyCalculator(QWidget *parent = 0);
~MyCalculator();
// 增加 paintEvent() 的声明
protected:
void paintEvent(QPaintEvent *); //重写paintEvent()
private:
Ui::MyCalculator *ui;
};
#endif // MYCALCULATOR_H
main.cpp:
#include "mycalculator.h"
#include <QApplication>
#include <QDesktopWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyCalculator w;
w.setFixedHeight(525);
w.setFixedWidth(300);
w.move(QApplication::desktop()->rect().center()-QPoint(200,250));
w.show();
return a.exec();
}
mycalculator.cpp:
#include "mycalculator.h"
#include "ui_mycalculator.h"
void Change(const char *S,char OPS[],int &len);
void EXchange(char B[], int len, double &result,bool &flag);
//构造函数
MyCalculator::MyCalculator(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyCalculator)
{
// 通过样式表给窗口设置背景图
// "MyCalculator" 为类名
// "../test/test.jpg": 为图片路径,相对于可执行程序的相对路径
this->setStyleSheet("MyCalculator{background-image: url(../MyCalculator/background1.png);} ");
ui->setupUi(this);
connect(ui->Button_0,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号0与槽函数
connect(ui->Button_1,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号1与槽函数
connect(ui->Button_2,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号2与槽函数
connect(ui->Button_3,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号3与槽函数
connect(ui->Button_4,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号4与槽函数
connect(ui->Button_5,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号5与槽函数
connect(ui->Button_6,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号6与槽函数
connect(ui->Button_7,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号7与槽函数
connect(ui->Button_8,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号8与槽函数
connect(ui->Button_9,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号9与槽函数
connect(ui->Button_add,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号+与槽函数
connect(ui->Button_sub,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号-与槽函数
connect(ui->Button_mul,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号*与槽函数
connect(ui->Button_div,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号/与槽函数
connect(ui->Button_backward,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号1/X与槽函数
connect(ui->Button_C,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号C与槽函数
connect(ui->Button_CE,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号CE与槽函数
connect(ui->Button_XY,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号X^Y与槽函数
connect(ui->Button_equal,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号=与槽函数
connect(ui->Button_right,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号)与槽函数
connect(ui->Button_remain,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号%与槽函数
connect(ui->Button_time,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号Time与槽函数
connect(ui->Button_left,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号(与槽函数
connect(ui->Button_point,SIGNAL(clicked()),this,SLOT(buttonClicked()));//信号.与槽函数
QPalette palette;
palette.setColor(QPalette::Text,Qt::red);//设置字体颜色
ui->lineEdit->setFont(QFont("Timers",20,QFont::Bold));//设置字体大小
ui->lineEdit->setPalette(palette);
input = "0";
}
//析构函数
MyCalculator::~MyCalculator()
{
delete ui;
}
// 重写paintEvent()
void MyCalculator::paintEvent(QPaintEvent *)
{
QStyleOption opt; // 需要头文件#include <QStyleOption>
opt.init(this);
QPainter p(this); // 需要头文件#include <QPainter>
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
void MyCalculator::buttonClicked()//按钮触动槽函数
{
lineEdit = ui->lineEdit;
input = lineEdit->text();//输入框
QPushButton *tb = qobject_cast<QPushButton *>(sender());//把信号的发送者转换成pushbutton类型
QString text = tb->text();//text:获取或设置文本框的文本内容。
if(text == QString("CE"))
{
text = input.left(input.length()-1); //减去一字符
lineEdit->setText(text);
}
else if(text == QString("C"))//清空所有
{
input="";
lineEdit->setText(input);
}
else if(text == QString("±"))
{
if(input=="")
text='-';
else
{
QString::iterator p=text.end();
p--;
if('-'==*p)
text=text.left(text.length()-1);
else
text=text+'-';
}
lineEdit->setText(text);
}
else if(text == QString("1/X"))
{
if(input != "")
lineEdit->setText("1/"+input);
else
{
lineEdit->setText("divisor can't be 0");
}
}
else if(text == QString("X^Y"))
{
if(input != "")
lineEdit->setText(input+"^");
}
else if(text == QString("Time"))
{
text=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm"); //格式化输出当前时间
lineEdit->setText(text);
}
else if(text == QString("="))
{
std::string str=input.toStdString();//QString转化为String
const char *S=str.c_str();//整个输入框的字符串
char OPS[50];//中缀表达式
int len;//去掉括号之后中缀表达式的长度
double result;//计算结果
QString change;
bool flag1 = true;//判断出书是否为0的标志
Change(S,OPS,len);
EXchange(OPS,len,result,flag1);
if(flag1 == false)
{
lineEdit->clear();
lineEdit->setText("divisor can't be 0");
}
else
{
change=QString::number(result,10,6);//将计算结果转换为字符串
lineEdit->setText(change);
}
}
else//+ - * / % 0-9
{
lineEdit->setText(input+=text);
}
}
void Change(const char *S,char OPS[],int &len)//将中缀表达式转变为后缀表达式
{
QStack<char> OPE;//符号栈
unsigned int i,j=0;
unsigned int tmp = strlen(S);
for (i = 0; i < tmp; i++)
{
switch (S[i])
{
case'+':
if(OPE.isEmpty())//栈为空
OPE.push(S[i]);
else if (OPE.top() == '*' || OPE.top() == '/' || OPE.top() == '%' || OPE.top()=='^')
{
OPS[j++] = OPE.pop();//弹出比'+'运算符优先级高和相等的运算符,依次加入后缀表达式
i--;
}
else
OPE.push(S[i]);
break;
case'-':
if(i!=0 && '('!=S[i-1])//正数
{
if(OPE.isEmpty())//栈为空
OPE.push(S[i]);
else if (OPE.top() == '*' || OPE.top() == '/'|| OPE.top() == '%' || OPE.top()=='^')//弹出比'-'运算符优先级高和相等的运算符,依次加入后缀表达式
{
OPS[j++] = OPE.pop();
i--;
}
else
OPE.push(S[i]);
}
else//负数
{
while ((S[i] >= '0'&&S[i] <= '9' ) || S[i] == '.' || ('-'==S[i]&&(S[i-1]<'0'||S[i-1]>'9')))
{
OPS[j++] = S[i];
if('-'==S[i])
OPS[j++]='@';
i++;
}
i--;
OPS[j++] = '#'; //数字中的间隔符
}
break;
case '^':
OPE.push(S[i]);
break;
case'*':
if(OPE.isEmpty())//栈为空
OPE.push(S[i]);
else if (OPE.top() == '^')
{
OPS[j++] = OPE.pop();//弹出比'/'运算符优先级高和相等的运算符,依次加入后缀表达式
i--;
}
else
OPE.push(S[i]);
break;
case'/':
if(OPE.isEmpty())//栈为空
OPE.push(S[i]);
else if (OPE.top() == '^')
{
OPS[j++] = OPE.pop();//弹出比'/'运算符优先级高和相等的运算符,依次加入后缀表达式
i--;
}
else
OPE.push(S[i]);
break;
case '%':
if(OPE.isEmpty())//栈为空
OPE.push(S[i]);
else if (OPE.top() == '^'|| OPE.top()=='*' || OPE.top()=='/')
{
OPS[j++] = OPE.pop();//弹出比'%'运算符优先级高和相等的运算符,依次加入后缀表达式
i--;
}
else
OPE.push(S[i]);
break;
case'(':
OPE.push(S[i]);
break;
case')':
while (OPE.top() != '(')//依次把栈中的运算符加入后缀表达式并将其出栈
{
OPS[j++] = OPE.pop();
}
OPE.pop();//从栈中弹出'('
break;
default:
while ((S[i] >= '0'&&S[i] <= '9') || S[i] == '.' || ('-'==S[i]&&S[i-1]<'0'&&S[i-1]>'9'))
{
OPS[j++] = S[i];
i++;
}
i--;
OPS[j++] = '#'; //数字中的间隔符
break;
}
}
while (!OPE.isEmpty())
{
OPS[j++] = OPE.pop();
}
len = j;
}
void EXchange(char B[], int len, double &result,bool &flag)//用后缀表达式计算结果
{
int i;
double a;
double b;
double c;
QStack<double>SZ;
for (i = 0; i < len; i++)
{
switch (B[i])
{
case'^':
{
a = SZ.pop();
b = SZ.pop();
c = pow(b,a);
SZ.push(c);
}
break;
case'+':
{
a = SZ.pop();
b = SZ.pop();
c = b + a;
SZ.push(c);
}
break;
case'-':
{
if('@'!=B[i+1])
{
a = SZ.pop();
b = SZ.pop();
c = b - a;
SZ.push(c);
}
else
{
int jx = 0;
double dx;
char *stx = new char[10];
while (B[i] != '#')
{
if('@'!=B[i])
stx[jx++] = B[i];
i++;
}
dx = atof(stx);//把字符串转换成浮点数
SZ.push(dx);
delete stx;
}
}
break;
case'*':
{
a = SZ.pop();
b = SZ.pop();
c = b*a;
SZ.push(c);
}
break;
case'/':
{
a = SZ.pop();
b = SZ.pop();
if (a == 0)
{
flag = false;
return;
}
c = b / a;
SZ.push(c);
}
break;
case'%':
{
a = SZ.pop();
b = SZ.pop();
if (a == 0)
{
flag = false;
return;
}
c = (int)b % (int)a;
SZ.push(c);
}
break;
default:
int j = 0;
double d;
char *st = new char[10];
while (B[i] != '#')
{
st[j++] = B[i];
i++;
}
d = atof(st);//把字符串转换成浮点数
SZ.push(d);
delete st;
break;
}
}
result=SZ.top();
}
界面文件:mycalculator.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MyCalculator</class>
<widget class="QMainWindow" name="MyCalculator">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>302</width>
<height>546</height>
</rect>
</property>
<property name="windowTitle">
<string>MyCalculator</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: qconicalgradient(cx:0, cy:0, angle:135, stop:0 rgba(255, 255, 0, 69), stop:0.375 rgba(255, 255, 0, 69), stop:0.423533 rgba(251, 255, 0, 145), stop:0.45 rgba(247, 255, 0, 208), stop:0.477581 rgba(255, 244, 71, 130), stop:0.518717 rgba(255, 218, 71, 130), stop:0.55 rgba(255, 255, 0, 255), stop:0.57754 rgba(255, 203, 0, 130), stop:0.625 rgba(255, 255, 0, 69), stop:1 rgba(255, 255, 0, 69));</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QPushButton" name="Button_time">
<property name="geometry">
<rect>
<x>80</x>
<y>450</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Time</string>
</property>
</widget>
<widget class="QPushButton" name="Button_6">
<property name="geometry">
<rect>
<x>130</x>
<y>370</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>6</string>
</property>
</widget>
<widget class="QPushButton" name="Button_0">
<property name="geometry">
<rect>
<x>30</x>
<y>450</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>0</string>
</property>
</widget>
<widget class="QPushButton" name="Button_7">
<property name="geometry">
<rect>
<x>30</x>
<y>330</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>7</string>
</property>
</widget>
<widget class="QPushButton" name="Button_point">
<property name="geometry">
<rect>
<x>130</x>
<y>450</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>.</string>
</property>
</widget>
<widget class="QPushButton" name="Button_C">
<property name="geometry">
<rect>
<x>180</x>
<y>290</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>C</string>
</property>
</widget>
<widget class="QPushButton" name="Button_1">
<property name="geometry">
<rect>
<x>30</x>
<y>410</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>1</string>
</property>
</widget>
<widget class="QPushButton" name="Button_8">
<property name="geometry">
<rect>
<x>80</x>
<y>330</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>8</string>
</property>
</widget>
<widget class="QPushButton" name="Button_5">
<property name="geometry">
<rect>
<x>80</x>
<y>370</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>5</string>
</property>
</widget>
<widget class="QPushButton" name="Button_remain">
<property name="geometry">
<rect>
<x>230</x>
<y>330</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>%</string>
</property>
</widget>
<widget class="QPushButton" name="Button_mul">
<property name="geometry">
<rect>
<x>180</x>
<y>370</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>*</string>
</property>
</widget>
<widget class="QPushButton" name="Button_4">
<property name="geometry">
<rect>
<x>30</x>
<y>370</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>4</string>
</property>
</widget>
<widget class="QPushButton" name="Button_XY">
<property name="geometry">
<rect>
<x>230</x>
<y>290</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string>X^Y</string>
</property>
</widget>
<widget class="QPushButton" name="Button_3">
<property name="geometry">
<rect>
<x>130</x>
<y>410</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>3</string>
</property>
</widget>
<widget class="QPushButton" name="Button_add">
<property name="geometry">
<rect>
<x>180</x>
<y>450</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>+</string>
</property>
</widget>
<widget class="QPushButton" name="Button_equal">
<property name="geometry">
<rect>
<x>230</x>
<y>410</y>
<width>41</width>
<height>61</height>
</rect>
</property>
<property name="text">
<string>=</string>
</property>
</widget>
<widget class="QPushButton" name="Button_backward">
<property name="geometry">
<rect>
<x>230</x>
<y>370</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>1/X</string>
</property>
</widget>
<widget class="QPushButton" name="Button_sub">
<property name="geometry">
<rect>
<x>180</x>
<y>410</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>-</string>
</property>
</widget>
<widget class="QPushButton" name="Button_div">
<property name="geometry">
<rect>
<x>180</x>
<y>330</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">border-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 0, 0, 255), stop:0.166 rgba(255, 255, 0, 255), stop:0.333 rgba(0, 255, 0, 255), stop:0.5 rgba(0, 255, 255, 255), stop:0.666 rgba(0, 0, 255, 255), stop:0.833 rgba(255, 0, 255, 255), stop:1 rgba(255, 0, 0, 255));</string>
</property>
<property name="text">
<string>/</string>
</property>
</widget>
<widget class="QPushButton" name="Button_CE">
<property name="geometry">
<rect>
<x>130</x>
<y>290</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>CE</string>
</property>
</widget>
<widget class="QPushButton" name="Button_right">
<property name="geometry">
<rect>
<x>80</x>
<y>290</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>)</string>
</property>
</widget>
<widget class="QPushButton" name="Button_left">
<property name="geometry">
<rect>
<x>30</x>
<y>290</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>(</string>
</property>
</widget>
<widget class="QPushButton" name="Button_2">
<property name="geometry">
<rect>
<x>80</x>
<y>410</y>
<width>41</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>2</string>
</property>
</widget>
<widget class="QPushButton" name="Button_9">
<property name="geometry">
<rect>
<x>130</x>
<y>330</y>
<width>41</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>9</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>281</width>
<height>61</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>302</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
运行结果:
运行结果检查:
1.除数为0
2.加法
3.减法
4。Time
5.综合测验