在C中更新时间表
问题描述:
该功能将按时添加
如果相同时间的重复描述将返回false
the function will add on schedule
if a duplicate description with same timing it will return false
bool add_new_appointment(
struct Appointment schedule[],
int numOfAptInSchedule,
char description[],
int year, int month, int day,
int startHour, int startMinute,
int endHour, int endMinute )
{
return false;
}
我的尝试:
What I have tried:
struct Appointment {
char description[50];
int year;
int month;
int day;
int startHour;
int startMinute;
int endHour;
int endMinute;
};
<pre>void print_schedule( struct Appointment schedule[], int numOfAptInSchedule )
{
printf("\nAppointment: %d\n%d-%d-%d\n%s\n%.2d:%.2d to %.2d:%.2d\n\n",
numOfAptInSchedule,
schedule->year,schedule->month, schedule->day,
schedule->description,
schedule->startHour,schedule->startMinute,
schedule->endHour,schedule->endMinute);
}
int main(){
struct Appointment mySchedule[20];
int numAptInSchedule = 0;
if ( add_new_appointment( mySchedule, numAptInSchedule,
"Train with Luke",
2018, 1, 28, 9, 0, 11, 30)){
numAptInSchedule++;
printf("Appointment added!\n");
print_schedule( mySchedule, numAptInSchedule);
}
}
答
这应该是微不足道的,假设传递之间有一对一的对应关系参数和struct Appointment
成员(你没有提供有关的信息)
That should be trivial, assuming there is one-by-one correspondence between passed parameters andstruct Appointment
members (you didn't provide info about)
bool add_new_appointment(
struct Appointment schedule[],
int numOfAptInSchedule,
char description[],
int year, int month, int day,
int startHour, int startMinute,
int endHour, int endMinute )
{
struct Appointment *pa = &schedule[numOfAptInSchedule]; // just an alias
// assuming struct Appointment.description is a character array
size_t size = sizeof(pa->description);
strncpy( pa->description, description, size - 1);
pa->description[size-1] = '\0';
pa->year = year;
pa->month = month;
//..
return true;
}
请注意:错误检查留作练习。
Please note: error checking left as exercise.