Advertisement
logancberrypie

Calculator source file

Jan 15th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QMessageBox>
  4.  
  5.  
  6. //Global Variables
  7. QString previous = "";
  8. QString current = "";
  9. QString awnser = "";
  10. int decimals = 0;
  11.  
  12. // 1 = +
  13. // 2 = *
  14. // 3 = -
  15. // 4 = /
  16. int state = 0;
  17.  
  18. bool inputted = false;
  19.  
  20.  
  21.  
  22. MainWindow::MainWindow(QWidget *parent) :
  23.     QMainWindow(parent),
  24.     ui(new Ui::MainWindow)
  25. {
  26.     ui->setupUi(this);
  27. }
  28.  
  29. MainWindow::~MainWindow()
  30. {
  31.     delete ui;
  32. }
  33.  
  34. void MainWindow::updateText()
  35. {
  36.     ui->label->setText(current);
  37. }
  38.  
  39. //Basic Number buttons
  40. void MainWindow::on_b_0_clicked()
  41. {
  42.     current += "0";
  43.     MainWindow::updateText();
  44. }
  45.  
  46. void MainWindow::on_b_1_clicked()
  47. {
  48.     current += "1";
  49.     MainWindow::updateText();
  50. }
  51.  
  52. void MainWindow::on_b_2_clicked()
  53. {
  54.     current += "2";
  55.     MainWindow::updateText();
  56. }
  57.  
  58. void MainWindow::on_b_3_clicked()
  59. {
  60.     current += "3";
  61.     MainWindow::updateText();
  62. }
  63.  
  64. void MainWindow::on_b_4_clicked()
  65. {
  66.     current += "4";
  67.     MainWindow::updateText();
  68. }
  69.  
  70. void MainWindow::on_b_5_clicked()
  71. {
  72.     current += "5";
  73.     MainWindow::updateText();
  74. }
  75.  
  76. void MainWindow::on_b_6_clicked()
  77. {
  78.     current += "6";
  79.     MainWindow::updateText();
  80. }
  81.  
  82. void MainWindow::on_b_7_clicked()
  83. {
  84.     current += "7";
  85.     MainWindow::updateText();
  86. }
  87.  
  88. void MainWindow::on_b_8_clicked()
  89. {
  90.     current += "8";
  91.     MainWindow::updateText();
  92. }
  93.  
  94. void MainWindow::on_b_9_clicked()
  95. {
  96.     current += "9";
  97.     MainWindow::updateText();
  98. }
  99.  
  100. //Adding a decimal to current
  101. void MainWindow::on_b_decimal_clicked()
  102. {
  103.     if (decimals ==1)
  104.     {
  105.         QMessageBox::information(this,tr("Decimal error"),tr("You cannot have more than one decimal in a value"));
  106.     }
  107.     else {
  108.         if (current == "")
  109.         {
  110.             current += "0.";
  111.             decimals += 1;
  112.         }
  113.         else {
  114.             current += ".";
  115.             decimals += 1;
  116.         }
  117.         MainWindow::updateText();
  118.     }
  119.  
  120. }
  121.  
  122. //Random number
  123. void MainWindow::on_b_random_clicked()
  124. {
  125.     current = QString::number(qrand());
  126.     MainWindow::updateText();
  127. }
  128.  
  129.  
  130.  
  131. void MainWindow::on_b_plus_clicked()
  132. {
  133.  
  134.     state = 1;
  135.     previous = QString::number(previous.toFloat() + current.toFloat());
  136.     current = "";
  137.     decimals = 0;
  138.     MainWindow::update();
  139.  
  140. }
  141.  
  142. void MainWindow::on_b_multiply_clicked()
  143. {
  144.     if (!inputted)
  145.     {
  146.         previous = QString::number(current.toFloat());
  147.     }
  148.     else {
  149.         previous = QString::number(previous.toFloat() * current.toFloat());
  150.     }
  151.     inputted = true;
  152.     state = 2;
  153.     current = "";
  154.     decimals = 0;
  155.     MainWindow::update();
  156. }
  157.  
  158. void MainWindow::on_b_divide_clicked()
  159. {
  160.     if (!inputted)
  161.     {
  162.         previous = QString::number(current.toFloat());
  163.     }
  164.     else {
  165.         previous = QString::number(previous.toFloat() / current.toFloat());
  166.     }
  167.     inputted = true;
  168.     state = 4;
  169.     current = "";
  170.     decimals = 0;
  171.     MainWindow::update();
  172. }
  173.  
  174. void MainWindow::on_b_minus_clicked()
  175. {
  176.     if (!inputted)
  177.     {
  178.         previous = QString::number(current.toFloat());
  179.     }
  180.     else {
  181.         previous = QString::number(previous.toFloat() - current.toFloat());
  182.     }
  183.     inputted = true;
  184.     state = 3;
  185.     current = "";
  186.     decimals = 0;
  187.     MainWindow::update();
  188. }
  189.  
  190. void MainWindow::on_b_equal_clicked()
  191. {
  192.     double temp = current.toFloat();
  193.     if (state == 1)
  194.     {
  195.         current = QString::number(previous.toFloat() + temp);
  196.         MainWindow::updateText();
  197.     }
  198.     else if (state == 2)
  199.     {
  200.         current = QString::number(previous.toFloat() * temp);
  201.         MainWindow::updateText();
  202.     }
  203.     else if (state ==3)
  204.     {
  205.         current = QString::number(previous.toFloat() - temp);
  206.         MainWindow::updateText();
  207.     }
  208.     else if (state = 4)
  209.     {
  210.         if (temp == 0)
  211.         {
  212.             current = "+ infinity and -Infinity (undertermined)";
  213.         }
  214.         else {
  215.             current = QString::number(previous.toFloat() / temp);
  216.         }
  217.         MainWindow::updateText();
  218.     }
  219.     awnser = current;
  220. }
  221.  
  222. void MainWindow::on_b_ans_clicked()
  223. {
  224.     current = awnser;
  225.     MainWindow::updateText();
  226. }
  227.  
  228. void MainWindow::on_b_ac_clicked()
  229. {
  230.     previous = "";
  231.     current = "";
  232.     decimals = 0;
  233.     state = 0;
  234.     inputted = false;
  235.     MainWindow::updateText();
  236. }
  237.  
  238. void MainWindow::on_b_del_clicked()
  239. {
  240.     current.remove(current.size()-1,1);
  241.     MainWindow::updateText();
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement