程序示例精选
Qt+C++桌面计算器源码
如需安装运行环境或远程调试,可点击 博主头像进入个人主页查看博主联系方式,由专业技术人员远程协助!
前言
这篇博客针对《Qt+C++桌面计算器源码》编写代码,代码整洁,规则,易读。 学习与应用推荐首选。运行结果
文章目录
一、所需工具软件二、使用步骤
1. 主要代码
2. 运行结果
三、在线协助
一、所需工具软件
1. VS2019, C++2. Qt
二、使用步骤
代码如下(示例):
<code class="language-plaintext hljs">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"));
}
</code>
运行结果
三、在线协助:
如需安装运行环境或远程调试,可点击博主头像,进入个人主页查看博主联系方式,由专业技术人员远程协助!
1)远程安装运行环境,代码调试
2)Visual Studio, Qt, C++, Python编程语言入门指导
3)界面美化
4)软件制作
博主个人主页:https://developer.aliyun.com/profile/expert/rfnzgp3sk3ahc
博主所有文章点这里:https://developer.aliyun.com/profile/expert/rfnzgp3sk3ahc
博主联系方式点这里:https://developer.aliyun.com/profile/expert/rfnzgp3sk3ahc