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

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

问题描述

我需要添加通过命令提示符用户给定分数格式

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

a/b b/c

我想我可以做这种方式:

I thought I could do it this way:

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,如果有一个斜线并且没有斜线,这可能不是正是你想要的后一个号码。一种解决方法是分母设置为1,如果从的atoi(PTR + 1)的值为0。这是一种逃避,而是prevents猖獗崩溃。或者你可以使用另一种完全不同的实现在函数体:

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...

使用功能是从不同的视角对更好。在程序员修炼,因此被称为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:


  • 子程序调用允许我们总结了违规参数列表,在这里我们可以很快地看到正在发生的事情。

  • 子程序本身总结了code的规律,如此反复模式不需要使用。

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

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