c++第二次作业

一时间增加

程序代码

[cpp]  view plain  copy
  1. #include <iostream>  
  2. using namespace std;  
  3. class Time  
  4. {  
  5. public:  
  6.     void set_time( );  
  7.     void show_time( );  
  8.     void add_a_sec();  //增加1秒钟  
  9.     void add_a_minute(); //增加1分钟  
  10.     void add_an_hour(); //增加1小时  
  11.     void add_seconds(int n1); //增加n秒钟  
  12.     void add_minutes(int n2); //增加n分钟  
  13.     void add_hours(int n3); //增加n小时  
  14. private:  
  15.     bool is_time(intintint);  
  16.     int hour;  
  17.     int minute;  
  18.     int sec;  
  19. };  
  20. void Time::set_time( )  
  21. {  
  22.     char c1,c2;  
  23.     cout<<"请输入时间(格式hh:mm:ss)"<<endl;  
  24.     while(1)  
  25.     {   cin>>hour>>c1>>minute>>c2>>sec;  
  26.         if(c1!=':'||c2!=':')  
  27.             cout<<"格式不正确,请重新输入"<<endl;  
  28.         else if (!is_time(hour,minute,sec))  
  29.             cout<<"时间非法,请重新输入"<<endl;  
  30.         else  
  31.             break;  
  32.     }  
  33. }  
  34. void Time::add_a_sec()  
  35. {  
  36.     sec++;  
  37.     if(sec>=60)  
  38.     {  
  39.         sec=sec%60;  
  40.         minute++;  
  41.         if(minute>=60)  
  42.         {  
  43.             minute=minute%60;  
  44.             hour++;  
  45.             if(hour>=24)  
  46.             {  
  47.                 hour=hour%24;  
  48.             }  
  49.         }  
  50.     }  
  51. }  
  52. void Time::add_a_minute()  
  53. {  
  54.     minute++;  
  55.         if(minute>=60)  
  56.         {  
  57.             minute=minute%60;  
  58.             hour++;  
  59.             if(hour>=24)  
  60.             {  
  61.                 hour=hour%24;  
  62.             }  
  63.         }  
  64. }  
  65. void Time::add_an_hour()  
  66. {  
  67.     hour++;  
  68.             if(hour>=24)  
  69.             {  
  70.                 hour=hour%24;  
  71.             }  
  72. }  
  73. void Time::add_seconds(int n1)  
  74. {  
  75.     cout<<"增加的秒数:"<<endl;  
  76.     cin>>n1;  
  77.     sec+=n1;  
  78.     if(sec>=60)  
  79.     {  
  80.   
  81.         minute+=(sec/60); sec=sec%60;  
  82.         if(minute>=60)  
  83.         {  
  84.             hour+=(minute/60);  
  85.             minute=minute%60;  
  86.             if(hour>=24)  
  87.             {  
  88.                 hour=hour%24;  
  89.             }  
  90.         }  
  91.     }  
  92. }  
  93. void Time::add_minutes(int n2)  
  94. {  
  95.     cout<<"增加的分数:"<<endl;  
  96.     cin>>n2;  
  97.     minute+=n2;  
  98.     if(minute>=60)  
  99.     {  
  100.         hour+=(minute/60);  
  101.         minute=minute%60;  
  102.         if(hour>=24)  
  103.         {  
  104.             hour=hour%24;  
  105.         }  
  106.     }  
  107. }  
  108. void Time::add_hours(int n3)  
  109. {  
  110.     cout<<"增加的时数:"<<endl;  
  111.     cin>>n3;  
  112.     hour+=n3;  
  113.     if(hour>=24)  
  114.     {  
  115.         hour=hour%24;  
  116.     }  
  117. }  
  118. void Time::show_time( )  
  119. {  
  120.     cout<<hour<<":"<<minute<<":"<<sec<<endl;  
  121. }  
  122. bool Time::is_time(int h,int m, int s)  
  123. {  
  124.     if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)  
  125.         return false;  
  126.     return true;  
  127. }  
  128. int main( )  
  129. {  
  130.     Time t1;  
  131.     int n1,n2,n3;  
  132.     t1.set_time( );  
  133.     t1.add_a_sec();  
  134.     t1.show_time( );  
  135.     t1.set_time( );  
  136.     t1.add_a_minute();  
  137.     t1.show_time( );  
  138.     t1.set_time( );  
  139.     t1.add_an_hour();  
  140.     t1.show_time( );  
  141.     t1.set_time( );  
  142.     t1.add_seconds(n1);  
  143.     t1.show_time( );  
  144.     t1.set_time( );  
  145.     t1.add_minutes(n2);  
  146.     t1.show_time( );  
  147.     t1.set_time( );  
  148.     t1.add_hours(n3);  
  149.     t1.show_time( );  
  150.     return 0;  
  151. }  

