Qt版科學計算器

2021-06-28 20:36:02 字數 4325 閱讀 1101

qt版科學計算器

之前做過《vc版科學計算器》,這也是我學vc++時的第乙個大作業,通過科學計算器的開發使用我學到了很多東西,也讓我逐漸喜歡上了程式設計。最近在學習qt,所以將當時在vc下寫過的一些東西在qt下重寫了一遍,其實主要還是與顯示等有關的東西需要重寫,要使用qt的顯示方式,而其他的核心的演算法等都還是使用vc下c++的原始碼。

下面是qt版的執行截圖:

標準版:

科學版:

標頭檔案中變數和槽的宣告:

class calculatorscientific : public qmainwindow

;

牟尼的專欄

訊號和槽的關聯**:

//numberkey

connect(ui->pushbutton_zero, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_one, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_two, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_three, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_four, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_five, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_six, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_seven, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_eight, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_nine, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_a, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_b, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_c, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_d, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_e, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_f, signal(clicked()), this, slot(digitclicked()));

connect(ui->pushbutton_pi, signal(clicked()), this, slot(digitclicked()));

//operatorkey

connect(ui->pushbutton_add, signal(clicked()), this, slot(additiveoperatorclicked()));

connect(ui->pushbutton_sub, signal(clicked()), this, slot(additiveoperatorclicked()));

connect(ui->pushbutton_mul, signal(clicked()), this, slot(multiplicativeoperatorclicked()));

connect(ui->pushbutton_div, signal(clicked()), this, slot(multiplicativeoperatorclicked()));

connect(ui->pushbutton_mod, signal(clicked()), this, slot(additiveoperatorclicked()));

connect(ui->pushbutton_and, signal(clicked()), this, slot(additiveoperatorclicked()));

connect(ui->pushbutton_xor, signal(clicked()), this, slot(additiveoperatorclicked()));

connect(ui->pushbutton_x_y, signal(clicked()), this, slot(additiveoperatorclicked()));

再看一下計算函式的實現**:

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

else if (pendingoperator == tr("-")) else if (pendingoperator == tr("*")) else if (pendingoperator == tr("/")) else if (pendingoperator == tr("%")) else if (pendingoperator == tr("and")) else if (pendingoperator == tr("xor")) else if (pendingoperator == tr("x^y"))

return true;

}

單目運算:

void calculatorscientific::unaryoperatorclicked()

result = sqrt(operand);

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

result = 1.0 / operand;

} else if (clickedoperator == tr("ln"))

result = log(operand);

} else if (clickedoperator == tr("log"))

result = log10(operand);

} else if (clickedoperator == tr("sin")) else if (clickedoperator == tr("sinh")) else if (clickedoperator == tr("cos")) else if (clickedoperator == tr("cosh")) else if (clickedoperator == tr("tan")) else if (clickedoperator == tr("tanh")) else if (clickedoperator == tr("n!"))

result = 1;

for(int i = 1; i <= (int)operand; i++)

result *= i;

} else if (clickedoperator == tr("e^x")) else if (clickedoperator == tr("10^x")) else if (clickedoperator == tr("int")) else if (clickedoperator == tr("not"))

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

waitingforoperand = true;

}

牟尼的專欄

QT實現計算器

1 新建calculatorstandard類和工程檔案 2 使用水平布局和把按鍵依次新增 ui介面如下圖所示 3 在calculatorstandard.h中宣告用到的槽函式 private slots void digitclicked 數字按鍵 void addandsubclicked 加減...

QT專案 計算器

在這裡 有不是很詳盡的注釋 以前曾經寫過一篇部落格,是給乙個表示式來求值,是在控制台上執行的。這段時間,稍微的學習一下qt,參照windows自帶的計算器擁有的功能,並利用以前的部分 實現了乙個帶介面的計算器。功能和使用方法與windows保持一致。需要處理的一些情況 當連續按下兩個不同的符號 如 ...

Qt實現 計算器

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