设为首页 加入收藏

TOP

1.4.1 设计ADT(1)
2014-03-11 13:02:11 来源: 作者: 【 】 浏览:85
Tags:1.4.1 设计 ADT

1.4.1  设计ADT(1)

设计ADT

在问题求解的过程中,抽象数据类型的设计很自然地会演变。为了说明这个过程是如何进行的,假定想要判断给定年份所有节假日的日期。完成这一任务的方法之一是查阅日历。也就是说,可以考虑某个年份中的所有日期,并检测某天是否为节假日。下面的伪代码是这个问题可能的解决方案之一:
 

  1. // Displays the dates of all holidays in a given year.  
  2. listHolidays(year)  
  3. datedate = date of first day of year  
  4. while (date is before the first day of year + 1)  
  5. {  
  6. if (date is a holiday )  
  7. write (date," is a holiday")  
  8. datedate = date of next day  
  9. }  

在此涉及哪些数据?很显然,这个问题需要操作日期,而日期由年月日组成。为了解决节假日问题需要执行什么操作?ADT必须指明并限定关于日期的合法操作,就像基本数据类型int限定类似于加法操作以及比较操作一样。从前面的伪代码中可以看出,必须:

判断给定年份的第一天的日期

判断某个日期是否在另一日期之前

判断某个日期是否为节假日

判断给定日期之后一天的日期

注释:某问题需要哪些数据

某问题需要哪些操作

因此,可以使用统一建模语言(UML)标记的伪代码为ADT日期指明下面的操作,UML在附录C中讲述:
 

  1. // Returns the date of the first day of a given year.  
  2. +getFirstDay(year: integer): Date  
  3. // Returns true if this date is before the given date; otherwise returns false.  
  4. +isBefore(otherDate: Date): boolean  
  5. // Returns true if this date is a holiday; otherwise returns false.  
  6. +isHoliday(): boolean  
  7. // Returns the date of the day after this date.  
  8. +getNextDay(): Date  

listHolidays伪代码现在如下所示:
 

  1. // Displays the dates of all holidays in a given year.  
  2. listHolidays(year: integer): void  
  3. date = getFirstDay(year)  
  4. while (date.isBefore(getFirstDay(year + 1)))  
  5. {  
  6. if (date.isHoliday())  
  7. write (date," is a holiday ")  
  8. datedate = date.getNextDay()  
  9. }  

这样,通过确定数据以及选择适合于问题的操作设计了ADT。当指明了操作之后,就可以用这些操作解决问题,而不依赖于ADT的实现细节。

预约簿  作为ADT设计的另一个示例,假定您想要创建一个跨度为一年的预约簿。假设只想在上午8点到下午5点之间的整点和半点做记录,并且想要系统用简短的记号存储约会的类型以及日期和时间。

为了解决这个问题,可以定义一个ADT预约簿。ADT中的数据项是约会,某个约会由日期、时间以及目的组成。有什么操作呢?有两个明显的操作:

根据日期、时间、目的创建一个约会(要小心,不要在已经被占用的时间内创建约会)

取消某个日期和时间的约会

除了这些操作之外,您或许还想:

查询在给定的时间内是否有约会

获取给定时间内约会的目的

最后,我们假定ADT通常具有初始化以及销毁操作,但是在此阶段并不指定这一内容。

这样,ADT预约簿将具有如下操作:
 

  1. // Returns true if an appointment exists for the date and time specified,  
  2. // false otherwise.  
  3. +isAppointment(apptDate: Date, apptTime: Time): boolean  
  4. // Inserts the appointment for the date, time, and purpose specified as long as  
  5. // it does not conflict with an existing appointment.  
  6. // Returns true if successful, false otherwise.  
  7. +makeAppointment(apptDate: Date, apptTime: Time,  
  8. apptPurpose: string): boolean  
  9. // Deletes the appointment for the date and time specified.  
  10. // Returns true if successful, false otherwise.  
  11. +cancelAppointment(apptDate: Date, apptTime: Time): boolean  
  12. // Gets the purpose of the appointment at the given date and time, if one exists.  
  13. // Otherwise returns an empty string.  
  14. +getAppointmentPurpose(apptDate: Date, apptTime: Time): string  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇1.4 抽象数据类型 下一篇1.4.1 设计ADT(2)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Libevent C++ 高并发 (2025-12-26 00:49:30)
·C++ dll 设计接口时 (2025-12-26 00:49:28)
·透彻理解 C 语言指针 (2025-12-26 00:22:52)
·C语言指针详解 (经典 (2025-12-26 00:22:49)
·C 指针 | 菜鸟教程 (2025-12-26 00:22:46)