void MainWindow::drawFunction() { QString expression = ui->expressionEdit->text(); if (expression.isEmpty()) return; QCustomPlot *plot = ui->plotWidget; plot->addGraph(); plot->graph()->setPen(QPen(Qt::blue)); plot->graph()->setName(expression); double xMin = -10, xMax = 10, yMin = -10, yMax = 10; plot->xAxis->setRange(xMin, xMax); plot->yAxis->setRange(yMin, yMax); QVector<double> xValues, yValues; for (double x = xMin; x <= xMax; x += 0.1) { double y = calculate(expression, x); xValues.append(x); yValues.append(y); } plot->graph()->setData(xValues, yValues); plot->replot(); } void MainWindow::clearPlot() { QCustomPlot *plot = ui->plotWidget; plot->clearGraphs(); plot->replot(); }这是一段将函数输入框里的函数解析然后使用qcustomplot进行绘图的代码,帮我改进一下(变量名称不要改变),使其能够支持幂函数、对数函数、指数函数、三角函数的图像绘制
时间: 2023-12-10 08:41:03 浏览: 151
Brackets-MainWindow:永远不会忘记主窗口!
可以通过添加不同的函数解析和计算来实现对幂函数、对数函数、指数函数、三角函数的支持。下面是修改后的代码:
```cpp
void MainWindow::drawFunction() {
QString expression = ui->expressionEdit->text();
if (expression.isEmpty()) return;
QCustomPlot *plot = ui->plotWidget;
plot->addGraph();
plot->graph()->setPen(QPen(Qt::blue));
plot->graph()->setName(expression);
double xMin = -10, xMax = 10, yMin = -10, yMax = 10;
plot->xAxis->setRange(xMin, xMax);
plot->yAxis->setRange(yMin, yMax);
QVector<double> xValues, yValues;
for (double x = xMin; x <= xMax; x += 0.1) {
double y = calculate(expression, x);
xValues.append(x);
yValues.append(y);
}
plot->graph()->setData(xValues, yValues);
plot->replot();
}
double MainWindow::calculate(QString expression, double x) {
// Parse the expression and calculate the result based on the function type
QRegularExpression re("([a-z]+)\\(([^\\)]+)\\)");
QRegularExpressionMatch match = re.match(expression);
if (match.hasMatch()) {
QString functionName = match.captured(1);
QString argument = match.captured(2);
if (functionName == "pow") {
QStringList args = argument.split(",");
if (args.size() != 2) return 0;
double base = args[0].toDouble();
double exponent = args[1].toDouble();
return std::pow(base, exponent);
} else if (functionName == "log") {
QStringList args = argument.split(",");
if (args.size() != 2) return 0;
double base = args[0].toDouble();
double value = args[1].toDouble();
return std::log(value) / std::log(base);
} else if (functionName == "exp") {
double exponent = argument.toDouble();
return std::exp(exponent);
} else if (functionName == "sin") {
double angle = argument.toDouble();
return std::sin(angle);
} else if (functionName == "cos") {
double angle = argument.toDouble();
return std::cos(angle);
} else if (functionName == "tan") {
double angle = argument.toDouble();
return std::tan(angle);
}
}
// If the expression is not a function, evaluate it as a simple math expression
QScriptEngine engine;
engine.evaluate("x = " + QString::number(x) + ";");
QScriptValue result = engine.evaluate(expression);
return result.toNumber();
}
void MainWindow::clearPlot() {
QCustomPlot *plot = ui->plotWidget;
plot->clearGraphs();
plot->replot();
}
```
在 `calculate` 函数中,使用正则表达式匹配函数名和参数,并根据不同的函数类型进行计算。对于非函数类型的表达式,直接使用 `QScriptEngine` 进行计算。这样,就可以支持幂函数、对数函数、指数函数、三角函数的图像绘制了。
阅读全文