文章目录
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 Stages | Estimated Time (minutes) | Actual Time (minutes) |
---|---|---|
Planning | ||
• Estimate | 30 | 25 |
Development | ||
• Analysis | 30 | 30 |
• Design Spec | 10 | 10 |
• Design Review | 5 | 5 |
• Coding Standard | 10 | 15 |
• Design | 20 | 25 |
• Coding | 120 | 110 |
• Code Review | 30 | 60 |
• Test | 30 | 30 |
Reporting | ||
• Test Report | 20 | 20 |
• Size Measurement | 10 | 15 |
• Postmortem & Process Improvement Plan | 10 | 10 |
Sum | 325 | 355 |
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
Code iteration process
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.