C++第二次实验_作业(4个源代码)

一、阅读、运行程序后,按要求增加类的功能:

[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_an_hour()   
  9.     {  
  10.         hour++;  
  11.     }  
  12.     void add_a_minute()  
  13.     {  
  14.         minute++;  
  15.         if(minute>=60)  
  16.         {  
  17.             add_an_hour();  
  18.                 minute=0;  
  19.         }  
  20.     }  
  21.     void add_a_sec()  
  22.     {  
  23.         sec++;  
  24.         if(sec>=60)  
  25.         {  
  26.             add_a_minute();  
  27.                 sec=0;  
  28.         }  
  29.     }  
  30.     void add_seconds(int n); //增加n秒钟  
  31.     void add_minutes(int n); //增加n分钟  
  32.     void add_hours(int n);  
  33. private:   
  34.     bool is_time(intintint);   
  35.     int hour;  
  36.     int minute;  
  37.     int sec;  
  38. };  
  39. void Time::set_time( )   
  40. {  
  41.     char c1,c2;  
  42.     cout<<"请输入时间(格式hh:mm:ss)";  
  43.     while(1)  
  44.     {   cin>>hour>>c1>>minute>>c2>>sec;  
  45.         if(c1!=':'||c2!=':')  
  46.             cout<<"格式不正确,请重新输入"<<endl;  
  47.         else if (!is_time(hour,minute,sec))  
  48.             cout<<"时间非法,请重新输入"<<endl;  
  49.         else   
  50.             break;  
  51.     }  
  52. }  
  53. void Time::show_time( )        
  54. {  
  55.     cout<<hour<<":"<<minute<<":"<<sec<<endl;  
  56. }  
  57. bool Time::is_time(int h,int m, int s)  
  58. {  
  59.     if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)  
  60.         return false;  
  61.     return true;  
  62. }  
  63. void Time::add_hours(int n)  
  64. {  
  65.     hour+=n;  
  66. }  
  67. void Time::add_minutes(int n)  
  68. {  
  69.     minute+=n;  
  70.     if(minute>60)  
  71.     {  
  72.         hour+=minute/60;  
  73.         minute%=60;  
  74.     }  
  75. }  
  76. void Time::add_seconds(int n)  
  77. {  
  78.     sec+=n;  
  79.     if(sec>60)  
  80.     {  
  81.         add_minutes(sec/60);  
  82.             sec/=60;  
  83.     }  
  84. }  
  85. int main( )  
  86. {  
  87.     Time t1;    
  88.     int n;  
  89.     t1.set_time( );     
  90.     t1.show_time( );  
  91.     cout<<"\n增加一秒后:";  
  92.     t1.add_a_sec();  
  93.     t1.show_time( );  
  94.     cout<<endl;  
  95.     cout<<"再增加一分后:";  
  96.     t1.add_a_minute();  
  97.     t1.show_time( );  
  98.     cout<<endl;  
  99.     cout<<"\n增加一时后:";  
  100.     t1.add_an_hour();  
  101.     t1.show_time( );  
  102.     cout<<"\n请输入需要增加的秒数:";  
  103.     cin>>n;  
  104.     cout<<"\n增加"<<n<<"秒后:";  
  105.     t1.add_seconds(n);  
  106.     t1.show_time( );  
  107.     cout<<"\n请输入需要增加的分数:";  
  108.     cin>>n;  
  109.     cout<<"\n增加"<<n<<"分后:";  
  110.     t1.add_minutes(n);  
  111.     t1.show_time( );  
  112.     cout<<"\n请输入需要增加的时数:";  
  113.     cin>>n;  
  114.     cout<<"\n增加"<<n<<"时后:";  
  115.     t1.add_hours(n);  
  116.     t1.show_time( );  
  117.     return 0;  
  118. }  


二、正整数类

