QT實現計算器

2021-07-24 12:30:12 字數 3068 閱讀 4214

1、新建calculatorstandard類和工程檔案

2、使用水平布局和把按鍵依次新增

ui介面如下圖所示:

3、在calculatorstandard.h中宣告用到的槽函式

private slots:

void digitclicked ();//數字按鍵

void addandsubclicked();//加減按鍵

void mulandderivativeclicked();//乘除按鍵

void equalclicked();//等號按鍵

void clearall();//清楚按鍵

void clear();

void backspaceclicked();//空格按鍵

void pointclicked();//小數點按鍵

void mcclicked();//清除記憶資料

void mrclicked();//新增記憶資料

void msclicked();//讀取記憶資料

void mplusclicked();//清除記憶資料

void plusminusclicked();//加減按鍵

void otheroperatorclicked();//開方。平方,求導數等

4、在calculatorstandard.cpp中實現槽函式

void calculatorstandard::digitclicked()

if (waitingforoperand)

ui->lineedit_display->settext(ui->lineedit_display->text() + qstring::number(digitvalue));

}void calculatorstandard::unaryoperatorclicked()

result = sqrt(operand);

} else if (clickedoperator == tr("x^2")) else if (clickedoperator == tr("1/x"))

result = 1.0 / operand;

}ui->lineedit_display->settext(qstring::number(result));

waitingforoperand = true;

}void calculatorstandard::additiveoperatorclicked()

qdebug()<< pendingadditiveoperator.isempty() settext(qstring::number(sumsofar));

} else

pendingadditiveoperator = clickedoperator;

waitingforoperand = true;

}void calculatorstandard::multiplicativeoperatorclicked()

ui->lineedit_display->settext(qstring::number(factorsofar));

} else

pendingmultiplicativeoperator = clickedoperator;

waitingforoperand = true;

}void calculatorstandard::equalclicked()

operand = factorsofar;

factorsofar = 0.0;

pendingmultiplicativeoperator.clear();

}if (!pendingadditiveoperator.isempty())

pendingadditiveoperator.clear();

} else

ui->lineedit_display->settext(qstring::number(sumsofar));

sumsofar = 0.0;

waitingforoperand = true;

}void calculatorstandard::pointclicked()

void calculatorstandard::changesignclicked()

else if (value < 0.0)

ui->lineedit_display->settext(text);

}void calculatorstandard::backspaceclicked()

ui->lineedit_display->settext(text);

}void calculatorstandard::clear()

void calculatorstandard::clearall()

void calculatorstandard::clearmemory()

void calculatorstandard::readmemory()

void calculatorstandard::setmemory()

void calculatorstandard::addtomemory()

void calculatorstandard::abortoperation()

bool calculatorstandard::calculate(double rightoperand, const qstring &pendingoperator)

else if (pendingoperator == tr("-")) else if (pendingoperator == tr("*")) else if (pendingoperator == tr("/"))

return true;

}5、執行結果如下圖所示

Qt實現 計算器

該計算器主要通過lineedit獲取和顯示數字,通過tableview顯示計算過程。通過tableview顯示計算的過程,qlineedit獲取輸入的數字,qcombobox實現不同的計算型別。效果 計算 void mainwindow on btn cal clicked else if resu...

Qt 計算器的實現

介面的 實現 include widget.h include include include w show w setfixedsize w width w height ret a.exec delete w return ret 執行的結果如圖所示 qt 計算器的實現 上面只是暫時生成了計算機...

QT實現簡易計算器

定義a用來儲存lineedit顯示內容 定義b用來儲存運算子等 ifndef widget h define widget h include qt begin namespace namespace ui qt end namespace class widget public qwidget e...