君应怜


私信TA

用户名:15121662051

访问量:3345

签 名:

等  级
排  名 9315
经  验 1166
参赛次数 2
文章发表 5
年  龄 0
在职情况 学生
学  校 冀中职业学院
专  业

  自我简介:

TA的其他文章

C++与C分别描述
浏览:435

________________________________________

面向对象

#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 人评分

  评论区

  • «
  • »