学Q5不久,小试牛刀,下面的截图是Qt5 编写的简单计算。
下面是源码
//calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include<QTextEdit>
#include<QPushButton>
#include<QAction>
//#include<QtWidgets>
#include<QVector>
/*
先将表达式的单个字符依次存储到容器内,当出现“=”时,再将其
取出送入到相应的操作数容器和操作符容器为尽可能的接受用户不
规范的输入,设置了很多标志开关,有些乱还有bug未能解决.
*/
class Calculator : public QWidget
{
Q_OBJECT
public:
Calculator(QWidget *parent = 0);
~Calculator();
private:
QTextEdit* screenTextEdit;//表达式显示屏
QPushButton* button1;
QPushButton* button2;
QPushButton* button3;
QPushButton* button4;
QPushButton* button5;
QPushButton* button6;
QPushButton* button7;
QPushButton* button8;
QPushButton* button9;
QPushButton* button0;
QPushButton* buttonx;// sign '*'
QPushButton* buttondivide;// sign '/'
QPushButton* buttonadd;// sign '+
QPushButton* buttonsub;// sign '-'
QPushButton* buttondot;// sign '.'
QPushButton* buttonequal;// sign '='
QPushButton* buttonclear;//清屏
QPushButton* buttonback;// backspace
QPushButton* buttonLArc;// sign '('
QPushButton* buttonRArc;// sign ')'
bool isEnter;//作为是否换行的依据,注意是光标先换行
double sum ;//临时存放操作数
int i;//转化成小数的除数
int j;//压入操作数栈的开关
double result;//计算所得结果
bool isPoint;//是否出现小数点
bool isLArc;//标记是否出现(以便判断区分出类似'(-2'形式
char sign;//用来存放符号
bool isTwoOptrsFlag;//++, -+ ...
bool isEqualClicked;//作为是否回退容器内的字符的开关
int f1[7];//栈内符号优先级
int f2[7];//栈外符号优先级
//建立操作数和操作符的栈
QVector<double>vectorOpnd;
QVector<char>vectorOptr;
QVector<char>vectorExp;
bool isTwoOptr(QPushButton *button);//++, -+ ...
void initConnect();
void initVector();
double operate(double a, char theat, double b);//进行两个操作数的运算,返回结果
char precedeSign(char optr1, char optr2);//比较栈内和栈外符号的优先级
int char2num(char optr);//获取符号在优先级数组中的序号
void initData();//初始化各个标志变量
char button2char(QPushButton *button);//按钮转化为相应符号
void getDigital(char digital);//进行数字的进栈操作
void getSign(char ch);//获取符号
bool pickSign(char ch);//分拣符号,以便进行相应的操作
bool pushVector(char optr);//进行符号的进栈操作
void getResult();//
private slots:
void getButton();//获取按下的按钮
void clearScreen();//清屏
void myBackSpace();//执行backspace
};
#endif // CALCULATOR_H
//calculator.cpp
#include "calculator.h"
#include<QGridLayout>
//#include<QDebug>
Calculator::Calculator(QWidget *parent)
: QWidget(parent)
{
initData();
setWindowTitle("计算器");
setWindowIcon(QIcon(":/calculator.png"));
screenTextEdit = new QTextEdit;
screenTextEdit->setFont(QFont("Courier 10 Pitch", 18, QFont::Bold));
screenTextEdit->setReadOnly(true);
// screenTextEdit->setEnabled(false);
button1 = new QPushButton("1");
button2 = new QPushButton("2");
button3 = new QPushButton("3");
button4 = new QPushButton("4");
button5 = new QPushButton("5");
button6 = new QPushButton("6");
button7 = new QPushButton("7");
button8 = new QPushButton("8");
button9 = new QPushButton("9");
button0 = new QPushButton("0");
buttonx = new QPushButton("×");
buttonadd = new QPushButton("+");//
buttonsub = new QPushButton("-");
buttonback = new QPushButton("back");
buttondivide = new QPushButton("/");
buttondot = new QPushButton(".");
buttonLArc = new QPushButton("(");
buttonRArc = new QPushButton(")");
buttonequal = new QPushButton("=");
buttonclear = new QPushButton("clear");
initConnect();