From 8850bab60dcb6405e597265ca03c35691d82c1ab Mon Sep 17 00:00:00 2001 From: Andywoww Date: Thu, 13 Jul 2023 19:41:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8B=A5=E5=B9=B2BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- calculator/calcer.cpp | 102 ++++++++++++++-- calculator/calcer.h | 5 + calculator/calcer.ui | 256 +++++++++++++++++++++++++++-------------- calculator/calcero.cpp | 42 ++++++- calculator/calcero.h | 2 + 5 files changed, 306 insertions(+), 101 deletions(-) diff --git a/calculator/calcer.cpp b/calculator/calcer.cpp index 7f70a85..6a9c853 100644 --- a/calculator/calcer.cpp +++ b/calculator/calcer.cpp @@ -6,13 +6,17 @@ calcer::calcer(QWidget *parent) , ui(new Ui::calcer) { setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint); - setFixedSize(339,582); + setFixedSize(392,581); ui->setupUi(this); ui->textEdit->setReadOnly(true); ui->textEdit2->setReadOnly(true); ui->textEdit->setText("0"); ui->textEdit2->setText("0"); ss=0; + connect(ui->pushe,&QPushButton::clicked,this,&calcer::pushe); + connect(ui->pushpi,&QPushButton::clicked,this,&calcer::pushpi); + connect(ui->pushx2,&QPushButton::clicked,this,&calcer::pushx2); + connect(ui->pushxn,&QPushButton::clicked,this,&calcer::pushxn); connect(ui->pushn0,&QPushButton::clicked,this,&calcer::pushn0); connect(ui->pushn1,&QPushButton::clicked,this,&calcer::pushn1); connect(ui->pushn2,&QPushButton::clicked,this,&calcer::pushn2); @@ -43,6 +47,75 @@ void calcer::keyPressEvent(QKeyEvent *event){ } break; } + if(event->modifiers() == Qt::ControlModifier) { // 如果按下了CTRL键 + if(event->key() == Qt::Key_V){ + QClipboard *clip=QApplication::clipboard(); + QString sa=clip->text(); + std::string s=sa.toStdString(); + if(s[s.length()-1]=='.')s.erase(s.end()-1); + int cnt=0; + string::iterator it; //指向string类的迭代器。你可以理解为指针 + for(it=s.begin();it!=s.end();it++){ + if(!((*it>='0'&&*it<='9')||*it=='('||*it==')'||*it=='+'||*it=='-'||*it=='*'||*it=='/'||*it=='^')){ + s.erase(it); + it--; + } + } + for(int i=0;itextEdit->toPlainText()=="0")ui->textEdit->setText(sa); + else ui->textEdit->setText(ui->textEdit->toPlainText()+sa); + } + } +} +void calcer::pushxn(){ + QString s=ui->textEdit->toPlainText(); + if(s[s.length()-1]=='=')s.erase(s.end()-1); + QChar a=s[s.length()-1]; + if(a=='.')s.erase(s.end()-1); + a=s[s.length()-1]; + if(a.isDigit()||a==')') + s+='^'; + ui->textEdit->setText(s); + return; +} +void calcer::pushe(){ + QString s=ui->textEdit->toPlainText(); + if(s=="0")s=""; + if(!s.isEmpty()){ + QChar a=s.back(); + if(!a.isDigit())s+="2.7182818284590452353602874713527"; + }else s="2.7182818284590452353602874713527"; + ui->textEdit->setText(s); + return; +} +void calcer::pushpi(){ + QString s=ui->textEdit->toPlainText(); + if(s=="0")s=""; + if(!s.isEmpty()){ + QChar a=s.back(); + if(!a.isDigit())s+="3.1415926535897932384626433832795"; + }else s="3.1415926535897932384626433832795"; + ui->textEdit->setText(s); + return; +} +void calcer::pushx2(){ + QString s=ui->textEdit->toPlainText(); + if(s[s.length()-1]=='=')s.erase(s.end()-1); + QChar a=s[s.length()-1]; + if(a=='.')s.erase(s.end()-1); + a=s[s.length()-1]; + if(a.isDigit()||a==')') + s+="^2"; + ui->textEdit->setText(s); + return; } void calcer::pushb(){ QString s=ui->textEdit->toPlainText(); @@ -133,6 +206,7 @@ void calcer::pushn9(){ } void calcer::pushjia(){ QString s=ui->textEdit->toPlainText(); + if(s[s.length()-1]=='=')s.erase(s.end()-1); QChar a=s[s.length()-1]; if(a=='.')s.erase(s.end()-1); a=s[s.length()-1]; @@ -143,6 +217,7 @@ void calcer::pushjia(){ } void calcer::pushjian(){ QString s=ui->textEdit->toPlainText(); + if(s[s.length()-1]=='=')s.erase(s.end()-1); if(s=="0")s=""; else{ QChar a=s[s.length()-1]; @@ -150,12 +225,14 @@ void calcer::pushjian(){ } /*QChar a=s[s.length()-1]; if(a.isDigit())*/ - s+='-'; + QChar a=s[s.length()-1]; + if(a.isDigit()||a=='(')s+='-'; ui->textEdit->setText(s); return; } void calcer::pushcheng(){ QString s=ui->textEdit->toPlainText(); + if(s[s.length()-1]=='=')s.erase(s.end()-1); QChar a=s[s.length()-1]; if(a=='.')s.erase(s.end()-1); a=s[s.length()-1]; @@ -166,6 +243,7 @@ void calcer::pushcheng(){ } void calcer::pushchu(){ QString s=ui->textEdit->toPlainText(); + if(s[s.length()-1]=='=')s.erase(s.end()-1); if(s=="0"){s+='/';} else{ QChar a=s[s.length()-1]; @@ -205,25 +283,34 @@ void calcer::pushnd(){ void calcer::pushdy(){ calcs a; std::string s=ui->textEdit->toPlainText().toStdString(); + if(s[s.length()-1]=='=')s.pop_back(); if(s[s.length()-1]=='.')s.erase(s.end()-1); + if(!isdigit(s.back())&&s.back()!='('){ + if(s.back()=='^')s+='1'; + else s+='0'; + } + int cnt=0; for(int i=0;itextEdit->toPlainText(); - if(y[y.length()-1]=='.'||y[y.length()-1]=='=')y.erase(y.end()-1); - ui->textEdit->setText(y+'='); + ui->textEdit->setText(y); QString k=QString::fromStdString(s); ui->textEdit2->setText(k); return; } void calcer::pushl(){ QString s=ui->textEdit->toPlainText(); + if(s[s.length()-1]=='=')s.erase(s.end()-1); if(s=="0")s=""; if(!ss){ s+='('; @@ -234,6 +321,7 @@ void calcer::pushl(){ } void calcer::pushr(){ QString s=ui->textEdit->toPlainText(); + if(s[s.length()-1]=='=')s.erase(s.end()-1); if(s=="0")s=""; if(ss){ s+=')'; diff --git a/calculator/calcer.h b/calculator/calcer.h index 77be6ef..fbb6971 100644 --- a/calculator/calcer.h +++ b/calculator/calcer.h @@ -4,6 +4,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE namespace Ui { class calcer; } QT_END_NAMESPACE @@ -20,7 +21,11 @@ class calcer : public QMainWindow void keyPressEvent(QKeyEvent *event); public slots: bool check(std::string *s); + void pushe(); + void pushpi(); void pushnd(); + void pushx2(); + void pushxn(); void pushb(); void pushc(); void pushn0(); diff --git a/calculator/calcer.ui b/calculator/calcer.ui index 0be1f9c..d73e11f 100644 --- a/calculator/calcer.ui +++ b/calculator/calcer.ui @@ -9,8 +9,8 @@ 0 0 - 339 - 582 + 392 + 581 @@ -22,7 +22,7 @@ 10 10 - 321 + 377 536 @@ -33,8 +33,8 @@ 0 - - + + 0 @@ -47,15 +47,15 @@ - 2 + 5 - 2 + 5 - - + + 0 @@ -68,15 +68,36 @@ - 5 + 3 - 5 + 3 + + + + + + + + 0 + 60 + + + + + 15 + + + + 0 + + + 0 - + 0 @@ -89,15 +110,15 @@ - 7 + 4 - 7 + 4 - - + + 0 @@ -110,15 +131,15 @@ - BACK + C - Backspace + C - - + + 0 @@ -131,15 +152,15 @@ - × + 7 - * + 7 - - + + 0 @@ -152,25 +173,40 @@ - + + ÷ - + + / - - + + 0 - 0 + 60 - + + + 15 + + + + - + + + - + + + + + + - 16777215 - 81 + 0 + 60 @@ -178,10 +214,16 @@ 15 + + . + + + . + - - + + 0 @@ -194,14 +236,14 @@ - 0 + + - 0 + + - + @@ -222,8 +264,8 @@ - - + + 0 @@ -236,15 +278,15 @@ - 4 + 2 - 4 + 2 - - + + 0 @@ -257,14 +299,14 @@ - 8 + ( - 8 + ( - + @@ -285,8 +327,8 @@ - - + + 0 @@ -299,15 +341,36 @@ - - + 8 - - + 8 + + + + + + + + 0 + 60 + + + + + 15 + + + + 1 + + + 1 - + 0 @@ -320,15 +383,15 @@ - 3 + BACK - 3 + Backspace - - + + 0 @@ -341,14 +404,14 @@ - C + × - C + * - + @@ -369,8 +432,8 @@ - - + + 0 @@ -383,14 +446,35 @@ - ÷ + 9 - / + 9 + + + + + + + + 0 + 60 + + + + + 15 + + + + e + + + E - + @@ -411,12 +495,18 @@ - - + + 0 - 60 + 0 + + + + + 16777215 + 81 @@ -424,16 +514,10 @@ 15 - - 1 - - - 1 - - - + + 0 @@ -446,15 +530,12 @@ - 9 - - - 9 + π - - + + 0 @@ -467,15 +548,12 @@ - ( - - - ( + - - + + 0 @@ -488,10 +566,10 @@ - . + x^n - . + ^ @@ -503,7 +581,7 @@ 0 0 - 339 + 392 22 diff --git a/calculator/calcero.cpp b/calculator/calcero.cpp index ff6ff36..85625f7 100644 --- a/calculator/calcero.cpp +++ b/calculator/calcero.cpp @@ -5,6 +5,10 @@ void calcs::mid2last(string a){ stacksigns; string lastf; for(int i=0;i='0'&&a[i]<='9')||a[i]=='.')lastf.push_back(a[i]); else{ if(lastf[lastf.length()-1]=='.')lastf.pop_back(); @@ -39,6 +43,21 @@ void calcs::mid2last(string a){ break; } } + if((a[i]=='^')){ + while(1){ + string s(1,a[i]); + if(signs.empty()||signs.top()=='(')signs.push(a[i]); + else if((signs.top()=='+'||signs.top()=='-'))signs.push(a[i]); + else{ + s.pop_back(); + s+=signs.top(); + signs.pop(); + last.push(s); + continue; + } + break; + } + } if(a[i]=='('){ signs.push('('); } @@ -71,12 +90,22 @@ string calcs::calc(){ try{ while(!ls2.empty()){ string x=ls2.top(); - if(x.front()>='0')datas.push(x); + if(x.front()>='0'&&x.front()<='9'||x.length()>1)datas.push(x); else{ - double s1=stod(datas.top()); + string s=datas.top(); + double s1,s2; + stringstream ss; + ss<>s1; + ss.clear(); + ss.str(""); datas.pop(); if(datas.empty())(throw -1); - double s2=stod(datas.top()); + s=datas.top(); + ss<>s2; + ss.clear(); + ss.str(""); datas.pop(); double res; if(x=="+")res=s1+s2; @@ -86,8 +115,11 @@ string calcs::calc(){ if(s1==0)throw -1; res=s2/s1; } - string y=to_string(res); - char *p=&y[y.length()-1]; + if(x=="^")res=pow(s2,s1); + ss< +#include #include #include #include #include +#include #include #include #include