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: -
#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;
-
}
-
}
-
6 1772
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:)
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: -
enum Color = {RED, BLUE};
-
-
if (var == BLUE)
-
rather than:
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: -
enum Color = {RED, BLUE};
-
-
if (var == BLUE)
-
rather than:
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.
Well, It seems that I worked it out: -
#include "stdio.h"
-
int main()
-
{
-
char *numberWords[3] = {"one", "two", "three"};
-
int i;
-
-
for(i=0;i<3;i++)
-
printf("%s ", numberWords[i]);
-
}
-
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?
Here's where you could use that enum: -
#include "stdio.h"
-
enum Words {ONE, TWO, THREE, TWENTY};
-
int main()
-
{
-
char *numberWords[4] = {"one", "two", "three", "twenty"};
-
int i;
-
-
printf("%s ", numberWords[TWENTY]);
-
}
-
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: -
#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;
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
| |