Hi all.
There is a challenge question I encountered recently, which says:
"In plain English, there are six different ways when you want to tell someone else about the current time:
It is five past seven.
It is eleven to ten.
It is half past nine.
It is a quarter past eight.
It is a quarter to ten.
It is three o'clock.
A Simulation to do this with computer would be like this:
10 5
9 49
9 30
8 15
9 45
3 0
0 0
After user entered these numbers using the keyboard, your program should generate exactly the above words. Note that the 0 0 is recognized as the ending input signal."
Well, I worked out part of the solution myself but got stucked in one critical issue: how to implement a proper index architecture to store all the "number words", say, "one", "two", "three", ..., all the way up till "twenty-nine"?
I am thinking to use a struct to store all the "number words", but that would be quite... dumb, as I have to manually input everything and link them altogether to get the index. Is there any better approach?
BTW, all suggestions or comments upon my code stub is welcomed and appreciated.
Here comes my code stub. please note that it would not print out anything:
There is a challenge question I encountered recently, which says:
"In plain English, there are six different ways when you want to tell someone else about the current time:
It is five past seven.
It is eleven to ten.
It is half past nine.
It is a quarter past eight.
It is a quarter to ten.
It is three o'clock.
A Simulation to do this with computer would be like this:
10 5
9 49
9 30
8 15
9 45
3 0
0 0
After user entered these numbers using the keyboard, your program should generate exactly the above words. Note that the 0 0 is recognized as the ending input signal."
Well, I worked out part of the solution myself but got stucked in one critical issue: how to implement a proper index architecture to store all the "number words", say, "one", "two", "three", ..., all the way up till "twenty-nine"?
I am thinking to use a struct to store all the "number words", but that would be quite... dumb, as I have to manually input everything and link them altogether to get the index. Is there any better approach?
BTW, all suggestions or comments upon my code stub is welcomed and appreciated.
Here comes my code stub. please note that it would not print out anything:
Code:
#include "stdio.h" #include "stdlib.h" #include "string.h" /*This struct is used to store each entered time.*/ struct timeRecordClass { int hourDigit; int minuteDigit; struct timeRecordClass *next; }; struct timeRecordClass typedef timeRecord; /*Here comes the function prototype section:*/ void getInput(timeRecord *sentinel); void addNode(timeRecord *sentinel, int hourDigit, int minuteDigit); void getOutput(timeRecord *sentinel); void printOutTime(timeRecord *current); void printOClock(int hour, int minute); /*The main function starts here:*/ int main() { /*A ring linked list with a sentinel would be used as the data structure.*/ timeRecord *sentinel = (timeRecord *) malloc(sizeof(timeRecord)); sentinel->hourDigit = -1; sentinel->next = sentinel; printf("Please enter two numbers for the hour and the minute, separated by a space.\n"); printf("Note that the valid range for the hour is from 0 to 12;\n"); printf("and the valid range for the minute is from 0 to 59\n"); printf("If you wanna finish keyboard input, enter 0 0\n"); getInput(sentinel); printf("Now check for the output!\n"); getOutput(sentinel); return 0; } void getInput(timeRecord *sentinel) { char *hour, *minute; char input[100]; int hourDigit, minuteDigit; fflush(stdin); /*Get user input and deal with it line by line.*/ do { if(fgets(input, 99, stdin)!=NULL) { hour = strtok(input, " "); minute = strtok(NULL, "\n"); hourDigit = atoi(hour); minuteDigit = atoi(minute); } else { printf("An error occurs when reading from user input!\n"); exit(1); } /*Check if user wanna stop entering the time from keyboard.*/ if(hourDigit==0&&minuteDigit==0) break; /*Add a new timeRecord node into the ring linked list for later use.*/ addNode(sentinel, hourDigit, minuteDigit); } while(1); } void addNode(timeRecord *sentinel, int hourDigit, int minuteDigit) { timeRecord *temp = (timeRecord *) malloc(sizeof(timeRecord)); timeRecord *current = sentinel; temp->hourDigit = hourDigit; temp->minuteDigit = minuteDigit; /*Iterate through the ring linked list to locate the right posiiton to enter a new timeRecord node.*/ while(current->next != sentinel) current = current->next; current->next = temp; temp->next = sentinel; } void getOutput(timeRecord *sentinel) { timeRecord *current = sentinel; int flag = 0; while(1) { /*Skip the sentinel node for the first time.*/ /*When you see it for the second time, you have finished printing out all the nodes.*/ if(current->hourDigit == -1) { current = current->next; flag =flag +1; if(flag == 1) continue; else break; } //printf("%d:%d\n", current->hourDigit, current->minuteDigit); printOutTime(current); current = current->next; } } void printOutTime(timeRecord *current) { int hour = current->hourDigit; int minute = current->minuteDigit; switch(minute) { case 0: printOClock(hour, minute); break; case 15: printQuarterPast(hour, minute); break; case 30: printHalfPast(hour, minute); break; case 45: printQuarterTo(hour, minute); break; default: printRegular(hour, minute); break; } }
Comment