Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 8:45 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: What Time Is It?
PostPosted: Sat Jun 30, 2007 11:02 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2272
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

Years ago, Brian Redman amused the entire networking community by asking on a newsgroup, ``What time is it? E-mail me and I'll summarize to the net.'' Ha ha.

Write a program to convert a time expressed as hour:minute to the (stylized, see below) American english language version of the time. Here are the rules to implement (note that they might be different from what you are used to and are certainly different from British rules):

* Capitalize the first letter of the output
* Compound english numbers are hyphenated, e.g.: forty-four
* Express x:00 as [x_in_english] o'clock
* Express x:15 as Quarter past [x_in_english]
* Express x:30 as [x_in_english] thirty
* Express x:45 as Quarter to [next_hour_in_english]
* Otherwise, express x:nn as:
o [x_in_english] [nn_in_english] when nn<45
o [60-nn_in_english] to [next_hour_in_english] when nn>45

Examples:

* 5:00 Five o'clock
* 10:10 Ten ten
* 9:22 Nine twenty-two
* 5:15 Quarter past five
* 2:30 Two thirty
* 6:40 Six forty
* 5:45 Quarter to six
* 8:47 Thirteen to nine

PROGRAM NAME: clock
INPUT FORMAT
A single line with a time expressed as hour:minutes. Each hour is in the range 1..12; minutes are always expressed in two digits and are in the range 0..59
SAMPLE INPUT (file clock.in)
5:45
OUTPUT FORMAT
A single line with the time expressed as English.
SAMPLE OUTPUT (file clock.out)

Quarter to six

The solution by ACM student :
Code:
/*

[u][b]PROG: clock[/b][/u]
*/

/* Ad hoc problem */

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

char *tens[]={"twenty","thirty","forty","fifty"};
char *ones[]={"","one","two","three","four","five","six","seven","eight","nine"};
char *pten[]={"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen"};

string noeng(int no)
{
  if ((no>0) && (no<10)) return ones[no];
  if ((no>=10) && (no<20)) return pten[no-10];
  string ab;
  ab=string(tens[(int)(no/10)-2])+(((no%10)==0)?"":"-"+string(ones[no%10]));
  return ab;
}

void main()
{
  fstream fin,fout;
  fin.open("clock.in",ios::in);
  cin=fin;
  fout.open("clock.out",ios::out); 

  string st;
  cin >> st;

  int hr=0,min=0,k;
  for (k=0; st[k]!=':'; k++)
    hr=hr*10+st[k]-'0';
  for (k++; k<st.size(); k++)
    min=min*10+st[k]-'0';

  string timer;
  int nexthr=(hr+1)%12;
  if (nexthr==0) nexthr=1;
  if (min==0) timer=noeng(hr)+" o'clock";
  else if (min==15) timer="Quarter past "+noeng(hr);
  else if (min==30) timer=noeng(hr)+" thirty";
  else if (min==45) timer="Quarter to "+noeng(nexthr);
  else if (min<45) timer=noeng(hr)+" "+noeng(min);
  else if (min>45) timer=noeng(60-min)+" to "+noeng(nexthr);
  if ((timer[0]>='a') && (timer[0]<='z')) timer[0]=timer[0]-'a'+'A';
  fout << timer << endl;
  fout.close();   
}

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 Plz Help,I want BOTH THREAD to work at a time in SAME WINDOW
 Selecting a Domain Name at the Time of Registration
 login using sessionid or time
 Time countdown
 Having a hard time with this class need help deseperately.
 calculate the load time for a page
 current system time
 Java Websphere Portal Developer Full-time position in IL
 Part Time Online Jobs
 First Name:raju visa process time

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team