如何排除从 C 中的命令提示符 argc argv 传递的参数? [英] How to exclude arguments passed from command prompt argc argv in C?

查看:14
本文介绍了如何排除从 C 中的命令提示符 argc argv 传递的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按照格式添加用户通过命令提示符给出的分数

I need to add fractions given by the user through command prompt in the format

a/b b/c

我以为我可以这样做:

n1 = atoi(argv[1]);
d1 = atoi(argv[3]);
n2 = atoi(argv[4]);
d2 = atoi(argv[6]);

从而跳过斜线,但这只会使程序崩溃.有没有办法跳过通过命令提示符作为参数传递的某些字符?提前致谢.

Thereby skipping the slashes, but this just crashes the program. Is there some way to skip over certain characters passed as arguments through command prompt? Thank you in advance.

推荐答案

如果用户写:

add_fractions 1/2 2/3

那么你的程序只有 3 个参数:

then your program is given just 3 arguments:

argv[0] = "add_fractions"
argv[1] = "1/2"
argv[2] = "2/3"
argv[3] = 0

argc 是 3.

你必须做这样的事情(在检查你得到了 2 个参数之后):

You would have to do something like this (after checking that you were given 2 arguments):

char *ptr;
n1 = atoi(argv[1]);
if ((ptr = strchr(argv[1], '/') != 0)
    d1 = atoi(ptr+1);
else
    d1 = 1;
n2 = atoi(argv[2]);
if ((ptr = strchr(argv[2], '/') != 0)
    d2 = atoi(ptr+1);
else
    d2 = 1;

或者您可以将重复的语句序列打包到一个函数中(逻辑略有不同):

Or you could pack the repeated sequence of statements into a function (with a slightly different twist in the logic):

void read_fraction(char const *str, int *num, int *den)
{
    char *ptr;
    *num = atoi(str);
    if ((ptr = strchr(str, '/') == 0)
        ptr = "/1";
    *den = atoi(ptr+1);
}

main()中:

read_fraction(argv[1], &n1, &d1);
read_fraction(argv[2], &n2, &d2);

您可能想考虑验证;如果字符串的第一部分没有数字,则分子变为 0,但如果有斜线且斜线后没有数字,则分母变为 0,这可能不是您想要的.一种解决方法是,如果 atoi(ptr+1) 的值为 0,则将分母设置为 1.这是一种逃避,但可以防止猖獗的崩溃.或者你可以在函数体中使用另一个完全不同的实现:

You might want to think about validation; the numerator becomes 0 if there isn't a number as the first part of the string, but the denominator becomes 0 if there is a slash and there isn't a number after the slash, which might not be exactly what you want. One fix is to set the denominator to 1 if the value from atoi(ptr+1) is 0. It's a cop-out, but prevents rampant crashes. Or you could use another completely different implementation in the body of the function:

int read_fraction(char const *str, int *num, int *den)
{
    if (sscanf(str, "%d/%d", num, den) != 2)
        return -1;
    return 0;
}

然后签入main():

if (read_fraction(argv[1], &n1, &d1) == 0 &&
    read_fraction(argv[2], &n2, &d2) == 0)
    ...process valid fractions...
else
    ...make appropriate comments about how to use the program...

从几个角度来看,使用函数是更好的".在The Pragmatic Programmer中,它被称为DRY原则:不要重复自己.

Using a function is 'better' from several points of view. In The Pragmatic Programmer, it is called the DRY principle: Don't Repeat Yourself.

Kernighan 和 Plauger 在他们的书编程风格的元素中对其进行了简洁的总结:

Kernighan and Plauger summarized it neatly in their book The Elements of Programming Style:

  • 子程序调用允许我们总结参数列表中的不规则之处,我们可以在其中快速查看发生了什么.
  • 子程序本身总结了代码的规律,因此不需要使用重复的模式.

这篇关于如何排除从 C 中的命令提示符 argc argv 传递的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