有问题可以加我QQ:2273864757 进行交流学习!!!!
整个计算器设计整体按照“插件式,微内核”进行开发,里面包含各个功能的插件,将这些插件集成到我们的主工程之上。
首先,展示相关的功能截图:
此计算器包含基本的加减乘除运算之外,还包含有大整数计算工具,线代计算工具,多项式求解等计算工具,可以在工具下拉菜单里进行选择:
管理器可以记录我们输入的历史记录,常规菜单可以对整个界面的语言和背景颜色等等进行设置:
整体界面设置通过读取配置文件进行整体界面的搭建,属性设置也通过保存到配置文件中进行存储,方便下次打开后使用上次保存的具体信息:
void Calculator::readSettings()
{
QSettings settings;
//The QSettings constructor takes arguments that identify your company and the name of the product.
//This ensures that the settings for different applications are kept separately.
//调整窗口的位置
QPoint pos = settings.value("pos", QPoint(350, 200)).toPoint();
move(pos);
//设置主窗口是否置顶
if(settings.value("TopHint").toBool())
{
topHintAction->setChecked(true);
setWindowFlags(Qt::WindowStaysOnTopHint);
}
if(settings.value("language").toBool())
{
actionChinese->setChecked(false);
actionEnglish->setChecked(true);
}
//设置背景颜色
isChangeColor=false;
QPalette bgpal;
int red,green,blue;
red=settings.value("red/window",152).toInt();
green=settings.value("green/window",204).toInt();
blue=settings.value("blue/window",222).toInt();
bgpal.setColor(QPalette::Background, QColor(red,green,blue));
setPalette(bgpal);
red=settings.value("red/textview",206).toInt();
green=settings.value("green/textview",224).toInt();
blue=settings.value("blue/textview",255).toInt();
bgpal.setColor(QPalette::Base,QColor(red,green,blue));
express_lineEdit->setPalette(bgpal);
result_lineEdit->setPalette(bgpal);
memory_lineEdit->setPalette(bgpal);
//设置字体的大小
isChangeFont=false;
QFont font;
QString sizeStr;
font.fromString(settings.value("Font","Arial,20,-1,5,50,0,0,0,0,0").toString());
sizeStr=QString("font-size : %1px").arg(font.pointSize());
express_lineEdit->setFont(font);
result_lineEdit->setFont(font);
memory_lineEdit->setFont(font);
express_lineEdit->setStyleSheet(sizeStr);
result_lineEdit->setStyleSheet(sizeStr);
memory_lineEdit->setStyleSheet(sizeStr);
//设置变量
settings.setValue("Variable_X_Bool",false);
settings.setValue("Variable_Y_Bool",false);
settings.setValue("Variable_Z_Bool",false);
//设置历史记录
QStringList historyRecord = settings.value("historyRecord").toStringList();
historyIndex=historyRecord.size()-1;
if(historyIndex>=0)
{
undo_pushButton->setEnabled(true);
redo_pushButton->setEnabled(true);
}
else
{
undo_pushButton->setEnabled(false);
redo_pushButton->setEnabled(false);
}
}
void Calculator::writeSettings()
{
//保存窗口位置
QSettings settings;
settings.setValue("pos", pos());
settings.setValue("TopHint",topHintAction->isChecked());
if(isChangeColor)
{
QMessageBox::StandardButton ret;
ret = QMessageBox::question(this,"Calculator/2012/08",tr("Background color,do you want to save it?"),
QMessageBox::Save| QMessageBox::Cancel);
if (ret != QMessageBox::Save){
settings.setValue("red/window",152);
settings.setValue("green/window",204);
settings.setValue("blue/window",222);
settings.setValue("red/textview",206);
settings.setValue("green/textview",224);
settings.setValue("blue/textview",255);
}
}
if(isChangeFont)
{
QMessageBox::StandardButton ret;
ret = QMessageBox::question(this,"Calculator/2012/08",tr("Font has been changes,do you want to save it?"),
QMessageBox::Save| QMessageBox::Cancel);
if (ret != QMessageBox::Save){
QFont font("Arial",20);
settings.setValue("Font",font.toString());
}
}
}
void Calculator::changeLanguage(bool isChange)
{
if(isChange==false)
return ;
else
{
int ret=QMessageBox::information(this,tr("Language setting"),
tr("Changes to the language setting will not take effect until the environment is restarted!"),
QMessageBox::Cancel|QMessageBox::Ok);
QSettings settings;
bool isEnglish=settings.value("language").toBool();
if (ret == QMessageBox::Ok) {
if(isEnglish==true)
actionEnglish->setChecked(false);
else
actionChinese->setChecked(false);
settings.setValue("language",!isEnglish);
}
else
{
if(isEnglish)
actionChinese->setChecked(false);
else
actionEnglish->setChecked(false);
}
}
}
各插件功能的按钮点击事件部分代如下,实现代码太多,可以查看源代码进行了解
QObject::connect(aboutAction, SIGNAL(triggered()), Calculator, SLOT(about()));
QObject::connect(exitAction, SIGNAL(triggered()), Calculator, SLOT(close()));
QObject::connect(helpAction, SIGNAL(triggered()), Calculator, SLOT(help()));
QObject::connect(Dec_radioButton, SIGNAL(clicked()), Calculator, SLOT(decSystem()));
QObject::connect(Bin_radioButton, SIGNAL(clicked()), Calculator, SLOT(binSystem()));
QObject::connect(Oct_radioButton, SIGNAL(clicked()), Calculator, SLOT(octSystem()));
QObject::connect(Hex_radioButton, SIGNAL(clicked()), Calculator, SLOT(hexSystem()));
QObject::connect(numSeven_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumSeven()));
QObject::connect(numFive_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumFive()));
QObject::connect(numNine_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumNine()));
QObject::connect(numEight_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumEight()));
QObject::connect(numOne_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumOne()));
QObject::connect(numFour_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumFour()));
QObject::connect(numTwo_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumTwo()));
QObject::connect(numZero_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumZero()));
QObject::connect(numThree_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumThree()));
QObject::connect(numSix_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumSix()));
QObject::connect(point_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputPoint()));
QObject::connect(leftBack_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputLeftBack()));
QObject::connect(rightBack_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputRightBack()));
QObject::connect(operAdd_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperAdd()));
QObject::connect(operSubtract_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperSubtract()));
QObject::connect(operMultiply_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperMultiply()));
QObject::connect(operDivide_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperDivide()));
QObject::connect(operDiv_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperDiv()));
QObject::connect(operMod_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperMod()));
QObject::connect(operPercent_pushButton, SIGNAL(clicked(bool)), Calculator, SLOT(inputOperPercent()));
QObject::connect(operFact_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperFact()));
QObject::connect(operComb_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperComb()));
QObject::connect(operSqr_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperSqr()));
QObject::connect(operCub_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperCub()));
QObject::connect(operExpE_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperExpE()));
QObject::connect(operExp10_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperExp10()));
QObject::connect(operSqrt_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperSqrt()));
QObject::connect(operCur_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperCur()));
QObject::connect(operXYsqrt_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperXYsqrt()));
QObject::connect(fun_Sin_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunSin()));
QObject::connect(fun_Cos_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunCos()));
QObject::connect(fun_Tan_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunTan()));
QObject::connect(fun_Cot_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunCot()));
QObject::connect(fun_Arcsin_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunArcSin()));
QObject::connect(fun_Arccos_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunArcCos()));
QObject::connect(fun_Arctan_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunArcTan()));
QObject::connect(fun_Arccot_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunArcCot()));
QObject::connect(fun_Lg_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunLg()));
QObject::connect(fun_Ln_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunLn()));
QObject::connect(fun_Log_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunLog()));
QObject::connect(fun_Abs_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunAbs()));
QObject::connect(fun_Pow_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunPow()));
QObject::connect(undo_pushButton, SIGNAL(clicked()), Calculator, SLOT(undo()));
QObject::connect(clear_pushButton, SIGNAL(clicked()), Calculator, SLOT(clearExpress()));
QObject::connect(redo_pushButton, SIGNAL(clicked()), Calculator, SLOT(redo()));
QObject::connect(backspace_pushButton, SIGNAL(clicked()), Calculator, SLOT(backspace()));
QObject::connect(Complex_radioButton, SIGNAL(clicked()), Calculator, SLOT(complexSystem()));
QObject::connect(Real_radioButton, SIGNAL(clicked()), Calculator, SLOT(realSystem()));
QObject::connect(fold_pushButton, SIGNAL(clicked()), Calculator, SLOT(fold()));
QObject::connect(checkBox_1, SIGNAL(clicked()), Calculator, SLOT(hyperbolicSystem()));
QObject::connect(result_pushButton, SIGNAL(clicked()), Calculator, SLOT(getResult()));
QObject::connect(express_lineEdit, SIGNAL(textChanged(QString)), express_lineEdit, SLOT(setFocus()));
QObject::connect(ANS_pushButton, SIGNAL(clicked()), Calculator, SLOT(insertNewResult()));
QObject::connect(MS_pushButton, SIGNAL(clicked()), Calculator, SLOT(insertMemorySystem()));
QObject::connect(MR_pushButton, SIGNAL(clicked()), Calculator, SLOT(insertMemoryResult()));
QObject::connect(constEuler_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputConst_e()));
QObject::connect(constGravity_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputConst_g()));
QObject::connect(constPi_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputConst_pi()));
QObject::connect(historyAction, SIGNAL(triggered()), Calculator, SLOT(showHistory()));
QObject::connect(express_lineEdit, SIGNAL(returnPressed()), Calculator, SLOT(getResult()));
QObject::connect(HugeCalcAction, SIGNAL(triggered()), Calculator, SLOT(getHugeCalcWidget()));
QObject::connect(suggestAction, SIGNAL(triggered()), Calculator, SLOT(giveSuggestions()));
QObject::connect(spinBox, SIGNAL(valueChanged(int)), Calculator, SLOT(changePrecision()));
QObject::connect(linearAlgebraAction, SIGNAL(triggered()), Calculator, SLOT(getLinearAlgebraWidget()));
QObject::connect(noLinearAlgebraAction, SIGNAL(triggered()), Calculator, SLOT(getnonLinearAlgebraWidget()));
QObject::connect(NumI_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumI()));
QObject::connect(numA_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumA()));
QObject::connect(numB_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumB()));
QObject::connect(numC_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumC()));
QObject::connect(numD_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumD()));
QObject::connect(numE_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumE()));
QObject::connect(numF_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumF()));
QObject::connect(numEi_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputNumEi()));
QObject::connect(fun_Gcd_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunGcd()));
QObject::connect(fun_Lcm_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputFunLcm()));
QObject::connect(Geometers_Sketchpad_Action, SIGNAL(triggered()), Calculator, SLOT(ElseTools()));
QObject::connect(System_Calculator_Action, SIGNAL(triggered()), Calculator, SLOT(ElseTools()));
QObject::connect(actionChinese, SIGNAL(triggered(bool)), Calculator, SLOT(changeLanguage(bool)));
QObject::connect(actionEnglish, SIGNAL(triggered(bool)), Calculator, SLOT(changeLanguage(bool)));
QObject::connect(topHintAction, SIGNAL(triggered()), Calculator, SLOT(setTopHint()));
QObject::connect(actionWindows_Color, SIGNAL(triggered()), Calculator, SLOT(showWindowColorDialog()));
QObject::connect(actionDefault_Settings, SIGNAL(triggered()), Calculator, SLOT(defaultSettings()));
QObject::connect(actionExpression_s_Font, SIGNAL(triggered()), Calculator, SLOT(showFontDialog()));
QObject::connect(variableAction, SIGNAL(triggered()), Calculator, SLOT(showVariable()));
QObject::connect(constantAction, SIGNAL(triggered()), Calculator, SLOT(showConstant()));
QObject::connect(actionTextViews_Color, SIGNAL(triggered()), Calculator, SLOT(showTextViewColorDialog()));
QObject::connect(operPower_pushButton, SIGNAL(clicked()), Calculator, SLOT(inputOperPower()));
result_pushButton->setDefault(false);
undo_pushButton->setDefault(false);
部分多项式求解的算法如下:
Polynomial::Polynomial(int nn,Complex *a)
{
n=nn;
p= new Complex [n+1];
for(int i=0;i<=n;i++)
p[i] = a[i];
}
Polynomial::Polynomial(const Polynomial &poly)
{
n=poly.n;
p= new Complex[n+1];
for(int i=0;i<=n;i++)
p[i] = poly.p[i];
}
Polynomial & Polynomial::operator = (Polynomial Obj)
{
n=Obj.n;
p= new Complex[n+1];
for(int i=0;i<=n;i++)
p[i] = Obj.p[i];
return *this;
}
Complex Polynomial::poly_value(const Complex &x) //多项式求值p(x)
{
Complex value;
value = p[0];
for(int k=1; k<=n; k++)
value = value*x + p[k];
return value;
}
void Polynomial::poly_reduce(const Complex &root)
{
for(int i=1;iR)
R=p[i].cfabs();
}
R=R+1;
for(int i=0;i<=n;i++)
{
p[i]*=pow(R,n-i);
}
return R;
}
double Polynomial::fitness(Complex x)
{
return 1/(1+poly_value(x).cfabs());
}