【发布时间】:2014-02-24 12:05:47
【问题描述】:
所以我正在尝试创建一个计算曲线下面积的主函数。我有三个独立的函数,每个函数都以不同的方式计算面积:函数 1 使用梯形规则计算,函数 2 使用辛普森规则计算,函数 3 使用高斯求积计算。我让每个单独的程序都运行良好,现在正试图将它们从 main 更改为要从名为“numericalIntegration”的单独 main 调用的函数。
到目前为止,我的主要工作是:
#include <math.h>
#include <stdlib.h>
#define pi 3.1415927;
void TrapezoidRule(float area);
void SimpsonsRule(float area);
void GaussQuadrature(float area);
int main() {
int userInput, N;
float area, error;
printf("Choose which method to use to calculate the area of the function sin(x) from 0 to pi:\n");
printf("Enter 1 to use the Trapezoid Rule, enter 2 to use Simpson's Rule, enter 3 to use Gauss' Quadrature.\n");
scanf("%d", &userInput);
printf("\nEnter the number of intervals to use to calculate the area.\n");
scanf("%d", &N);
if (userInput == 1) { //Call Trapezoid rule function
TrapezoidRule(area);
}
if (userInput == 2) {
SimpsonsRule(area);
}
if (userInput == 3) {
GaussQuadrature(area);
}
//Print the area calculated using the chosen method
return 0;
}
如果需要,我将包含三个单独的函数,但为了使这篇文章更短,我现在将它们排除在外。它们各自被称为:
void TrapezoidRule(float area) {
void GaussQuadrature(float area) {
void SimpsonsRule(float area) {
我在尝试编译时遇到一个错误
(使用icc -o num numericalIntegration.c GaussQuadrature.c TrapezoidRule.c SimpsonsRule.c)
是梯形规则和辛普森规则都在其中使用一个小函数将度数转换为弧度,我猜这在 numericIntegration 中没有被正确调用。
以下是我的具体问题:
- 此方法在调用主函数时是否正确?
- 我应该将弧度角函数作为一个头文件包含在主文件中吗?我不知道该怎么做...
更新:我收到的错误是: 第一个错误:“'degtorad' 的多重定义” 第二个错误:首先在这里定义
我想我明白我需要将函数 degtorad 设为头文件,但我不知道该怎么做?
【问题讨论】:
-
不要用散文描述错误信息。按照编译器生成的 100% 剪切和粘贴它。它是否说 unresolved symbol 之类的东西?
-
不需要定义
pi,<math.h>已经有M_PI- 至少应该。 -
M_PI不是标准的。 -
@drahnr
M_PI既不是 C89,也不是 C99,也不是 C11。 -
你说得对,它不是 ISO C