第八周C++实验报告(一)

方案一:用类的成员函数完成运算符的重载

[cpp]  view plain copy
  1. #include<iostream>    
  2. using namespace std;    
  3. class Complex    
  4. {    
  5. public:    
  6.     Complex(){real=0;imag=0;}    
  7.     Complex(double r,double i){real=r;imag=i;}    
  8.     Complex operator+(Complex &c2);    
  9.     Complex operator-(Complex &c2);    
  10.     Complex operator*(Complex &c2);    
  11.     Complex operator/(Complex &c2);    
  12.     void display();    
  13. private:    
  14.     double real;    
  15.     double imag;    
  16. };    
  17. //下面定义成员函数  
  18.   
  19. //复数的相加运算:    
  20. Complex Complex::operator+(Complex &c2)        
  21. {      
  22.     Complex c;       
  23.     c.real=real+c2.real;      
  24.     c.imag=imag+c2.imag;      
  25.     return c;      
  26. }      
  27.   
  28. //复数的相减运算:          
  29. Complex Complex::operator-(Complex &c2)          
  30. {     
  31.     Complex c;  
  32.     c.real=real-c2.real;  
  33.     c.imag=imag-c2.imag;  
  34.     return c;          
  35. }          
  36. //复数的相乘运算:      
  37. Complex Complex::operator*(Complex &c2)     
  38. {          
  39.     Complex c;          
  40.     c.real=real*c2.real-imag*c2.imag;          
  41.     c.imag=imag*c2.real+real*c2.imag;          
  42.     return c;          
  43. }          
  44. //复数的相除运算:      
  45. Complex Complex::operator/(Complex &c2)     
  46. {          
  47.     Complex  c;          
  48.     double d=c2.real*c2.real+c2.imag*c2.imag;      
  49.     c.real=(real*c2.real+imag*c2.imag)/d;           
  50.     c.imag=(imag*c2.real-real*c2.imag)/d;          
  51.     return c;          
  52. }       
  53. void Complex::display( )        
  54. {      
  55.     cout<<"("<<real<<","<<imag<<"i)"<<endl;      
  56. }      
  57.   
  58. int main()    
  59. {    
  60.     Complex c1(3,4),c2(5,-10),c3;   
  61.     cout<<"c1=";    
  62.     c1.display();    
  63.     cout<<"c2=";    
  64.     c2.display();    
  65.     c3=c1+c2;    
  66.     cout<<"c1+c2=";    
  67.     c3.display();    
  68.     c3=c1-c2;    
  69.     cout<<"c1-c2=";    
  70.     c3.display();    
  71.     c3=c1*c2;    
  72.     cout<<"c1*c2=";    
  73.     c3.display();    
  74.     c3=c1/c2;    
  75.     cout<<"c1/c2=";    
  76.     c3.display();    
  77.     system("pause");    
  78.     return 0;    
  79. }   

方案二:用类的友元函数,而不是成员函数,完成运算符的重载

