Local visual calculator program based on QT &C++

1 Project abstract


This blog is about how to make a visual calculator with QT. And this calculator has some simple function such as addition, subtraction, multiplication and division.

The address of downloading the code (must have QT)

2 Assignment table

​| The Link Your Class | https://bbs.csdn.net/forums/ssynkqtd-04 |

| The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/617332156 |

| The Aim of This Assignment | create a calculator using QT |

| MU STU ID and FZU STU ID | 21127026_832101302 |

3 PSP Table

Personal Software Process StagesEstimated Time (minutes)Actual Time (minutes)
Planning
• Estimate3025
Development
• Analysis3030
• Design Spec1010
• Design Review55
• Coding Standard1015
• Design2025
• Coding120110
• Code Review3060
• Test3030
Reporting
• Test Report2020
• Size Measurement1015
• Postmortem & Process Improvement Plan1010
Sum325355

4 Problem-solving ideas

To make a calculator, I use the QT create an interactive interface. At this process, I need to solve three issues.

1 How to make the interactive interface

By looking around the web, I found that QT was relatively easy to create interactive interfaces.

2 How to solve operator priority problem

Through code iteration, operators are assigned by priority and then compared by value.

3 How to run multiple number and letter operations

Through code iteration, you set up two stacks to hold numbers and operators, and then write a function to perform the operation.

5 Design and implementation process

Make the interactive interface

Set up a user interface through QT, with buttons with different representations (numbers or operators) associated with the code through slots. Input and output are displayed through 2 text boxes.

Data processing & Calculate

When the user enters a string of numbers that need to be calculated, it is passed to the code through the slot, and then the code is iterated and divided into two stacks. In the process of sorting, different priorities are performed by comparing the priorities of operators, and finally the results are returned to the second text box through the code.

flow chart

User calculation process

Created with Raphaël 2.3.0 start user press the button if " = " button is pressed? calculate to result yes no

Code iteration process

Created with Raphaël 2.3.0 start user's input " a " i=0 i++ is i <strlen(a) ? is it a number ? number deposit to stack s1 operator Is the precedence of the operator greater than the previous one? deposit to stack s2 Calculates and clears the two s1 values previously stored, and stores the resulting value into stack s1, clearing an s2 operator end, output the result yes no yes no yes no

6 Code description

main code, Including the interaction of code and buttons, and the calculation of data

#include "widget.h"
#include "./ui_widget.h"
#include<iostream>
#include<cmath>
#include<QStack>

using namespace std;
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    s="";
    res="";
    setWindowTitle("计算器");
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    s+="1";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_4_clicked()
{
    s+="2";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_5_clicked()
{
    s+="3";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_2_clicked()
{
    s+="4";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_7_clicked()
{
    s+="5";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_10_clicked()
{
    s+="6";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_3_clicked()
{
    s+="7";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_8_clicked()
{
    s+="8";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_11_clicked()
{
    s+="9";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_16_clicked()
{
    s+="00";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_9_clicked()
{
    s+="0";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_12_clicked()
{
    s+=".";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_13_clicked()
{
    s+="+";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_14_clicked()
{
    s+="-";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_18_clicked()
{
    s+="/";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_15_clicked()
{
    s+="*";
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_6_clicked()
{
    s=s.left(s.length()-1);
    ui->textEdit->setText(s);
}


void Widget::on_pushButton_17_clicked()
{
    s="";
    res="";
    ui->textEdit->setText(s);
    ui->textEdit_3->setText(res);
}

//-----------------------------------------------------------------------------

int symbol(char c)
{
    switch (c)
    {
    case '#': return 0;
    case '+': return 2;
    case '-': return 2;
    case '*': return 3;
    case '/': return 3;

    default: break;
    }
    return 0;
}

double jisuan( char c='#',double b=1,double a=1)
{
    switch (c)
    {
    case '+': return b + a;
    case '-': return b - a;
    case '*': return b * a;
    case '/': return b / a;
    default: break;
    }
    return 0;
}
void Widget::on_pushButton_19_clicked()
{
    QStack<double>s1;
    QStack<char>s2;
    s = s + '#';
    char *a=s.toLocal8Bit().data();

    s2.push('#');

    for (int i = 0;i<strlen(a); i++)
    {
        if (a[i] <= 47||a[i]==124)                  //用阿斯克码判断扫描到的是数字还是运算符
        {
            if (a[i] == '#'&&s2.top() == '#') break; //两个#碰到一起运算结束

            if (symbol(s2.top()) >= symbol(a[i])) //判断运算符优先级
            {
                char temp1 = s2.top();
                s2.pop();
                double temp2 = s1.top();
                s1.pop();
                double temp3 = s1.top();
                s1.pop();
                s1.push(jisuan(temp1, temp3, temp2));
                i--;//要把a[i]再走一遍 不然同级运算a[i]不能push
                continue;
            }
            else
            {
                s2.push(a[i]);
                continue;
            }
        }
        else                     //对数字的运算
        {
            double sum = static_cast<int>(a[i]) - 48;
            for (; a[i + 1] > 47&& a[ i + 1 ] != 124 ; i++)     //实现多位数的运算
                sum = sum * 10 + static_cast<int>(a[i + 1]) - 48;
            if(static_cast<int>(a[i+1])==46)    //计算小数
            {
                int j=1;
                i=i+2;
                for(;a[i]>47&&a[ i ] != 124;i++)
                {
                    sum=sum+pow(10,-j)*(static_cast<int>(a[i]) - 48);
                    j++;
                }
                i--;
            }
            s1.push(sum);
        }
    }
    double num = s1.top();
    res = QString::number(num);
    ui->textEdit_3->setText(res);
}

6 Final Result

calculator

7 Summary

From this assignment, I learned how to use QT software to make interactive interface, which deepened my understanding of C++. It also consolidated the basic process of making a software, allowing me to apply what I learned in class.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值