vladkomarr

calculatorQT

Nov 18th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //main.cpp
  2. #include "mainwindow.h"
  3. #include "calculator.h"
  4. #include <QApplication>
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.     QApplication app(argc, argv);
  9.     Calculator calculator;
  10.  
  11.     calculator.setWindowTitle("Calculator");
  12.     calculator.resize(230, 200);
  13.  
  14.     calculator.show();
  15.     return app.exec();
  16. }
  17.  
  18. //calculator.h
  19. #ifndef CALCULATOR_H
  20. #define CALCULATOR_H
  21.  
  22. #include <QPushButton>
  23. #include <QBoxLayout>
  24. #include <QWidget>
  25. #include <QStack>
  26.  
  27. class QLCDNumber;
  28. class QPushButton;
  29.  
  30. //===============================================================
  31.  
  32. class Calculator : public QWidget {
  33.     Q_OBJECT
  34. private:
  35.     QLCDNumber*         m_plcd;
  36.     QStack<QString>     m_stk;
  37.     QString             m_strDisplay;
  38.  
  39. public:
  40.     Calculator(QWidget* pwgt = 0);
  41.  
  42.     QPushButton* createButton(const QString& str);
  43.     void calculate(     );
  44.  
  45. public slots:
  46.     void slotButtonClicked();
  47. };
  48. #endif // CALCULATOR_H
  49.  
  50. //calculator.cpp
  51.  
  52. #include "calculator.h"
  53. #include <QLCDNumber>
  54. Calculator::Calculator(QWidget *pwgt/*= 0*/) : QWidget(pwgt) {
  55.     m_plcd = new QLCDNumber(12);
  56.     m_plcd->setSegmentStyle(QLCDNumber::Flat);
  57.     m_plcd->setMinimumSize(150, 50);
  58.  
  59.     QChar aButtons[4][4] = {{'7', '8', '9', '/'},
  60.                             {'4', '5', '6', '*'},
  61.                             {'1', '2', '3', '-'},
  62.                             {'0', '.', '=', '+'}
  63.                            };
  64.     //Layout setup
  65.     QGridLayout *ptopLayout = new QGridLayout;
  66.     ptopLayout->addWidget(m_plcd, 0, 0, 1, 4);
  67.     ptopLayout->addWidget(createButton("CE"), 1, 3);
  68.  
  69.     for(int i = 0; i < 4; ++i) {
  70.         for(int j = 0; j < 4; ++j) {
  71.             ptopLayout->addWidget(createButton(aButtons[i][j]), i+2, j);
  72.         }
  73.     }
  74.  
  75.     setLayout(ptopLayout);
  76. }
  77.  
  78. QPushButton* Calculator::createButton(const QString &str) {
  79.     QPushButton* pcmd = new QPushButton(str);
  80.     pcmd->setMinimumSize(40, 40);
  81.     connect(pcmd, SIGNAL(clicked()), SLOT(slotButtonClicked()));
  82.     return pcmd;
  83. }
  84.  
  85. void Calculator::calculate() {
  86.     double          dOperand2      =   m_stk.pop().toDouble();
  87.     QString         strOperation   =   m_stk.pop();
  88.     double          dOperand1      =   m_stk.pop().toDouble();
  89.     double          dResult        =   0;
  90.  
  91.     if(strOperation == "+") {
  92.         dResult = dOperand1 + dOperand2;
  93.     }
  94.     if(strOperation == "-") {
  95.         dResult = dOperand1 - dOperand2;
  96.     }
  97.     if(strOperation == "/") {
  98.         dResult = dOperand1 / dOperand2;
  99.     }
  100.     if(strOperation == "*") {
  101.         dResult = dOperand1 * dOperand2;
  102.     }
  103.  
  104.     m_plcd->display(dResult);
  105. }
  106.  
  107. void Calculator::slotButtonClicked() {
  108.     QString str = ((QPushButton*)sender())->text();
  109.  
  110.     if(str == "CE") {
  111.         m_stk.clear();
  112.         m_strDisplay = "";
  113.         m_plcd->display("0");
  114.         return;
  115.     }
  116.     if(str.contains(QRegExp("[0-9]"))) {
  117.         m_strDisplay += str;
  118.         m_plcd->display(m_strDisplay.toDouble());
  119.     }
  120.     else if(str == ".") {
  121.         m_strDisplay += str;
  122.         m_plcd->display(m_strDisplay);
  123.     }
  124.     else {
  125.         if(m_stk.count() >= 2) {
  126.             m_stk.push(QString().setNum(m_plcd->value()));
  127.             calculate();
  128.             m_stk.clear();
  129.             m_stk.push(QString().setNum(m_plcd->value()));
  130.             if(str != "=") {
  131.                 m_stk.push(str);
  132.             }
  133.         }
  134.         else {
  135.             m_stk.push(QString::number(m_plcd->value()));
  136.             m_stk.push(str);
  137.             m_strDisplay = "";
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment