用qt写一个计算器代码包含加减乘除,括号,sin函数,cos函数,清空,删除操作
时间: 2023-11-08 15:03:54 浏览: 162
好的,以下是一个简单的基于Qt的计算器,包含加减乘除、括号、sin函数、cos函数、清空和删除操作:
```cpp
#include <QtWidgets>
class Calculator : public QWidget
{
public:
Calculator(QWidget *parent = 0);
private:
QLineEdit *display;
QPushButton *buttons[20];
QString calcVal;
bool hasOperator;
bool hasOperand;
bool isDecimal;
double calcResult;
enum { NumDigitButtons = 10 };
QPushButton *digitButtons[NumDigitButtons];
void createButtons();
void connectButtons();
double calculate(QString operation);
void clear();
void del();
void evaluate();
};
Calculator::Calculator(QWidget *parent)
: QWidget(parent), hasOperator(false), hasOperand(false), isDecimal(false), calcResult(0.0)
{
display = new QLineEdit("0");
display->setReadOnly(true);
display->setAlignment(Qt::AlignRight);
display->setMaxLength(15);
QPalette palette;
palette.setColor(QPalette::Base, Qt::black);
palette.setColor(QPalette::Text, Qt::white);
display->setPalette(palette);
QFont font("Courier", 18, QFont::Bold);
display->setFont(font);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(display);
createButtons();
for (int i = 0; i < 4; ++i) {
QHBoxLayout *row = new QHBoxLayout;
for (int j = 0; j < 5; ++j) {
row->addWidget(buttons[i * 5 + j]);
}
layout->addLayout(row);
}
setLayout(layout);
setWindowTitle(tr("Calculator"));
}
void Calculator::createButtons()
{
for (int i = 0; i < NumDigitButtons; ++i) {
digitButtons[i] = createButton(QString::number(i), SLOT(digitClicked()));
}
buttons[0] = createButton(tr("sin"), SLOT(sinClicked()));
buttons[1] = createButton(tr("cos"), SLOT(cosClicked()));
buttons[2] = createButton(tr("("), SLOT(openParenClicked()));
buttons[3] = createButton(tr(")"), SLOT(closeParenClicked()));
buttons[4] = createButton(tr("C"), SLOT(clear()));
buttons[5] = createButton(tr("7"), SLOT(digitClicked()));
buttons[6] = createButton(tr("8"), SLOT(digitClicked()));
buttons[7] = createButton(tr("9"), SLOT(digitClicked()));
buttons[8] = createButton(tr("/"), SLOT(operatorClicked()));
buttons[9] = createButton(tr("del"), SLOT(del()));
buttons[10] = createButton(tr("4"), SLOT(digitClicked()));
buttons[11] = createButton(tr("5"), SLOT(digitClicked()));
buttons[12] = createButton(tr("6"), SLOT(digitClicked()));
buttons[13] = createButton(tr("*"), SLOT(operatorClicked()));
buttons[14] = createButton(tr("sqrt"), SLOT(sqrtClicked()));
buttons[15] = createButton(tr("1"), SLOT(digitClicked()));
buttons[16] = createButton(tr("2"), SLOT(digitClicked()));
buttons[17] = createButton(tr("3"), SLOT(digitClicked()));
buttons[18] = createButton(tr("-"), SLOT(operatorClicked()));
buttons[19] = createButton(tr("="), SLOT(evaluate()));
}
QPushButton *Calculator::createButton(const QString &text, const char *member)
{
QPushButton *button = new QPushButton(text);
button->setFont(QFont("Courier", 18, QFont::Bold));
connect(button, SIGNAL(clicked()), this, member);
return button;
}
void Calculator::connectButtons()
{
for (int i = 0; i < NumDigitButtons; ++i) {
connect(digitButtons[i], SIGNAL(clicked()), this, SLOT(digitClicked()));
}
}
void Calculator::digitClicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
int digitValue = clickedButton->text().toInt();
if (isDecimal) {
calcVal.append(QString::number(digitValue));
display->setText(calcVal);
} else {
if (calcVal == "0") {
if (digitValue == 0) {
return;
} else {
calcVal = clickedButton->text();
display->setText(calcVal);
hasOperand = true;
}
} else {
calcVal.append(QString::number(digitValue));
display->setText(calcVal);
hasOperand = true;
}
}
}
void Calculator::operatorClicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
QString clickedOperator = clickedButton->text();
if (hasOperator && hasOperand) {
calcResult = calculate(clickedOperator);
calcVal = QString::number(calcResult);
display->setText(calcVal);
hasOperator = true;
hasOperand = false;
isDecimal = false;
} else {
calcResult = calcVal.toDouble();
calcVal.clear();
hasOperator = true;
hasOperand = false;
isDecimal = false;
}
}
void Calculator::openParenClicked()
{
calcVal.append("(");
display->setText(calcVal);
}
void Calculator::closeParenClicked()
{
calcVal.append(")");
display->setText(calcVal);
}
void Calculator::sinClicked()
{
calcResult = sin(calcVal.toDouble());
calcVal = QString::number(calcResult);
display->setText(calcVal);
hasOperand = true;
}
void Calculator::cosClicked()
{
calcResult = cos(calcVal.toDouble());
calcVal = QString::number(calcResult);
display->setText(calcVal);
hasOperand = true;
}
void Calculator::sqrtClicked()
{
calcResult = sqrt(calcVal.toDouble());
calcVal = QString::number(calcResult);
display->setText(calcVal);
hasOperand = true;
}
void Calculator::clear()
{
calcVal = "0";
calcResult = 0.0;
display->setText(calcVal);
hasOperator = false;
hasOperand = false;
isDecimal = false;
}
void Calculator::del()
{
if (calcVal.length() > 0) {
calcVal.chop(1);
display->setText(calcVal);
}
}
void Calculator::evaluate()
{
if (hasOperand && hasOperator) {
calcResult = calculate("");
calcVal = QString::number(calcResult);
display->setText(calcVal);
hasOperator = false;
hasOperand = false;
isDecimal = false;
}
}
double Calculator::calculate(QString operation)
{
double operand = calcVal.toDouble();
if (operation == "+") {
return calcResult + operand;
} else if (operation == "-") {
return calcResult - operand;
} else if (operation == "*") {
return calcResult * operand;
} else if (operation == "/") {
if (operand == 0.0) {
QMessageBox::warning(this, tr("Error"), tr("Division by zero!"));
clear();
return 0.0;
} else {
return calcResult / operand;
}
} else if (operation == "") {
QString lastOperator = calcVal.right(1);
if (lastOperator == "+") {
return calcResult + operand;
} else if (lastOperator == "-") {
return calcResult - operand;
} else if (lastOperator == "*") {
return calcResult * operand;
} else if (lastOperator == "/") {
if (operand == 0.0) {
QMessageBox::warning(this, tr("Error"), tr("Division by zero!"));
clear();
return 0.0;
} else {
return calcResult / operand;
}
}
}
return 0.0;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Calculator calc;
calc.show();
return app.exec();
}
```
这个计算器支持双精度浮点数计算,包含加减乘除、括号、sin函数、cos函数、清空和删除操作。界面使用了Qt的布局管理器,可以适应不同的窗口大小。
阅读全文