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 15:41:12 浏览: 32
可以在这段代码中添加函数解析的部分,并根据不同的函数类型进行不同的处理。以下是一个可能的改进:
```c++
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 = 0;
QString functionName;
QStringList functionArgs;
if (parseFunction(expression, x, y, functionName, functionArgs)) {
xValues.append(x);
yValues.append(y);
}
}
plot->graph()->setData(xValues, yValues);
plot->replot();
}
void MainWindow::clearPlot() {
QCustomPlot *plot = ui->plotWidget;
plot->clearGraphs();
plot->replot();
}
bool MainWindow::parseFunction(const QString &expression, double x, double &y, QString &functionName, QStringList &functionArgs) {
const QStringList functions = {"sin", "cos", "tan", "log", "exp", "pow"};
foreach (const QString &function, functions) {
if (expression.toLower().startsWith(function + "(")) {
int endIndex = expression.indexOf(')', function.length() + 1);
if (endIndex == -1) return false;
QString argsStr = expression.mid(function.length() + 1, endIndex - function.length() - 1);
functionArgs = argsStr.split(',');
for (int i = 0; i < functionArgs.size(); ++i) {
functionArgs[i] = functionArgs[i].trimmed();
}
if (functionArgs.isEmpty()) return false;
functionName = function;
break;
}
}
if (functionName.isEmpty()) {
// Parse regular expression
y = calculate(expression, x);
return true;
}
if (functionName == "sin") {
if (functionArgs.size() != 1) return false;
y = sin(functionArgs[0].toDouble());
} else if (functionName == "cos") {
if (functionArgs.size() != 1) return false;
y = cos(functionArgs[0].toDouble());
} else if (functionName == "tan") {
if (functionArgs.size() != 1) return false;
y = tan(functionArgs[0].toDouble());
} else if (functionName == "log") {
if (functionArgs.size() != 1) return false;
y = log10(functionArgs[0].toDouble());
} else if (functionName == "exp") {
if (functionArgs.size() != 1) return false;
y = exp(functionArgs[0].toDouble());
} else if (functionName == "pow") {
if (functionArgs.size() != 2) return false;
y = pow(functionArgs[0].toDouble(), functionArgs[1].toDouble());
}
return true;
}
```
这里我们添加了一个 `parseFunction` 函数,用于解析函数并计算出对应的 y 值。该函数首先检查表达式是否以支持的函数名开头,如果是,则解析出函数名和参数,并根据不同的函数类型计算对应的 y 值。如果表达式不是以函数名开头,则直接计算表达式的值。
需要注意的是,这个改进并不完整,还有很多需要改进的地方,比如对输入的函数进行更严格的检查、支持更多的函数类型等等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)