[cpp]  view plain copy
  1. #include<iostream>    
  2. using namespace std;    
  3. class Complex    
  4. {    
  5. public:    
  6.     Complex(){real=0;imag=0;}    
  7.     Complex(double r,double i){real=r;imag=i;}    
  8.     friend Complex operator+(Complex &c1,Complex &c2);    
  9.     friend Complex operator-(Complex &c1,Complex &c2);    
  10.     friend Complex operator*(Complex &c1,Complex &c2);    
  11.     friend Complex operator/(Complex &c1,Complex &c2);    
  12.     friend void display(Complex &c2);    
  13. private:    
  14.     double real;    
  15.     double imag;    
  16. };    
  17. //下面定义友元函数  
  18.   
  19. //复数的相加运算:    
  20. Complex operator+(Complex &c1,Complex &c2)        
  21. {      
  22.     Complex c;       
  23.     c.real=c1.real+c2.real;      
  24.     c.imag=c1.imag+c2.imag;      
  25.     return c;      
  26. }      
  27.   
  28.   
  29. //复数的相减运算:          
  30. Complex operator-(Complex &c1,Complex &c2)          
  31. {          
  32.     Complex c;          
  33.     c.real=c1.real-c2.real;          
  34.     c.imag=c1.imag-c2.imag;          
  35.     return c;          
  36. }          
  37.   
  38. //复数的相乘运算:      
  39.   
  40. Complex operator*(Complex &c1,Complex &c2)     
  41. {          
  42.     Complex  c;          
  43.     c.real=c1.real*c2.real-c1.imag*c2.imag;          
  44.     c.imag=c1.imag*c2.real+c1.real*c2.imag;          
  45.     return c;          
  46. }          
  47.   
  48. //复数的相除运算:     
  49. Complex operator/(Complex &c1,Complex &c2)     
  50. {          
  51.     Complex  c;          
  52.     double d=c2.real*c2.real+c2.imag*c2.imag;      
  53.     c.real=(c1.real*c2.real+c1.imag*c2.imag)/d;           
  54.     c.imag=(c1.imag*c2.real-c1.real*c2.imag)/d;          
  55.     return c;          
  56. }       
  57.   
  58. void display(Complex &c2)        
  59. {      
  60.     cout<<"("<<c2.real<<","<<c2.imag<<"i)"<<endl;      
  61. }      
  62.   
  63. int main()    
  64. {    
  65.     Complex c1(3,4),c2(5,-10),c3;    
  66.     cout<<"c1=";    
  67.     display(c1);    
  68.     cout<<"c2=";    
  69.     display(c2);    
  70.     c3=c1+c2;    
  71.     cout<<"c1+c2=";    
  72.     display(c3);    
  73.     c3=c1-c2;    
  74.     cout<<"c1-c2=";    
  75.     display(c3);    
  76.     c3=c1*c2;    
  77.     cout<<"c1*c2=";    
  78.     display(c3);    
  79.     c3=c1/c2;    
  80.     cout<<"c1/c2=";    
  81.     display(c3);    
  82.     system("pause");    
  83.     return 0;    
  84. }   


方案三:扩展+、-、*、/运算符的功能,使之能与double型数据进行运算

