【问题标题】:C: Making a polynomial function via command line argumentsC:通过命令行参数制作多项式函数
【发布时间】:2020-09-26 08:36:55
【问题描述】:

对于我的微积分课,经常被要求找到两点之间的平均速度以找到瞬时速度,需要我做f(b) - f(a) / b - a。我想将参数传递给程序(最终加载到我的 Ti-84 上)以快速为我解决这个问题,理想情况下 像这样工作:./a.out 2 4 -1 , 2.9 2.9999 产生多项式2x^2 + 4x - 1,它将在2.92.9999 上运行。到目前为止,我已经在这段代码中编写了大部分逻辑,但是我很难将它放入正确的 C 中。

#include<stdio.h>
int main(int argc, char *argv[])
{
        int comma; // Ex: ` ./a.out 1 2 3 , 4 5 ` comma is at index 3
        // Find out where comman is located
        for ( int i=1;i<argc; i++)
        {
                if (argv[i] == ','){
                        int comma = i;
                        break;
                }
        }

        printf("%d", comma);

        // PSUEDO-CODE START
        //
        // Problem: f(b) - f(a) / b - a
        //
        // Idea
        // ------------------------------------------------------------
        // ./a.out 7 2 3 , 2 5
        // // Right now argc is 6
        // // Right now comma is 4
        // polynomialOrder = argc - comma
        //
        // // Get bigger number for the / b - a part
        // firstNum  = argv[comma + 1]
        // secondNum = argv[comma + 2]
        // biggerNum = ( firstNum > secondNum ) ? firstNum : secondNum;
        // smallerNum = ( firstNum < secondNum ) ? firstNum : secondNum;
        //
        // ------------------------------------------------------------
        //
        // Psuedo-code:
        //
        // for  (int i=polynomialOrder; i >= 0; i--)
        // {
        //      if (i == 0){ function += argv[i]} // I don't want 3x^2 + 2x^1 + 4^0 , the
4^0 should be 4
        //      else{
        //              // Generate a function?
        //              polynomial += argv[i]^i;  // The += appends to the function
        //      }
        // }
        // // result is -7x^2 + 2x^1 + 1
        //
        // Function generated:
        // -------------------------------------------------------
        // float polynomial(a, b) {
        //      float result;
        //      float a_result = (-7*(a**2)) + (2*(a**1)) + 1;
        //      float b_result = same as ^, substitute in b
        //      float result = (b_result - a_result) / ( b - a);
        //      return return;
        // }
        // -------------------------------------------------------
        // }
        // -------------------------------------------------------
        // polynomial( smallerNum, biggerNum );

        return 0;
}

【问题讨论】:

  • “我很难将它放入正确的 C 中。”问题是什么?什么症状?
  • 对于./a.out 7 2 3 , 2 5,你应该有argc == 7,你会看到argv[0]指向字符串./a.out
  • 尽管您的编译器可能允许这样做,但您不应在main() 中定义polynomial()

标签: c polynomials calculus ti-basic


【解决方案1】:

至少有这些问题:

比较错误

',' 不是字符串,并且是 strcmp() 的无效参数。 strcmp() 在字符串相等时返回 0。

// if (strcmp(argv[i], ',')) // Check if arg is ','
if (strcmp(argv[i], ",") == 0) // Check if arg is ","

本地对象

comma 仅在该块本地。这个comma 休息后不复存在。后来polynomialOrder = argc - comma; 没有comma 可以访问。

{
   printf("Parameters to function");
   int comma = i; // All before are parts of function, after params
   break;
}

电源代码无效

// (-7*(a**2)) + (2*(a**1)) + 1;
-7*pow(a,2) + 2*pow(a,1) + 1;
// or 
-7*a*a + 2*a + 1;
// or numerical best stability as 
(-7*a + 2)*a + 1;

floatdouble

当空间很重要或速度对精度/范围很重要时,请使用float。在 C 中,最好使用double 作为默认的浮点类型。

发布编译器警告/错误

OP 的代码无法编译。发布您的错误、警告和有关剩余问题的详细问题。

如果所有代码都是伪代码,请将其发布在代码的开头。

【讨论】:

    猜你喜欢
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 2021-05-01
    • 1970-01-01
    相关资源
    最近更新 更多