这是代码的第一个版本。它的目的是使每个数字都超过argc(5)并像多项式一样工作。但是,它需要考虑argc(5)之后的所有输入。然后将其传递给变量x i我要计算的变量,但为简单起见,请仅将其赋值为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);
^~~~~
另外,如果我的问题的格式有误,请让我知道我可以做些什么来改善它
最佳答案
这里有两个问题。首先,calcX
接受三个参数,但是您只传递了一个参数:
calcX(x);
您还需要传递
argc
和argv
:calcX(argc, argv, x);
其次,在声明函数之前先使用函数
calcX
,以便隐式声明该函数以接受未知数量的参数并返回int
。这与实际定义冲突。要解决此问题,请将
calcX
的定义移到main
上方。关于c - 我无法从命令行将argc和argv转换为函数调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51392221/