实验一类和对象(续)

过道和栅栏的造价

描述

一矩形体育场如下图所示,现在需在其周围建一矩形过道,并在四周围安上栅栏。栅栏价格为50元/米,过道造价为240元/平方米。过道宽为1.5米,体育场的长宽由键盘输入。

体育场和矩形过道都是如下Rectangle的对象,请实现以下Rectagnle类,编写main函数计算并输出过道和栅栏的造价。

class Rectangle

{

private:

  double length; //长

  double width; //宽

public:

  Rectangle(double Length=10.,double Width=5.);

  double Area(); //获取面积

  double Perimeter();//获取周长

};

输入

体育场的长和宽

输出

输出2行

第一行是栅栏的造价

第二行是过道的造价

输入样例 1 

2.4 1.2

输出样例 1

960
4752

输入样例 2 

2 1

输出样例 2

900
4320

提示

体育场用对象 rect1(x, y)来表示的话,过道外的矩形就用对象 rect2(x + 3, y + 3)表示

实例一(助教)、

#include <iostream>
using namespace std;
 
class Rectangle
{
private:
	double length;
	double width;
 
public:
	Rectangle( double Length = 10., double Width = 5.)
	{
		this->length = Length;
		this->width = Width;
	}
	double Area()
	{
		return length * width;
	}
	double Perimeter()
	{
		return 2.0*(length + width);
	}
};
 
int main()
{
	double length, width;
 
	while( cin >> length >> width )
	{
		Rectangle Playground(length, width);
		Rectangle All_Playground(length+3.0, width+3.0);
 
		double Area = All_Playground.Area() - Playground.Area();
		double Perimeter = All_Playground.Perimeter();
 
		cout << Perimeter * 50 << endl;
 
		cout << 240*Area << endl;
	}
 
	return 0;
}

实例二(班草)

#include<iostream>
 
using namespace std;
 
class Rectangle
{
	private:
		double length; //长
 		double width; //宽
 	public:
 		Rectangle(double Length=10.,double Width=5.);
 		double Area(); //获取面积
 		double Perimeter();//获取周长
 } ;
 
Rectangle::Rectangle(double Length,double Width)
{
	length=Length;
	width=Width;
}
 
double Rectangle::Area()
{
	return length*width; 
}
 
double Rectangle::Perimeter()
{
	return (length+width)*2;
}
 
int main()
{
	int m=50,n=240;
	double x,y;
	double area;
	cin>>x>>y;
	
	Rectangle rect1(x,y),rect2(x+3,y+3);
	cout<<rect2.Perimeter()*m<<endl;
	area=rect2.Area()-rect1.Area();
	cout<<area*n<<endl;
	
	return 0;
	
}

实例三(宗)

#include <iostream>
#include <iomanip>
using namespace std ;
class Rectangle{
	private:
  		double length; 
  		double width; 
	public:
  		Rectangle(double Length=10.,double Width=5.);
  		double Area(); 
  		double Perimeter();
};
double Rectangle::Area(){
	double c;
	c=(length+width)*2; 
	return c;
}
double Rectangle::Perimeter(){
	double s;
	s=length*width; 
	return s;
}
Rectangle::Rectangle(double Length,double Width){
	length=Length;
	width=Width;
}
int main(){
	double l;
	double h;
	cin>>l;
	cin>>h;
	Rectangle rect1(l,h);
	Rectangle rect2(l+3,h+3);
	rect1.Perimeter();
	rect2.Area();
	rect2.Perimeter();
	cout<<(rect2.Area()*50)<<endl<<((rect2.Perimeter()-rect1.Perimeter())*240)<<endl;
}

实例四(编程天才)

	#include <iostream>
using namespace std;
class Rectangle
{
	private:
		double length;
		double width;
	public:
		Rectangle(double Length=10.,double Width=5.);
		double Area();
		double Perimeter();
} ;
Rectangle::Rectangle(double Length,double Width)
{
	this->length=Length;
	this->width=Width;
}
double Rectangle::Area()
{
	return length*width;
}
double Rectangle::Perimeter()
{
	return (length+width)*2;
}
int main()
{
	double x,y;
	cin>>x>>y;
	Rectangle rect1(x,y);
	Rectangle rect2(x+3.0,y+3.0);
	cout<<50*rect2.Perimeter()<<endl;
	cout<<240*(rect2.Area()-rect1.Area());
	return 0;
}

