代码拉取完成,页面将自动刷新
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <cstring>
#include <cmath>
#include <QDebug>
#include <QPainter>
#include <QStack>
#include <QQueue>
#define PI 3.1415926
#define e 2.7182818
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//初始化
str="";
resultStr="";
flag=0;
ui->LineAnswer->setText("0");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_Pclear_clicked()
{
str="";
resultStr="";
flag=0;
ui->LineAnswer->setText("0");
}
void MainWindow::on_P0_clicked()
{
if(flag==0||str==""){
ui->LineAnswer->setText("0");
haveZero=1;
//flag=1;
}
else {
str+="0";
ui->LineAnswer->setText(str);
}
//qDebug()<<str;
}
void MainWindow::on_P1_clicked()
{
if(flag==0){
str="1";
flag=1;
}
else str+="1";
ui->LineAnswer->setText(str);
}
void MainWindow::on_P2_clicked()
{
if(flag==0){
str="2";
flag=1;
}
else str+="2";
ui->LineAnswer->setText(str);
}
void MainWindow::on_P3_clicked()
{
if(flag==0){
str="3";
flag=1;
}
else str+="3";
ui->LineAnswer->setText(str);
}
void MainWindow::on_P4_clicked()
{
if(flag==0){
str="4";
flag=1;
}
else str+="4";
ui->LineAnswer->setText(str);
}
void MainWindow::on_P5_clicked()
{
if(flag==0){
str="5";
flag=1;
}
else str+="5";
ui->LineAnswer->setText(str);
}
void MainWindow::on_P6_clicked()
{
if(flag==0){
str="6";
flag=1;
}
else str+="6";
ui->LineAnswer->setText(str);
}
void MainWindow::on_P7_clicked()
{
if(flag==0){
str="7";
flag=1;
}
else str+="7";
ui->LineAnswer->setText(str);
}
void MainWindow::on_P8_clicked()
{
if(flag==0){
str="8";
flag=1;
}
else str+="8";
ui->LineAnswer->setText(str);
}
void MainWindow::on_P9_clicked()
{
if(flag==0){
str="9";
flag=1;
}
else str+="9";
ui->LineAnswer->setText(str);
}
//自然对数的底数e
void MainWindow::on_Pe_clicked()
{
if(flag==0){
str="2.7182818";
flag=1;
}
else str+="2.7182818";
ui->LineAnswer->setText(str);
}
//圆周率π
void MainWindow::on_Ppi_clicked()
{
if(flag==0){
str+="3.1415926";
flag=1;
}
else str+="3.1415926";
ui->LineAnswer->setText(str);
}
void MainWindow::on_Padd_clicked()
{
if(str==""){
str=resultStr;
str+="+";
ui->LineAnswer->setText(str);
}
if(flag==0){
ui->LineAnswer->setText("0");
}
else if(str[str.size()-1]=='+'||str[str.size()-1]=='('||str[str.size()-1]=='-'||str[str.size()-1]=='*'||str[str.size()-1]=='/'||str[str.size()-1]=='^'){
return ;
}
else {
str+="+";
ui->LineAnswer->setText(str);
}
}
void MainWindow::on_Psub_clicked()
{
if(str==""&&resultStr!=""){
str=resultStr;
str+="-";
ui->LineAnswer->setText(str);
}
else if(flag==0){
str='-';
flag=1;
ui->LineAnswer->setText(str);
}
else if(str[str.size()-1]=='+'||str[str.size()-1]=='-'||str[str.size()-1]=='*'||str[str.size()-1]=='/'||str[str.size()-1]=='^'){
return ;
}
else {
str+="-";
ui->LineAnswer->setText(str);
}
}
void MainWindow::on_Pmul_clicked()
{ if(str==""){
str=resultStr;
str+="*";
ui->LineAnswer->setText(str);
}
if(flag==0){
ui->LineAnswer->setText("0");
}
else if(str[str.size()-1]=='+'||str[str.size()-1]=='('||str[str.size()-1]=='-'||str[str.size()-1]=='*'||str[str.size()-1]=='/'||str[str.size()-1]=='^'){
return ;
}
else {
str+="*";
ui->LineAnswer->setText(str);
}
}
void MainWindow::on_Pdiv_clicked()
{
if(str==""){
str=resultStr;
str+="/";
ui->LineAnswer->setText(str);
}
if(flag==0){
ui->LineAnswer->setText("0");
}
else if(str[str.size()-1]=='+'||str[str.size()-1]=='-'||str[str.size()-1]=='*'||str[str.size()-1]=='/'||str[str.size()-1]=='('||str[str.size()-1]=='^'){
return ;
}
else{
str+="/";
ui->LineAnswer->setText(str);
}
}
//乘方
void MainWindow::on_Ppower_clicked()
{
if(str==""){
str=resultStr;
str+="^";
ui->LineAnswer->setText(str);
}
if(flag==0){
ui->LineAnswer->setText("0");
}
else if(str[str.size()-1]=='+'||str[str.size()-1]=='-'||str[str.size()-1]=='*'||str[str.size()-1]=='/'||str[str.size()-1]=='('||str[str.size()-1]=='^'){
return ;
}
else{
str+="^";
ui->LineAnswer->setText(str);
}
}
//左括号
void MainWindow::on_Pleft_clicked()
{
if(str=="")
return ;
if(flag==0){
ui->LineAnswer->setText("0");
}
//左括号前面不能为数字 小数点 右括号
else if((str[str.size()-1]<='9') && (str[str.size()-1]>='0') || (str[str.size()-1]=='.')|| (str[str.size()-1]==')') ){
return ;
}
else {
str+="(";
status++;
ui->LineAnswer->setText(str);
}
}
//右括号
void MainWindow::on_Pright_clicked()
{
if(str=="")
return ;
if(flag==0){
ui->LineAnswer->setText("0");
}
//前面不能为加减乘除 小数点 左括号
else if(status==0||str[str.size()-1]=='+'||str[str.size()-1]=='-'||str[str.size()-1]=='*'||str[str.size()-1]=='/'||str[str.size()-1]=='('||str[str.size()-1]=='.'){
return ;
}
else {
str+=")";
status--;
ui->LineAnswer->setText(str);
}
}
//退格
void MainWindow::on_Pbackspace_clicked()
{
if(str!=""){
if(str[str.size()-1]==')'){
status++;
}
str.chop(1);
ui->LineAnswer->setText(str);
}
if(str=="")
ui->LineAnswer->setText("0");
}
//小数点
void MainWindow::on_Pdot_clicked()
{
if(flag==0){
if(haveZero==1){
str="0.";
ui->LineAnswer->setText(str);
haveZero=0;
flag=1;
return ;
}
ui->LineAnswer->setText("0");
}
else if(str[str.size()-1]=='.'||str[str.size()-1]=='+'||str[str.size()-1]=='-'||str[str.size()-1]=='*'||str[str.size()-1]=='/'||str[str.size()-1]=='('||str[str.size()-1]==')') {
return ;
}
else {
str+=".";
//一串小数只能有一个小数点的判断
int num=0;
for(int i=str.size()-2;i>=0;i--){
if(str[i]=='.') num++;
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/') break;
}
if(num>=1) str.chop(1);//如果小数点超出,则删除该次输入
ui->LineAnswer->setText(str);
}
}
void MainWindow::on_Presult_clicked()
{
resultStr=calculater(str);
str="";
ui->LineAnswer->setText(calculater(resultStr));
}
/* 计算结果
* 将中缀表达式转化为后缀表达式计算
*/
QString MainWindow::calculater(const QString& str)
{
QQueue<QString> q=Split(str); //分离中缀
q=Transfer(q); //转换为后缀
return Calculate(q); //返回结果
}
/*void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter p(this);
p.drawPixmap(0,0,width(),height(),QPixmap("../black.png"));
}*/
//分离前缀
QQueue<QString> MainWindow::Split(const QString &str)
{
QQueue<QString> ret;
QString num="";
for(int i=0;i<str.length();i++)
{
if( (str[i]=='.') || ( (str[i]>='0') && (str[i]<='9') )) //判断小数点和数字
{
num += str[i];
}
else if(str[i]== '(' || str[i]== ')' || str[i]== '*' || str[i]== '/' ||(str[i]=='^') )
{
if(!num.isEmpty())
{
ret.enqueue(num); //将数字入队列
num.clear();
}
ret.enqueue(str[i]);
}
else if(str[i]== '+' || str[i]== '-') // + - 需要特殊处理
{
if(i==0) //表达式开头,说明是正负号
{
num+= str[i];
}
else if(str[i-1]=='(' || str[i-1]=='+' || str[i-1]=='-' || str[i-1]=='*' || str[i-1]=='/')
{
num+= str[i];
}
else //否则是加减运算符
{
if(!num.isEmpty())
{
ret.enqueue(num); //将数字入队列
num.clear();
}
ret.enqueue(str[i]);
}
}
}
if(!num.isEmpty()) //遍历完成,判断是否还有数字
{
ret.enqueue(num);
num.clear();
}
return ret;
}
//将中缀队列转换为后缀队列
QQueue<QString> MainWindow::Transfer(QQueue<QString> &str)
{
QStack<QString> stack;
QQueue<QString> ret;
bool num_ok;
QString symbol;
while(!str.isEmpty())
{
symbol = str.dequeue(); //出队列
symbol.toDouble(&num_ok);
if(num_ok==true) //数字
{
stack.push(symbol);
}
else if(symbol=="+"||symbol=="-")
{
while(!stack.isEmpty() &&(stack.top()!="("))
{
ret.enqueue(stack.pop()); //取出栈顶运算符并入队列
}
stack.push(symbol);
}
else if(symbol=="*"||symbol=="/")
{
while(!stack.isEmpty() && (stack.top()!="(") && (stack.top()!="+") && (stack.top()!="-"))
{
ret.enqueue(stack.pop()); //取出栈顶运算符并入队列
}
stack.push(symbol);
}
else if(symbol=="^"){
while(!stack.isEmpty() && (stack.top()!="(") && (stack.top()!="+") && (stack.top()!="-") && (stack.top()!="*") && (stack.top()!="/"))
{
ret.enqueue(stack.pop()); //取出栈顶运算符并入队列
}
stack.push(symbol);
}
else if(symbol == "(")
{
stack.push(symbol);
}
else if(symbol ==")")
{
while(!stack.isEmpty() && (stack.top()!="("))
{
ret.enqueue(stack.pop()); //取出栈顶运算符并入队列
}
if(stack.top()=="(")
stack.pop();
}
}
while(!stack.isEmpty()&& (stack.top()!="(")) //遍历完成,判断栈里是否为空
{
ret.enqueue(stack.pop()); //取出栈顶运算符并入队列
}
return ret;
}
//将后缀队列计算出结果
QString MainWindow::Calculate(QQueue<QString> &str)
{
QStack<QString> stack;
QString symbol,L,R,op,ret;
bool num_ok;
while(!str.isEmpty())
{
symbol = str.dequeue(); //出队列
symbol.toDouble(&num_ok);
if(num_ok==true) //数字
{
stack.push(symbol);
}
else //运算符
{
if(stack.size()<2)
return "Error";
R= stack.pop();
L= stack.pop();
ret = Calculate(L,symbol,R );
if(ret==NULL)
return ret;
stack.push(ret);
}
}
if(stack.size()==1) //遍历完成,结果只有一个
{
return ValidNum(stack.pop());
}
else
{return "Error";
}
}
//后缀表达式中出栈两两计算
QString MainWindow::Calculate(QString &l, QString &op, QString &r)
{
double left,right,res;
QString ret="";
left = l.toDouble();
right = r.toDouble();
if(op == "+")
{
res = left + right;
}
else if(op == "-")
{
res = left - right;
}
else if(op == "*")
{
res = left * right;
}
else if(op == "/")
{
if( (right>(-0.000000000000001)) && (right<(0.000000000000001)) ) //判断除数为0
return NULL;
else
res = left/right;
}
else if(op=="^"){
res = pow(left,right);
}
ret.sprintf("%f",res);
return ret;
}
QString MainWindow::ValidNum(QString str) //如果不是小数直接返回,是小数则去掉多余的尾0和小数点
{
QString num;
if(str.indexOf(".")== -1) //判断是否小数
return str;//没找到小数点 直接返回
while(str.length()>1) //避免0被去掉
{
num=str.right(1);
if(num=="."||num=="0")
{
str.chop(1);
if(num==".")
return str;
}
else
return str;
}
return str;
}
//以下为按下即生效的按键,直接产生结果,相当于按下后再按下等号
//转换正负
void MainWindow::on_Pchange_clicked(){
if(str==""){
if(resultStr[0]=='-'){
resultStr[0]='+';
ui->LineAnswer->setText(resultStr);
return ;
}
if(resultStr[0]=='+'){
resultStr[0]='-';
ui->LineAnswer->setText(resultStr);
return ;
}
if(resultStr[0]>='0'&&str[0]<='9'){
resultStr.insert(0,'-');
ui->LineAnswer->setText(resultStr);
return ;
}
}
else{
if(str[0]=='-'){
str[0]='+';
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
return ;
}
if(str[0]=='+'){
str[0]='-';
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
return ;
}
if(str[0]>='0'&&str[0]<='9'){
str.insert(0,'-');
ui->LineAnswer->setText(str);
resultStr=str;
str="";
return ;
}
}
}
//百分数
void MainWindow::on_Ppercent_clicked(){
if(str==""){
double dou=resultStr.toDouble();
dou/=100;
resultStr=QString::number(dou);
ui->LineAnswer->setText(resultStr);
}
else{
double dou=str.toDouble();
dou/=100;
str=QString::number(dou);
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
}
}
//平方
void MainWindow::on_Psquare_clicked()
{
if(str==""){
double dou=resultStr.toDouble();
dou*=dou;
resultStr=QString::number(dou);
ui->LineAnswer->setText(resultStr);
}
else{
double dou=str.toDouble();
dou*=dou;
str=QString::number(dou);
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
}
}
//立方
void MainWindow::on_Pcube_clicked()
{
if(str==""){
double dou=resultStr.toDouble();
dou=dou*dou*dou;
resultStr=QString::number(dou);
ui->LineAnswer->setText(resultStr);
}
else{
double dou=str.toDouble();
dou=dou*dou*dou;
str=QString::number(dou);
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
}
}
//开根号
void MainWindow::on_Psqrt_clicked()
{
if(str==""){
double dou=resultStr.toDouble();
dou=sqrt(dou);
resultStr=QString::number(dou);
ui->LineAnswer->setText(resultStr);
}
else{
double dou=str.toDouble();
dou=sqrt(dou);
str=QString::number(dou);
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
}
}
//倒数
void MainWindow::on_Preciprocal_clicked()
{
if(str==""){
double dou=resultStr.toDouble();
dou=1/dou;
resultStr=QString::number(dou);
ui->LineAnswer->setText(resultStr);
}
else{
double dou=str.toDouble();
dou=1/dou;
str=QString::number(dou);
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
}
}
//阶乘
void MainWindow::on_Pfactorial_clicked()
{
long long int res=1;
if(str==""){
if(resultStr.indexOf('.')!=-1) return ;//小数不能阶乘
double dou=resultStr.toDouble();
for(int i=1;i<=dou;++i){
res*=i;
}
dou=res;
resultStr=QString::number(dou);
ui->LineAnswer->setText(resultStr);
}
else{
if(str.indexOf('.')!=-1) return ;//小数不能阶乘
double dou=str.toDouble();
for(int i=1;i<=dou;++i){
res*=i;
}
dou=res;
str=QString::number(dou);
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
}
}
//正弦
void MainWindow::on_Psin_clicked()
{
if(str==""){
double dou=resultStr.toDouble();
dou=sin(dou);
resultStr=QString::number(dou);
ui->LineAnswer->setText(resultStr);
}
else{
double dou=str.toDouble();
dou=sin(dou);
str=QString::number(dou);
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
}
}
//余弦
void MainWindow::on_Pcos_clicked()
{
if(str==""){
double dou=resultStr.toDouble();
dou=cos(dou);
resultStr=QString::number(dou);
ui->LineAnswer->setText(resultStr);
}
else{
double dou=str.toDouble();
dou=cos(dou);
str=QString::number(dou);
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
}
}
//正切
void MainWindow::on_Ptan_clicked()
{
if(str==""){
double dou=resultStr.toDouble();
dou=tan(dou);
resultStr=QString::number(dou);
ui->LineAnswer->setText(resultStr);
}
else{
double dou=str.toDouble();
dou=tan(dou);
str=QString::number(dou);
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
}
}
//e的x次方
void MainWindow::on_PePower_clicked()
{
if(str==""){
double dou=resultStr.toDouble();
dou=exp(dou);
resultStr=QString::number(dou);
ui->LineAnswer->setText(resultStr);
}
else{
double dou=str.toDouble();
dou=exp(dou);
str=QString::number(dou);
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
}
}
//10的x次方
void MainWindow::on_P10power_clicked()
{
if(str==""){
double dou=resultStr.toDouble();
dou=pow(10,dou);
resultStr=QString::number(dou);
ui->LineAnswer->setText(resultStr);
}
else{
double dou=str.toDouble();
dou=pow(10,dou);
str=QString::number(dou);
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
}
}
//自然对数
void MainWindow::on_Pln_clicked()
{
if(str==""){
double dou=resultStr.toDouble();
dou=log(dou);
resultStr=QString::number(dou);
ui->LineAnswer->setText(resultStr);
}
else{
double dou=str.toDouble();
dou=log(dou);
str=QString::number(dou);
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
}
}
//10为底的对数
void MainWindow::on_Plog10_clicked()
{
if(str==""){
double dou=resultStr.toDouble();
dou=log10(dou);
resultStr=QString::number(dou);
ui->LineAnswer->setText(resultStr);
}
else{
double dou=str.toDouble();
dou=log10(dou);
str=QString::number(dou);
ui->LineAnswer->setText(str);
//相当于按下等号
resultStr=str;
str="";
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。