________________________________________
面向对象
#include<iostream>
using namespace std;
class Time
{
public:
Time(int h,int m):m_hour(h),m_minuter(m){}
void getTime();
void changeHour();
void changeMinuter();
char* DecisionFigure(int i);
void split(int i);
private:
int m_hour;
int m_minuter;
};
char* Time::DecisionFigure(int i)
{
switch(i)
{
case 0:return "zero";
case 1:return "one";
case 2:return "two";
case 3:return "three";
case 4:return "four";
case 5:return "five";
case 6:return "six";
case 7:return "seven";
case 8:return "eight";
case 9:return "nine";
case 10:return "ten";
case 11:return "eleven";
case 12:return "twelve";
case 13:return "thirteen";
case 14:return "fourteen";
case 15:return "fifteen";
case 16:return "sixteen";
case 17:return "seventeen";
case 18:return "eighteen";
case 19:return "nineteen";
case 20:return "twenty";
case 30:return "thirty";
case 40:return "forty";
case 50:return "fifty";
}
}
void Time::split(int i)
{
int temp1,temp2;
temp2 = i%10;
temp1 = i-temp2;
cout << DecisionFigure(temp1) << " " << DecisionFigure(temp2)<< " ";
}
void Time::changeHour()
{
int temp = m_hour;
if(0<=temp&&temp<=20)
cout << DecisionFigure(temp) << " ";
else if(temp>20)
split(temp);//拆分
}
void Time::changeMinuter()
{
int temp = m_minuter;
if(0<=temp&&temp<=20)
cout << DecisionFigure(temp) << endl;
else if(20<temp&&temp<60)
split(temp);
}
void Time::getTime()
{
changeHour();
if(m_minuter!=0)
changeMinuter();
else
cout << "o'clock" << endl;
}
void test()
{
int h,m;
cin >> h;
cin >> m;
Time t1(h,m);
t1.getTime();
}
int main(void)
{
test();
}
____________________________________
做一个简单的优化
————————————————————
#include<iostream>
using namespace std;
class Time
{
public:
Time(int h,int m):m_hour(h),m_minuter(m){}
void getTime();
void changeHour();
void changeMinuter();
void DecisionFigure(int i);
void split(int i);
private:
int m_hour;
int m_minuter;
};
void Time::DecisionFigure(int i)
{
switch(i)
{
case 0: printf("zero "); break;
case 1: printf("one "); break;
case 2: printf("two "); break;
case 3: printf("three "); break;
case 4: printf("four "); break;
case 5: printf("five "); break;
case 6: printf("six "); break;
case 7: printf("seven "); break;
case 8: printf("eight "); break;
case 9: printf("nine "); break;
case 10: printf("ten "); break;
case 11: printf("eleven "); break;
case 12: printf("twelve "); break;
case 13: printf("thirteen "); break;
case 14: printf("fourteen "); break;
case 15: printf("fifteen "); break;
case 16: printf("sixteen "); break;
case 17: printf("seventeen "); break;
case 18: printf("eighteen "); break;
case 19: printf("nineteen "); break;
case 20: printf("twenty "); break;
case 30: printf("thirty "); break;
case 40: printf("forty "); break;
case 50: printf("fifty "); break;
default: break;
}
}
void Time::split(int i)
{
int temp1,temp2;
temp2 = i%10;
temp1 = i-temp2;
DecisionFigure(temp1);
DecisionFigure(temp2);
}
void Time::changeHour()
{
int temp = m_hour;
if(0<=temp&&temp<=20)
DecisionFigure(temp);
else if(temp>20)
split(temp);//拆分
}
void Time::changeMinuter()
{
int temp = m_minuter;
if(0<=temp&&temp<=20)
DecisionFigure(temp);
else if(20<temp&&temp<60)
split(temp);
}
void Time::getTime()
{
changeHour();
if(m_minuter!=0)
changeMinuter();
else
cout << "o'clock" << endl;
}
void test()
{
int h,m;
cin >> h;
cin >> m;
Time t1(h,m);
t1.getTime();
}
int main(void)
{
test();
}
________________________________
面向过程
———————————————————————
#include<stdio.h>
void DecisionFigure(int t){
switch(t){
case 0: printf("zero "); break;
case 1: printf("one "); break;
case 2: printf("two "); break;
case 3: printf("three "); break;
case 4: printf("four "); break;
case 5: printf("five "); break;
case 6: printf("six "); break;
case 7: printf("seven "); break;
case 8: printf("eight "); break;
case 9: printf("nine "); break;
case 10: printf("ten "); break;
case 11: printf("eleven "); break;
case 12: printf("twelve "); break;
case 13: printf("thirteen "); break;
case 14: printf("fourteen "); break;
case 15: printf("fifteen "); break;
case 16: printf("sixteen "); break;
case 17: printf("seventeen "); break;
case 18: printf("eighteen "); break;
case 19: printf("nineteen "); break;
case 20: printf("twenty "); break;
case 30: printf("thirty "); break;
case 40: printf("forty "); break;
case 50: printf("fifty "); break;
default: break;
}
}
void getTime(int h,int m)
{
if(0<=h&&h<=20)
DecisionFigure(h);
else
{
DecisionFigure(h/10*10);
DecisionFigure(h%10);
}
if(m == 0)
printf("o'clock");
else if(m >= 1 && m <= 20)
DecisionFigure(m);
else{
DecisionFigure(m/10*10);
DecisionFigure(m%10);
}
}
int main(void)
{
int h,m;
scanf("%d%d",&h,&m);
getTime(h,m);
}
0.0分
0 人评分
C二级辅导-计负均正 (C语言代码)浏览:556 |
奖学金 (C++代码)浏览:2053 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:644 |
上车人数 (C语言代码)浏览:816 |
数组输出 (C语言代码)--此题的题目描述有问题浏览:1844 |
WU-小九九 (C++代码)浏览:1713 |
C语言程序设计教程(第三版)课后习题10.4 (C语言代码)浏览:943 |
sizeof的大作用 (C语言代码)浏览:1593 |
Tom数 (C语言代码)浏览:517 |
printf基础练习2 (C语言代码)浏览:547 |