【问题标题】:Put command line input into an array and parse this array into two new arrays将命令行输入放入一个数组,并将这个数组解析成两个新数组
【发布时间】:2015-01-31 03:26:21
【问题描述】:

有人告诉我用适当的 cmets 和我正在处理的实际代码重新制作它。 我正在尝试获取命令行输入并将其放入数组中。然后我想把这个数组解析成两个新数组。

问题是 argv[1] 是一个字符 *,所以我在转换为 INT 时遇到了问题。 我也不知道如何从 argv[1] 中获取每个元素(例如 1010101)并将这些 1 和 0 放入一个数组中。一旦我弄清楚这一点,如果输入大于 5,我将获取这个数组并解析它。传入的命令行参数将是 5 长度或 10 长度。如果是 5,我什么都不做,如果是 10,我将输入解析为两个数组。

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char * argv[]) {


int i;
//starting using char * arrays in order to try to grab input from the char * argv[1] command line arg
char *aA[10] = {0};
char *aB[10] = {0};
char *aC[10] = {0}; 
char *s = '1';
char *k = '0';

//read in from command the command line.  Print the arguments and save
//the 1 and 0 inputs into an array.
//need to check for EOF and next lines
for (i = 0; i < argc; i++)
{
    if (argv[i] == (s | k)) //attempting to find a way to look at 1 and 0 as a char
    {
        aA[i] = argv[i]; //place the 1 or 0 into array aA
    }

    printf("arg %d: %s\n", i,  argv[i]);
}

printf("\n");

//print array aA to see if it caught all of the 1's and 0's from the command line argument
for (i = 0; i < 8; i++)
{
    printf("%s ", aA[i]);
}

//next check if array aA is 5 strlen or 10 strlen
//if 5, do nothing
//if 10, parse array aA into two arrays aB and aC
//aB gets a[0] a[2] a[4] a[6] a[8]
//aC gets a[1] a[3] a[5] a[7] a[9] 
//print the results of aB and aC to make sure aA was correctly parsed   


printf("\n");
return 0;

}

【问题讨论】:

  • 请举例说明你的意思:你期望什么命令行参数,你想用它们做什么?
  • 10101 a 1000110001 以上是从命令行输入的示例。我需要将 10101 放入一个数组中,检查下一行并注意它是一个 a,它告诉我执行一个加法。接下来,我检查最后一行,它是两个数字的组合,需要先将其解析为两个数组,然后才能对它们执行任何操作
  • 我应该尝试使用 atoi 吗?就像做一个 int b = atoi argv[1] 然后我会有一个 int # 像 10001,也许有一种方法可以通过使用像 aA[0] = b | 这样的掩码进行解析来将其分解为单个数字0x10000 抢第一个 1?
  • 你用什么逻辑得出1000110001是两个数字的组合?
  • 每个数字是 5 位长,所以一个十位长度的字符串将是两个数字

标签: c arrays parsing command argv


【解决方案1】:
given a command line: myexecutable 10101   or myexecutable 1010101010

char originalArray[10] = {'\0'};
int array1[5] = {0};
int array2[5] = {0};
int i; // loop counter

if (2 == argc)
{ // then parameter exists
    if( (10 == strlen(argv[1])) || (5 == strlen(argv[1]) )
    { // then valid parameter length
        strncpy( originalArray, argv[1], strlen(argv[1]) );
    }

    else
    { // not valid parameter length
        // handle error
        exit( EXIT_FAILURE );
    }


    for( i=0; i<5; i++ )
    {
        if( ('1' == originalArray[i]) || ('0' == originalArray[i]) )
        { // then valid char 
            array1[i] = originalArray[i] - '0';
        }

        else
        { // invalid char in parameter
            // handle error
             exit( EXIT_FAILURE );
        } // end if
    } // end for


    if( 10 == strlen(originalArray) )
    { // then set second array
        for( i=5; i<10;i++ )
        {
            if( ('1' == originalArray[i]) || ('0' == originalArray[i]) )
            { // then valid character
                array2[i-5] = originalArray[i] - '0';
            }

            else
            { // invalid char in parameter
                // handle error
                exit( EXIT_FAILURE );
            } // end if
        } // end for
    } // end if

【讨论】:

    猜你喜欢
    • 2011-03-14
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多