程序示例精选
Qt+C++桌面计算器源码
如需安装运行环境或远程调试,见文章底部个人 QQ名片,由专业技术人员远程协助!
前言
这篇博客针对<<Qt+C++桌面计算器源码>>编写代码,代码整洁,规则,易读。 学习与应用推荐首选。
文章目录
一、所需工具软件
二、使用步骤
1. 引入库
2. 主要代码
3. 运行结果
三、在线协助
一、所需工具软件
1. VS, Qt
2. C++
二、使用步骤
1.引入库
代码如下(示例):
plain
`#include <QDebug>
#include <iostream>
#include <cmath>
#include <QChar>
#include <QDialog>
#include <QFile>`
2.主要代码
代码如下(示例):
plain
`void MainWindow::Calc_Suffix()
{
for(int i=0;i<out.size();i++)
{
bool is_Num =true;
out[i].toDouble(&is_Num);
if(is_Num)//如果是数字,直接入栈
{
s2.push(out[i].toDouble(&is_Num));
}
else if(out[i]=='s'||out[i]=='c'||out[i]=='t'||out[i]=='^')
{
double x=s2.pop();
if(out[i]=='s')
x=sin(x);
else if(out[i]=='c')
x=cos(x);
else if(out[i]=='t')
x=tan(x);
else if(out[i]=='l')
x=log(x);
else if(out[i]=='^')
{
int y=x;
x=s2.pop();
int z=x;
for(int j=1;j<y;j++)
x=x*z;
}
s2.push(x);
}
else if(out[i]=="+"||out[i]=="-"||out[i]=="*"||out[i]=="/")//如果是运算符,弹栈两次,并进行运算,运算后,结果入栈
{
double x=s2.pop();
double y=s2.pop();
if(out[i]=="+")
{
s2.push(y+x);
}else if(out[i]=="-"){
s2.push(y-x);
}else if(out[i]=="*"){
s2.push(y*x);
}else if(out[i]=="/"){
s2.push(y/x);
}
}
}
result = s2.pop();
qDebug()<<result;
}
void MainWindow::m_init()
{
ba.clear();
out.clear();
}
void MainWindow::on_pushButton_clicked()
{
in=ui->lineEdit->text();//获取表达式
getChar();
getSuffix();
Calc_Suffix();
ui->lineEdit->clear();//清空文本编辑框
ui->lineEdit->setText(QString("%1%2").arg('=').arg(result));//显示结果
m_init();
}
void MainWindow::on_pushButton_2_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg(0));
}
void MainWindow::on_pushButton_3_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg(1));
}
void MainWindow::on_pushButton_4_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg(2));
}
void MainWindow::on_pushButton_5_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg(3));
}
void MainWindow::on_pushButton_6_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg(4));
}
void MainWindow::on_pushButton_7_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg(5));
}
void MainWindow::on_pushButton_8_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg(6));
}
void MainWindow::on_pushButton_9_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg(7));
}
void MainWindow::on_pushButton_10_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg(8));
}
void MainWindow::on_pushButton_11_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg(9));
}
void MainWindow::on_pushButton_12_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg("+"));
}
void MainWindow::on_pushButton_13_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg("-"));
}
void MainWindow::on_pushButton_14_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg("*"));
}
void MainWindow::on_pushButton_15_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg("/"));
}
void MainWindow::on_pushButton_20_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg("."));
}
void MainWindow::on_pushButton_18_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg("("));
}
void MainWindow::on_pushButton_19_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg(")"));
}
void MainWindow::on_pushButton_16_clicked()
{
//清空所有内容,并初始化
ui->lineEdit->clear();
m_init();
}
void MainWindow::on_pushButton_17_clicked()
{
//退格删除一个字符
ui->lineEdit->backspace();
}
void MainWindow::on_pushButton_21_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg("sin"));
}
void MainWindow::on_pushButton_22_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg("cos"));
}
void MainWindow::on_pushButton_23_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg("tan"));
}
void MainWindow::on_pushButton_24_clicked()//最小公倍数,c_mul
{
QString str=ui->lineEdit->text();
QString l1="0",l2="0";
QByteArray yue = str.toLatin1();
bool hasSpace = false;
QString c_div="1";
int max_yue;
for(int i=0;i<yue.size();i++)
{
char x=yue[i];
if(x=='.')
hasSpace=true;
else if(x!='.'&&hasSpace)
l2+=QString(x);
else
l1+=QString(x);
}
int s1=l1.toInt(nullptr,10);
int s2=l2.toInt(nullptr,10);
int max;
if(s1>s2)
max=s1;
else
max=s2;
for(int j=2;j<max;j++)
{
if(s1 % j==0&&s2%j==0)
{
max_yue=j;
}
}
int z=s1*s2/max_yue;
QString c_mul=QString::number(z);
ui->lineEdit->setText(c_mul);
}
void MainWindow::on_pushButton_25_clicked()//公约数,两个数以小数点分格
{
QString str=ui->lineEdit->text();
QString l1="0",l2="0";
QByteArray yue = str.toLatin1();
bool hasSpace = false;
QString c_div="1";
for(int i=0;i<yue.size();i++)
{
char x=yue[i];
if(x=='.')
hasSpace=true;
else if(x!='.'&&hasSpace)
l2+=QString(x);
else
l1+=QString(x);
}
int s1=l1.toInt(nullptr,10);
int s2=l2.toInt(nullptr,10);
int max;
if(s1>s2)
max=s1;
else
max=s2;
for(int j=2;j<max;j++)
{
if(s1 % j==0&&s2%j==0)
{
c_div+="*";
c_div+=QString::number(j);
}
}
ui->lineEdit->setText(c_div);
}
void MainWindow::on_pushButton_26_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg("^"));
}
void MainWindow::on_pushButton_27_clicked()
{
QString str=ui->lineEdit->text();
ui->lineEdit->setText(QString("%1%2").arg(str).arg("log"));
}
`
3.参数定义
代码如下(示例):
plain
`if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--weights', nargs='+', type=str, default='yolov5_best_road_crack_recog.pt', help='model.pt path(s)')
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
parser.add_argument('--view-img', action='store_true', help='display results')
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
parser.add_argument('--classes', nargs='+', type=int, default='0', help='filter by class: --class 0, or --class 0 2 3')
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
parser.add_argument('--augment', action='store_true', help='augmented inference')
parser.add_argument('--update', action='store_true', help='update all models')
parser.add_argument('--project', default='runs/detect', help='save results to project/name')
parser.add_argument('--name', default='exp', help='save results to project/name')
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
opt = parser.parse_args()
`
3.运行结果如下
三、在线协助:
如需安装运行环境或远程调试,见文章底部个人 QQ 名片,由专业技术人员远程协助!
1)远程安装运行环境,代码调试
2)Qt, C++, Python入门指导
3)界面美化
4)软件制作
当前文章连接: https://blog.csdn.net/alicema1111/article/details/132608892
博主推荐文章: python人脸识别统计人数qt窗体-CSDN博客
博主推荐文章: Python Yolov5火焰烟雾识别源码分享-CSDN博客
Python OpenCV识别行人入口进出人数统计_python识别人数-CSDN博客
个人博客主页: alicema1111的博客_CSDN博客-Python,C++,网页领域博主
博主所有文章点这里 : alicema1111的博客_CSDN博客-Python,C++,网页领域博主