简体   繁体   English

C将值存储在数组中

[英]C store values in an array

My program reads the following data: Date , Distance and Time . 我的程序读取以下数据: 日期距离时间

So for example an input would look like: 例如,输入看起来像:

Date: 10.10.2013 日期: 2013年10月10日

Distance (m): 500 距离(m): 500

Time (HH:MM:SS): 01:20:05 时间(HH:MM:SS):01:20:05

I want to store all this information in an array. 我想将所有这些信息存储在一个数组中。 The problem is that I want the user to input date, distance and time several times and the program has to save all the data in an array. 问题是我希望用户多次输入日期,距离和时间,程序必须将所有数据保存在一个数组中。

I can't store it like this, because then how would I know which index is the date index? 我不能像这样存储它,因为那我怎么知道哪个索引是日期索引? And how should I store the time? 我应该如何存储时间? I can't store it with : . 我无法存储它:

arr[0] = 10.10.2013 arr [0] = 10.10.2013

arr[1] = 500 arr [1] = 500

arr[2] = 012005 arr [2] = 012005

arr[3] = 22.10.2013 arr [3] = 22.10.2013

arr[4] = 200 arr [4] = 200

arr[5] = 000510 arr [5] = 000510

You can make a struct : 你可以制作一个struct

struct Data{
   Date date;
   Distance distance;
   Time time;
}

Then declare an array of Data and use it like that: 然后声明一个Data数组并使用它:

Data arr[5];
arr[0].date = //some date;
arr[0].distane =//some distance
arr[0].time=//some time

Because the type of data you will need to store, it needs type char , so it can be stored in strings. 因为您需要存储的数据类型,它需要类型char ,所以它可以存储在字符串中。 (ie "10.10.2013") (即“10.10.2013”​​)
First, Define a struct PARAM: 首先,定义一个struct PARAM:

typedef struct {
   char date[20];
   char distance[20];
   char time[20];
} PARAM;

use PARAM to create an array of your struct: 使用PARAM创建结构数组:

PARAM param[20];

Now, you can use it like this for example: 现在,您可以像这样使用它,例如:

int main(void)
{
    strcpy(param[0].date, "10.10.2013");    
    strcpy(param[0].time, "05:02:10");    
    strcpy(param[0].distance, "500");
    //and so on for all the struct array elements, 0 through 20
    return 0;
}

Or, better yet , using printf() and scanf() statements as necessary, you can prompt the user for input in a loop, and store in your struct array: 或者,更好的是 ,根据需要使用printf()scanf()语句,您可以提示用户输入循环,并存储在struct数组中:

int main(void)
{
    int i;
    for(i=0;i<20;i++)
    {
        printf("enter date %d:", i+1);
        scanf("%s", param[i].date);


        printf("enter distance %d:", i+1);
        scanf("%s", param[i].distance);


        printf("enter time %d:", i+1);
        scanf("%s", param[i].time);
    }
    return 0;
}  

EDIT Regarding question in comment Therefore I guess its best to store them in date.day, date.month and date.year. 编辑关于评论中的问题因此我认为最好将它们存储在date.day,date.month和date.year中。 Right? 对? That approach would work, and I include it below, but it is a little more tedious to enter data that way. 这种方法可行,我将其包含在下面,但以这种方式输入数据有点繁琐。 I included a second example below that might improve both, entering data, and storing data. 我在下面添加了第二个示例,它可以改进,输入数据和存储数据。

So, Per your comment, two ways jump to mind : 所以,根据你的评论,有两种方式可以想到

ONE , create struct members that contain the discrete members of time and date as integers: ie ONE ,创建包含时间和日期的离散成员的结构成员作为整数:即

typedef struct {
    int day;
    int month;
    int year;
    int hour;
    int minute;
    int second;
}TIMEDATE;

Use this struct as a member of the PARAM struct ; 使用此结构作为PARAM struct的成员;

typedef struct  {
    int distance;
    TIMEDATE timedate;
}PARAM;  

PARAM param[20];

Now, just modify and expand the example of the last main() function to include scanning in values for the new struct members. 现在,只需修改并展开最后一个main()函数的示例,以包括扫描新结构成员的值。 This will be more tedious for the person using your program, but will allow you to keep all the input values as numbers as you have indicated. 对于使用您的程序的人来说,这将更加繁琐,但允许您将所有输入值保持为您指示的数字。

//Note the changes in scanf format specifiers for int, "%d":
//     in all the statements
int main(void)
{
    int i;
    for(i=0;i<20;i++)
    {
        printf("enter date %d:", i+1);
        scanf("%d", &param[i].timedate.day);

        printf("enter time %d:", i+1);
        scanf("%d", &param[i].timedate.month);

        printf("enter time %d:", i+1);
        scanf("%d", &param[i].timedate.year);

        printf("enter time %d:", i+1);
        scanf("%d", &param[i].timedate.hour);

        printf("enter time %d:", i+1);
        scanf("%d", &param[i].timedate.minute);

        printf("enter time %d:", i+1);
        scanf("%d", &param[i].timedate.second);

        printf("enter time %d:", i+1);
        scanf("%d", &param[i].distance);
    }
    return 0;
}

Two , modify the first approach to include using strings AND integers, all in the same struct. ,修改第一种方法,包括使用字符串整数,所有这些都在同一个结构中。 This will make it easier for the user user to enter time and date information, and possible for you to manipulate the data easier. 这将使用户用户更容易输入时间和日期信息,并且可以更轻松地操作数据。 And a bonus, it will demonstrate how to parse the user input string data into integer data. 还有一个好处,它将演示如何将用户输入的字符串数据解析为整数数据。

typedef struct {
    char date[20];//keep as char
    char time[20];//keep as char
    int distance; //changed to int
    TIMEDATE timedate;//container for in data   
} PARAM;

//use PARAM to create an array of your struct:

PARAM param[20], *pParam; //create a pointer to pass

int GetIntData(PARAM *p, int index);//prototype for new function

//Note the changes in scanf format specifiers for int, "%d":
//     in all the statements
int main(void)
{
    int i, loops;
    pParam = &param[0]; //initialize pointer to struct

        printf("How many sets of data would you like to enter? :");
        scanf("%d", &loops);


    for(i=0;i<loops;i++)
    {
        printf("enter date (eg:MM.DD.YYYY): %d:", i+1);
        scanf("%s", pParam[i].date);

        printf("enter time (eg HH:MM:SS): %d:", i+1);
        scanf("%s", pParam[i].time);

        printf("enter distance %d:", i+1);
        scanf("%d", &pParam[i].distance);

        GetIntData(pParam, i);
    }
    return 0;
} 
//reads string members into integer members
int GetIntData(PARAM *p, int index)
{
    char *buf=0;
    if(strstr(p[index].date, ".")==NULL) return -1;

    p[index].timedate.month = atoi(strtok(p[index].date, ".")); 
    p[index].timedate.day = atoi(strtok(NULL, "."));    
    p[index].timedate.year = atoi(strtok(NULL, "."));   

    if(strstr(p[index].time, ":")==NULL) return -1;
    buf=0;

    p[index].timedate.hour = atoi(strtok(p[index].time, ":"));  
    p[index].timedate.minute = atoi(strtok(NULL, ":")); 
    p[index].timedate.second = atoi(strtok(NULL, ":"));

    return 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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