Switch to full style
For C/C++ coders discussions and solutions
Post a reply

What Time Is It?

Sat Jun 30, 2007 11:02 pm

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();   
}




Post a reply
  Related Posts  to : What Time Is It?
 run time polymorphism     -  
 explode time     -  
 Time countdown     -  
 max execution time     -  
 login using sessionid or time     -  
 current system time     -  
 Calculate process time     -  
 How to get system time in java     -  
 time for which a key was kept pressed by user     -  
 Time Stamp with mktime()     -  

Topic Tags

C++ Algorithms