main.cpp
#include "calculator.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
calculator w;
w.show();
return a.exec();
}
calculator.h的代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
class calculator : public QMainWindow
{
Q_OBJECT
public:
calculator(QWidget *parent = 0);
~calculator();
private:
QLineEdit *inputLine;//显示框
QString input="0"; //输入框
bool flat=false;
//0-9按钮
QPushButton *zeroButton;
QPushButton *oneButton;
QPushButton *twoButton;
QPushButton *threeButton;
QPushButton *fourButton;
QPushButton *fiveButton;
QPushButton *sixButton;
QPushButton *sevenButton;
QPushButton *eightButton;
QPushButton *nineButton;
QPushButton *addButton;
QPushButton *subButton;
QPushButton *divButton;
QPushButton *mulButton;
QPushButton *equButton;
QPushButton *timButton; //Time
QPushButton *decButton;
QPushButton *botButton;
QPushButton *CEButton;
QPushButton *ACButton;
QPushButton *lefButton;
QPushButton *rigButton;
private slots:
void buttonZeroClicked();
void buttonOneClicked();
void buttonTwoClicked();
void buttonThreeClicked();
void buttonFourClicked();
void buttonFiveClicked();
void buttonSixClicked();
void buttonSevenClicked();
void buttonEightClicked();
void buttonNineClicked();
void buttonAddClicked();//+
void buttonSubClicked();//-
void buttonMulClicked();//乘
void buttonDivClicked();//除
void buttonTimClicked(); //时间
void buttonDecClicked();//小数点
void buttonBotClicked(); //加减号
void buttonEquClicked();//等于号
void buttonLefClicked();//左括号
void buttonRigClicked();//右括号
void buttonCEClicked();//CE
void buttonACClicked();//AC
};
#endif // MAINWINDOW_H
calculator.cpp的代码
#include "calculator.h"
#include
using namespace std;
#include
#include
#include
#include
#pragma execution_character_set("utf-8")
bool isNum(char ch);
bool isOperate(char ch);
int level(char ch);
double scd(string s);
double getValue(vector V);
vector midToPost(string s);
calculator::calculator(QWidget *parent) : QMainWindow(parent)
{
QWidget *widget=new QWidget; //构建一个QWidget布局将设置的布局添加进这个QWidget
this->setCentralWidget(widget);
inputLine=new QLineEdit;
inputLine->setText(input);
zeroButton=new QPushButton("0");
oneButton=new QPushButton("1");
twoButton=new QPushButton("2");
threeButton=new QPushButton("3");
fourButton=new QPushButton("4");
fiveButton=new QPushButton("5");
sixButton=new QPushButton("6");
sevenButton=new QPushButton("7");
eightButton=new QPushButton("8");
nineButton=new QPushButton("9");
decButton=new QPushButton(".");
botButton=new QPushButton("±");
timButton=new QPushButton("Time");
addButton=new QPushButton("+");
subButton=new QPushButton("-");
mulButton=new QPushButton("*");
divButton=new QPushButton("/");
equButton=new QPushButton("=");
lefButton=new QPushButton("(");
rigButton=new QPushButton(")");
CEButton=new QPushButton("CE");
ACButton=new QPushButton("AC");
QGridLayout *H=new QGridLayout(this); //声明了一个网格布局
inputLine->setFixedHeight(50); //调整第一行大小
CEButton->setFixedHeight(50);
ACButton->setFixedHeight(50);
H->addWidget(inputLine,0,0,1,3);
H->setRowStretch(0,100);
H->addWidget(CEButton,0,3);
H->addWidget(ACButton,0,4);
H->addWidget(oneButton,1,0);
H->addWidget(twoButton,1,1);
H->addWidget(threeButton,1,2);
H->addWidget(botButton,1,3);
H->addWidget(timButton,1,4);
H->addWidget(fourButton,2,0);
H->addWidget(fiveButton,2,1);
H->addWidget(sixButton,2,2);
H->addWidget(addButton,2,3);
H->addWidget(subButton,2,4);
H->addWidget(sevenButton,3,0);
H->addWidget(eightButton,3,1);
H->addWidget(nineButton,3,2);
H->addWidget(mulButton,3,3);
H->addWidget(divButton,3,4);
H->addWidget(zeroButton,4,0);
H->addWidget(decButton,4,1);
H->addWidget(lefButton,4,2);
H->addWidget(rigButton,4,3);
H->addWidget(equButton,4,4);
widget->setLayout(H); //将布局显示出来
connect(zeroButton,SIGNAL(clicked()),this,SLOT(buttonZeroClicked()));//信号与槽函数
connect(oneButton,SIGNAL(clicked()),this,SLOT(buttonOneClicked()));
connect(twoButton,SIGNAL(clicked()),this,SLOT(buttonTwoClicked()));
connect(threeButton,SIGNAL(clicked()),this,SLOT(buttonThreeClicked()));
connect(fourButton,SIGNAL(clicked()),this,SLOT(buttonFourClicked()));
connect(fiveButton,SIGNAL(clicked()),this,SLOT(buttonFiveClicked()));
connect(sixButton,SIGNAL(clicked()),this,SLOT(buttonSixClicked()));
connect(sevenButton,SIGNAL(clicked()),this,SLOT(buttonSevenClicked()));
connect(eightButton,SIGNAL(clicked()),this,SLOT(buttonEightClicked()));
connect(nineButton,SIGNAL(clicked()),this,SLOT(buttonNineClicked()));
connect(addButton,SIGNAL(clicked()),this,SLOT(buttonAddClicked()));
connect(subButton,SIGNAL(clicked()),this,SLOT(buttonSubClicked()));
connect(mulButton,SIGNAL(clicked()),this,SLOT(buttonMulClicked()));
connect(divButton,SIGNAL(clicked()),this,SLOT(buttonDivClicked()));
connect(botButton,SIGNAL(clicked(bool)),this,SLOT(buttonBotClicked()));//正负号
connect(timButton,SIGNAL(clicked(bool)),this,SLOT(buttonTimClicked()));//时间
connect(decButton,SIGNAL(clicked(bool)),this,SLOT(buttonDecClicked()));//点号
connect(lefButton,SIGNAL(clicked(bool)),this,SLOT(buttonLefClicked()));//左括号
connect(rigButton,SIGNAL(clicked(bool)),this,SLOT(buttonRigClicked()));//右括号
connect(CEButton,SIGNAL(clicked()),this,SLOT(buttonCEClicked()));
connect(ACButton,SIGNAL(clicked()),this,SLOT(buttonACClicked()));
connect(equButton,SIGNAL(clicked()),this,SLOT(buttonEquClicked())); //等于号*/
}
void calculator::buttonZeroClicked() //以下是实现相应的槽函数
{
if(input=="0")
input='0';
else input=input+'0';
inputLine->setText(input);
}
void calculator::buttonOneClicked()
{
if(input=="0")
input='1';
else input=input+'1';
inputLine->setText(input);
}
void calculator::buttonTwoClicked()
{
if(input=="0")
input='2';
else input=input+'2';
inputLine->setText(input);
}
void calculator::buttonThreeClicked()
{
if(input=="0")
input='3';
else input=input+'3';
inputLine->setText(input);
}
void calculator::buttonFourClicked()
{
if(input=="0")
input='4';
else input=input+'4';
inputLine->setText(input);
}
void calculator::buttonFiveClicked()
{
if(input=="0")
input='5';
else input=input+'5';
inputLine->setText(input);
}
void calculator::buttonSixClicked()
{
if(input=="0")
input='6';
else input=input+'6';
inputLine->setText(input);
}
void calculator::buttonSevenClicked()
{
if(input=="0")
input='7';
else input=input+'7';
inputLine->setText(input);
}
void calculator::buttonEightClicked()
{
if(input=="0")
input='8';
else input=input+'8';
inputLine->setText(input);
}
void calculator::buttonNineClicked()
{
if(input=="0")
input='9';
else input=input+'9';
inputLine->setText(input);
}
void calculator::buttonAddClicked()
{
if(input=="0")
input='+';
else input=input+'+';
inputLine->setText(input);
}
void calculator::buttonSubClicked()
{
if(input=="0")
input='-';
else input=input+'-';
inputLine->setText(input);
}
void calculator::buttonMulClicked()
{
if(input=="0")
input='*';
else input=input+'*';
inputLine->setText(input);
}
void calculator::buttonDivClicked()
{
if(input=="0")
input='/';
else input=input+'/';
inputLine->setText(input);
}
void calculator::buttonTimClicked() //Time
{
input=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz"); //格式化输出当前时间
inputLine->setText(input);
}
void calculator::buttonDecClicked() //小数点
{
if(input=="0")
input='.';
else input=input+'.';
inputLine->setText(input);
}
void calculator::buttonBotClicked() //加减号
{
if(input=="0")
input='-';
else
{
QString::iterator p=input.end();
p--;
if('-'==*p)
input=input.left(input.length()-1);
else input=input+'-';
}
inputLine->setText(input);
}
void calculator::buttonLefClicked()//左括号
{
if(input=="0")
input='(';
else input=input+'(';
inputLine->setText(input);
}
void calculator::buttonRigClicked()//右括号
{
if(input=="0")
input=')';
else input=input+')';
inputLine->setText(input);
}
void calculator::buttonCEClicked()
{
input = input.left(input.length()-1); //减去一字符
inputLine->setText(input);
}
void calculator::buttonACClicked() //直接清空
{
input='0';
inputLine->setText(input);
}
void calculator::buttonEquClicked()//等于号
{
string inputTemp = input.toStdString();
vector expression = midToPost(inputTemp);
double value = getValue(expression);
if(value != INT_MAX) {
input=input+"="+QString::number(value);
inputLine->setText(input);
}else{
input=input+"=syntax error";
inputLine->setText(input);
}
}
bool isNum(char ch) {
if(ch >= '0' && ch <= '9') return true;
else return false;
}
bool isOperate(char ch) {
if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')')
return true;
else return false;
}
int level(char ch) {
switch(ch) {
case '(' :
return 5;
case '*' :
return 4;
case '/' :
return 4;
case '+' :
return 3;
case '-' :
return 3;
case ')':
return 2;
}
}
/*字符串转数字*/
double scd(string s) {
if(s.length() == 0) return INT_MAX;
bool flag = false;
for(int i = 0; i < s.length(); i++) {
if(i == 0 && s[i] == '-') continue;
else if(s[i] == '.' && !flag) {
if(i > 0 && isNum(s[i-1])) {
flag = true;
continue;
}
else return INT_MAX;
}
else if(isNum(s[i])) continue;
else return INT_MAX;
}
double result = atof(s.c_str());
return result;
}
/*由中缀表达式得到后缀表达式, 向量V存储结果*/
vector midToPost(string s) {
stack S; /*符号栈*/
vector V; /*后缀表达式*/
int i = 0;
while(i < s.length()) {
if(isNum(s[i])) {
string str = "";
while(isNum(s[i]) || s[i] == '.') {
str += s[i];
i++;
}
V.push_back(str);
}
else if(isOperate(s[i])){
/*负数情况*/
if(s[i] == '-' && (i == 0 || !isNum(s[i-1])) {
string str = "-"; i++;
while(isNum(s[i]) || s[i] == '.') {
str += s[i]; i++;
}
V.push_back(str);
}else{
if(S.empty()){
S.push(s[i]); i++;
}else {
int initial = level(s[i]);
if(initial == 2) {
while(level(S.top()) != 5 && !s.empty()) {
string str = "";
str += S.top();
V.push_back(str);
S.pop();
}
if(S.top() == '(') S.pop(); i++;
} else {
while(!S.empty() && initial <= level(S.top()) && level(S.top()) != 5) {
string str = "";
str += S.top();
V.push_back(str);
S.pop();
}
S.push(s[i]); i++;
}
}
}
}
else{
cout << "表达式出错" << endl;
V.clear();
return V;
}
}
while(!S.empty()) {
string str = ""; str += S.top();
S.pop();
V.push_back(str);
}
//for(int i = 0; i < V.size(); i++) cout << V[i] << "[]";
return V;
}
/*后缀表达式得到最终结果*/
double getValue(vector V) {
stack S;
for(int i = 0; i < V.size(); i++) {
/*操作运算符*/
if(V[i].length() == 1 && isOperate(V[i][0])) {
double a = 0, b = 0;
if(!S.empty()) {
a = S.top(); S.pop();
}else return INT_MAX;
if(!S.empty()) {
b = S.top(); S.pop();
}else return INT_MAX;
switch(V[i][0]) {
case '+':
S.push(b+a);
break;
case '-':
S.push(b-a);
break;
case '*':
S.push(b*a);
break;
case '/':
S.push(b/a);
break;
default:
return INT_MAX;
}
}else {
if(scd(V[i]) == INT_MAX) return INT_MAX;
else S.push(scd(V[i]));
}
}
if(S.empty()) return INT_MAX;
double value = S.top();
S.pop();
return value;
}
calculator::~calculator()
{
}