C文件输入/梯形规则程序

标签 c file input openmp

有点像 2 partner。首先,我试图在所有 c 中执行此操作。首先,我将继续发布我的程序

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include <string.h>

double f(double x);    
void Trap(double a, double b, int n, double* integral_p);

int main(int argc, char* argv[]) {

   double  integral=0.0;  //Integral Result
   double  a=6, b=10;   //Left and Right Points
   int    n;     //Number of Trapezoids (Higher=more accurate) 
   int degree;

  if (argc != 3) {
      printf("Error: Invalid Command Line arguements, format:./trapezoid N filename");
      exit(0);
   }
   n = atoi(argv[2]);

   FILE *fp = fopen( argv[1], "r" );

#  pragma omp parallel 
   Trap(a, b, n, &integral);
   printf("With n = %d trapezoids....\n", n);
   printf("of the integral from %f to %f = %.15e\n",a, b, integral);
   return 0;
}  

double f(double x) {
   double return_val;
   return_val = pow(3.0*x,5)+pow(2.5*x,4)+pow(-1.5*x,3)+pow(0*x,2)+pow(1.7*x,1)+4;
   return return_val;
}  
void Trap(double a, double b, int n, double* integral_p) {
   double  h, x, my_integral;
   double  local_a, local_b;
   int  i, local_n;
   int my_rank = omp_get_thread_num();
   int thread_count = omp_get_num_threads();

   h = (b-a)/n;
   local_n = n/thread_count;
   local_a = a + my_rank*local_n*h;
   local_b = local_a + local_n*h;
   my_integral = (f(local_a) + f(local_b))/2.0;
   for (i = 1; i <= local_n-1; i++) {
     x = local_a + i*h;
     my_integral += f(x);
   }
   my_integral = my_integral*h;

#  pragma omp critical
   *integral_p += my_integral;
}  

如您所见,它计算给定间隔的梯形规则。 首先,如果您对值和函数进行硬编码,它确实有效。但是我需要从

格式的文件中读取
5
3.0 2.5 -1.5 0.0 1.7 4.0
6 10

这意味着: 度数为 5(永远不超过 50) 3.0x^5 +2.5x^4 −1.5x^3 +1.7x+4 是多项式(我们跳过 ^2 因为它是 0) 间隔从 6 到 10

我主要关心的是我硬编码的 f(x) 函数。除了从字面上输入 50 个 POWS 并读取值以查看它们可能是什么之外,我不知道如何让它最多占用 50 个......其他人可能有任何想法吗?

此外,读取文件的最佳方式是什么? fgetc?我不太确定何时读取 C 输入(特别是因为我读取的所有内容都是 INT,是否有某种方法可以转换它们?)

最佳答案

对于大次数的多项式,这样的东西行得通吗?

double f(double x, double coeff[], int nCoeff)
{
    double return_val = 0.0;
    int exponent = nCoeff-1;

    int i;
    for(i=0; i<nCoeff-1; ++i, --exponent)
    {
        return_val = pow(coeff[i]*x, exponent) + return_val;
    }
    /* add on the final constant, 4, in our example */
    return return_val + coeff[nCoeff-1];  
}

在您的示例中,您可以这样调用它:

sampleCall()
{
    double coefficients[] = {3.0, 2.5, -1.5, 0, 1.7, 4};
    /* This expresses 3x^5 + 2.5x^4 + (-1.5x)^3 + 0x^2 + 1.7x + 4 */
    my_integral = f(x, coefficients, 6);
}

通过传递一组系数(假定指数),您不必处理可变参数。最难的部分是构造数组,这很简单。


不言而喻,如果将系数数组和系数个数放入全局变量中,则 f(x) 的签名无需更改:

double f(double x)
{
   // access glbl_coeff and glbl_NumOfCoeffs, instead of parameters
}

关于C文件输入/梯形规则程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4307401/

相关文章:

c++ - C 和 C++ 中整数/算术类型的大小保证

Python:确定一个对象是否类似于文件?

validation - SSIS 2005 如何以编程方式解决错误 "input column has lineage id that was not previously used in the data flow task"

java - 从/向路径读取和写入 CSV

c - 在 C 语言中使用 fopen 打开文件

c - 为什么C有下划线开头的关键字

c - 为什么这段代码不能识别任何 if 语句?

c++ - 如何迭代 IO Kit 的键?

python - 如何获取一个文件中包含另一个文件中的字符串(重复)的行?

php - $_FILES 不将名称发送到 MYSQL