U E D R , A S I H C RSS

[Lovely]boy^_^/USACO/What Time Is It?

  • 좀 잡다한 함수가 마니 들어갔군..
  • 자주 만들어서 쓰는 함수는 라이브러리화를 해야겠다


~cpp 
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;

ifstream fin("clock.in");
ofstream fout("clock.out");

int StringConvertToInt(const string& str);
int CharToInt(char ch);
int Jegob(int c, int e);

void InputInitData(int& h, int& m);
void OutputData(int&h, int& m);
void SetTable();
string Upcase(const string& str);

map<int,string> table;

int main()
{
	int hour, min;
	SetTable();

	InputInitData(hour, min);
	OutputData(hour, min);

	return 0;
}

void OutputData(int& hour, int& min)
{
	if(min >= 45)
	{
		if(hour != 12)
			fout << Upcase(table[60-min]) << " to " << table[hour+1];
		else
			fout << Upcase(table[60-min]) << " to " << table[1];
	}
	else if(min == 15)
		fout << Upcase(table[15]) << " past " << table[hour];
	else if(min%10)	
	{
		if(min/10)
		{
			if(min/10 == 1)
				fout << Upcase(table[hour]) << " " << table[min];
			else
				fout << Upcase(table[hour]) << " " << table[(int)(min/10*10)] << "-" << table[min%10];
		}
		else
			fout << Upcase(table[hour]) << " " << table[min%10];		
	}
	else
		fout << Upcase(table[hour]) << " " << table[min];	
	fout << endl;
}

string Upcase(const string& str)
{
	string ret = str;
	ret[0] = toupper(str[0]);
	return ret;
}

int StringConvertToInt(const string& str)
{
	int ret = 0;
	for(int i = 0 ; i < str.length() ; ++i)
	{
		ret += CharToInt(str[i]) * Jegob(10,(str.length() - i));
	}
	return ret;
}

int CharToInt(char ch)
{
	return ch - 48;
}

int Jegob(int c, int e)
{
	int ret = 1;
	for(int i = 0 ; i < e - 1 ; ++i)
	{
		ret *= c;
	}
	return ret;
}

void InputInitData(int& hour, int& min)
{
	string a;
	fin >> a;

	int token = 0;
	for(int i = 0 ; i < a.length() ; ++i)
	{
		if(a[i] == ':')
			token = i;
	}
	string h(a.begin(), a.begin() + token);
	hour = StringConvertToInt(h);

	string m(a.begin() + token + 1, a.end());
	min = StringConvertToInt(m);
}

void SetTable()
{
	table[0] = "o'clock";
	table[1] = "one";
	table[2] = "two";
	table[3] = "three";
	table[4] = "four";
	table[5] = "five";
	table[6] = "six";
	table[7] = "seven";
	table[8] = "eight";
	table[9] = "nine";
	table[10] = "ten";
	table[11] = "eleven";
	table[12] = "twelve";
	table[13] = "thirteen";
	table[14] = "fourteen";
	table[15] = "quarter";
	table[16] = "sixteen";
	table[17] = "seventeen";
	table[18] = "eighttenn";
	table[19] = "nineteen";
	table[20] = "twenty";
	table[30] = "thirty";
	table[40] = "forty";
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:37
Processing time 0.0167 sec