473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1852
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 made more efficient. Right now if you do use a...
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 (in the format of HH:MM), and another script...
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 and needs the clients timeoffset for showing the...
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 ulimit of 0 bytes. Which calls would be expected to...
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 launch the Print Dialog (like 'Ctrl-P' or...
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 am using that word very often in my tutorial of...
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 is from the tut thanks index = 0
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 slider control of similar sort.plz let me knw if...
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 comes 2.5. Every thing seems to work the same except...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.