二 正整数类

程序代码

[cpp]  view plain  copy
  1. #include<iostream>  
  2. using namespace std;  
  3. class NaturalNumber  
  4. {  
  5. private:  
  6.     int n;  
  7. public:  
  8.     void setValue (int x);//置数据成员n的值,要求判断是否是正整数  
  9.     int getValue();  //返回私有数据成员n的值  
  10.     bool isPrime();  //判断数据成员n是否为素数,是返回true,否则返回false  
  11.     void printFactor();  //输出数据成员n的所有因子,包括1和n自身  
  12.     bool isPerfect(); //判断数据成员n是否为完全数。若一个正整数n的所有小于n的因子之和等于n, 则称n为完全数, 如6=1+2+3是完全数。  
  13.     bool isReverse(int x);//判断形式参数x是否为数据成员n的逆向数(例321是123的逆向数)。  
  14.     bool isDaffodil(int x); //判断形式参数x是否是水仙花数。水仙花数的各位数字立方和等于该数,如153=1*1*1+5*5*5+3*3*3  
  15.     void printDaffodils(); //显示所有大于1,且小于数据成员n的水仙花数;  
  16. };  
  17. void NaturalNumber::setValue(int x)//置数据成员n的值,要求判断是否是正整数  
  18. {  
  19.     n=x;  
  20.     if(n>0&&n%1==0)  
  21.         cout<<"是正整数"<<endl;  
  22.     else cout<<"不是正整数"<<endl;  
  23. }  
  24. int NaturalNumber::getValue()//返回私有数据成员n的值  
  25. {  
  26.     return n;  
  27. }  
  28. bool NaturalNumber::isPrime()//判断数据成员n是否为素数,是返回true,否则返回false  
  29. {  
  30.     int i;  
  31.     for(i=2;i<n;i++)  
  32.     {  
  33.         if(n%i==0)  
  34.             return false;  
  35.     }  
  36.     return true;  
  37. }  
  38. void NaturalNumber::printFactor()//输出数据成员n的所有因子,包括1和n自身  
  39. {  
  40.     int i;  
  41.     for(i=1;i<=n;i++)  
  42.     {  
  43.         if(n%i==0)  
  44.             cout<<i<<" ";  
  45.     }  
  46.     cout<<endl;  
  47. }  
  48. bool NaturalNumber::isPerfect()//判断数据成员n是否为完全数。若一个正整数n的所有小于n的因子之和等于n, 则称n为完全数, 如6=1+2+3是完全数。  
  49. {  
  50.     int i,sum;  
  51.     sum=0;  
  52.     for(i=1;i<n;i++)  
  53.     {  
  54.         if(n%i==0)  
  55.             sum+=i;  
  56.     }  
  57.     if(sum==n)  
  58.         cout<<"是完全数"<<endl;  
  59.     else cout<<"不是完全数"<<endl;  
  60. }  
  61. bool NaturalNumber::isReverse(int x)//判断形式参数x是否为数据成员n的逆向数(例321是123的逆向数)  
  62. {  
  63.     int sum=0;  
  64.     while(x)  
  65.     {  
  66.         sum=sum*10+x%10;  
  67.         x/=10;  
  68.     }  
  69.     if(n==sum)  
  70.         cout<<"是逆向数"<<endl;  
  71.     else  
  72.         cout<<"不是逆向数"<<endl;  
  73. }  
  74. bool NaturalNumber::isDaffodil(int x)//判断形式参数x是否是水仙花数。水仙花数的各位数字立方和等于该数,如153=1*1*1+5*5*5+3*3*3  
  75. {  
  76.     int a,b,c;  
  77.     a=x/100;  
  78.     b=(x/10)%10;  
  79.     c=x%10;  
  80.     if(x==a*a*a+b*b*b+c*c*c)  
  81.         cout<<"是水仙花数"<<endl;  
  82.     else cout<<"不是水仙花数"<<endl;  
  83. }  
  84. void NaturalNumber::printDaffodils()//显示所有大于1,且小于数据成员n的水仙花数;  
  85. {  
  86.     int i;  
  87.     int a,b,c;  
  88.     for(i=2;i<n;i++)  
  89.     {  
  90.         a=i/100;  
  91.         b=(i/10)%10;  
  92.         c=i%10;  
  93.         if(i==a*a*a+b*b*b+c*c*c)  
  94.         cout<<i<<" ";  
  95.     }  
  96. }  
  97. int main()  
  98. {  
  99.     NaturalNumber nn;   //定义类的一个实例(对象)  
  100.     nn.setValue (321);  
  101.     nn.getValue();  
  102.     cout<<"ok"<<endl;  
  103.     nn.isPrime();  
  104.     cout<<endl;  
  105.     nn.printFactor();  
  106.     cout<<endl;  
  107.     nn.isPerfect();  
  108.     cout<<endl;  
  109.     nn.isReverse(123);  
  110.     cout<<endl;  
  111.     nn.isDaffodil(200);  
  112.     cout<<endl;  
  113.     nn.printDaffodils();  
  114.     cout<<endl;  
  115. return 0;  
  116. }  

