468,272 Members | 1,980 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

[proper index architecture needed]Print out time in plain English

121 100+
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:



Expand|Select|Wrap|Line Numbers
  1.     #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4.  
  5. /*This struct is used to store each entered time.*/
  6. struct timeRecordClass
  7. {
  8.     int hourDigit;
  9.     int minuteDigit;
  10.     struct timeRecordClass *next;
  11. };
  12. struct timeRecordClass typedef timeRecord;
  13.  
  14. /*Here comes the function prototype section:*/
  15. void getInput(timeRecord *sentinel);
  16. void addNode(timeRecord *sentinel, int hourDigit, int minuteDigit);
  17. void getOutput(timeRecord *sentinel);
  18. void printOutTime(timeRecord *current);
  19. void printOClock(int hour, int minute);
  20.  
  21. /*The main function starts here:*/
  22. int main()
  23. {
  24.     /*A ring linked list with a sentinel would be used as the data structure.*/
  25.     timeRecord *sentinel = (timeRecord *) malloc(sizeof(timeRecord));
  26.     sentinel->hourDigit = -1;
  27.     sentinel->next = sentinel;
  28.  
  29.     printf("Please enter two numbers for the hour and the minute, separated by a space.\n");
  30.     printf("Note that the valid range for the hour is from 0 to 12;\n");
  31.     printf("and the valid range for the minute is from 0 to 59\n");
  32.     printf("If you wanna finish keyboard input, enter 0 0\n");
  33.  
  34.     getInput(sentinel);
  35.  
  36.     printf("Now check for the output!\n");
  37.     getOutput(sentinel);
  38.  
  39.     return 0;   
  40. }
  41.  
  42. void getInput(timeRecord *sentinel)
  43. {
  44.     char *hour, *minute;
  45.     char input[100];
  46.     int hourDigit, minuteDigit;
  47.  
  48.     fflush(stdin);
  49.  
  50.     /*Get user input and deal with it line by line.*/
  51.     do
  52.     {
  53.         if(fgets(input, 99, stdin)!=NULL)
  54.         {
  55.             hour = strtok(input, " ");
  56.             minute = strtok(NULL, "\n");
  57.             hourDigit = atoi(hour);
  58.             minuteDigit = atoi(minute);
  59.         }
  60.         else
  61.         {
  62.             printf("An error occurs when reading from user input!\n");    
  63.             exit(1);
  64.         }
  65.  
  66.         /*Check if user wanna stop entering the time from keyboard.*/
  67.         if(hourDigit==0&&minuteDigit==0)
  68.             break;
  69.  
  70.         /*Add a new timeRecord node into the ring linked list for later use.*/
  71.         addNode(sentinel, hourDigit, minuteDigit);
  72.  
  73.     }
  74.     while(1);
  75. }
  76.  
  77. void addNode(timeRecord *sentinel, int hourDigit, int minuteDigit)
  78. {
  79.     timeRecord *temp = (timeRecord *) malloc(sizeof(timeRecord));
  80.     timeRecord *current = sentinel;
  81.  
  82.     temp->hourDigit = hourDigit;
  83.     temp->minuteDigit = minuteDigit;
  84.  
  85.     /*Iterate through the ring linked list to locate the right posiiton to enter a new timeRecord node.*/
  86.     while(current->next != sentinel)
  87.         current = current->next;
  88.  
  89.     current->next = temp;
  90.     temp->next = sentinel;
  91. }
  92.  
  93. void getOutput(timeRecord *sentinel)
  94. {
  95.     timeRecord *current = sentinel;
  96.     int flag = 0;
  97.  
  98.     while(1)
  99.     {
  100.         /*Skip the sentinel node for the first time.*/
  101.         /*When you see it for the second time, you have finished printing out all the nodes.*/
  102.         if(current->hourDigit == -1)
  103.         {
  104.             current = current->next;
  105.             flag =flag +1;
  106.             if(flag == 1)
  107.                 continue;
  108.             else break;
  109.         }
  110.         //printf("%d:%d\n", current->hourDigit, current->minuteDigit);
  111.         printOutTime(current);
  112.         current = current->next;
  113.     }
  114. }
  115.  
  116. void printOutTime(timeRecord *current)
  117. {
  118.     int hour = current->hourDigit;
  119.     int minute = current->minuteDigit;
  120.  
  121.     switch(minute)
  122.     {
  123.         case 0:
  124.             printOClock(hour, minute);
  125.             break;
  126.         case 15:
  127.             printQuarterPast(hour, minute);
  128.             break;
  129.         case 30:
  130.             printHalfPast(hour, minute);
  131.             break;
  132.         case 45:
  133.             printQuarterTo(hour, minute);
  134.             break;
  135.         default:
  136.             printRegular(hour, minute);
  137.             break;    
  138.     }
  139. }
  140.  
