当前位置: 代码迷 >> QT开发 >> 一个计算器程序,帮忙看看哪错了,该如何解决
  详细解决方案

一个计算器程序,帮忙看看哪错了,该如何解决

热度:51   发布时间:2016-04-25 05:01:48.0
一个计算器程序,帮忙看看哪错了
C/C++ code
//calc.h#ifndef CALC_H#define CALC_H#include <QtGui/QMainWindow>#include <QToolButton>#include <QLineEdit>class QToolButton;class QLineEdit;class calc : public QWidget{    Q_OBJECTpublic:    calc(QWidget *parent = 0);    ~calc();public slots:    void digitClick();    void operatClick();    void equalClick();    void changesignClick();    void pointClick();    void backspaceCk();    void clearAll();    void clear();    void clearMemory();    void readMemory();    void setMemory();    void addToMemory();    void unarryClick();    void wrong();private:    QLineEdit *display;    QToolButton *digitButton[10];    double sum;    double memory;    double right;    QString ch;    bool waitingForOperand;};#endif // CALC_H


C/C++ code
//calc.cpp#include <QtGui>#include <cmath>#include "calc.h"calc::calc(QWidget *parent)    : QWidget(parent){    sum = 0.0;    memory = 0.0;    right = 0.0;    ch = "";    waitingForOperand = true;    display = new QLineEdit("0");    display->setReadOnly(true);    display->setAlignment(Qt::AlignRight);    display->setMaxLength(15);    QFont font = display->font();    font.setPointSize(font.pointSize() + 8);    display->setFont(font);    for (int i = 0; i < 10; ++i)    {        digitButton = new QToolButton;        digitButton->setText(QString::number(i));        connect(digitButton,SIGNAL(clicked()),this,SLOT(digitClick()));    }    QToolButton *clearMemoryBtn = new QToolButton;    clearMemoryBtn->setText("MC");    connect(clearMemoryBtn,SIGNAL(clicked()),this,SLOT(clearMemory()));    QToolButton *readMemoryBtn = new QToolButton;    readMemoryBtn->setText("MR");    connect(readMemoryBtn,SIGNAL(clicked()),this,SLOT(readMemory()));    QToolButton *setMemoryBtn = new QToolButton;    setMemoryBtn->setText("MS");    connect(setMemoryBtn,SIGNAL(clicked()),this,SLOT(setMemory()));    QToolButton *addMemoryBtn = new QToolButton;    addMemoryBtn->setText("M+");    connect(addMemoryBtn,SIGNAL(clicked()),this,SLOT(addToMemory()));    QToolButton *changeBtn = new QToolButton;    changeBtn->setText("+/-");    connect(changeBtn,SIGNAL(clicked()),this,SLOT(changesignClick()));    QToolButton *pointBtn = new QToolButton;    pointBtn->setText(".");    connect(pointBtn,SIGNAL(clicked()),this,SLOT(pointClick()));    QToolButton *mulBtn = new QToolButton;    mulBtn->setText("*");    connect(mulBtn,SIGNAL(clicked()),this,SLOT(operatClick()));    QToolButton *divBtn = new QToolButton;    divBtn->setText("/");    connect(divBtn,SIGNAL(clicked()),this,SLOT(operatClick()));    QToolButton *addBtn = new QToolButton;    addBtn->setText("+");    connect(addBtn,SIGNAL(clicked()),this,SLOT(operatClick()));    QToolButton *subBtn = new QToolButton;    subBtn->setText("-");    connect(subBtn,SIGNAL(clicked()),this,SLOT(operatClick()));    QToolButton *sqrtBtn = new QToolButton;    sqrtBtn->setText("sqrt");    connect(sqrtBtn,SIGNAL(clicked()),this,SLOT(unarryClick()));    QToolButton *powBtn = new QToolButton;    powBtn->setText("x2");    connect(powBtn,SIGNAL(clicked()),this,SLOT(unarryClick()));    QToolButton *countdownBtn = new QToolButton;    countdownBtn->setText("1/x");    connect(countdownBtn,SIGNAL(clicked()),this,SLOT(unarryClick()));    QToolButton *equalBtn = new QToolButton;    equalBtn->setText("=");    connect(equalBtn,SIGNAL(clicked()),this,SLOT(operatClick()));    QToolButton *backspaceBtn = new QToolButton;    backspaceBtn->setText("Backspace");    connect(backspaceBtn,SIGNAL(clicked()),this,SLOT(backspaceCk()));    QToolButton *clearBtn = new QToolButton;    clearBtn->setText("CE");    connect(clearBtn,SIGNAL(clicked()),this,SLOT(clear()));    QToolButton *clearAllBtn = new QToolButton;    clearAllBtn->setText("C");    connect(clearAllBtn,SIGNAL(clicked()),this,SLOT(clearAll()));    QLabel *label = new QLabel;    QGridLayout *calcu = new QGridLayout;    for (int i = 0;i < 10; ++i)    {        if (i == 0)            calcu->addWidget(digitButton,3,0);        else            calcu->addWidget(digitButton,(9 - i) / 3,i % 3 ? i % 3 - 1 : 2);    }    calcu->addWidget(changeBtn,3,1);    calcu->addWidget(pointBtn,3,2);    calcu->addWidget(divBtn,0,3);    calcu->addWidget(mulBtn,1,3);    calcu->addWidget(subBtn,2,3);    calcu->addWidget(addBtn,3,3);    calcu->addWidget(sqrtBtn,0,4);    calcu->addWidget(powBtn,1,4);    calcu->addWidget(countdownBtn,2,4);    calcu->addWidget(equalBtn,3,4);    QVBoxLayout *memoryLayout = new QVBoxLayout;    memoryLayout->addWidget(clearMemoryBtn);    memoryLayout->addWidget(readMemoryBtn);    memoryLayout->addWidget(setMemoryBtn);    memoryLayout->addWidget(addMemoryBtn);    QHBoxLayout *cLayout = new QHBoxLayout;    cLayout->addLayout(memoryLayout);    cLayout->addLayout(calcu);    QGridLayout *upLayout = new QGridLayout;    upLayout->addWidget(display,0,0,1,4);    upLayout->addWidget(label,0,1);    upLayout->addWidget(backspaceBtn,1,1);    upLayout->addWidget(clearBtn,1,2);    upLayout->addWidget(clearAllBtn,1,3);    QVBoxLayout *mainLayout = new QVBoxLayout;    mainLayout->addLayout(upLayout);    mainLayout->addLayout(cLayout);    setLayout(mainLayout);    setWindowTitle("calculator");}void calc::digitClick(){    QToolButton *clickedButton = qobject_cast<QToolButton *>(sender());    int digit = clickedButton->text().toInt();    if (display->text() == "0" && digit == 0)        return;    if (display->text() == "0")        display->setText("");    if (waitingForOperand == false)    {        display->clear();        waitingForOperand = true;    }    display->setText(display->text() + QString::number(digit));}void calc::operatClick(){   if (waitingForOperand == false)       return;   QToolButton *clickedButton = qobject_cast<QToolButton *>(sender());   sum = display->text().toDouble();   ch = clickedButton->text();   display->setText(display->text());   waitingForOperand = false;}void calc::equalClick() //处理等于按钮{    if (ch == "")        return;    right = display->text().toDouble();    if (ch == "+")        sum += right;    else if (ch == "-")        sum -= right;    else if (ch == "*")        sum *= right;    else if (ch == "/")    {        if (right == 0)        {            wrong();            return;        }        sum /= right;    }    display->setText(QString::number(sum));    ch = "";    waitingForOperand = true;}void calc::changesignClick(){    if (waitingForOperand == false)    {        ch = "";        waitingForOperand = true;    }    double digit = display->text().toDouble();    digit = -digit;    display->setText(QString::number(digit));}void calc::pointClick(){    if (waitingForOperand == false)    {        sum = display->text().toDouble();        display->setText("0");    }    if (!display->text().contains("."))        display->setText(display->text() + tr("."));}void calc::backspaceCk(){    if (waitingForOperand == false)        return;    QString text = display->text();    text.chop(1);    if (text.isEmpty())        text = '0';    display->setText(text);}void calc::clearAll(){    sum = 0.0;    memory = 0.0;    right = 0.0;    ch = "";    waitingForOperand = true;    display->setText("0");}void calc::clear(){    if (waitingForOperand == false)        return;    display->setText("0");}void calc::clearMemory(){    memory = 0.0;}void calc::readMemory(){    display->setText(QString::number(memory));}void calc::setMemory(){    memory = display->text().toDouble();}void calc::addToMemory(){    double temp = display->text().toDouble();    memory += temp;}void calc::unarryClick(){    QToolButton *clickedButton = qobject_cast<QToolButton *>(sender());    QString str = clickedButton->text();    sum = display->text().toDouble();    if (str == "sqrt")        sum = sqrt(sum);    else if (str == "x2")        sum *= sum;    else if (str == "1/x")        sum = 1.0 / sum;    display->setText(QString::number(sum));}void calc::wrong(){    display->setText("error");}calc::~calc(){}
  相关解决方案