简体   繁体   中英

C, reading multiple numbers from single input line

I need to create ac program that handles many cases.

In order to solve all the cases easily i need to take all the input elements in one line separated by a comma.

for example user given input like this

5,60,700,8000

should create an array like this {5,,,60,,,700,,,8000} which means array is of size 7 and a[1],a[3],a[5] value is comma ,

thank you

There is no need to store the , . Use something similar to

int a[4];
scanf("%d,%d,%d,%d", &a[0], &a[1], &a[2], &a[3]);

If you need to put the commas back for printing later you can use

printf("%d,%d,%d,%d", a[0], a[1], a[2], a[3]);

Edit : There are actually several ways to do what you are asking: array of struct (may contain several data types), array of struct with union (allows run-time selection of data type), string array (requires converting numeric values)

The only way to literally construct an array to hold both numeric and string variable types is to use a union . Clearly (and normally) there are much better ways to handle strings of alphanumeric data, but since unions were mentioned in the comments under your question, I will use that method to address your very specific request:

Results of following example:
在此处输入图像描述

Code example:

typedef union   {
    int num;      //member to contain numerics
    char str[4];  //member to contain string (",,,")
}MIX;
typedef struct  {
    BOOL type;//type tag:  1 for char , 0 for number
    MIX c;
}ELEMENT;

ELEMENT element[7], *pElement;//instance of array 7 of ELEMENT and pointer to same

void assignValues(ELEMENT *e);

int main(void)
{
    char buf[80];//buffer for printing out demonstration results
    buf[0]=0;
    int i;

    pElement = &element[0];//initialize pointer to beginning of struct element

    //assign values to array:
    assignValues(pElement);

    for(i=0;i<sizeof(element)/sizeof(element[0]);i++)
    {
        if(pElement[i].type == 1)
        {   //if union member is a string, concatenate it to buffer
            strcpy(element[i].c.str, pElement[i].c.str);
            strcat(buf,pElement[i].c.str); 
        }
        else
        {   //if union member is numeric, convert and concatenate it to buffer
            sprintf(buf, "%s%d",buf, pElement[i].c.num); 
        }
    }
    printf(buf);

    getchar();

    return 0;   
}

//hardcoded to illustrate your exact numbers
// 5,,,60,,,700,,,8000
void assignValues(ELEMENT *e)
{
    e[0].type = 0; //fill with 5
    e[0].c.num = 5;

    e[1].type = 1; //fill with ",,,"
    strcpy(e[1].c.str, ",,,");

    e[2].type = 0; //fill with 60
    e[2].c.num = 60;

    e[3].type = 1; //fill with ",,,"
    strcpy(e[3].c.str, ",,,");

    e[4].type = 0; //fill with 700
    e[4].c.num = 700;

    e[5].type = 1;  //fill with ",,,"
    strcpy(e[5].c.str, ",,,");

    e[6].type = 0; //fill with 8000
    e[6].c.num = 8000;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM