在C中更新时间表 [英] Update schedule in C

查看:61
本文介绍了在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 and struct 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.


这篇关于在C中更新时间表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