程序员2007.12算法题

/**
 * 题目:时间表达
 *    英语表达时间有如下6种类型:
 *       It is five past seven.(7点5分)
 *       It is eleven to ten.(9点49分)
 *       It is half past nine.(9点30分)
 *       It is a quarter past eight.(8点15分)
 *       It is a quarter to ten.(9点45分)
 *       It is three o’clock.(3点)
 *
 * 输入:
 *    输入时间,格式“A B”,A表示小时(0<=A<13),B表示分钟(0<=B<60)
 *
 * 输出:
 *    输出相应的时间表达字符串,输入“0 0”时表示输入已结束
 */

/**
 * main.c
 * 作者:小夜
 */

#include <stdio.h>
#include <stdlib.h>

/**
 * 数字对应英文字符串
 */
const char *numString[31] = { "zero", "one", "two", "three", "four", "five", "six", "seven",
        "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "a quarter", "sixteen",
        "seventeen", "eighteen", "nineteen", "twenty", "twenty-one", "twenty-two", "twenty-three",
        "twenty-four", "twenty-five", "twenty-six", "twenty-seven", "twenty-eight", "twenty-nine",
        "half" };

/**
 * 时间数据结构
 */
typedef struct _Time
{
    unsigned short hour;    // 小时
    unsigned short minute;  // 分钟
} Time;

/**
 * 时间节点
 */
typedef struct _TimeNode
{
    Time time;                  // 时间
    struct _TimeNode * next;    // 下一节点
} TimeNode;

/**
 * 时间列表
 */
typedef struct _TimeList
{
    TimeNode * head;    // 头节点
    TimeNode * tail;    // 尾节点
    int len;            // 长度
} TimeList;

/**
 * 对输入的时间进行验证,把时间转化为结构存储
 */
static TimeNode *readTimeNode(unsigned int hour, unsigned int minute)
{
    TimeNode *node;
    if (hour<13&& minute<60&& hour+minute!=0)
    {
        node = (TimeNode *)malloc(sizeof(TimeNode));
        if (node != NULL)
        {
            node->time.hour = hour;
            node->time.minute = minute;
            node->next = NULL;
            return node;
        }
    }
    return NULL;
}

/**
 * 新建时间列表,并对时间列表进行初始化处理
 */
static TimeList * newTimeList()
{
    TimeList *list;
    list = (TimeList *)malloc(sizeof(TimeList));
    if (list != NULL)
    {
        list->len = 0;
        list->head = NULL;
        list->tail = NULL;
        return list;
    }
    return NULL;
}

/**
 * 释放时间列表
 */
static void deleteTimeList(TimeList *list)
{
    TimeNode *node, *next;

    if (list == NULL)
        return;

    for (node=list->tail; node!=NULL; node = next)
    {
        next = node->next;
        if (node != NULL)
        {
            free(node);
        }
    }

    free(list);
}

/**
 * 约束条件:
 *     list != NULL
 *     node != NULL
 *     node->next !=NULL
 */
static void appendTimeList(TimeList *list, TimeNode *node)
{
    if (list->len == 0)
    {
        list->head = node;
        list->tail = node;
    }
    else
    {
        list->tail->next = node;
        list->tail = list->tail->next;
    }
    list->len++;
}

/**
 * 输出一个时间信息
 */
static void printTime(Time *time)
{
    int mode;
    mode = (time->minute + 29)/30;
    // 把六种表示模式转化为3种
    switch (mode)
    {
        case 0: // 分钟为0
            printf("It is %s o'clock./n", numString[time->hour]);
            break;
        case 1: //分钟为1~30
            printf("It is %s past %s./n", numString[time->minute], numString[time->hour]);
            break;
        case 2: //分钟为31~59
            printf("It is %s to %s./n", numString[60-time->minute], numString[time->hour+1]);
            break;
        default:
            break;
    }
}

/**
 * 输出列表中的所有时间信息
 */
static void printTimeList(TimeList *list)
{
    TimeNode *node;
    for (node=list->head; node!=NULL; node=node->next)
        printTime(&node->time);
}

int main()
{
    TimeNode *node;
    TimeList *inTimeList;
    unsigned int hour, minute;

    // 初始化
    inTimeList = newTimeList();
    if (inTimeList == NULL)
        return -1;

    // 获取输入
    while (1)
    {
        scanf("%u %u", &hour, &minute);
        node = readTimeNode(hour, minute);
        if (node != NULL)
            appendTimeList(inTimeList, node);
        else
            break;
    }

    // 输出
    printTimeList(inTimeList);

    // 释放内存
    deleteTimeList(inTimeList);
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值