Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp
- #include "mainwindow.h"
- #include "calculator.h"
- #include <QApplication>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- Calculator calculator;
- calculator.setWindowTitle("Calculator");
- calculator.resize(230, 200);
- calculator.show();
- return app.exec();
- }
- //calculator.h
- #ifndef CALCULATOR_H
- #define CALCULATOR_H
- #include <QPushButton>
- #include <QBoxLayout>
- #include <QWidget>
- #include <QStack>
- class QLCDNumber;
- class QPushButton;
- //===============================================================
- class Calculator : public QWidget {
- Q_OBJECT
- private:
- QLCDNumber* m_plcd;
- QStack<QString> m_stk;
- QString m_strDisplay;
- public:
- Calculator(QWidget* pwgt = 0);
- QPushButton* createButton(const QString& str);
- void calculate( );
- public slots:
- void slotButtonClicked();
- };
- #endif // CALCULATOR_H
- //calculator.cpp
- #include "calculator.h"
- #include <QLCDNumber>
- Calculator::Calculator(QWidget *pwgt/*= 0*/) : QWidget(pwgt) {
- m_plcd = new QLCDNumber(12);
- m_plcd->setSegmentStyle(QLCDNumber::Flat);
- m_plcd->setMinimumSize(150, 50);
- QChar aButtons[4][4] = {{'7', '8', '9', '/'},
- {'4', '5', '6', '*'},
- {'1', '2', '3', '-'},
- {'0', '.', '=', '+'}
- };
- //Layout setup
- QGridLayout *ptopLayout = new QGridLayout;
- ptopLayout->addWidget(m_plcd, 0, 0, 1, 4);
- ptopLayout->addWidget(createButton("CE"), 1, 3);
- for(int i = 0; i < 4; ++i) {
- for(int j = 0; j < 4; ++j) {
- ptopLayout->addWidget(createButton(aButtons[i][j]), i+2, j);
- }
- }
- setLayout(ptopLayout);
- }
- QPushButton* Calculator::createButton(const QString &str) {
- QPushButton* pcmd = new QPushButton(str);
- pcmd->setMinimumSize(40, 40);
- connect(pcmd, SIGNAL(clicked()), SLOT(slotButtonClicked()));
- return pcmd;
- }
- void Calculator::calculate() {
- double dOperand2 = m_stk.pop().toDouble();
- QString strOperation = m_stk.pop();
- double dOperand1 = m_stk.pop().toDouble();
- double dResult = 0;
- if(strOperation == "+") {
- dResult = dOperand1 + dOperand2;
- }
- if(strOperation == "-") {
- dResult = dOperand1 - dOperand2;
- }
- if(strOperation == "/") {
- dResult = dOperand1 / dOperand2;
- }
- if(strOperation == "*") {
- dResult = dOperand1 * dOperand2;
- }
- m_plcd->display(dResult);
- }
- void Calculator::slotButtonClicked() {
- QString str = ((QPushButton*)sender())->text();
- if(str == "CE") {
- m_stk.clear();
- m_strDisplay = "";
- m_plcd->display("0");
- return;
- }
- if(str.contains(QRegExp("[0-9]"))) {
- m_strDisplay += str;
- m_plcd->display(m_strDisplay.toDouble());
- }
- else if(str == ".") {
- m_strDisplay += str;
- m_plcd->display(m_strDisplay);
- }
- else {
- if(m_stk.count() >= 2) {
- m_stk.push(QString().setNum(m_plcd->value()));
- calculate();
- m_stk.clear();
- m_stk.push(QString().setNum(m_plcd->value()));
- if(str != "=") {
- m_stk.push(str);
- }
- }
- else {
- m_stk.push(QString::number(m_plcd->value()));
- m_stk.push(str);
- m_strDisplay = "";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment