实验报告 《3》4

本文介绍了一个用于计算长方柱体积和表面积的C++程序,包括输入长宽高的格式验证、体积计算、表面积计算等功能。
 
  1. #include <iostream>       
  2. using namespace std;     
  3. int n=0;     
  4. class square      
  5. {      
  6. public:      
  7.     
  8.     void cin_date();      
  9.     void volume( );      
  10.    void show_volume( );      
  11.     void areas( );       
  12.     void show_areas();      
  13. private:       
  14.     int length;      
  15.    int width;      
  16.     int heigth;    
  17. };      
  18.     
  19. int main( )      
  20. {      
  21.     square s1,s2,s3;     
  22.     ++n;    
  23.     s1.cin_date();      
  24.     s1.volume( );      
  25.     s1.areas( );      
  26.     ++n;    
  27.    s2.cin_date();      
  28.    s2.volume( );      
  29.     s2.areas( );      
  30.     ++n;    
  31.     s3.cin_date();      
  32.     s3.volume( );      
  33.     s3.areas( );      
  34.     system("PAUSE");     
  35.     return 0;      
  36. }      
  37.     
  38. void square::cin_date( )       
  39. {      
  40.         
  41.     char c1,c2;    
  42.     double length1,width1,heigth1;    
  43.     cout<<"请输入"<<"第"<<n<<"个长方柱的长宽高(格式:长:宽:高 )"<<endl;     
  44.     while(1)      
  45.     {      
  46.         cin>>length1>>c1>>width1>>c2>>heigth1;      
  47.         length=length1;    
  48.         width=width1;    
  49.         heigth=heigth1;    
  50.             
  51.         if(c1!=':'||c2!=':')      
  52.             cout<<"格式不正确,请重新输入"<<endl;      
  53.         else       
  54.             break;      
  55.     }      
  56.     return;      
  57. }      
  58. void square::volume( )      
  59. {      
  60.     double volume;    
  61.     volume=length*width*heigth;    
  62.     cout<<"第"<<n<<"个长方柱的体积为:"<<volume<<endl;      
  63.     return;      
  64. }      
  65.     
  66.     
  67. void square::areas( )      
  68. {      
  69.     double areas;    
  70.     areas=2*(length*width+length*heigth+width*heigth);    
  71.     cout<<"第"<<n<<"个长方柱的表面积为:"<<areas<<endl;     
  72.     return;      
  73. }      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值