[cpp]  view plain  copy
  1. #include<iostream>    
  2. using namespace std;    
  3. class NaturalNumber    
  4. {private:    
  5.     int n;     
  6. public:    
  7.     void setValue (int x);  
  8.     int getValue();  
  9.     bool isPrime();    
  10.     void printFactor();   
  11.     bool isPerfect();  
  12.     bool isDaffodil(int x);  
  13.     void printDaffodils();  
  14. };  
  15. void NaturalNumber::setValue (int x)  
  16. {  
  17.     if(x>0&&(int)x==x)  
  18.         cout<<x<<"是正整数"<<endl;  
  19.     n=x;  
  20. }  
  21. int NaturalNumber::getValue()  
  22. {  
  23. return n;  
  24. }  
  25. bool NaturalNumber::isPrime()  
  26. {  
  27.     int a;  
  28.     for(a=2;a<=n;a++)  
  29.     {  
  30.         if(n%a==0)  
  31.             break;  
  32.     }  
  33.     if(n==a)  
  34.         return true;  
  35.     else  
  36.         return false;  
  37. }  
  38. void NaturalNumber::printFactor()  
  39. {  
  40.     int a;  
  41.     for(a=1;a<=n;a++)  
  42.     {  
  43.         if(n%a==0)  
  44.             cout<<a<<" ";  
  45.     }  
  46. }  
  47. bool NaturalNumber::isPerfect()  
  48. {  
  49.     int a,sum=0;  
  50.     for(a=1;a<n;a++)  
  51.     {  
  52.         if(n%a==0)  
  53.             sum=sum+a;  
  54.     }  
  55.     if(sum==n)  
  56.     return true;  
  57.     else  
  58.         return false;  
  59. }  
  60. bool NaturalNumber::isDaffodil(int x)  
  61. {  
  62.     if(x==1)  
  63.         return true;  
  64.     else if(x<10)  
  65.         return false;  
  66.     else if(x<100)  
  67.     {int a,b;  
  68.     a=x/10;b=x-a*10;  
  69.     if((a*a*a+b*b*b)==x)  
  70.         return true;  
  71.     else  
  72.         return false;  
  73.     }  
  74.     else if(x<1000)  
  75.     {  
  76.         int a,b,c;  
  77.         a=x/100;b=(x-a*100)/10;c=x-a*100-b*10;  
  78.         if(x==(a*a*a+b*b*b+c*c*c))  
  79.             return true;  
  80.         else  
  81.             return false;  
  82.     }  
  83.   
  84. }  
  85. void NaturalNumber::printDaffodils()  
  86. {  
  87.     int x;  
  88.     for(x=2;x<n;x++)  
  89.     {  
  90.         if(x<100&&x>10)  
  91.         {int a,b;  
  92.         a=x/10;b=x-a*10;  
  93.         if((a*a*a+b*b*b)==x)  
  94.             cout<<x<<" ";  
  95.         }  
  96.         else if(x<1000&&x>100)  
  97.         {  
  98.             int a,b,c;  
  99.             a=x/100;b=(x-a*100)/10;c=x-a*100-b*10;  
  100.             if(x==(a*a*a+b*b*b+c*c*c))  
  101.                 cout<<x<<" ";  
  102.         }  
  103.     }  
  104. }  
  105.     
  106. void main(void)    
  107. {    
  108.     NaturalNumber nn;   
  109.     nn.setValue (6);    
  110.     cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;    
  111.     
  112.     ;nn.setValue (37);     
  113.     cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;    
  114.     
  115.     nn.setValue (84);     
  116.     cout<<nn.getValue()<<"的因子有:";    
  117.     nn.printFactor();   
  118.     cout<<endl;  
  119.     nn.setValue(888);  
  120.     cout<<nn.getValue()<<"的水仙花数有:  ";  
  121.     nn.printDaffodils();  
  122.     cout<<endl;  
  123. }  


三、BOOK类

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<string>  
  3. using namespace std;  
  4. class Book  
  5. {  
  6. public:  
  7. void setBook(string n,string p,string w,double pri,int num);  
  8. int borrow();  
  9. int restore();  
  10. void printf();  
  11. void setNo(int a);  
  12. int getNo();  
  13. private:  
  14. string name,publicer,writer;  
  15. double price;  
  16. int No,number;  
  17. };  
  18. void Book::setBook(string n,string p,string w,double pri,int num)  
  19. {  
  20. name=n;publicer=p;writer=w;price=pri;number=num;  
  21. }  
  22. int Book::borrow()  
  23. {  
  24. return(number=number-1);  
  25. }  
  26. int Book::restore()  
  27. {  
  28. return(number=number+1);  
  29. }  
  30. void Book::printf()  
  31. {  
  32. cout<<"书名: "<<name<<endl;  
  33. cout<<"出版社: "<<publicer<<endl;  
  34. cout<<"作者: "<<writer<<endl;  
  35. cout<<"价格: "<<price<<endl;  
  36. cout<<"书号: "<<No<<endl;  
  37. cout<<"数量: "<<number<<endl;  
  38. }  
  39. void Book::setNo(int a)  
  40. {  
  41. No=a;  
  42. }  
  43. int Book::getNo()  
  44. {  
  45. return No;  
  46. }  
  47. void main()  
  48. {  
  49. Book b1;  
  50. b1.setBook("C++语言基础教程","北京邮电大学出版社","王更生",33,1);  
  51. b1.setNo(12345678);  
  52. b1.printf();  
  53. }  


四、分数类

