话不多说,先贴图
所用到的图形都是基于UI界面设计来的,所以自己需要写的代码很少
Widget.h代码如下
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QString>
#include<QStack>
#include<QTime>
#include<iterator>
#include<QLineEdit>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
QString input="0"; //输入框
bool flat=false;
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
void on_pushButton_3_clicked();
void on_pushButton_you_clicked();
void on_pushButton_ce_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
void on_pushButton_6_clicked();
void on_pushButton_jia_clicked();
void on_pushButton_clear_clicked();
void on_pushButton_7_clicked();
void on_pushButton_8_clicked();
void on_pushButton_9_clicked();
void on_pushButton_jian_clicked();
void on_pushButton_chu_clicked();
void on_pushButton_0_clicked();
void on_pushButton_deng_clicked();
void on_pushButton_cheng_clicked();
void on_pushButton_zuo_clicked();
void on_pushButton_point_clicked();
void on_pushButton_10_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
Widget.cpp代码如下
#include "widget.h"
#include "ui_widget.h"
#include<iostream>
using namespace std;
#include<stack>
#include<vector>
#include<cstdlib>
#include<limits.h>
#include<QDebug>
bool isNum(char ch);
bool isOperate(char ch);
int level(char ch);
double scd(string s);
double getValue(vector<string> V);
vector<string> midToPost(string s);
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_2_clicked()
{
if(input=="0")
input='2';
else input=input+'2';
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_clicked()
{
if(input=="0")
input='1';
else input=input+'1';
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_3_clicked()
{
if(input=="0")
input='3';
else
{
input=input+'3';
}
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_you_clicked()
{
if(input=="0")
input=')';
else input=input+')';
// inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_ce_clicked()
{
input = input.left(input.length()-1); //减去一字符
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_4_clicked()
{
if(input=="0")
input='4';
else input=input+'4';
// inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_5_clicked()
{
if(input=="0")
input='5';
else input=input+'5';
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_6_clicked()
{
if(input=="0")
input='6';
else input=input+'6';
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_jia_clicked()
{
if(input=="0")
input='+';
else input=input+'+';
// inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_clear_clicked()
{
input='0';
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_7_clicked()
{
if(input=="0")
input='7';
else input=input+'7';
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_8_clicked()
{
if(input=="0")
input='8';
else input=input+'8';
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_9_clicked()
{
if(input=="0")
input='9';
else input=input+'9';
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_jian_clicked()
{
if(input=="0")
input='-';
else input=input+'-';
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_chu_clicked()
{
if(input=="0")
input='/';
else input=input+'/';
// inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_0_clicked()
{
if(input=="0")
input='0';
else input=input+'0';
// inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_cheng_clicked()
{
if(input=="0")
input='*';
else input=input+'*';
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_zuo_clicked()
{
if(input=="0")
input='(';
else input=input+'(';
// inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_point_clicked()
{
if(input=="0")
input='.';
else input=input+'.';
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
void Widget::on_pushButton_deng_clicked()
{
string inputTemp = input.toStdString();
vector<string> expression = midToPost(inputTemp);
double value = getValue(expression);
if(value != INT_MAX) {
input=input+"="+QString::number(value);
// inputLine->setText(input);
ui->lineEdit->setText(input);
}else{
input=' ';
input=input+"错误,请重新输入";
//inputLine->setText(input);
ui->lineEdit->setText(input);
}
}
bool isNum(char ch) {
if(ch >= '0' && ch <= '9') return true;
else return false;
}
bool isOperate(char ch) {
if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')')
return true;
else return false;
}
int level(char ch) {
switch(ch) {
case '(' :
return 5;
case '*' :
return 4;
case '/' :
return 4;
case '+' :
return 3;
case '-' :
return 3;
case ')':
return 2;
}
}
/*字符串转数字*/
double scd(string s) {
if(s.length() == 0) return INT_MAX;
bool flag = false;
for(int i = 0; i < s.length(); i++) {
if(i == 0 && s[i] == '-') continue;
else if(s[i] == '.' && !flag) {
if(i > 0 && isNum(s[i-1])) {
flag = true;
continue;
}
else return INT_MAX;
}
else if(isNum(s[i])) continue;
else return INT_MAX;
}
double result = atof(s.c_str());
return result;
}
/*由中缀表达式得到后缀表达式, 向量V存储结果*/
vector<string> midToPost(string s) {
stack<char> S; /*符号栈*/
vector<string> V; /*后缀表达式*/
int i = 0;
while(i < s.length()) {
if(isNum(s[i])) {
string str = "";
while(isNum(s[i]) || s[i] == '.') {
str += s[i];
i++;
}
V.push_back(str);
}
else if(isOperate(s[i])){
/*负数情况*/
if(s[i] == '-' && (i == 0 || !isNum(s[i-1]))) {
string str = "-"; i++;
while(isNum(s[i]) || s[i] == '.') {
str += s[i]; i++;
}
V.push_back(str);
}else{
if(S.empty()){
S.push(s[i]); i++;
}else {
int initial = level(s[i]);
if(initial == 2) {
while(level(S.top()) != 5 && !s.empty()) {
string str = "";
str += S.top();
V.push_back(str);
S.pop();
}
if(S.top() == '(') S.pop(); i++;
} else {
while(!S.empty() && initial <= level(S.top()) && level(S.top()) != 5) {
string str = "";
str += S.top();
V.push_back(str);
S.pop();
}
S.push(s[i]); i++;
}
}
}
}
else{
cout << "表达式出错" << endl;
V.clear();
return V;
}
}
while(!S.empty()) {
string str = ""; str += S.top();
S.pop();
V.push_back(str);
}
//for(int i = 0; i < V.size(); i++) cout << V[i] << "[]";
return V;
}
/*后缀表达式得到最终结果*/
double getValue(vector<string> V) {
stack<double> S;
for(int i = 0; i < V.size(); i++) {
/*操作运算符*/
if(V[i].length() == 1 && isOperate(V[i][0])) {
double a = 0, b = 0;
if(!S.empty()) {
a = S.top(); S.pop();
}else return INT_MAX;
if(!S.empty()) {
b = S.top(); S.pop();
}else return INT_MAX;
switch(V[i][0]) {
case '+':
S.push(b+a);
break;
case '-':
S.push(b-a);
break;
case '*':
S.push(b*a);
break;
case '/':
S.push(b/a);
break;
default:
return INT_MAX;
}
}else {
if(scd(V[i]) == INT_MAX) return INT_MAX;
else S.push(scd(V[i]));
}
}
if(S.empty()) return INT_MAX;
double value = S.top();
S.pop();
return value;
}
void Widget::on_pushButton_10_clicked()
{
this->close();//退出计算器
}
上面是用到的主要代码部分,Ui文件等资源无法上传已放到如下链接
链接:https://pan.baidu.com/s/1XqPfYlFbz0njPvn3CGzRlQ? 提取码:mr3r
|