472,340 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,340 software developers and data experts.

[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 1772
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,208 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,208 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

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

Similar topics

9
by: kosh | last post by:
I was wondering if there is or there could be some way to pass a generator an optional starting index so that if it supported that slicing could be...
6
by: Kenneth | last post by:
Hello, I'm having some serious problems debugging a script that I'm trying to make work. I'm working on a form where a user can type in a time...
2
by: hourang | last post by:
ok im getting tired of looking for an answer and coming up short with scripts that dont work. i have a application that uses GMT for all its times...
9
by: Jon LaBadie | last post by:
Suppose I'm using stdio calls to write to a disk file. One possible error condition is no space on file system or even (in unix environment) a...
13
by: cefrancke | last post by:
I have a custom toolbar for users during any preview of a report launched by a button click on a form. I would like to add a toolbar button to...
66
by: jacob navia | last post by:
The english word "Initialized" exists. (Cambridge dictionary finds it). The word "Uninitialized" doesn't seem to exist, and no dictionary has it. I...
2
by: Gary Wessle | last post by:
I am going through this tut from http://ibiblio.org/obp/thinkCS/python/english/chap07.htm I am getting errors running those 2 groups as below as...
1
by: bux | last post by:
hi every body, i want to know if any one has used the kind of time slider control which is used in the google finance. i need to implement a time...
3
by: Hutch | last post by:
PythonWin has been a very good ide from early version thru 2.4. All work ok on THREE of my computers with THREE different HP printers. Now...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.