Guest User

Untitled

a guest
Aug 7th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include "calc.h"
  2.  
  3. Calculator::Calculator(QWidget *wgt)
  4. {
  5.     m_plcd = new QLCDNumber(12);
  6.     m_plcd->setSegmentStyle(QLCDNumber::Flat);
  7.     m_plcd->setMinimumSize(150, 50);
  8.  
  9.     QChar aButtons[4][4] = {{'7', '8', '9', '/'},
  10.                             {'4', '5', '6', '*'},
  11.                             {'1', '2', '3', '-'},
  12.                             {'0', '.', '=', '+'}};
  13.  
  14.     QGridLayout *ptopLayout = new QGridLayout;
  15.     ptopLayout->addWidget(m_plcd, 0, 0, 1, 4);
  16.     ptopLayout->addWidget(createButton("CE"), 1, 3);
  17.  
  18.     for (int i = 0; i < 4; ++i)
  19.     {
  20.         for (int j = 0; j < 4; ++j)
  21.         {
  22.             ptopLayout->addWidget(createButton(aButtons[i][j]), i + 2, j);
  23.         }
  24.     }
  25.     setLayout(ptopLayout);
  26. }
  27.  
  28. QPushButton* Calculator::createButton(const QString& str)
  29. {
  30.     QPushButton *pcmd = new QPushButton(str);
  31.     pcmd->setMinimumSize(40, 40);
  32.     connect(pcmd, SIGNAL(clicked), SLOT(slotButtonClicked));
  33.     return pcmd;
  34. }
  35.  
  36. void Calculator::calculate()
  37. {
  38.     double dOperand2 = m_stk.pop().toDouble();
  39.     QString strOperation = m_stk.pop();
  40.     double dOperand1 = m_stk.pop().toDouble();
  41.     double dResult = 0;
  42.  
  43.     if(strOperation == "+")
  44.     {
  45.         dResult = dOperand1 + dOperand2;
  46.     }
  47.     if(strOperation == "-")
  48.     {
  49.         dResult = dOperand1 - dOperand2;
  50.     }
  51.     if(strOperation == "/")
  52.     {
  53.         dResult = dOperand1 / dOperand2;
  54.     }
  55.     if(strOperation == "*")
  56.     {
  57.         dResult = dOperand1 * dOperand2;
  58.     }
  59.  
  60.     m_plcd->display(dResult);
  61. }
  62.  
  63. void Calculator::slotButtonClicked()
  64. {
  65.     QString str = ((QPushButton *)sender())->text();
  66.  
  67.     if (str == "CE")
  68.     {
  69.         m_stk.clear();
  70.         m_strDisplay = "";
  71.         m_plcd->display("0");
  72.         return;
  73.     }
  74.     if (str.contains(QRegExp("[0-9]")))
  75.     {
  76.         m_strDisplay += str;
  77.         m_plcd->display(m_strDisplay.toDouble());
  78.     }
  79.     else if (str == ".")
  80.     {
  81.         m_strDisplay += str;
  82.         m_plcd->display(m_strDisplay);
  83.     }
  84.     else
  85.     {
  86.         if (m_stk.count() >= 2)
  87.         {
  88.             m_stk.push(QString.setNum(m_plcd->value()));
  89.             calculate();
  90.             m_stk.clear();
  91.             m_stk.push(QString.setNum(m_plcd.value()));
  92.             if (str != "=")
  93.             {
  94.                 m_stk.push(str);
  95.             }
  96.         }
  97.         else
  98.         {
  99.             m_stk.push(QString().setNum(m_plcd->value()));
  100.             m_strDisplay = "";
  101.             m_plcd->display("0");
  102.         }
  103.     }
  104. }
Add Comment
Please, Sign In to add comment