Dec 18 '07 #1
6 1593
mattmao
121 100+
After spending some time online, I found a very useful data structure: enumeration, which could be helpful to my question.

I would try to figure it out myself, hope I am on the right track:)
Dec 18 '07 #2
weaknessforcats
9,207 Expert Mod 8TB
After spending some time online, I found a very useful data structure: enumeration, which could be helpful to my question.
An enum is not a data structure. It occupies no memory. An enum is a list of named integer values so you can use the name of the value in your code rather than the value itself:

Expand|Select|Wrap|Line Numbers
  1. enum Color = {RED, BLUE};
  2.  
  3. if (var == BLUE)
  4.  
rather than:
Expand|Select|Wrap|Line Numbers
  1. if (var == 1)
  2.  
Dec 18 '07 #3
mattmao
121 100+
An enum is not a data structure. It occupies no memory. An enum is a list of named integer values so you can use the name of the value in your code rather than the value itself:

Expand|Select|Wrap|Line Numbers
  1. enum Color = {RED, BLUE};
  2.  
  3. if (var == BLUE)
  4.  
rather than:
Expand|Select|Wrap|Line Numbers
  1. if (var == 1)
  2.  
Well, yeah. After reading the enumeration tutorial for one minute I realized that...

Now my question comes back to the beginning one: which would be the proper data structure to hold such a "index" list of those 29 "number words"?

I really don't want to be a guy that manually make 29 nodes and link them together to solve this problem. Reasons are obvious:
1) It works, but in a dumb way.
2) The search time would be linear, since it is a linked list.




I don't know if there are any other data collections types in ANSI C, besides the linked list.
Dec 19 '07 #4
mattmao
121 100+
Well, It seems that I worked it out:

Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2. int main()
  3. {
  4.     char *numberWords[3] = {"one", "two", "three"};
  5.     int i;
  6.  
  7.     for(i=0;i<3;i++)
  8.         printf("%s   ", numberWords[i]);
  9. }
  10.  
This uses an array of pointers to characters, then I can visit any element in the array by its index. I wonder if there are other proper solutions?
Dec 19 '07 #5
weaknessforcats
9,207 Expert Mod 8TB
Here's where you could use that enum:
Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2. enum Words {ONE, TWO, THREE, TWENTY};
  3. int main()
  4. {
  5.     char *numberWords[4] = {"one", "two", "three", "twenty"};
  6.     int i;
  7.  
  8.    printf("%s   ", numberWords[TWENTY]);
  9. }
  10.  
Dec 19 '07 #6
whodgson
542 512MB
Making a function called say int convert(int x) which includes switch(int x) for integers 0 to 14 and 16 to 29 takes care of all conversions of hours and minutes needed because after 30 min 60-? is required.
i.e. case 2: cout<<"two"<<endl;break;
The 4 special statements i.e. if (min==0)cout<<"The time is "<<convert(h)<<"o`clock<<endl; and so on for other 3.
Hope this not too agricultural.

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:



