问题计算器问题 - c ++

时间:2015-10-22 22:46:36

标签: c++ qt calculator

我在Qt中创建了一个简单的计算器,但是我想添加一个按钮来帮我做一个因子,但它不起作用 有人可以帮我吗? 我已经实现了一个方法阶乘

#include "mainwindow.h"
#include <QtCore/QCoreApplication>

QString value="",total="";
double fNum,sNum;
bool addBool=false, substractBool=false, multiplyBool=false, divideBool=false,Factorbool=false;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    label=new QLabel("0",this);
    label->setGeometry(QRect(QPoint(75,25),QSize(50,200)));

    clear_button=new QPushButton("C",this);
    clear_button->setGeometry(QRect(QPoint(50,300),QSize(50,50)));
    connect(clear_button,SIGNAL(released()),this,SLOT(clear()));

    equals_button=new QPushButton("=",this);
    equals_button->setGeometry(QRect(QPoint(100,300),QSize(50,50)));
    connect(equals_button,SIGNAL(released()),this,SLOT(equals()));

    add_button=new QPushButton("+",this);
    add_button->setGeometry(QRect(QPoint(200,150),QSize(50,50)));
    connect(add_button,SIGNAL(released()),this,SLOT(add()));

    Factor_button =new QPushButton("n!",this);
    Factor_button->setGeometry(QRect(QPoint(250,150),QSize(50,50)));
    connect(Factor_button ,SIGNAL(released()),this,SLOT(add()));

    substract_button=new QPushButton("-",this);
    substract_button->setGeometry(QRect(QPoint(200,200),QSize(50,50)));
    connect(substract_button,SIGNAL(released()),this,SLOT(substract()));

    multiply_button=new QPushButton("X",this);
    multiply_button->setGeometry(QRect(QPoint(200,250),QSize(50,50)));
    connect(multiply_button,SIGNAL(released()),this,SLOT(multiply()));

    divide_button=new QPushButton("/",this);
    divide_button->setGeometry(QRect(QPoint(200,300),QSize(50,50)));
    connect(divide_button,SIGNAL(released()),this,SLOT(divide()));

    for(int i=0;i<10;i++){
        QString digit=QString::number(i);
        buttons[i]=new QPushButton(digit,this);
        connect(buttons[i],SIGNAL(released()),this,SLOT(buttonPushed()));
    }
    setGeo();
}

void MainWindow::setGeo()
{
    for(int i=0;i<1;i++)
    {
        buttons[i]->setGeometry(QRect(QPoint(50,300),QSize(50,50)));
    }
    for(int i=0;i<4;i++)
    {
        buttons[i]->setGeometry(QRect(QPoint(50*i,250),QSize(50,50)));
    }
    for(int i=4;i<7;i++)
    {
        buttons[i]->setGeometry(QRect(QPoint(50*(i-3),200),QSize(50,50)));
    }
    for(int i=7;i<10;i++)
    {
        buttons[i]->setGeometry(QRect(QPoint(50*(i-6),150),QSize(50,50)));
    }
}

void MainWindow::buttonPushed()
{
    QPushButton *button=(QPushButton *)sender();
    emit numberEnitted(button->text()[0].digitValue());
    value+=QString::number(button->text()[0].digitValue());
    label->setText(value);
}

void MainWindow::clear(){
    value="";
    label->setText(value);

}

void MainWindow::add(){
    fNum=value.toDouble();
    value="";
    label->setText(value);
    addBool=true;
}
 int factorial( int n)
{
    if (n == 0)
       return 1;
    return n * factorial(n - 1);
}


void MainWindow::equals(){
    sNum=value.toDouble();
    if(addBool){
        total=QString::number(fNum+sNum);
        label->setText(total);
    }
    if(substractBool){
        total=QString::number(fNum-sNum);
        label->setText(total);
    }
    if(multiplyBool){
        total=QString::number(fNum*sNum);
        label->setText(total);
    }
    if(divideBool){
        total=QString::number(fNum/sNum);
        label->setText(total);
    }
    if(Factorbool)
    {
        total=QString::number(factorial(fNum));
        label->setText(total);
    }
}


void MainWindow::substract(){
    fNum=value.toDouble();
    value="";
    label->setText(value);
    substractBool=true;
}

void MainWindow::multiply(){
    fNum=value.toDouble();
    value="";
    label->setText(value);
    multiplyBool=true;
}

void MainWindow::divide(){
    fNum=value.toDouble();
    value="";
    label->setText(value);
    divideBool=true;
}
void MainWindow::Factor(){
    fNum=value.toDouble();
    value="";
    label->setText(value);
    Factorbool=true;
}
MainWindow::~MainWindow()
{
}

主要

#include "mainwindow.h"
#include <QApplication>
#include <QDesktopWidget>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.showMaximized();
    w.setFixedSize(300,400);
    w.move(QApplication::desktop()->screen()->rect().center()-w.rect().center());
    w.show();

    return a.exec();
}

.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QLabel>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

signals:
    void numberEnitted(int number);

private slots:
    void clear();
    void add();
    void equals();
    void substract();
    void multiply();
    void divide();
    void Factor();
    void buttonPushed();
    void setGeo();

private:
    QLabel *label;
    QPushButton *clear_button;
    QPushButton *add_button;
    QPushButton *equals_button;
    QPushButton *substract_button;
    QPushButton *multiply_button;
    QPushButton *Factor_button;
    QPushButton *divide_button;
    QPushButton *zero_button;
    QPushButton *buttons[10];

};

#endif

我在等你的帮助:)

1 个答案:

答案 0 :(得分:3)

按下Factor_button时触发的信号未连接到正确的插槽。

这一行

    connect(Factor_button, SIGNAL(released()), this, SLOT(add()));

应该是

    connect(Factor_button, SIGNAL(released()), this, SLOT(Factor()));