【发布时间】:2018-07-18 09:29:08
【问题描述】:
这是代码的第一个版本。它的目的是让每一个数字都超过 argc(5) 并表现得好像它是一个多项式。但是,它需要考虑 argc(5) 之后的所有输入。然后将其传递给我将计算的变量 X,但为简单起见,我将其分配为 5。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char* argv[])
{
double begin = atof (argv[1]); //start of graph
double end = atof (argv[2]); //end of graph
double inc = atof (argv[3]); //level of incriments FIXME
double low = atof (argv[4]); // lower section (what?)
double high = atof (argv[5]); //higher section(what?)
// need nested for loop use I total out of loop to get additive number.
// NO NEGATIVE NECESSARY
double sum=0;
double x=5;
printf("argc :%d\n", argc); //argc counts initialization character
double j=argc-5-2;
printf("initial j %lf\n", j);
for (int i=6; i<argc; i++)
{
sum = sum + (atof(argv[i]) * pow(x,j));
j--;
}
printf("%lf\n", sum);
return 0;
}
这是代码的第二个版本,我将 x 的计算放在函数 cal calcX 中,我只将变量 x 传递给函数,我认为这是问题所在。我该怎么做才能将 argc 和 argv 的所有实例以及变量 x 传递给函数
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char* argv[])
{
double begin = atof (argv[1]); //start of graph
double end = atof (argv[2]); //end of graph
double inc = atof (argv[3]); //level of incriments FIXME
double low = atof (argv[4]); // lower section (what?)
double high = atof (argv[5]); //higher section(what?)
double width = (high-low) / inc;
double x=5;
calcX(x);
return 0;
}
void calcX(int argc, char*argv[], double x)
{
double sum=0;
double j=argc-5-2;
printf("initial j %lf\n", j);
for (int i=6; i<argc; i++)
{
sum = sum + (atof(argv[i]) * pow(x,j));
j--;
}
printf("%lf\n", sum);
return;
}
这些是程序给我的错误,看起来很标准,但我不确定如何修复它。感谢您的宝贵时间。
$ gcc 2v.c
2v.c: In function ‘main’:
2v.c:17:1: warning: implicit declaration of function ‘calcX’ [-Wimplicit-function-declaration]
calcX(x);
^~~~~
2v.c: At top level:
2v.c:24:6: warning: conflicting types for ‘calcX’
void calcX(int argc, char*argv[], double x)
^~~~~
2v.c:17:1: note: previous implicit declaration of ‘calcX’ was here
calcX(x);
^~~~~
另外,如果我的问题格式有误,请告诉我我可以做些什么来改进它
【问题讨论】:
-
"它的目的是让每个数字都超过 argc(5) 并像多项式一样工作。但是,它需要考虑 argc(5) 之后的所有输入。它将然后传递我将计算的变量 X,但为简单起见,我将其分配为 5。”,对我来说没有意义,请阅读 How to Ask。
-
打开你最喜欢的C书,当你调用一个函数时,你需要传递它的所有参数,
calcX(argc, argv, x); -
尝试将函数
calcX()移到main()上方。这应该会改变您收到的错误消息,并更深入地了解问题所在。 -
calcX必须在调用之前声明。 (旧版本的 C 对此规则较为宽松,但请忽略这一点。)在main之前添加声明void calcX(int argc, char*argv[], double x);。将calcX的整个定义移到main之上是另一种方法。