[英]Reduce code for program with command line interface?
I want my program to have an interface that looks like this:我希望我的程序有一个如下所示的界面:
gen_data [start] [stop] [step]
The [start]
, [stop]
and [step]
are optional and are by default set to -3*PI/2
, 3*PI/2
and 0.01
. [start]
、 [stop]
和[step]
是可选的,默认设置为-3*PI/2
、 3*PI/2
和0.01
。 I have the following code:我有以下代码:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.14569
int main (int argc, char **argv)
{
float i, start, stop, step;
printf ("# gnuplot data\n"
"# x sin(x) cos(x)\n");
switch (argc) {
case 1:
start = -(3*PI)/2;
stop = (3*PI)/2;
step = 0.01;
break;
case 2:
start = atof (argv[1]);
stop = (3*PI)/2;
step = 0.01;
break;
case 3:
start = atof (argv[1]);
stop = atof (argv[2]);
step = 0.01;
break;
case 4:
start = atof (argv[1]);
stop = atof (argv[2]);
step = atof (argv[3]);
break;
}
for (i = start; i <= stop; i += step)
printf ("%6f\t%6f\t%6f\n", i, sin (i), cos (i));
return 0;
}
As you can see all three variables start
, stop
and step
are being assigned every time - isnt this redundant?正如您所看到的,所有三个变量start
、 stop
和step
每次都被分配 - 这不是多余的吗? I was roughly thinking of something like this:我大致在想这样的事情:
start
to command line arg if argc = 2: 仅将start
设置为命令行 argstart
and stop
to command line args if argc = 3:仅将start
和stop
设置为命令行 argsstart
, stop
and step
to command line args如果 argc = 4:设置start
、 stop
和step
到命令行 args The reason I used a switch
- case
is to be able to exploit fall through - but could not get it to work.我使用switch
的原因 - case
是为了能够利用失败 - 但无法让它工作。 Any thoughts?有什么想法吗? Is the code fine as it is?代码是否正常?
That is really quite easy using a ternary .使用三元组真的很容易。 You can simply do:你可以简单地做:
if (argc < 2) {
fputs ("error: insufficient arguments\n"
"usage: ./program start [stop] [step]\n", stderr);
return 1;
}
char *endptr;
float start = strtof (argv[1], &endptr), /* validation omitted */
stop = argc > 2 ? strtof (argv[2], &endptr) : -3*PI/2.,
step = argc > 3 ? strtof (argv[3], &endptr) : 3*PI/2.;
/* rest of code */
( note: suggest using double
and strtod()
instead of float
unless on a microcontroller) (注意:除非在微控制器上,否则建议使用double
和strtod()
而不是float
)
That way you will optionally set stop
and step
if sufficient arguments are given, and if not, you will use your default values.这样,如果给出足够的 arguments,您可以选择设置stop
和step
,如果没有,您将使用默认值。
Avoid using atoi()
, atof()
in practice, they provide zero error detection and provide no indication if a failure occurs.在实践中避免使用atoi()
、 atof()
,它们提供零错误检测,并且在发生故障时不提供任何指示。 atof()
will happily take atof("my cow");
atof()
将愉快地接受atof("my cow");
and silently fail returning 0
without you ever knowing.并且在你不知道的情况下默默地返回0
。
Look things over and let me know if you have further questions.如果您还有其他问题,请仔细查看并告诉我。
You can set default values for all the variables first.您可以先为所有变量设置默认值。 Then set them not according to equality of argc
, but by >
for example然后不根据argc
的相等性设置它们,而是通过>
例如
#include <stdlib.h>
#include <stdio.h>
int main (int argc, char **argv)
{
float start = -(3*PI)/2;
float stop (3*PI)/2;
float step = 0.01;
printf ("# gnuplot data\n"
"# x sin(x) cos(x)\n");
if (argc > 1)
start = atof (argv[1]);
if (argc > 2)
stop = atof (argv[2]);
if (argc > 3)
step = atof (argv[3]);
for (float i = start; i <= stop; i += step)
printf ("%6f\t%6f\t%6f\n", i, sin (i), cos (i));
return 0;
}
As mentioned, the use of float
and atof
leaves much to be desired, but that is another question.如前所述, float
和atof
的使用还有很多不足之处,但这是另一个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.