三 Book类

程序代码

[cpp]  view plain  copy
  1. #include<iostream>  
  2. using namespace std;  
  3. class Book  
  4. {  
  5. public:  
  6.     void setBook(string n,string w,string N,string pub,float pri,int num);  
  7.     int borrow();  
  8.     int restore();  
  9.     void print();  
  10.     string set_NO(string a);  
  11.     string get_NO();  
  12. private:  
  13.     string name;  
  14.     string writer;  
  15.     string publicer;  
  16.     float price;  
  17.     int number;  
  18.     string NO;  
  19. };  
  20. void Book::setBook(string n,string w,string N,string pub,float pri,int num)  
  21. {  
  22.     name=n;  
  23.     writer=w;  
  24.     NO=N;  
  25.     publicer=pub;  
  26.     price=pri;  
  27.     number=num;  
  28. }  
  29. int Book::borrow()  
  30. {  
  31.     number--;  
  32.     return number;  
  33. }  
  34. int Book::restore()  
  35. {  
  36.     number++;  
  37.     return number;  
  38. }  
  39. void Book::print()  
  40. {  
  41.     cout<<name<<" "<<writer<<" "<<NO<<" "<<publicer<<" "<<price<<" "<<number<<endl;  
  42. }  
  43. string Book::set_NO(string a)  
  44. {  
  45.     NO=a;  
  46.     return NO;  
  47. }  
  48. string Book::get_NO()  
  49. {  
  50.     return NO;  
  51. }  
  52. int main()  
  53. {  
  54.     Book b1;  
  55.     b1.setBook("C++","某某","12.34","某某出版社",30,100);  
  56.     b1.print();  
  57.     cout<<b1.borrow()<<endl;  
  58.     cout<<b1.restore()<<endl;  
  59.     cout<<b1.set_NO("12.35")<<endl;  
  60.     cout<<b1.get_NO()<<endl;  
  61.     return 0;  
  62. }  
  63. //编写一个Book类,包含name(书名)、writer(作者)、publicer(出版社)、price(价格)、number(数量)、NO(书号)等数据成员。  
  64. //定义成员函数setBook,用来给书籍的数据成员赋值,定义成员函数borrow和restore,分别办理借书和还书(借、还书时,暂时只完成数量的增1减1)业务,  
  65. //定义成员函数print用于输出—有关书的信息,定义set_NO与get_NO来实现修改类对象的书号与获得类对象的书号。  
  66. //自己写main函数,用来测试你编写的类。

四 分数类

程序代码