实例五(明钰)

#include <iostream> 
using namespace std;
 
class Rectangle
{
private:
  double length; //长
  double width; //宽
 
public:
  Rectangle(double Length=10.,double Width=5.);
  double Area(); //获取面积
  double Perimeter();//获取周长
};
 
Rectangle::Rectangle(double Length,double Width)
{
	length=Length;
	width=Width;
}
 
double Rectangle::Area()
{
	return length*width;
}
 
double Rectangle::Perimeter()
{
	return (length+width)*2;
}
 
int main()
{
	double x,y,z,g;
	cin>>x>>y;
	Rectangle rect1(x,y);
	Rectangle rect2(x+3,y+3);
	z=50*rect2.Perimeter();
	g=240*(rect2.Area()-rect1.Area());
	cout<<z<<endl<<g;
	return 0;
}

实例六(雪莹)

#include <iostream>
#include<stdlib.h>
#include<cstring>
using namespace std;
class Rectangle 
{
private:
  double length; //长
  double width; //宽
public:
  Rectangle(double Length=10.,double Width=5.);
  double Area(); //获取面积
  double Perimeter();//获取周长
};
double Rectangle::Area(){
	return length*width;
}
double Rectangle::Perimeter(){
	return 2*(length+width);
}
Rectangle::Rectangle(double Length,double Width){
	length=Length;
	width=Width;
}
int main(){
	double width,length;
	cin>>length>>width;
	Rectangle rec1(width,length),rec2(width+3,length+3);
	cout<<50*rec2.Perimeter()<<endl;
	cout<<240*(rec2.Area()-rec1.Area());
	return 0;
}

实例七(咏琪)

#include<iostream>
using namespace std;
 
class Rectangle {
private:
	double length; //长
	double width; //宽
public:
	Rectangle(double Length, double Width);
	void Area(); //获取面积
	void Perimeter();//获取周长
};
Rectangle::Rectangle(double Length, double Width) {
	length = Length;
	width = Width;
}
void Rectangle::Area(){
	double area;
	area = (length + 3) *3+ width * 3;
	cout <<240*area<<endl;
}
void Rectangle::Perimeter() {
	double peri;
	peri = length  * 2 + width  * 2;
	cout <<50*peri<<endl;
}
 
int main() {
	double x, y;
	
	cin >> x;
	
	cin >> y;
	Rectangle rect1(x, y);//体育场
	Rectangle rect2(x + 3, y + 3);//过道外的矩形
	rect2.Perimeter();
	rect1.Area();
	return 0;	
}

实例八(格日)

#include <iostream>
using namespace std;
class Rectangle
{
private:
	double length; //长
	double width; //宽
public:
	Rectangle(double Length = 10., double Width = 5.) 
	{
		this->length = Length;
		this->width = Width;
	};
	double Area() 
	{
		return (length + 3.0)*1.5 * 2 + width * 1.5 * 2;
	}; //获取面积
	double Perimeter()
	{
		return (length + 3.0)*2.0 + (width + 3.0)*2.0;
	};//获取周长
};
int main()
{
	double length, width, area, perimeter;
	cin >> length >> width;
	Rectangle rect(length, width);
	area = rect.Area();
	perimeter = rect.Perimeter();
	cout << perimeter * 50 << endl;
	cout << area * 240 << endl;
}

实例九(顺)

#include <iostream>
using namespace std;
class Rectangle
{
private:
	double length; //长
	double width; //宽
public:
	Rectangle(double Length = 10., double Width = 5.) 
	{
		this->length = Length;
		this->width = Width;
	};
	double Area() 
	{
		return (length + 3.0)*1.5 * 2 + width * 1.5 * 2;
	}; //获取面积
	double Perimeter()
	{
		return (length + 3.0)*2.0 + (width + 3.0)*2.0;
	};//获取周长
};
int main()
{
	double length, width, area, perimeter;
	cin >> length >> width;
	Rectangle rect(length, width);
	area = rect.Area();
	perimeter = rect.Perimeter();
	cout << perimeter * 50 << endl;
	cout << area * 240 << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值