[cpp]  view plain copy
  1. #include<iostream>    
  2. using namespace std;    
  3. class Complex    
  4. {    
  5. public:    
  6.     Complex(){real=0;imag=0;}    
  7.     Complex(double r,double i){real=r;imag=i;}    
  8.     friend Complex operator+(Complex &c1,Complex &c2);    
  9.     friend Complex operator-(Complex &c1,Complex &c2);    
  10.     friend Complex operator-(Complex &c2);    
  11.     friend Complex operator*(Complex &c1,Complex &c2);    
  12.     friend Complex operator/(Complex &c1,Complex &c2);    
  13.     friend Complex operator+(Complex &c1,const double &d);    
  14.     friend Complex operator+(const double &d, Complex &c);    
  15.       friend void display(Complex &c2);    
  16. private:    
  17.     double real;    
  18.     double imag;    
  19. };    
  20. //下面定义友元函数    
  21.     
  22. //复数相加: (a+bi)+(c+di)=(a+c)+(b+d)i.    
  23. Complex operator+(Complex &c1,Complex &c2)        
  24. {      
  25.     Complex c;       
  26.     c.real=c1.real+c2.real;      
  27.     c.imag=c1.imag+c2.imag;      
  28.     return c;      
  29. }      
  30. Complex operator+(Complex &c1,const double &d)    
  31. {    
  32.     return Complex(c1.real+d, c1.imag);    
  33. }    
  34. Complex operator+(const double &d, Complex &c1)    
  35. {    
  36.     return Complex(c1.real+d, c1.imag);    
  37. }    
  38.       
  39. //复数相减:(a+bi)-(c+di)=(a-c)+(b-d)i.          
  40. Complex operator-(Complex &c1,Complex &c2)          
  41. {          
  42.     Complex c;          
  43.    c.real=c1.real-c2.real;          
  44.     c.imag=c1.imag-c2.imag;          
  45.       return c;          
  46. }          
  47. Complex operator-(Complex &c2)    
  48. {    
  49.     return Complex(-c2.real, -c2.imag);    
  50.   
  51. }    
  52. //复数相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.      
  53.        
  54. Complex operator*(Complex &c1,Complex &c2)     
  55. {          
  56.     Complex  c;          
  57.     c.real=c1.real*c2.real-c1.imag*c2.imag;          
  58.     c.imag=c1.imag*c2.real+c1.real*c2.imag;          
  59.     return c;          
  60. }                
  61. //复数相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i      
  62. Complex operator/(Complex &c1,Complex &c2)     
  63. {          
  64.     Complex  c;          
  65.     double d=c2.real*c2.real+c2.imag*c2.imag;      
  66.     c.real=(c1.real*c2.real+c1.imag*c2.imag)/d;     //此处有危险未排除:除法溢出      
  67.     c.imag=(c1.imag*c2.real-c1.real*c2.imag)/d;          
  68.     return c;          
  69. }       
  70.          
  71. void display(Complex &c2)        
  72. {      
  73.     cout<<"("<<c2.real<<","<<c2.imag<<"i)"<<endl;      
  74. }      
  75.     
  76. int main()    
  77. {    
  78.     Complex c1(3,4),c2(5,-10),c3,c4;    
  79.     cout<<"c1=";    
  80.    display(c1);    
  81.     cout<<"c2=";    
  82.     display(c2);    
  83.     c3=c1+c2;    
  84.     cout<<"c1+c2=";    
  85.     display(c3);    
  86.     c3=c1-c2;    
  87.     cout<<"c1-c2=";    
  88.     display(c3);    
  89.     c3=c1*c2;    
  90.     cout<<"c1*c2=";    
  91.     display(c3);    
  92.     c3=c1/c2;    
  93.     cout<<"c1/c2=";    
  94.     display(c3);    
  95.     c4=c1+3.14;    
  96.     cout<<"c1+3.14=";    
  97.     display(c4);    
  98.     c4=3.14+c1;    
  99.     cout<<"3.14+c1=";    
  100.     display(c4);    
  101.         c4=-c1;    
  102.     cout<<"-c1=";    
  103.     display(c4);    
  104.     system("pause");   
  105.      return 0;  
  106.  }    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个停车场管理系统的C++实验报告。 首先,我们需要定义停车场类,包括停车场名称,停车场容量,已停车辆数,停车场收费标准等信息。以下是停车场类的定义: ```C++ class ParkingLot { public: ParkingLot(string name, int capacity, int feePerHour); ~ParkingLot(); bool park(Car* car); bool leave(Car* car); void printStatus(); private: string name_; int capacity_; int feePerHour_; int parkedCount_; vector<Car*> parkedCars_; }; ``` 在该类中,构造函数用于初始化停车场的名称,容量和收费标准。park函数用于将一辆车停入停车场,leave函数用于将一辆车从停车场中取出,printStatus函数用于打印停车场的状态。 接下来,我们需要定义车辆类,包括车牌号,车辆类型等信息。以下是车辆类的定义: ```C++ class Car { public: Car(string plateNumber, string type); ~Car(); string getPlateNumber(); string getType(); private: string plateNumber_; string type_; }; ``` 在该类中,构造函数用于初始化车牌号和车辆类型,getPlateNumber函数用于获取车牌号,getType函数用于获取车辆类型。 最后,我们需要编写主函数,用于测试停车场管理系统。以下是主函数的代码: ```C++ int main() { ParkingLot parkingLot("ABC Parking Lot", 50, 10); Car car1("123456", "SUV"); parkingLot.park(&car1); Car car2("789012", "Sedan"); parkingLot.park(&car2); Car car3("345678", "Truck"); parkingLot.park(&car3); parkingLot.printStatus(); parkingLot.leave(&car2); parkingLot.printStatus(); return 0; } ``` 在主函数中,我们首先创建一个停车场对象,然后分别创建三辆车并将其停入停车场中。接着打印停车场的状态,然后将第二辆车取出,并再次打印停车场的状态。 以上就是停车场管理系统程序的C++实验报告

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值