简体   繁体   中英

How to access values in a struct array

Structures:

struct tod{
    int minute;
    int hour;
};
struct event
{
    int start, end;
}; 

Array:

int main (void)
{
    struct event schedule[] = {{{9,45},{9,55}},{{13,0},
        {14,20}},{{15,0},{16,30}}};
    printf ("%d\n", freetime (schedule,3,8,0));

}

How come when i do schedule[0].end I get 9 and not 45? or schedule[1].end I get 13 instead of 55. How do I get the minute values in the array? Aren't the first set of 3 curly braces the start times? and the second set the end times? I don't know how to use the structures above to save these values.

Here is my code

int freetime (struct event schedule[], int n, int hour, int min)
{
    struct tod time1;
    struct tod time2;
    int i;
    int result = 1;
    for (i=0; i<n; i++)
    {
        time1.hour = schedule[i].start;
        time1.minute = schedule[i].end;
        i++;
        time2.hour = schedule[i].start;
        time2.minute = schedule[i].end;
        if(hour >= time1.hour && hour < time2.hour)
        {
            if(min >= time1.minute && min < time2.minute)
            {
                result = 0;
                break;
            }
        }
    }
    return result;
}

freetime is supposed to return 1 if the specified time (hour and minute) is not part of any scheduled event; returns 0 otherwise. The value n specifies the size of the array containing the schedule.

Your event struct should probably look like this:

struct event
{
    tod start, end;
}; 

as you were trying to store tod 's in an int . This led to you storing your first 'time' in the first event, and the second 'time' was pused into your second event (and so on)

Try this, for testing:

//returns true if tod1 comes before tod2
bool tod_before(tod tod1, tod tod2)
{
     return !((tod1.hour > tod2.hour) 
            || ((tod1.hour == tod2.hour) 
                && (tod1.minute > tod2.minute))
}


int freetime (struct event schedule[], int n, int hour, int min)
{
    struct tod test_time;
    struct tod time1;
    struct tod time2;
    int i;
    int result = 1;

    test_time.hour = hour;
    test_time.minute = min;

    //handle edge cases
    if (tod_before(test_time, schedule[0].start) || tod_before(schedule[n-1].end, test_time)
    {return 1;}

    //handle general case
    for (i=0; i<n-1; i++)
    {
        time1 = schedule[i].end;
        time2 = schedule[i+1].start;

        if (tod_before(time1, time) && tod_before(time, time2))
        {return 1;}
    }
    //if we get to here, then time wasn't found in a break
    return 0;
}

This is assuming that each event is in order of course.

Replace

struct event schedule[] = {{{9,45},{9,55}},{{13,0},
        {14,20}},{{15,0},{16,30}}};

by

struct event schedule[] =
{
    {9,45},
    {9,55},
    {13,0},
    {14,20},
    {15,0},
    {16,30}
} ;

Here the basic problem is that your structure contains only two variables and you have an array of that kind of structure. So

struct event schedule[] = {{{9,45},{9,55}},{{13,0},{14,20}},{{15,0},{16,30}}};

will have a size of 3. And your variables of schedule, start & end is initialized with first in the sets ie 9,9 13,14 15,16 only.In this case the schedule array will have 3 elements

 schedule[] = {{9,45},{9,55},{13,0}, {14,20},{15,0},{16,30}};

will initialize the start & end variables with 9,15 9,55 13,0 and so on. In this case the schedule array will have 6 elements

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