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进行绘图的代码,帮我改进一下(变量名称不要改变),使其能够支持一元一次函数,幂函数、对数函数、指数函数、三角函数的图像绘制(sin(x)+1这种也要有),要求代码都写在同一个槽函数、
时间: 2023-12-10 09:42:14 浏览: 62
可以使用 switch 语句来判断输入的表达式类型,并根据不同的类型进行相应的计算和绘制。以下是修改后的代码:
```
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;
switch (getExpressionType(expression)) {
case LINEAR:
y = calculateLinear(expression, x);
break;
case POWER:
y = calculatePower(expression, x);
break;
case LOGARITHM:
y = calculateLogarithm(expression, x);
break;
case EXPONENTIAL:
y = calculateExponential(expression, x);
break;
case TRIGONOMETRY:
y = calculateTrigonometry(expression, x);
break;
default:
break;
}
xValues.append(x);
yValues.append(y);
}
plot->graph()->setData(xValues, yValues);
plot->replot();
}
void MainWindow::clearPlot() {
QCustomPlot *plot = ui->plotWidget;
plot->clearGraphs();
plot->replot();
}
int MainWindow::getExpressionType(QString expression) {
if (expression.contains("x", Qt::CaseInsensitive)) {
if (expression.contains("^")) {
return POWER;
} else if (expression.contains("log", Qt::CaseInsensitive)) {
return LOGARITHM;
} else if (expression.contains("exp", Qt::CaseInsensitive)) {
return EXPONENTIAL;
} else if (expression.contains("sin", Qt::CaseInsensitive) || expression.contains("cos", Qt::CaseInsensitive) || expression.contains("tan", Qt::CaseInsensitive)) {
return TRIGONOMETRY;
} else {
return LINEAR;
}
} else {
return -1; // Not a valid expression
}
}
double MainWindow::calculateLinear(QString expression, double x) {
QRegExp re("([+-]?\\d+(?:\\.\\d+)?)\\s*\\*?\\s*x\\s*([+-]\\s*\\d+(?:\\.\\d+)?)?");
if (re.indexIn(expression) != -1) {
double a = re.cap(1).toDouble();
double b = (re.captureCount() == 2) ? re.cap(2).toDouble() : 0;
return a * x + b;
} else {
return 0;
}
}
double MainWindow::calculatePower(QString expression, double x) {
QRegExp re("([+-]?\\d+(?:\\.\\d+)?)\\s*\\*?\\s*x\\s*\\^\\s*([+-]?\\d+(?:\\.\\d+)?)");
if (re.indexIn(expression) != -1) {
double a = re.cap(1).toDouble();
double b = re.cap(2).toDouble();
return qPow(x, b) * a;
} else {
return 0;
}
}
double MainWindow::calculateLogarithm(QString expression, double x) {
QRegExp re("log\\s*\\(\\s*([+-]?\\d+(?:\\.\\d+)?)\\s*\\*?\\s*x\\s*\\+?\\s*([+-]\\s*\\d+(?:\\.\\d+)?)?\\s*\\)");
if (re.indexIn(expression) != -1) {
double a = re.cap(1).toDouble();
double b = (re.captureCount() == 3) ? re.cap(2).toDouble() : 0;
return qLn(a * x + b) / qLn(10.0);
} else {
return 0;
}
}
double MainWindow::calculateExponential(QString expression, double x) {
QRegExp re("exp\\s*\\(\\s*([+-]?\\d+(?:\\.\\d+)?)\\s*\\*?\\s*x\\s*\\+?\\s*([+-]\\s*\\d+(?:\\.\\d+)?)?\\s*\\)");
if (re.indexIn(expression) != -1) {
double a = re.cap(1).toDouble();
double b = (re.captureCount() == 3) ? re.cap(2).toDouble() : 0;
return qExp(a * x + b);
} else {
return 0;
}
}
double MainWindow::calculateTrigonometry(QString expression, double x) {
if (expression.contains("sin", Qt::CaseInsensitive)) {
QRegExp re("sin\\s*\\(\\s*([+-]?\\d+(?:\\.\\d+)?)\\s*\\*?\\s*x\\s*\\+?\\s*([+-]\\s*\\d+(?:\\.\\d+)?)?\\s*\\)");
if (re.indexIn(expression) != -1) {
double a = re.cap(1).toDouble();
double b = (re.captureCount() == 3) ? re.cap(2).toDouble() : 0;
return qSin(a * x + b);
}
} else if (expression.contains("cos", Qt::CaseInsensitive)) {
QRegExp re("cos\\s*\\(\\s*([+-]?\\d+(?:\\.\\d+)?)\\s*\\*?\\s*x\\s*\\+?\\s*([+-]\\s*\\d+(?:\\.\\d+)?)?\\s*\\)");
if (re.indexIn(expression) != -1) {
double a = re.cap(1).toDouble();
double b = (re.captureCount() == 3) ? re.cap(2).toDouble() : 0;
return qCos(a * x + b);
}
} else if (expression.contains("tan", Qt::CaseInsensitive)) {
QRegExp re("tan\\s*\\(\\s*([+-]?\\d+(?:\\.\\d+)?)\\s*\\*?\\s*x\\s*\\+?\\s*([+-]\\s*\\d+(?:\\.\\d+)?)?\\s*\\)");
if (re.indexIn(expression) != -1) {
double a = re.cap(1).toDouble();
double b = (re.captureCount() == 3) ? re.cap(2).toDouble() : 0;
return qTan(a * x + b);
}
}
return 0;
}
```
其中,`getExpressionType()` 函数用于判断输入的表达式类型,返回值为以下常量之一:
```
enum ExpressionType {
LINEAR,
POWER,
LOGARITHM,
EXPONENTIAL,
TRIGONOMETRY
};
```
`calculateLinear()` 函数用于计算一元一次函数,`calculatePower()` 函数用于计算幂函数,`calculateLogarithm()` 函数用于计算对数函数,`calculateExponential()` 函数用于计算指数函数,`calculateTrigonometry()` 函数用于计算三角函数。这些函数都接受一个表达式和一个自变量 x,返回相应的函数值 y。注意,对于三角函数,只支持 sin、cos、tan 三种基本函数,不支持其它形式,例如 sin(2x)、cos(x+1) 等。
阅读全文