[cpp]  view plain  copy
  1. #include<iostream>  
  2. using namespace std;  
  3. class CFraction  
  4. {  
  5. public:  
  6.     CFraction(int nu=0,int de=1);   //构造函数,初始化用  
  7.     void set(int nu=0,int de=1);    //置值,改变值时用  
  8.     void input();               //按照"nu/de"的格式,如"5/2"的形式输入  
  9.     void simplify();            //化简(使分子分母没有公因子)  
  10.     void amplify(int n);        //放大n倍,如2/3放大5倍为10/3  
  11.     void output(int style=0);   //输出:以8/6为例,style为0时,原样输出8/6;  
  12.                              //style为1时,输出化简后形式4/3;  
  13.                         //style为2时,输出1(1/3)形式,表示一又三分之一;  
  14.                             //style为3时,用小数形式输出,如1.3333;  
  15.                             //默认方式0  
  16. private:  
  17.     int nume;  // 分子  
  18.     int deno;  // 分母  
  19. };  
  20. CFraction::CFraction(int nu,int de)  
  21. {  
  22.     nume=nu;  
  23.     deno=de;  
  24. }  
  25. void CFraction::set(int nu,int de) //置值,改变值时用  
  26. {  
  27.     nume=nu;  
  28.     deno=de;  
  29. }  
  30. void CFraction::input()//按照"nu/de"的格式,如"5/2"的形式输入  
  31. {  
  32.     char c;  
  33.     cout<<"请输入值"<<endl;  
  34.     cin>>nume>>c>>deno;  
  35.     if(c!='/')  
  36.     {  
  37.         cout<<"输入错误"<<endl;  
  38.     }  
  39. }  
  40. void CFraction::simplify()//化简(使分子分母没有公因子)  
  41. {  
  42.     int t,r;  
  43.     int n,d;  
  44.     n=nume;  
  45.     d=deno;  
  46.     if(n<d)  
  47.     {  
  48.         t=n;  
  49.         n=d;  
  50.         d=t;  
  51.     }  
  52.     while(d!=0)  
  53.     {  
  54.         r=n%d;  
  55.         n=d;  
  56.         d=r;  
  57.     }  
  58.     //cout<<nume/n<<" "<<deno/n<<endl;  
  59.     nume=nume/n;  
  60.     deno=deno/n;  
  61. }  
  62. void CFraction::amplify(int n)//放大n倍,如2/3放大5倍为10/3  
  63. {  
  64.     cout<<nume*n<<"/"<<deno<<endl;  
  65. }  
  66. void CFraction::output(int style)  //输出:以8/6为例,style为0时,原样输出8/6;  
  67.                              //style为1时,输出化简后形式4/3;  
  68.                         //style为2时,输出1(1/3)形式,表示一又三分之一;  
  69.                             //style为3时,用小数形式输出,如1.3333;  
  70.                             //默认方式0  
  71. {  
  72.     if(style==0)  
  73.     {  
  74.         cout<<nume<<"/"<<deno<<endl;  
  75.     }  
  76.     if(style==1)  
  77.     {  
  78.         simplify();  
  79.         cout<<nume<<"/"<<deno<<endl;  
  80.     }  
  81.     if(style==2)  
  82.     {  
  83.         if(nume>deno)  
  84.         {  
  85.             int i,j;  
  86.             i=nume/deno;  
  87.             j=nume%deno;  
  88.             cout<<i<<"("<<j<<"/"<<deno<<")"<<endl;  
  89.         }  
  90.         else cout<<nume<<"/"<<deno<<endl;  
  91.     }  
  92.     if(style==3)  
  93.     {  
  94.         double p;  
  95.         p=nume/deno;  
  96.         cout<<p<<endl;  
  97.     }  
  98. }  
  99. int main()  
  100. {  
  101.     int nu,de,style,n;  
  102.     CFraction c;  
  103.     c.set(nu=0,de=1);    //置值,改变值时用  
  104.     c.input();               //按照"nu/de"的格式,如"5/2"的形式输入  
  105.     c.simplify();  
  106.     c.output();        //化简(使分子分母没有公因子)  
  107.     c.amplify(2);        //放大n倍,如2/3放大5倍为10/3  
  108.     c.output(style=0);  
  109.     return 0;  
  110. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值