Expand|Select|Wrap|Line Numbers
  1.     #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4.  
  5. /*This struct is used to store each entered time.*/
  6. struct timeRecordClass
  7. {
  8.     int hourDigit;
  9.     int minuteDigit;
  10.     struct timeRecordClass *next;
  11. };
  12. struct timeRecordClass typedef timeRecord;
  13.  
  14. /*Here comes the function prototype section:*/
  15. void getInput(timeRecord *sentinel);
  16. void addNode(timeRecord *sentinel, int hourDigit, int minuteDigit);
  17. void getOutput(timeRecord *sentinel);
  18. void printOutTime(timeRecord *current);
  19. void printOClock(int hour, int minute);
  20.  
  21. /*The main function starts here:*/
  22. int main()
  23. {
  24.     /*A ring linked list with a sentinel would be used as the data structure.*/
  25.     timeRecord *sentinel = (timeRecord *) malloc(sizeof(timeRecord));
  26.     sentinel->hourDigit = -1;
  27.     sentinel->next = sentinel;
  28.  
  29.     printf("Please enter two numbers for the hour and the minute, separated by a space.\n");
  30.     printf("Note that the valid range for the hour is from 0 to 12;\n");
  31.     printf("and the valid range for the minute is from 0 to 59\n");
  32.     printf("If you wanna finish keyboard input, enter 0 0\n");
  33.  
  34.     getInput(sentinel);
  35.  
  36.     printf("Now check for the output!\n");
  37.     getOutput(sentinel);
  38.  
  39.     return 0;   
  40. }
  41.  
  42. void getInput(timeRecord *sentinel)
  43. {
  44.     char *hour, *minute;
  45.     char input[100];
  46.     int hourDigit, minuteDigit;
  47.  
  48.     fflush(stdin);
  49.  
  50.     /*Get user input and deal with it line by line.*/
  51.     do
  52.     {
  53.         if(fgets(input, 99, stdin)!=NULL)
  54.         {
  55.             hour = strtok(input, " ");
  56.             minute = strtok(NULL, "\n");
  57.             hourDigit = atoi(hour);
  58.             minuteDigit = atoi(minute);
  59.         }
  60.         else
  61.         {
  62.             printf("An error occurs when reading from user input!\n");    
  63.             exit(1);
  64.         }
  65.  
  66.         /*Check if user wanna stop entering the time from keyboard.*/
  67.         if(hourDigit==0&&minuteDigit==0)
  68.             break;
  69.  
  70.         /*Add a new timeRecord node into the ring linked list for later use.*/
  71.         addNode(sentinel, hourDigit, minuteDigit);
  72.  
  73.     }
  74.     while(1);
  75. }
  76.  
  77. void addNode(timeRecord *sentinel, int hourDigit, int minuteDigit)
  78. {
  79.     timeRecord *temp = (timeRecord *) malloc(sizeof(timeRecord));
  80.     timeRecord *current = sentinel;
  81.  
  82.     temp->hourDigit = hourDigit;
  83.     temp->minuteDigit = minuteDigit;
  84.  
  85.     /*Iterate through the ring linked list to locate the right posiiton to enter a new timeRecord node.*/
  86.     while(current->next != sentinel)
  87.         current = current->next;
  88.  
  89.     current->next = temp;
  90.     temp->next = sentinel;
  91. }
  92.  
  93. void getOutput(timeRecord *sentinel)
  94. {
  95.     timeRecord *current = sentinel;
  96.     int flag = 0;
  97.  
  98.     while(1)
  99.     {
  100.         /*Skip the sentinel node for the first time.*/
  101.         /*When you see it for the second time, you have finished printing out all the nodes.*/
  102.         if(current->hourDigit == -1)
  103.         {
  104.             current = current->next;
  105.             flag =flag +1;
  106.             if(flag == 1)
  107.                 continue;
  108.             else break;
  109.         }
  110.         //printf("%d:%d\n", current->hourDigit, current->minuteDigit);
  111.         printOutTime(current);
  112.         current = current->next;
  113.     }
  114. }
  115.  
  116. void printOutTime(timeRecord *current)
  117. {
  118.     int hour = current->hourDigit;
  119.     int minute = current->minuteDigit;
  120.  
  121.     switch(minute)
  122.     {
  123.         case 0:
  124.             printOClock(hour, minute);
  125.             break;
  126.         case 15:
  127.             printQuarterPast(hour, minute);
  128.             break;
  129.         case 30:
  130.             printHalfPast(hour, minute);
  131.             break;
  132.         case 45:
  133.             printQuarterTo(hour, minute);
  134.             break;
  135.         default:
  136.             printRegular(hour, minute);
  137.             break;    
  138.     }
  139. }
  140.  
Dec 27 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by kosh | last post: by
6 posts views Thread by Kenneth | last post: by
9 posts views Thread by Jon LaBadie | last post: by
13 posts views Thread by cefrancke | last post: by
66 posts views Thread by jacob navia | last post: by
2 posts views Thread by Gary Wessle | last post: by
1 post views Thread by bux | last post: by
3 posts views Thread by Hutch | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.