Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QMessageBox>
- //Global Variables
- QString previous = "";
- QString current = "";
- QString awnser = "";
- int decimals = 0;
- // 1 = +
- // 2 = *
- // 3 = -
- // 4 = /
- int state = 0;
- bool inputted = false;
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- void MainWindow::updateText()
- {
- ui->label->setText(current);
- }
- //Basic Number buttons
- void MainWindow::on_b_0_clicked()
- {
- current += "0";
- MainWindow::updateText();
- }
- void MainWindow::on_b_1_clicked()
- {
- current += "1";
- MainWindow::updateText();
- }
- void MainWindow::on_b_2_clicked()
- {
- current += "2";
- MainWindow::updateText();
- }
- void MainWindow::on_b_3_clicked()
- {
- current += "3";
- MainWindow::updateText();
- }
- void MainWindow::on_b_4_clicked()
- {
- current += "4";
- MainWindow::updateText();
- }
- void MainWindow::on_b_5_clicked()
- {
- current += "5";
- MainWindow::updateText();
- }
- void MainWindow::on_b_6_clicked()
- {
- current += "6";
- MainWindow::updateText();
- }
- void MainWindow::on_b_7_clicked()
- {
- current += "7";
- MainWindow::updateText();
- }
- void MainWindow::on_b_8_clicked()
- {
- current += "8";
- MainWindow::updateText();
- }
- void MainWindow::on_b_9_clicked()
- {
- current += "9";
- MainWindow::updateText();
- }
- //Adding a decimal to current
- void MainWindow::on_b_decimal_clicked()
- {
- if (decimals ==1)
- {
- QMessageBox::information(this,tr("Decimal error"),tr("You cannot have more than one decimal in a value"));
- }
- else {
- if (current == "")
- {
- current += "0.";
- decimals += 1;
- }
- else {
- current += ".";
- decimals += 1;
- }
- MainWindow::updateText();
- }
- }
- //Random number
- void MainWindow::on_b_random_clicked()
- {
- current = QString::number(qrand());
- MainWindow::updateText();
- }
- void MainWindow::on_b_plus_clicked()
- {
- state = 1;
- previous = QString::number(previous.toFloat() + current.toFloat());
- current = "";
- decimals = 0;
- MainWindow::update();
- }
- void MainWindow::on_b_multiply_clicked()
- {
- if (!inputted)
- {
- previous = QString::number(current.toFloat());
- }
- else {
- previous = QString::number(previous.toFloat() * current.toFloat());
- }
- inputted = true;
- state = 2;
- current = "";
- decimals = 0;
- MainWindow::update();
- }
- void MainWindow::on_b_divide_clicked()
- {
- if (!inputted)
- {
- previous = QString::number(current.toFloat());
- }
- else {
- previous = QString::number(previous.toFloat() / current.toFloat());
- }
- inputted = true;
- state = 4;
- current = "";
- decimals = 0;
- MainWindow::update();
- }
- void MainWindow::on_b_minus_clicked()
- {
- if (!inputted)
- {
- previous = QString::number(current.toFloat());
- }
- else {
- previous = QString::number(previous.toFloat() - current.toFloat());
- }
- inputted = true;
- state = 3;
- current = "";
- decimals = 0;
- MainWindow::update();
- }
- void MainWindow::on_b_equal_clicked()
- {
- double temp = current.toFloat();
- if (state == 1)
- {
- current = QString::number(previous.toFloat() + temp);
- MainWindow::updateText();
- }
- else if (state == 2)
- {
- current = QString::number(previous.toFloat() * temp);
- MainWindow::updateText();
- }
- else if (state ==3)
- {
- current = QString::number(previous.toFloat() - temp);
- MainWindow::updateText();
- }
- else if (state = 4)
- {
- if (temp == 0)
- {
- current = "+ infinity and -Infinity (undertermined)";
- }
- else {
- current = QString::number(previous.toFloat() / temp);
- }
- MainWindow::updateText();
- }
- awnser = current;
- }
- void MainWindow::on_b_ans_clicked()
- {
- current = awnser;
- MainWindow::updateText();
- }
- void MainWindow::on_b_ac_clicked()
- {
- previous = "";
- current = "";
- decimals = 0;
- state = 0;
- inputted = false;
- MainWindow::updateText();
- }
- void MainWindow::on_b_del_clicked()
- {
- current.remove(current.size()-1,1);
- MainWindow::updateText();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement