【问题标题】:Can someone critique my C code on evaluating a definite integral? [closed]有人可以批评我的 C 代码评估定积分吗? [关闭]
【发布时间】:2020-03-01 13:41:41
【问题描述】:

下面的代码不处理任何数值积分或与之相关的任何事情——只是评估 Ax^2 + Bx + C 形式的定积分的基本规则。我很想听听你们的意见,因为我'我相对较新,仍在学习。
此编码练习旨在测试我们在过去几周学到的知识,因此可能不鼓励使用比以下关键字更复杂的内容以及其他标准库函数。

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

void inputDegree(int *deg) {
    printf("Enter the degree of the polynomial: ");
    scanf("%d", deg);
}

void inputCoeffs(int deg, double *coeffs) {
    printf("\nEnter the coefficients of the polynomial: \n\n");
    for(int i = 0; i <= deg; i++) {
        printf("Coefficient of degree %d: ", deg - i);
        scanf("%lf", &coeffs[i]);
    }
}

void inputLimits(double *lowerL, double *upperL) {
    printf("\nEnter the lower limit of integration: ");
    scanf("%lf", lowerL);
    printf("\nEnter the upper limit of integration: ");
    scanf("%lf", upperL);
}

void computeIntegralCoeffs(int deg, double *coeffs, double *integCoeffs) {
    for(int i = 0; i <= deg; i++) {
        integCoeffs[i] = coeffs[i] / (deg + 1 - i);
    }
}

double evaluateIntegral(int degree, double *integCoeffs, double lowerLimit, double upperLimit) {
    double lower = 1, upper = 1;
    for(int i = 0; i <= degree; i++) {
        lower += integCoeffs[i] * pow(lowerLimit, (degree + 1 - i));
    }
    for(int i = 0; i <= degree; i++) {
        upper += integCoeffs[i] * pow(upperLimit, (degree + 1 - i));
    }
    return upper - lower;
}

int main() {
    int degree;
    double lowerLimit;
    double upperLimit;
    double integral;
    double *coefficients = NULL;
    double *integralCoefficients = NULL;

    inputDegree(&degree);

    coefficients = (double *)malloc((degree + 1) * sizeof(double));
    integralCoefficients = (double *)malloc((degree + 1) * sizeof(double));

    inputCoeffs(degree, coefficients);

    inputLimits(&lowerLimit, &upperLimit);

    computeIntegralCoeffs(degree, coefficients, integralCoefficients);

    integral = evaluateIntegral(degree, integralCoefficients, lowerLimit, upperLimit);

    printf("\n\nEvaluating the definite integral gives us the following area: \t%lf\n", integral);

    free(coefficients);
    free(integralCoefficients);

    return 0;
}

【问题讨论】:

  • 我可能会按原样提交,但我很想听听你们的意见,以便我可以更好地编码。
  • 问题:evaluateIntegral()函数下,lowerupper的初始化值有关系吗?我的意思是在这种情况下将其初始化为 0 而不是 1 更好吗?他们给出相同的结果。
  • Mickael B. 感谢您将我推荐给论坛。我会重新发布。
  • 使用int main(void)int main() 结构已被回避了几十年,只允许支持遗留代码。在 1987 年到 1992 年(或者更早)之间的某个时间点,有人认为允许它是一个好主意,认为旧风格会很快消亡。显然,它并没有消亡,但仍在被教导。不要使用它。

标签: arrays c function pointers


【解决方案1】:

不分先后:

  1. Don't cast the return value from malloc.
  2. 不测试来自scanf 的返回值是自找麻烦。
  3. 在 C 中是 int main(void)
  4. 没有终止换行符的printf 可能需要fflush(stdout) 才能显示输出。
  5. 缩进您的代码,使其保持在每行大约 70 个字符的范围内。这样可以避免在编辑器中出现难看的换行以及在 stackoverflow 等网站上出现水平滚动条。
  6. 如果您接下来要做的是从malloc 为指针分配一个值,那么将NULL 分配给指针是没有意义的。
  7. EXIT_SUCCESS(或EXIT_FAILURE)从stdlib.h 主返回。
  8. 减少无用的冗长。 us the following -> the.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-22
    • 2023-04-03
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 2017-12-22
    • 2011-07-20
    • 1970-01-01
    相关资源
    最近更新 更多