成果如下 :
应学校老师要求,已经实现基础的计算,显示历史记录,语言播放数字的功能。
mainwindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QVector>
#include <QMainWindow>
#include <QString>
#include <QFile>
#include "fileshow.h"
#include "ui_fileshow.h"
#include <QKeyEvent>
#include "sound.h"
#define FILEPATH "historyCaculateRecords.txt"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
//按钮链接槽函数
void allbtnconnect();
//输入数字的信号
void inputNum(char num);
//输入符号的信号
void inputOperator(char sign);
//对但输入数据进行处理的符号 1/x x^2 sqrt(x) +/-
void processData(char sign);
//输入等号的处理
void equalCalculation();
//数 符号 数 即+ — * / 的处理
void ordinaryCalculation(char sign);
//等号或者 输入两符号后的运算处理
QString Calculate();
//取倒数1/x
void reciprocalCalculation();
//平方
void squareCalculation();
//开根
void openRootCalculation();
//取反
void reverseCalculations();
//%百分号处理
void percentageCalculation();
//修改数据的三个操作
void modifyNumbers(char sign);
//C操作
void clearAction();
//CE 操作
void clearEntryAction();
//Del操作
void deletAction();
//报错 0/0:结果未定义 x/0:除数不能为0 开根负数:无效输入
void reportError(QString error);
//小屏显示 num:1 为过程显示 num:0 为结果显示
void smallScreenDisplay(int num);
//大屏幕显示 //1:输入数字1 2:输入数字2
void largeScreenDisplay(int num);
//写历史记录
void writeRecords();
//读取历史记录
void readRecords();
//清除历史记录
void clearRecords();
//播放声音
void playSound(char sd);
//当其他按键按下时打断结果的朗读
void interruptTheResulreading();
private:
Ui::MainWindow *ui;
protected:
//键盘按下事件
virtual void keyPressEvent(QKeyEvent *evet);
public:
//存放输入的数
QVector <QString> vInputStr;
//数字1
QString num1;
int num1Size;
int isSetNum1; //是否将num1放到v容器中
//运算符
QString operator1;
int isSetOpreator1;
//数字2
QString num2;
int num2Size;
int num2Lock;
QString frontResult;
QString sShowNum1;
QString sShowNum2;
QString errorsShowNum1;
QString errorsShowNum2;
//现在处理的数据是否经过开根 平方 或者取倒数处理
int isProcessNum1;
int isProcessNum2;
QString tmpNum2;
QString tmpOPerator1;
//显示历史结果的界面
FileShow *fs;
int isSecondEqual;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QString>
#include <QLatin1String>
#include <QTextStream>
#include <QDir>
#include "fileshow.h"
#include "ui_fileshow.h"
#include <QSound>
#include <QChar>
#include <QMediaPlayer>
#include <QColor>
#include <QColorDialog>
extern QMediaPlayer *myPlayer;
extern QList<QString> m_lstMediaFile;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
num1="";
num2="";
operator1="";
num1Size=0;
num2Size=0;
isSetOpreator1=0;
isSetNum1=0;
num2Lock=0;
isSecondEqual=0;
// vInputStr.push_back(num1);
// vInputStr.push_back(operator1);
// vInputStr.push_back(num2);
largeScreenDisplay(1);
isProcessNum1=0;
isProcessNum2=0;
fs=new FileShow();
//结果播放
myPlayer= new QMediaPlayer;
myPlayer->setVolume(100);
//将多媒体播放器状态改变的信号和 状态需要改变让下一声音播放的槽函数链接起来
// QObject::connect(myPlayer,&QMediaPlayer::stateChanged,sltStateChanged);
QObject::connect(myPlayer,&QMediaPlayer::stateChanged,[=](QMediaPlayer::State state){
if (state == 0)
{
playMedia();
}
});
//设置倍速
myPlayer->setPlaybackRate(1.5);
clearRecords();
//欢迎语句
m_lstMediaFile.append("./sound/welcome.wav");
playMedia();
//按钮链接
allbtnconnect();
QPixmap bkg("./sound/p3.jpg");
QPalette pal;
pal.setBrush(QPalette::Background,bkg);
this->setPalette(pal);
ui->label_2->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
}
MainWindow::~MainWindow()
{
fs->close();
delete ui;
}
//输入数字的信号
void MainWindow::inputNum(char num)
{
qDebug()<<num;
if(isSetOpreator1==0)
{
//结果已经出来不可改变 只能清空num1
if(isSetNum1==1 )
{
operator1="";
num2="";
num1="";
num1Size=0;
isSetNum1=0;
sShowNum1=num1;
sShowNum2="";
largeScreenDisplay(1);
}
if(num1Size<16)
{
if((num1Size==1 && num1[0]=='0' && num=='0')|| (num1Size==2 && num1[0]=='-' && num1[1]=='0'&&num==0) ||(num1.indexOf(".")!=-1 && num=='.' ))
{
}
else if(num1Size==1 && num1[0]=='0'&& num!='.') //09
{
num1="";
num1.append(num);
num1Size=1;
sShowNum1=num1;
largeScreenDisplay(1);
}
else if(num1Size==2 && num1[0]=='-' && num1[1]=='0'&&num!='.') //-09
{
num1="-";
num1.append(num);
num1Size=2;
sShowNum1=num1;
largeScreenDisplay(1);
}
else if(num1Size==0 && num=='.')
{
num1Size+=2;
num1.append('0');
num1.append(num);
sShowNum1=num1;
largeScreenDisplay(1);
}
else if(num1Size==0 && num=='0')
{
}
else
{
num1Size+=1;
num1.append(num);
qDebug()<<num1 ;
sShowNum1=num1;
largeScreenDisplay(1);
}
}
}
else
{
if(num2Size<16 && num2Lock==0)
{
if((num2Size==1 && num2[0]=='0' && num=='0')|| (num2Size==2 && num2[0]=='-' && num2[1]=='0'&&num==0)||(num2.indexOf(".")!=-1 && num=='.' ))
{
}
else if(num2Size==1 && num2[0]=='0'&& num!='.') //09
{
num2="";
num2.append(num);
num2Size=1;
// sShowNum2=num1;
largeScreenDisplay(2);
}
else if(num2Size==2 && num2[0]=='-' && num2[1]=='0'&&num!='.') //-09
{
num2="-";
num2.append(num);
num2Size=2;
// sShowNum2=num2;
largeScreenDisplay(2);
}
else if(num2Size==0 && num=='.')
{
num2Size+=2;
num2.append('0');
num2.append(num);
if(num2Size!=0 && num2[0]=='-'){ if(num2.indexOf(".")==-1 &&num2.mid(1,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2; sShowNum2.insert(0,"("); sShowNum2.append(")");} else {if(num2.indexOf(".")==-1 && num2.mid(0,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2;}
largeScreenDisplay(2);
}
else if(num2Size==0 && num=='0')
{
}
else
{
num2Size+=1;
num2.append(num);
qDebug()<<num2 ;
if(num2Size!=0 && num2[0]=='-'){ if(num2.indexOf(".")==-1 &&num2.mid(1,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2; sShowNum2.insert(0,"("); sShowNum2.append(")");} else {if(num2.indexOf(".")==-1 && num2.mid(0,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2;}
largeScreenDisplay(2);
}
}
}
}
//输入符号的信号
void MainWindow::inputOperator(char sign)
{
// qDebug()<<sign;
//num1默认为0;
if(num1Size==0 )
{
num1Size=1;
num1="0";
sShowNum1=num1;
}
//对但输入数据进行处理的符号 1/x x^2 sqrt(x) +/- %
if(sign=='R' || sign=='2' || sign=='S' || sign=='N' || sign=='%')
{
processData(sign);
qDebug()<<sign;
}
else if(sign=='+' || sign=='-' || sign=='*' || sign=='/')
{
isProcessNum1=0;
num2Lock=0;
ordinaryCalculation(sign);
qDebug()<<sign;
}
else if(sign=='=')
{
num2Lock=0;
equalCalculation();
qDebug()<<sign;
}
else if(sign=='C' ||sign=='E' || sign=='D')
{
modifyNumbers(sign);
qDebug()<<sign;
}
}
// R 2 S N
//对但输入数据进行处理的符号 1/x x^2 sqrt(x) +/-
void MainWindow::processData(char sign)
{
if(sign=='R')
{
reciprocalCalculation();
}
else if(sign=='2')
{
squareCalculation();
}
else if(sign=='S')
{
openRootCalculation();
}
else if(sign=='N')
{
reverseCalculations();
}
else if(sign=='%')
{
percentageCalculation();
}
//清空tmpoperator 和tmpNum2
tmpNum2="";
tmpOPerator1="";
}
//输入等号的处理 =
void MainWindow::equalCalculation()
{
qDebug()<<"issen1:"<<isSetNum1<<"num1"<<num1<<"op1"<<"n1size"<<num1Size<<operator1<<"isseo"<<isSetOpreator1<<"num2"<<num2<<"n2size"<<num2Size<<"sshown1"<<sShowNum1<<"sshN2"<<sShowNum2<<"tmpOPerator1"<<tmpOPerator1<<"tmpn2:"<<tmpNum2;
num2Lock=0;
if(num1Size==0) //未输入num1
{
frontResult="0";
num1="0";
// vInputStr[0]=num1;
num1Size=1;
isSetNum1=1;
}
else if(isSetNum1==0 && num1Size!=0) //输入了num1 但未设置
{
frontResult=num1;
// vInputStr[0]=num1;
isSetNum1=1;
}
//按两下等号的操作
else if(isSetNum1==1 && isSetOpreator1==0)
{
if(isProcessNum1==0 && tmpOPerator1!="")
{
operator1=tmpOPerator1;
isSetOpreator1=1;
if(tmpNum2!="")
{
num2=tmpNum2;
num2Size=num2.size();
num2Lock=1;
sShowNum2=num2;
}
else
{
operator1=tmpOPerator1;
isSetOpreator1=1;
num2="";
num2Size=0;
num2Lock=1;
}
}
else if(isProcessNum1==1)
{
qDebug()<<"llal";
isSetNum1=0;
num1Size=num1.size();
}
else
{
isSetNum1=0;
}
// sShowNum1=num1;
// sShowNum2=num2;
isSecondEqual=1;
equalCalculation();
}
else if(isSetNum1==1 && isSetOpreator1==1) //设置了num1 也设置了符号 未设置num2 则使num2=num1
{
if(num2Size==0)
{
num2=num1;
num2Size=num2.size();
}
// vInputStr[2]=num2;
num1=Calculate();
// vInputStr[0]=num1;
isSetNum1=1;
qDebug()<<"num2error"<<num2.size();
qDebug()<<"num2error"<<num2;
qDebug()<<"num2error"<<sShowNum2;
num1Size=num1.size();
tmpNum2=num2;
tmpOPerator1=operator1;
isSetOpreator1=0;
if(isProcessNum2!=1)
{
if(num2Size!=0 && num2[0]=='-'){ if(num2.indexOf(".")==-1 &&num2.mid(1,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2; sShowNum2.insert(0,"("); sShowNum2.append(")");} else {if(num2.indexOf(".")==-1 && num2.mid(0,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2;}
}
num2="";
num2Size=0;
}
if(frontResult=="error")
{
num1="";
// vInputStr[0]=num1;
isSetNum1=1;
num1Size=num1.size();
isSetOpreator1=0;
num2="";
num2Size=0;
}
else
{
qDebug()<<"isSecondEqual:::::"<<isSecondEqual<<"issen1:"<<isSetNum1<<"num1"<<num1<<"op1"<<"n1size"<<num1Size<<operator1<<"isseo"<<isSetOpreator1<<"num2"<<num2<<"n2size"<<num2Size<<"sshown1"<<sShowNum1<<"sshN2"<<sShowNum2<<"tmpOPerator1"<<tmpOPerator1<<"tmpn2:"<<tmpNum2;
if(isSecondEqual!=1)
{
qDebug()<<"hh";
writeRecords();
smallScreenDisplay(0);
largeScreenDisplay(0);
sShowNum1=num1;
sShowNum2="";
operator1="";
playTheWholeNumber(frontResult);
}
else if((isSecondEqual==1))
{
isSecondEqual=0;
}
}
qDebug()<< frontResult;
isProcessNum2=0;
isProcessNum1=0;
}
//数 符号 数 即+ — * / 的处理
void MainWindow::ordinaryCalculation(char sign)
{
//两个数和运算符都已经输入 则在输入运算符要更行num1的值和运算符
if(isSetOpreator1==1 && num2Size!=0)
{
isProcessNum2=0;
// vInputStr[2]=num2;
num1=Calculate();
if(frontResult!="error")
{
//smallScreenDisplay(1);
largeScreenDisplay(1);
writeRecords();
// vInputStr[0]=num1;
num1Size=num1.size();
operator1[0]=sign;
// vInputStr[1]=operator1;
isSetOpreator1=1;
num2="";
num2Size=0;
sShowNum1=num1;
if(num2Size!=0 && num2[0]=='-'){ if(num2.indexOf(".")==-1 &&num2.mid(1,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2; sShowNum2.insert(0,"("); sShowNum2.append(")");} else {if(num2.indexOf(".")==-1 && num2.mid(0,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2;}
smallScreenDisplay(1);
}
else
{
QString displayStr;
QString showopt;
if(operator1=="/")
{
showopt ="÷";
}
else if(operator1=="*")
{
showopt="×";
}
else
{
showopt=operator1;
}
QString showopt2;
if(sign=='/')
{
showopt2 ="÷";
}
else if(sign=='*')
{
showopt2="×";
}
else
{
showopt2+=sign;
}
displayStr=sShowNum1+showopt+sShowNum2+showopt2;
qDebug()<<"wwwwwwww";
ui->label->setText(displayStr);
num1="";
num2="";
operator1="";
num1Size=0;
num2Size=0;
isSetOpreator1=0;
isSetNum1=0;
num2Lock=0;
// vInputStr[0]=num1;
// vInputStr[1]=operator1;
// vInputStr[2]=num2;
frontResult="";
}
}
//运算符已输入 第二个数为输入则更新运算符
else if(isSetOpreator1==1 && num2Size==0)
{
operator1[0]=sign;
// vInputStr[1]=operator1;
smallScreenDisplay(1);
}
//运算符还没设置则设置运算符
else if(isSetOpreator1==0)
{
if(num1Size==0)
{
num1="0";
}
// vInputStr[0]=num1;
isSetNum1=1;
operator1[0]=sign;
// vInputStr[1]=operator1;
isSetOpreator1=1;
smallScreenDisplay(1);
}
}
//修改数据的三个操作 C CE del
void MainWindow::modifyNumbers(char sign)
{
if(sign=='C')
{
clearAction();
}
else if(sign=='E')
{
clearEntryAction();
}
else if(sign=='D')
{
deletAction();
}
}
//等号或者 输入两符号后的运算处理 + - * /运算
QString MainWindow::Calculate()
{
qDebug()<<"cacula";
double num1d;
num1d=num1.toDouble();
double num2d;
double result;
num2d=num2.toDouble();
qDebug()<<"aaaaa"<<"num1:"<<num1<<"num2:"<<num2d;
qDebug()<<"aaaaa"<<"num1s:"<<num1Size<<"is1"<<isSetNum1<<"isop"<<isSetOpreator1;
if(operator1=="+")
{
result=num1d+num2d;
}
else if(operator1=="-")
{
result=num1d-num2d;
}
else if(operator1=="*")
{
result=num1d*num2d;
}
else if(operator1=="/")
{
if(num2d==0)
{
if(num1d==0)
{
reportError("结果未定义");
playError(2);
}
else
{
reportError("除数不能为0");
playError(3);
}
frontResult="error";
return "";
}
else
{
result=num1d/num2d;
}
}
QString strResult;
strResult=QString::number(result);
qDebug()<<"num1d:"<<num1d<<"num2d:"<<num2d;
qDebug()<<"aaaaa"<<"num1:"<<num1<<"num2:"<<num2d;
qDebug()<<strResult;
//qDebug()<<strResult.size();
frontResult=strResult;
return strResult;
}
//取倒数1/x
void MainWindow::reciprocalCalculation()
{
if(isSetOpreator1==0 && isSetNum1==1)
{
isProcessNum1=0;
}
// if( (num1Size!=0 && isSetNum1==0 &&isSetOpreator1==0))
if( (isSetOpreator1==0))
{
double num1d;
num1d=num1.toDouble();
if(num1d==0)
{
reportError("除数不能为0");
sShowNum1.insert(0,"1/(");
sShowNum1.append(")");
smallScreenDisplay(1);
playError(3);
num1="";
num2="";
operator1="";
num1Size=0;
num2Size=0;
isSetOpreator1=0;
isSetNum1=0;
num2Lock=0;
isProcessNum1=0;
isProcessNum2=0;
tmpNum2="";
tmpOPerator1="";
// vInputStr[0]=num1;
// vInputStr[1]=operator1;
// vInputStr[2]=num2;
sShowNum1=num1;
sShowNum2=num2;
}
else
{
num1d=1/num1d;
num1=QString::number(num1d);
num1Size=num1.size();
qDebug()<<"num1:"<<num1;
isProcessNum1=1;
sShowNum1.insert(0,"1/(");
sShowNum1.append(")");
smallScreenDisplay(1);
largeScreenDisplay(1);
}
}
else if(isSetOpreator1==1 )
{
if(num2Size==0)
{
num2=num1;
if(num2Size!=0 && num2[0]=='-'){ if(num2.indexOf(".")==-1 &&num2.mid(1,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2; sShowNum2.insert(0,"("); sShowNum2.append(")");} else {if(num2.indexOf(".")==-1 && num2.mid(0,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2;}
}
double num2d;
num2d=num2.toDouble();
if(num2d==0)
{
reportError("除数不能为0");
sShowNum2.insert(0,"1/(");
sShowNum2.append(")");
smallScreenDisplay(2);
playError(3);
num1="";
num2="";
operator1="";
num1Size=0;
num2Size=0;
isSetOpreator1=0;
isSetNum1=0;
num2Lock=0;
isProcessNum1=0;
isProcessNum2=0;
tmpNum2="";
tmpOPerator1="";
// vInputStr[0]=num1;
// vInputStr[1]=operator1;
// vInputStr[2]=num2;
sShowNum1=num1;
sShowNum2=num2;
}
else
{
num2d=1/num2d;
num2=QString::number(num2d);
num2Size=num2.size();
num2Lock=1; //第二个数做了倒数处理要锁定不能改变值
qDebug()<<"num2:"<<num2;
isProcessNum2=1;
sShowNum2.insert(0,"1/(");
sShowNum2.append(")");
smallScreenDisplay(2);
largeScreenDisplay(2);
}
}
}
//平方
void MainWindow::squareCalculation()
{
if(isSetOpreator1==0 && isSetNum1==1)
{
isProcessNum1=0;
}
//if( num1Size!=0 && isSetNum1==0 &&isSetOpreator1==0)
if( (isSetOpreator1==0))
{
double num1d;
num1d=num1.toDouble();
num1d=num1d*num1d;
num1=QString::number(num1d);
num1Size=num1.size();
qDebug()<<"num1:"<<num1;
isProcessNum1=1;
qDebug()<<"sShowNum1:"<<sShowNum1;
sShowNum1.insert(0,"sqr(");
sShowNum1.append(")");
qDebug()<<"sShowNum1:"<<sShowNum1;
smallScreenDisplay(1);
largeScreenDisplay(1);
}
else if(isSetOpreator1==1 )
{
if(num2Size==0)
{
num2=num1;
if(num2Size!=0 && num2[0]=='-'){ if(num2.indexOf(".")==-1 &&num2.mid(1,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2; sShowNum2.insert(0,"("); sShowNum2.append(")");} else {if(num2.indexOf(".")==-1 && num2.mid(0,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2;}
}
double num2d;
num2d=num2.toDouble();
num2d=num2d*num2d;
num2=QString::number(num2d);
num2Size=num2.size();
num2Lock=1; //第二个数做了倒数处理要锁定不能改变值
qDebug()<<"num2:"<<num2;
isProcessNum2=1;
sShowNum2.insert(0,"sqr(");
sShowNum2.append(")");
smallScreenDisplay(2);
largeScreenDisplay(2);
}
}
//开根
void MainWindow::openRootCalculation()
{
if(isSetOpreator1==0 && isSetNum1==1)
{
isProcessNum1=0;
}
if( (isSetOpreator1==0))
{
if(num1Size==0)
{
num1="0";
}
double num1d;
num1d=num1.toDouble();
if(num1d<0)
{
reportError("无效输入");
sShowNum1.insert(0,"√(");
sShowNum1.append(")");
smallScreenDisplay(1);
playError(1);
num1="";
num2="";
operator1="";
num1Size=0;
num2Size=0;
isSetOpreator1=0;
isSetNum1=0;
num2Lock=0;
isProcessNum1=0;
isProcessNum2=0;
tmpNum2="";
tmpOPerator1="";
// vInputStr[0]=num1;
// vInputStr[1]=operator1;
// vInputStr[2]=num2;
sShowNum1=num1;
sShowNum2=num2;
}
else
{
num1d=num1.toDouble();
num1d=sqrt(num1d);
num1=QString::number(num1d);
num1Size=num1.size();
qDebug()<<"num1:"<<num1;
isProcessNum1=1;
sShowNum1.insert(0,"√(");
sShowNum1.append(")");
smallScreenDisplay(1);
largeScreenDisplay(1);
}
}
else if(isSetOpreator1==1 )
{
if(num2Size==0)
{
num2=num1;
if(num2Size!=0 && num2[0]=='-'){ if(num2.indexOf(".")==-1 &&num2.mid(1,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2; sShowNum2.insert(0,"("); sShowNum2.append(")");} else {if(num2.indexOf(".")==-1 && num2.mid(0,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2;}
}
double num2d;
num2d=num2.toDouble();
if(num2d<0)
{
reportError("无效输入");
sShowNum2.insert(0,"√(");
sShowNum2.append(")");
smallScreenDisplay(2);
playError(1);
num1="";
num2="";
operator1="";
num1Size=0;
num2Size=0;
isSetOpreator1=0;
isSetNum1=0;
num2Lock=0;
isProcessNum1=0;
isProcessNum2=0;
tmpNum2="";
tmpOPerator1="";
// vInputStr[0]=num1;
// vInputStr[1]=operator1;
// vInputStr[2]=num2;
sShowNum1=num1;
sShowNum2=num2;
}
else
{
num2d=sqrt(num2d);
num2=QString::number(num2d);
num2Size=num2.size();
num2Lock=1; //第二个数做了倒数处理要锁定不能改变值
qDebug()<<"num2:"<<num2;
isProcessNum2=1;
sShowNum2.insert(0,"√(");
sShowNum2.append(")");
smallScreenDisplay(2);
largeScreenDisplay(2);
}
}
}
//取反
void MainWindow::reverseCalculations()
{
if( (isSetOpreator1==0))
{
if(num1[0]!='-' && num1!="0")
{
num1=num1.insert(0,'-');
num1Size+=1;
largeScreenDisplay(1);
}
else
{
num1=num1.remove(0,1);
num1Size-=1;
largeScreenDisplay(1);
}
qDebug()<<"num1:"<<num1;
if(isProcessNum1==1)
{
sShowNum1.insert(0,"negate(");
sShowNum1.append(")");
smallScreenDisplay(1);
largeScreenDisplay(1);
}
else
{
qDebug()<<"llll"<<num1;
qDebug()<<"1112"<<num2;
sShowNum1=num1;
}
}
else if(isSetOpreator1==1)
{
if(num2Size==0)
{
num2=num1;
if(num2Size!=0 && num2[0]=='-'){ if(num2.indexOf(".")==-1 &&num2.mid(1,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2; sShowNum2.insert(0,"("); sShowNum2.append(")");} else {if(num2.indexOf(".")==-1 && num2.mid(0,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2;}
}
if(num2[0]!='-')
{
num2.insert(0,'-');
num2Size+=1;
if(num2Size!=0 && num2[0]=='-'){ if(num2.indexOf(".")==-1 &&num2.mid(1,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2; sShowNum2.insert(0,"("); sShowNum2.append(")");} else {if(num2.indexOf(".")==-1 && num2.mid(0,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2;}
}
else
{
num2.remove(0,1);
num2Size-=1;
}
qDebug()<<"num2:"<<num2;
largeScreenDisplay(2);
if(isProcessNum2==1)
{
sShowNum2.insert(0,"negate(");
sShowNum2.append(")");
smallScreenDisplay(2);
largeScreenDisplay(2);
}
else
{
}
}
}
//%百分号处理
void MainWindow::percentageCalculation()
{
if(isSetOpreator1==1 )
{
if(num2Size==0)
{
num2=num1;
if(num2Size!=0 && num2[0]=='-'){ if(num2.indexOf(".")==-1 &&num2.mid(1,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2; sShowNum2.insert(0,"("); sShowNum2.append(")");} else {if(num2.indexOf(".")==-1 && num2.mid(0,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2;}
}
double num2d;
num2d=num2.toDouble();
num2d=num2d/100.0;
num2=QString::number(num2d);
num2Size=num2.size();
num2Lock=1; //第二个数做了倒数处理要锁定不能改变值
qDebug()<<"num2:"<<num2;
if(num2Size!=0 && num2[0]=='-'){ if(num2.indexOf(".")==-1 &&num2.mid(1,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2; sShowNum2.insert(0,"("); sShowNum2.append(")");} else {if(num2.indexOf(".")==-1 && num2.mid(0,1)=="0"){num2=num2.remove(1,1);}sShowNum2=num2;}
smallScreenDisplay(2);
largeScreenDisplay(2);
}
}
//C操作
void MainWindow::clearAction()
{
num1="";
num2="";
operator1="";
num1Size=0;
num2Size=0;
isSetOpreator1=0;
isSetNum1=0;
num2Lock=0;
isProcessNum1=0;
isProcessNum2=0;
tmpNum2="";
tmpOPerator1="";
// vInputStr[0]=num1;
// vInputStr[1]=operator1;
// vInputStr[2]=num2;
sShowNum1=num1;
sShowNum2=num2;
smallScreenDisplay(1);
largeScreenDisplay(1);
frontResult="";
}
//CE 操作
void MainWindow::clearEntryAction()
{
if( isSetOpreator1==0)
{
num1="";
num1Size=0;
largeScreenDisplay(1);
}
else if(isSetOpreator1==1)
{
num2="";
num2Size=0;
num2Lock=0;
largeScreenDisplay(2);
}
}
//Del操作
void MainWindow::deletAction()
{
if(isSetNum1==0 && isSetOpreator1==0 && num1Size!=0)
{
num1.remove(num1Size-1,1);
num1Size-=1;
largeScreenDisplay(1);
qDebug()<<"num1:"<<num1;
}
else if(isSetOpreator1==1 && num2Size!=0)
{
num2.remove(num2Size-1,1);
num2Size-=1;
largeScreenDisplay(2);
qDebug()<<"num2:"<<num2;
}
}
//小屏显示 num:1 为过程显示 num:2 第二数字进行过处理 num:0 为结果显示
void MainWindow::smallScreenDisplay(int num)
{
QString showopt;
if(operator1=="/")
{
showopt ="÷";
}
else if(operator1=="*")
{
showopt="×";
}
else
{
showopt=operator1;
}
if(sShowNum2.mid(sShowNum2.size()-1,1)==".")
{
sShowNum2=sShowNum2.mid(0,sShowNum2.size()-1);
}
if(sShowNum1.size()>=1 && sShowNum1[sShowNum1.size()-1]=='.')
{
sShowNum1.remove(sShowNum1.size()-1,1);
}
if(sShowNum2.size()>=1 && sShowNum2[sShowNum2.size()-1]=='.')
{
sShowNum2.remove(sShowNum2.size()-1,1);
}
QString displayStr;
if(num==1)
{
displayStr=sShowNum1+showopt;
qDebug()<<"small"<<sShowNum1<<showopt;
}
else if(num==2)
{
displayStr=sShowNum1+showopt+sShowNum2;
qDebug()<<"small"<<sShowNum1<<showopt<<sShowNum2;
}
else if(num==0)
{
displayStr=sShowNum1+showopt+sShowNum2+"=";
qDebug()<<"small"<<sShowNum1<<showopt<<sShowNum2<<"=";
}
ui->label->setText(displayStr);
}
//大屏幕显示
void MainWindow::largeScreenDisplay(int num)
{
QString displayStr;
if(num==1)
{
if(num1Size==0)
{
displayStr="0";
qDebug()<<"big:"<<"0";
}
else
{
displayStr=num1;
qDebug()<<"big:"<<num1;
}
}
else if(num==2)
{
if(num2Size==0)
{
displayStr="0";
qDebug()<<"big:"<<"0";
}
else
{
displayStr=num2;
qDebug()<<"big:"<<num2;
}
}
else if(num==0)
{
displayStr=frontResult;
qDebug()<<"big:"<<frontResult;
}
ui->label_2->setText(displayStr);
}
//大屏幕报错 0/0:
//x/0:除数不能为0 开根负数:无效输入
void MainWindow::reportError(QString error)
{
qDebug()<<"big:"<<error;
ui->label_2->setText(error);
}
//写历史记录
void MainWindow::writeRecords()
{
QFile *file = new QFile(FILEPATH);
//QFile file(FILEPATH);
if(sShowNum2.mid(sShowNum2.size()-1,1)==".")
{
sShowNum2=sShowNum2.mid(0,sShowNum2.size()-1);
}
if(!file->open(QIODevice::WriteOnly |QIODevice::Append))
{
qDebug()<<"File opening failed";
}
else
{
QString recordStr;
QByteArray br;
QString showopt;
if(operator1=="/")
{
showopt ="÷";
}
else if(operator1=="*")
{
showopt="×";
}
else
{
showopt=operator1;
}
recordStr=sShowNum1+showopt+sShowNum2+"="+frontResult;
recordStr=recordStr.toUtf8();
QTextStream txtOutput(file);
txtOutput.setCodec("UTF-8");
txtOutput<<recordStr<<endl;
qDebug()<<recordStr;
qDebug()<<"write ok";
}
file->close();
}
//读取显示历史记录
void MainWindow::readRecords()
{
//阻塞窗口
fs->setWindowModality(Qt::ApplicationModal);
fs->showHistory();
fs->show();
}
//清除历史记录
void MainWindow::clearRecords()
{
QFile *file=new QFile(FILEPATH);
file->open(QIODevice::WriteOnly | QIODevice::Truncate);
file->close();
}
//按钮链接槽函数
void MainWindow::allbtnconnect()
{
//将按键0~9和小数点信号和槽函数链接
connect(ui->btn0,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('0');
inputNum('0');
});
connect(ui->btn1,&QPushButton::clicked,[=](){
interruptTheResulreading();
inputNum('1');
playSound('1');
});
connect(ui->btn2,&QPushButton::clicked,[=](){
interruptTheResulreading();
inputNum('2');
playSound('2');
});
connect(ui->btn3,&QPushButton::clicked,[=](){
interruptTheResulreading();
inputNum('3');
playSound('3');
});
connect(ui->btn4,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('4');
inputNum('4');
});
connect(ui->btn5,&QPushButton::clicked,[=](){
playSound('5');
inputNum('5');
interruptTheResulreading();
});
connect(ui->btn6,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('6');
inputNum('6');
});
connect(ui->btn7,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('7');
inputNum('7');
;
});
connect(ui->btn8,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('8');
inputNum('8');
});
connect(ui->btn9,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('9');
inputNum('9');
});
connect(ui->btnPoint,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('.');
//小数点为.
inputNum('.');
});
//将运算符点击信号和槽函数输入运算符相连
connect(ui->btnAdd,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('+');
inputOperator('+');
});
connect(ui->btnSub,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('-');
inputOperator('-');
});
connect(ui->btnC,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('C');
inputOperator('C');
});
connect(ui->btnCE,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('E');
inputOperator('E');
});
connect(ui->btnDel,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('D');
inputOperator('D');
});
connect(ui->btnMul,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('*');
inputOperator('*');
});
connect(ui->btnDiv,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('/');
inputOperator('/');
});
connect(ui->btnEqual,&QPushButton::clicked,[=](){
interruptTheResulreading();
//playSound('=');
inputOperator('=');
//playTheWholeNumber("-1.9e-5");
//
});
connect(ui->btnNegate,&QPushButton::clicked,[=](){
//取反
interruptTheResulreading();
inputOperator('N');
playSound('N');
});
connect(ui->btnRec,&QPushButton::clicked,[=](){
//1/x倒数
interruptTheResulreading();
playSound('R');
inputOperator('R');
});
connect(ui->btnSqrt,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('S');
//开根
inputOperator('S');
});
connect(ui->btnSquare,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('q');
//平方
inputOperator('2');
});
connect(ui->btnPercent,&QPushButton::clicked,[=](){
interruptTheResulreading();
playSound('%');
//百分号 1*50%=
inputOperator('%');
});
connect(ui->btnHistory,&QPushButton::clicked,[=](){
readRecords();
});
connect(ui->btn_color,&QPushButton::clicked,[=](){
//设置背景颜色 颜色对话框
QPalette pal;
QColor color =QColorDialog::getColor(QColor(255,0,0));
pal.setColor(QPalette::Background,color);
this->setPalette(pal);
this->fs->setPalette(pal);
qDebug()<<"r="<<color.red()<<"g="<<color.green()<<color.blue();
});
}
//键盘按下事件
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(event->key()==Qt::Key_0)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btn0->clicked();
return;
}
if(event->key()==Qt::Key_1)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btn1->clicked();
return;
}
if(event->key()==Qt::Key_2)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btn2->clicked();
return;
}
if(event->key()==Qt::Key_3)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btn3->clicked();
return;
}
if(event->key()==Qt::Key_4)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btn4->clicked();
return;
}
if(event->key()==Qt::Key_5)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btn5->clicked();
return;
}
if(event->key()==Qt::Key_6)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btn6->clicked();
return;
}
if(event->key()==Qt::Key_7)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btn7->clicked();
return;
}
if(event->key()==Qt::Key_8)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btn8->clicked();
return;
}
if(event->key()==Qt::Key_9)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btn9->clicked();
return;
}
//小数点
if(event->key()==Qt::Key_Period)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btnPoint->clicked();
return;
}
if(event->key()==Qt::Key_Backspace)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btnDel->clicked();
return;
}
if(event->key()==Qt::Key_Enter || event->key()==Qt::Key_Return)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btnEqual->clicked();
return;
}
if(event->key()==Qt::Key_Plus)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btnAdd->clicked();
return;
}
if(event->key()==Qt::Key_Minus)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btnSub->clicked();
return;
}
if(event->key()==Qt::Key_Asterisk)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btnMul->clicked();
return;
}
if(event->key()==Qt::Key_Minus)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btnSub->clicked();
return;
}
if(event->key()==Qt::Key_Slash)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btnDiv->clicked();
return;
}
if(event->key()==Qt::Key_H)
{
if(event->isAutoRepeat())
{
return ;
}
emit ui->btnHistory->clicked();
return;
}
}
//播放声音
void MainWindow::playSound(char sd)
{
QString soundPath=getSoundPath(sd);
QSound::play(soundPath);
}
//当其他按键按下时打断结果的朗读
void MainWindow::interruptTheResulreading()
{
m_lstMediaFile.clear();
myPlayer->stop();
}
fileshow.h
#ifndef FILESHOW_H
#define FILESHOW_H
#include <QWidget>
//历史记录页面
namespace Ui {
class FileShow;
}
class FileShow : public QWidget
{
Q_OBJECT
public:
//更新历史记录显示
void showHistory();
explicit FileShow(QWidget *parent = 0);
Ui::FileShow *ui;
~FileShow();
private:
};
#endif // FILESHOW_H
fileshow.cpp
#ifndef FILESHOW_H
#define FILESHOW_H
#include <QWidget>
//历史记录页面
namespace Ui {
class FileShow;
}
class FileShow : public QWidget
{
Q_OBJECT
public:
//更新历史记录显示
void showHistory();
explicit FileShow(QWidget *parent = 0);
Ui::FileShow *ui;
~FileShow();
private:
};
#endif // FILESHOW_H
sound.h
#ifndef SOUND_H
#define SOUND_H
#include<QString>
#include <QCoreApplication>
#include <QFileInfo>
#include <QMediaPlayer>
QString getSoundPath(char sd);
//获取声音地址
QString getSoundPath(char sd);
//状态改变后的动作槽函数
void sltStateChanged(QMediaPlayer::State state);
//播放列表声音
void playMedia();
//播放完整数字(结果播报) 最大读到千亿 0000|0000|0000.
void playTheWholeNumber(QString num);
//传入一个小于1万的数,播放声音添加万以内的读法 3 2 1
void addReadingWithinTenThousand(QString strnum,QList<QString> &m_lstMediaFile);
//播放错误信息
//num1 无效输入 2 结果未定义 3除数不能为0
void playError(int num);
//科学计数转化为普通计数 0.003e+02 -> 0.3
void scientificCountingToOrdinaryCount(QString &num);
#endif // SOUND_H
sound.cpp
#ifndef SOUND_CPP
#define SOUND_CPP
#include "sound.h"
#include <QSound>
#include <QDebug>
#include <QThread>
#include <QTime>
#include <QEventLoop>
#include <QMediaPlaylist>
#include <QString>
#include <QCoreApplication>
#include <QFileInfo>
#include <QMediaPlayer>
#include <QCharRef>
#include <typeinfo>
QMediaPlayer *myPlayer;
QList<QString> m_lstMediaFile;
//获取声音地址
QString getSoundPath(char sd)
{
QString soundPath;
switch (sd){
case '0':
soundPath="./sound/0.wav";
break;
case '1':
soundPath="./sound/1.wav";
break;
case '2':
soundPath="./sound/2.wav";
break;
case '3':
soundPath="./sound/3.wav";
break;
case '4':
soundPath="./sound/4.wav";
break;
case '5':
soundPath="./sound/5.wav";
break;
case '6':
soundPath="./sound/6.wav";
break;
case '7':
soundPath="./sound/7.wav";
break;
case '8':
soundPath="./sound/8.wav";
break;
case '9':
soundPath="./sound/9.wav";
break;
case '.':
soundPath="./sound/point.wav";
break;
case '+':
soundPath="./sound/add.wav";
break;
case '*':
soundPath="./sound/mul.wav";
break;
case '/':
soundPath="./sound/div.wav";
break;
case 'S':
soundPath="./sound/sqrt.wav";
break;
case 'q':
soundPath="./sound/square.wav";
break;
case '%':
soundPath="./sound/percent.wav";
break;
case 'N':
soundPath="./sound/Negate.wav";
break;
case 'R':
soundPath="./sound/Rec.wav";
break;
case 'C':
soundPath="./sound/clear.wav";
break;
case 'E':
soundPath="./sound/ce.wav";
break;
case 'D':
soundPath="./sound/del.wav";
break;
case '=':
soundPath="./sound/equal.wav";
break;
case '-':
soundPath="./sound/sub.wav";
break;
case 'Q': //千
soundPath="./sound/qian.wav";
break;
case 'B': //百
soundPath="./sound/bai.wav";
break;
case 'W': //万
soundPath="./sound/wan.wav";
break;
case 's': //十
soundPath="./sound/shi.wav";
break;
case 'Y': //亿
soundPath="./sound/yi.wav";
break;
case 'F': //负
soundPath="./sound/fu.wav";
break;
case 'c': //数字超过可读范围
soundPath="./sound/C.wav";
break;
}
//QSound::play(soundPath);
return soundPath;
}
void playMedia()
{
if(m_lstMediaFile.size()>0)
{
myPlayer->setMedia(QUrl::fromLocalFile( m_lstMediaFile.at(0)));
myPlayer->play();
m_lstMediaFile.removeAt(0);
}
}
void sltStateChanged(QMediaPlayer::State state)
{
if (state == 0)
{
playMedia();
}
}
//播放完整数字(结果播报) 最大读到千亿 0000|0000|0000.
void playTheWholeNumber(QString num)
{
m_lstMediaFile.append("./sound/equal.wav");
//小数点的位置
int point_pos;
//小数点前数字的字符串
QString beforePointNum="";
QString backPointNum="";
//不包含符号的小数点之前数的长度
int noSignBeforeNumSize;
//strNum3 |strNum2 |strNum1
//0000|0000|0000
QString strNum1;
QString strNum2;
QString strNum3;
//整数有及部分 1~3
int partCount=0;
//讲科学计数华为标准形式
scientificCountingToOrdinaryCount(num);
//读取小数点位置
point_pos=num.indexOf(".");
//有小数部分
if(point_pos!=-1)
{
beforePointNum=num.mid(0,point_pos);
qDebug()<<"beforePointNum"<<beforePointNum;
backPointNum=num.mid(point_pos+1,num.size()-point_pos-1);
qDebug()<<"backPointNum"<<backPointNum;
}
else
{
beforePointNum=num;
qDebug()<<"beforePointNum"<<beforePointNum;
}
//数字读取
//读个负号
if(beforePointNum[0]=='-')
{
m_lstMediaFile.append(getSoundPath('F'));
beforePointNum.remove(0,1);
}
//整数分为三部分
//strNum3 |strNum2 |strNum1
//0000|0000|0000
noSignBeforeNumSize=beforePointNum.size();
if(noSignBeforeNumSize>12)
{
QSound::play(getSoundPath('c'));
return;
}
else if(noSignBeforeNumSize<=12&& noSignBeforeNumSize>8)
{
partCount=3;
qDebug()<<"before"<<beforePointNum;
strNum1=beforePointNum.mid(noSignBeforeNumSize-4,4);
beforePointNum=beforePointNum.remove(noSignBeforeNumSize-4,4);
strNum2=beforePointNum.mid(noSignBeforeNumSize-4-4,4);
beforePointNum=beforePointNum.remove(noSignBeforeNumSize-4-4,4);
strNum3=beforePointNum;
qDebug()<<strNum3<<" "<<strNum2<<" "<<strNum1;
}
else if(noSignBeforeNumSize<=8 && noSignBeforeNumSize>4)
{
partCount=2;
strNum1=beforePointNum.mid(noSignBeforeNumSize-4,4);
beforePointNum=beforePointNum.remove(noSignBeforeNumSize-4,4);
strNum2=beforePointNum;
}
else if(noSignBeforeNumSize<=4)
{
partCount=1;
strNum1=beforePointNum;
}
qDebug()<<strNum3<<" "<<strNum2<<" "<<strNum1;
//整数只有0要读出来
if(partCount==1 && strNum1=="0")
{
m_lstMediaFile.append(getSoundPath('0'));
}
else if(partCount>=1)
{
for(int i=partCount;i>0;i--)
{
switch (i)
{
case 3:
addReadingWithinTenThousand(strNum3,m_lstMediaFile);
m_lstMediaFile.append(getSoundPath('Y'));
break;
case 2:
addReadingWithinTenThousand(strNum2,m_lstMediaFile);
m_lstMediaFile.append(getSoundPath('W'));
break;
case 1:
addReadingWithinTenThousand(strNum1,m_lstMediaFile);
break;
}
}
}
char nc;
QString soundPath;
//读取小数部分
if(backPointNum!="")
{
m_lstMediaFile.append(getSoundPath('.'));
}
for(int i=0;i<backPointNum.size();i++)
{
nc=backPointNum.at(i).toLatin1();
soundPath=getSoundPath(nc);
m_lstMediaFile.append(soundPath);
}
m_lstMediaFile.append("./sound/el.wav");
// for(int i=0;i<m_lstMediaFile.size();i++)
// {
// qDebug()<<m_lstMediaFile[i];
// }
playMedia();
}
//添加万以内的读法 3 2 1
void addReadingWithinTenThousand(QString strnum,QList<QString> &m_lstMediaFile)
{
QString soundPath;
QString str_0="0000";
if(strnum.size()!=4)
{
strnum=strnum.insert(0,str_0.mid(0,4-strnum.size()));
}
char num[4];
for(int i=0;i<4;i++)
{
num[i]=strnum.at(i).toLatin1();
}
for(int i=0;i<4;i++)
{
if(num[i]!='0')
{
soundPath=getSoundPath(num[i]);
m_lstMediaFile.append(soundPath);
switch (i)
{
case 0:
//千
m_lstMediaFile.append(getSoundPath('Q'));
break;
case 1:
m_lstMediaFile.append(getSoundPath('B'));
break;
case 2:
m_lstMediaFile.append(getSoundPath('s'));
break;
}
//
if((i==0 && num[1]=='0' &&(num[2]!='0' || num[3]!='0') ) ||(i==1 && num[2]=='0' && num[3]!='0') )
{
m_lstMediaFile.append(getSoundPath('0'));
}
}
}
}
//num1 无效输入 2 结果未定义 3除数不能为0
void playError(int num)
{
switch(num)
{
case 1:
//按钮链接
m_lstMediaFile.append("./sound/e1.wav");
break;
case 2:
m_lstMediaFile.append("./sound/e2.wav");
break;
case 3:
m_lstMediaFile.append("./sound/e3.wav");
break;
}
//打印播放声音列表
// for(int i=0;i<m_lstMediaFile.size();i++)
// {
// qDebug()<<m_lstMediaFile[i];
// }
playMedia();
}
//科学计数转化为普通计数 0.003e+02 -> 0.3
void scientificCountingToOrdinaryCount(QString &num)
{
//e的位置
int e_pos;
int movePointSize=0; //需要移动小数点的位数
int pointBackCharSize=0; //小数点后非0数字的个数
QString str_0="0000000000000000"; //用来补0
e_pos=num.indexOf("e");
//若是科学计数法要处理,不是科学计数法则不需要处理
if(e_pos!=-1)
{
//读取移动位数
movePointSize=(num.mid(e_pos+2,2)).toInt();
//判断e后面的符号 小数点右移 决定是*10^n 还是*10^(-n)
if(num[e_pos+1]=='+')
{
//去除科学计数法e后的数字
num=num.mid(0,e_pos);
//移除小数点
if(num[0]=='-')
{
//小数点后有效数字的信息
pointBackCharSize=num.size()-3;
num=num.remove(2,1);
//小数点多余
if(movePointSize>pointBackCharSize)
{
num=num.insert(num.size(),str_0.mid(0,movePointSize-pointBackCharSize));
}
//如果需要小数点
if(movePointSize<pointBackCharSize)
{
num=num.insert(2+movePointSize,".");
}
while(num[1]=='0')
{
num.remove(1,1);
}
if(num[1]=='.')
{
num.insert(1,"0");
}
}
else
{
//小数点后有效数字的信息
pointBackCharSize=num.size()-2;
num=num.remove(1,1);
//小数点多余
if(movePointSize>pointBackCharSize)
{
num=num.insert(num.size(),str_0.mid(0,movePointSize-pointBackCharSize));
}
//如果需要小数点
if(movePointSize<pointBackCharSize)
{
num=num.insert(1+movePointSize,".");
}
//去0操作
while(num[0]=='0')
{
num.remove(0,1);
}
if(num[0]=='.')
{
num.insert(0,"0");
}
}
}
//小数点左移情况 1.333333e-05
else if(num[e_pos+1]=='-')
{
//去除科学计数法e后的数字
num=num.mid(0,e_pos);
//移除小数点
if(num[0]=='-')
{
num=num.remove(2,1);
//前面补0操作
num.insert(1,str_0.mid(0,movePointSize));
//写入小数点
num.insert(2,".");
}
else
{
num=num.remove(1,1);
//前面补0操作
num.insert(0,str_0.mid(0,movePointSize));
//写入小数点
num.insert(1,".");
}
}
qDebug()<<"num"<<num;
}
}
#endif // SOUND_CPP
main.c
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
showfile.ui
mainwindow.ui
标签:sound,clicked,QT,void,return,ui,同款,UI,include From: https://blog.csdn.net/m0_72812391/article/details/143078131