[cpp]  view plain  copy
  1. #include<iostream>  
  2. using namespace std;  
  3. class CFraction    
  4. {    
  5. private:    
  6.     int nume;  // 分子    
  7.     int deno;  // 分母    
  8. public:  
  9.     CFraction(int nu=0,int de=1)  
  10.     {  
  11.         nume=nu;deno=de;  
  12.     }  
  13.     void set(int nu=0,int de=1)  
  14.     {  
  15.     nume=nu;deno=de;  
  16.     }    
  17.     void input();               //按照"nu/de"的格式,如"5/2"的形式输入    
  18.     void simplify();            //化简(使分子分母没有公因子)    
  19.     void amplify(int n);        //放大n倍,如2/3放大5倍为10/3    
  20.     void output(int style=0)   //输出:以8/6为例,style为0时,原样输出8/6;    
  21.      {  
  22.         if(style==0)  
  23.             cout<<"原样输出:"<<nume<<"/"<<deno<<endl;  
  24.         else if(style==1)  
  25.         {cout<<"输出化简后的分数: ";simplify();}  
  26.         else if(style==2)  
  27.         {  
  28.             int a,b;  
  29.             if(nume>deno)  
  30.             {  
  31.                 a=nume-deno;  
  32.                 while(1)  
  33.                 {  
  34.                     if(a>deno)  
  35.                         a=a-deno;  
  36.                     else if(a<deno)  
  37.                         break;  
  38.                 }  
  39.             }  
  40.             b=(nume-a)/deno;  
  41.             cout<<"输出真分数形式: "<<b<<"("<<a<<"/"<<deno<<")"<<endl;  
  42.         }  
  43.         else if(style==3)  
  44.         {  
  45.             double c;  
  46.             c=double(nume)/double(deno);  
  47.             cout<<"输出小数形式: "<<c<<endl;  
  48.         }  
  49.     }                           
  50. };  
  51. void CFraction::input()  
  52. {  
  53.     char a;  
  54.     cout<<"请输入一个分数(格式a/b): "<<endl;  
  55.     while(1)  
  56.     {  
  57.         cin>>nume>>a>>deno;  
  58.         if(a!='/')  
  59.             cout<<"输入格式不正确,请重新输入"<<endl;  
  60.         else  
  61.             break;  
  62.     }  
  63. }  
  64. void CFraction::simplify()  
  65. {  
  66.     int a;  
  67.     for(a=2;a<=nume;a++)  
  68.     {  
  69.         if(nume%a==0)  
  70.         {  
  71.             if(deno%a==0)  
  72.             {nume=nume/a;deno=deno/a;}  
  73.         }  
  74.     }  
  75.     cout<<nume<<"/"<<deno<<endl;  
  76. }  
  77. void CFraction::amplify(int n)  
  78. {  
  79.     nume=nume*n;  
  80. }  
  81. void main()  
  82. {  
  83.     CFraction a1;  
  84.     a1.set(10,4);  
  85.     a1.output();  
  86.     a1.output(1);  
  87.     a1.output(3);  
  88.     a1.input();  
  89.     a1.output(2);  
  90.     a1.amplify(10);  
  91.     cout<<"将分数增大10倍后,分数为: ";  
  92.     a1.output();  
  93.     a1.output(3);  
  94.     a1.output(2);  
  95. }  


1. 掌握继承、多形及相关的概念; 2. 了解类层次的设计方法,初步了解运算符重载、静态/动态联编及其在OOP中的应用等内容 1. 阅读附件中的VCAD程序的源代码,并将其编译、运行,简单地试验一下它的各项功能; 2. 分析VCAD程序的源代码中类层次的设计和实现。报告中应包含(但不限于)以下内容:  与图元类(CEntity、CLine、CRectangle、CCircle、CArc等类)和图元创建命令类(CCommand、CCreateLine、CCreateRect、CCreateCircle、CCreateArc等类)的各自的层次设计。要求画出类层次示意图;  对每一个(除了类CEntity之外)图元类,从其基类中继承了哪些成员,自行定义了哪些成员,其中有哪些成员是对基类相应成员的重写?  CEntity::Draw、CEntity::Draw、CEntity::Pick等函数为什么声明为虚函数?如果不声明为虚函数,对程序有什么影响?  找出各图元类实际绘制该图元对象的成员函数,并分析它是如何实现的,以了解在MFC环境下下如何绘制简单的图形;  针对一个图元创建命令类(CCreateLine、CCreateRect、CCreateCircle、CCreateArc中任选一个),分析该命令类是如何创建/绘制相关图元对象的;  找出一个运算符重载的例子(包括定义、实现和调用)来说明运算符重载的用途和使用运算符重载的的好处。 3. 向阅读附件中的VCAD程序加入以下功能(报告中要给出相应的设计思路和算法描述),并进行编译、运行和简单地测试(报告中要给出测试数据和结果):  增加对三角形图元的处理,包括绘制、创建、选取、平移、旋转、保存/打开等操作 4. 根据对原程序的分析和你对该程序的扩充实践,总结出设计类的继承应采取的原则,设计使用虚函数应遵循的原则,以及使用继承和多形对程序的代码重用所起
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值