calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include <QMainWindow>
namespace Ui {
class Calculator;
}
class Calculator : public QMainWindow
{
Q_OBJECT
public:
explicit Calculator(QWidget *parent = 0);
~Calculator();
private slots:
void on_btn0_clicked();
void on_btn1_clicked();
void on_btn2_clicked();
void on_btn3_clicked();
void on_btn4_clicked();
void on_btn5_clicked();
void on_btn6_clicked();
void on_btn7_clicked();
void on_btn8_clicked();
void on_btn9_clicked();
void on_btnAdd_clicked();
void on_btnSub_clicked();
void on_btnMul_clicked();
void on_btnDiv_clicked();
void on_btnSign_clicked();
void on_btnC_clicked();
void on_btnCE_clicked();
void on_btnDel_clicked();
void on_btnSqrt_clicked();
void on_btnPercent_clicked();
void on_btnRooting_clicked();
void on_btnMC_clicked();
void on_btnMR_clicked();
void on_btnMS_clicked();
void on_btnMAdd_clicked();
void on_btnMSub_clicked();
void on_btnEqual_clicked();
void on_btnPoint_clicked();
private:
Ui::Calculator *ui;
double current,past,memory;
int cntDecDig;
char sign;
};
#endif // CALCULATOR_H
calculator.cpp
#include "calculator.h"
#include "ui_calculator.h"
#include <cmath>
Calculator::Calculator(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Calculator)
{
ui->setupUi(this);
//初始化
sign = 0;
memory = 0;
past = current = cntDecDig = 0;
//禁止调整窗口大小
setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint);
setFixedSize(this->width(),this->height());
//修改按钮样式
setStyleSheet("QPushButton{background-color:#F0FFFF;color:black;}");
}
Calculator::~Calculator()
{
delete ui;
}
//数字0
void Calculator::on_btn0_clicked()
{
if(cntDecDig>0){//小数
cntDecDig++;
}else{//整数
current = current*10;
}
ui->down->setText(QString::number(current,'f',10));
}
//数字1
void Calculator::on_btn1_clicked()
{
if(cntDecDig>0){//小数
current += 1*pow(10,-cntDecDig);
cntDecDig++;
}else{//整数
current = current*10+1;
}
ui->down->setText(QString::number(current,'f',10));
}
//数字2
void Calculator::on_btn2_clicked()
{
if(cntDecDig>0){//小数
current += 2*pow(10,-cntDecDig);
cntDecDig++;
}else{//整数
current = current*10+2;
}
ui->down->setText(QString::number(current,'f',10));
}
//数字3
void Calculator::on_btn3_clicked()
{
if(cntDecDig>0){//小数
current += 3*pow(10,-cntDecDig);
cntDecDig++;
}else{//整数
current = current*10+3;
}
ui->down->setText(QString::number(current,'f',10));
}
//数字4
void Calculator::on_btn4_clicked()
{
if(cntDecDig>0){//小数
current += 4*pow(10,-cntDecDig);
cntDecDig++;
}else{//整数
current = current*10+4;
}
ui->down->setText(QString::number(current,'f',10));
}
//数字5
void Calculator::on_btn5_clicked()
{
if(cntDecDig>0){//小数
current += 5*pow(10,-cntDecDig);
cntDecDig++;
}else{//整数
current = current*10+5;
}
ui->down->setText(QString::number(current,'f',10));
}
//数字6
void Calculator::on_btn6_clicked()
{
if(cntDecDig>0){//小数
current += 6*pow(10,-cntDecDig);
cntDecDig++;
}else{//整数
current = current*10+6;
}
ui->down->setText(QString::number(current,'f',10));
}
//数字7
void Calculator::on_btn7_clicked()
{
if(cntDecDig>0){//小数
current += 7*pow(10,-cntDecDig);
cntDecDig++;
}else{//整数
current = current*10+7;
}
ui->down->setText(QString::number(current,'f',10));
}
//数字8
void Calculator::on_btn8_clicked()
{
if(cntDecDig>0){//小数
current += 8*pow(10,-cntDecDig);
cntDecDig++;
}else{//整数
current = current*10+8;
}
ui->down->setText(QString::number(current,'f',10));
}
//数字9
void Calculator::on_btn9_clicked()
{
if(cntDecDig>0){//小数
current += 9*pow(10,-cntDecDig);
cntDecDig++;
}else{//整数
current = current*10+9;
}
ui->down->setText(QString::number(current,'f',10));
}
//加法
void Calculator::on_btnAdd_clicked()
{
if(sign!=0){
sign = '+';
return;
}
ui->up->setText(QString::number(current,'f',10));
past = current;
current = 0;
cntDecDig = 0;
sign = '+';
ui->down->setText(QString::number(current,'f',10));
}
//减法
void Calculator::on_btnSub_clicked()
{
if(sign!=0){
sign = '-';
return;
}
ui->up->setText(QString::number(current,'f',10));
past = current;
current = 0;
cntDecDig = 0;
sign = '-';
ui->down->setText(QString::number(current,'f',10));
}
//乘法
void Calculator::on_btnMul_clicked()
{
if(sign!=0){
sign = '*';
return;
}
ui->up->setText(QString::number(current,'f',10));
past = current;
current = 0;
cntDecDig = 0;
sign = '*';
ui->down->setText(QString::number(current,'f',10));
}
// 除法
void Calculator::on_btnDiv_clicked()
{
if(sign!=0){
sign = '/';
return;
}
ui->up->setText(QString::number(current,'f',10));
past = current;
current = 0;
cntDecDig = 0;
sign = '/';
ui->down->setText(QString::number(current,'f',10));
}
//点击± 符号变号
void Calculator::on_btnSign_clicked()
{
current = -current;
ui->down->setText(QString::number(current,'f',10));
}
//归零 清除当前的计算
void Calculator::on_btnC_clicked()
{
ui->up->setText("0.0000000000");
current = 0;
sign = 0;
cntDecDig = 0;
ui->down->setText(QString::number(current,'f',10));
}
//清除显示的数字
void Calculator::on_btnCE_clicked()
{
current = 0;
cntDecDig = 0;
ui->down->setText("0.0000000000");
}
//删除最后一位数字
void Calculator::on_btnDel_clicked()
{
QString s = QString::number(current);
s = s.left(s.size()-1);
current = s.toDouble();
ui->down->setText(QString::number(current,'f',10));
}
//平方根
void Calculator::on_btnSqrt_clicked()
{
ui->up->setText(QString::number(current,'f',10));
past = current;
current = sqrt(current);
ui->down->setText(QString::number(current,'f',10));
current = 0;
}
//显示某个数的百分比
void Calculator::on_btnPercent_clicked()
{
QString s = QString::number(current);
current = s.toDouble();
cntDecDig -= 2;
if(cntDecDig<0)cntDecDig = 0;
ui->down->setText(QString::number(current*100,'f',10)+"%");
}
//n次方根
void Calculator::on_btnRooting_clicked()
{
if(sign!=0){
sign = 'R';
return;
}
ui->up->setText(QString::number(current,'f',10));
sign = 'R';
past = current;
current = 0;
ui->down->setText(QString::number(current,'f',10));
}
//清除存储器中的数值
void Calculator::on_btnMC_clicked()
{
memory = 0;
}
//将存于存储器中的数显示在计算器的显示框上
void Calculator::on_btnMR_clicked()
{
ui->up->setText(QString::number(current,'f',10));
past = current;
ui->down->setText(QString::number(current,'f',10));
current = 0;
}
//将显示框的数值存于存储器中
void Calculator::on_btnMS_clicked()
{
ui->up->setText(QString::number(current,'f',10));
past = current;
memory = current;
cntDecDig = 0;
current = 0;
}
//将显示框的数与存储器中的数相加并进行存储
void Calculator::on_btnMAdd_clicked()
{
ui->up->setText(QString::number(current,'f',10));
past = current;
current += memory;
ui->down->setText(QString::number(current,'f',10));
}
//将显示框的数与存储器中的数相减并进行存储
void Calculator::on_btnMSub_clicked()
{
ui->up->setText(QString::number(current,'f',10));
past = current;
current -= memory;
ui->down->setText(QString::number(current,'f',10));
current = 0;
}
//计算最终结果
void Calculator::on_btnEqual_clicked()
{
if(sign==0){
ui->up->setText(QString::number(current,'f',10));
past = current;
}else{
double tmp = past;
past = current;
switch(sign){
case '+':
current = tmp + past;
break;
case '-':
current = tmp - past;
break;
case '*':
current = tmp * past;
break;
case '/':
if(past == 0)break;
current = tmp / past;
break;
case 'R'://n次根号
current = pow(tmp,(double)1.0/past);
break;
}
ui->up->setText(QString::number(past,'f',10));
ui->down->setText(QString::number(current,'f',10));
sign = 0;
cntDecDig = 0;
past = current;
current = 0;
}
}
void Calculator::on_btnPoint_clicked()
{
if(cntDecDig==0)cntDecDig++;
}
main.cpp
#include "calculator.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Calculator w;
w.show();
return a.exec();
}
calculator.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Calculator</class>
<widget class="QMainWindow" name="Calculator">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>369</width>
<height>436</height>
</rect>
</property>
<property name="windowTitle">
<string>Calculator</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>20</x>
<y>0</y>
<width>497</width>
<height>391</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="0">
<widget class="QLineEdit" name="down">
<property name="text">
<string>0.0000000000</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLineEdit" name="up">
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="5" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QPushButton" name="btn4">
<property name="text">
<string>4</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn5">
<property name="text">
<string>5</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn6">
<property name="text">
<string>6</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMul">
<property name="text">
<string>*</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnRooting">
<property name="text">
<string>1/x</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="btnMC">
<property name="text">
<string>MC</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMR">
<property name="text">
<string>MR</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMS">
<property name="text">
<string>MS</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMAdd">
<property name="text">
<string>M+</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnMSub">
<property name="text">
<string>M-</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="btnDel">
<property name="text">
<string>←</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnCE">
<property name="text">
<string>CE</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnC">
<property name="text">
<string>C</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnSign">
<property name="text">
<string>±</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnSqrt">
<property name="text">
<string>√</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="4" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="btn7">
<property name="text">
<string>7</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn8">
<property name="text">
<string>8</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn9">
<property name="text">
<string>9</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnDiv">
<property name="text">
<string>/</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnPercent">
<property name="text">
<string>%</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="6" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QPushButton" name="btn1">
<property name="text">
<string>1</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="btn2">
<property name="text">
<string>2</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="btn3">
<property name="text">
<string>3</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QPushButton" name="btnSub">
<property name="text">
<string>-</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="4" rowspan="2">
<widget class="QPushButton" name="btnEqual">
<property name="enabled">
<bool>true</bool>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="text">
<string>=</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QPushButton" name="btn0">
<property name="text">
<string>0</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="btnPoint">
<property name="text">
<string>.</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QPushButton" name="btnAdd">
<property name="text">
<string>+</string>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
<property name="default">
<bool>false</bool>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</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>