C++ 实验 03

实验3.1

设计一个用来表示直角坐标系的Location类,有两个double型私有数据成员x,y;主程序中,输入相应的值,创建类Location的两个对象a和b,分别采用成员函数和友元函数计算给定两个坐标点之间的距离。

【提示】类Location的参考框架如下:

class Location

{

public:

        Location(double a,double b);   //构造函数

        double getX();    //成员函数,取x坐标的值

        double getY();    //成员函数,取y坐标的值

        double distance1(Location &);    //成员函数,求给定两点之间的距离

        friend double distance1(Location &,Location&);   //友元函数,求给定两点之间的距离

private:

        double x,y;

};

-10 8

-3 -5

第一个点:A(-10,8)

第二个点:B(-3,-5)

调用成员函数,Distance=14.7648

调用友元函数,Distance=14.7648

5 15

3 2

第一个点:A(5,15)

第一个点:B(3,2)

调用成员函数,Distance=13.1529

调用友元函数,Distance=13.1529

89 34

67 35

第一个点:A(89,34)

第一个点:B(67,35)

调用成员函数,Distance=22.0227

调用友元函数,Distance=22.0227

#include<iostream>
using namespace std;
#include<cmath>
class Location
{
public:
	Location(double a,double b):x(a),y(b){};   //构造函数
	double getX();    //成员函数,取x坐标的值
	double getY();    //成员函数,取y坐标的值
	double distance1(Location &);    //成员函数,求给定两点之间的距离
	friend double distance1(Location &,Location &);	 //友元函数,求给定两点之间的距离
private:
	double x,y;
};
double Location::getX()
{
	return x;
}
double Location::getY()
{
	return y;
}
double Location:: distance1(Location &point)
{
	return sqrt(pow((this->x-point.x),2)+pow((this->y-point.y),2));
}
double distance1(Location &point1,Location &point2)
{
	return sqrt(pow((point1.x-point2.x),2)+pow((point1.y-point2.y),2));
}
int main()
{
	double x,y;
	cin>>x>>y;
	Location a(x,y);
	cin>>x>>y;
	Location b(x,y);
	cout<<"第一个点:A("<<a.getX()<<","<<a.getY()<<")"<<endl;
	cout<<"第二个点:B("<<b.getX()<<","<<b.getY()<<")"<<endl;
	cout<<"调用成员函数,Distance="<<a.distance1(b)<<endl;
	cout<<"调用友元函数,Distance="<<distance1(a,b)<<endl;
	return 0;
}

实验3.2

设计一个计算薪水的类Payroll,数据成员包括:单位小时工资、周工作小时、每周应付工资,其中每周应付工资=单位小时工资*周工作小时。要求:定义构造函数、析构函数、拷贝构造函数,常成员函数output()用来输出对象的数据成员值。主函数中定义两个对象:第一个对象的单位小时工资、周工作小时由键盘输入,第二个对象定义为常对象,他的单位小时工资为第一个对象的1.5倍,周工作小时相同,输出每个对象的信息。

25 38

Constructor is called!

小时工资:25 周工作时数:38 周应付工资:950

Copy constructor is called!

小时工资:37.5 周工作时数:38 周应付工资:1425

Destructor is called!

Destructor is called!

30 30

Constructor is called!

小时工资:30 周工作时数:30 周应付工资:900

Copy constructor is called!

小时工资:45 周工作时数:30 周应付工资:1350

Destructor is called!

Destructor is called!

35 28

Constructor is called!

小时工资:35 周工作时数:28 周应付工资:980

Copy constructor is called!

小时工资:52.5 周工作时数:28 周应付工资:1470

Destructor is called!

Destructor is called!

#include<iostream>
using namespace std;
class Payroll
{
public:
	Payroll(double Hourwage,int Workhour)
	{
		m_Hourwage=Hourwage;
		m_Workhour=Workhour;
		m_Paywage=m_Hourwage*m_Workhour;
		cout<<"Constructor is called!"<<endl;
	}
	Payroll(Payroll& person)
	{
		m_Hourwage=person.m_Hourwage*1.5;
		m_Workhour=person.m_Workhour;
		m_Paywage=m_Hourwage*m_Workhour;
		cout<<"Copy constructor is called!"<<endl;
	}
	~Payroll()
	{
        static bool flag=true;
        if(flag)
        {
		    cout<<"Destructor is called!"<<endl;
            flag=false;
        }
        else
        {
            cout<<"Destructor is called!";
        }
	}
	void output()const
	{
		cout<<"小时工资:"<<m_Hourwage<<" 周工作时数:"<<m_Workhour<<" 周应付工资:"<<m_Paywage<<endl;
	}
private:
	double m_Hourwage;
	int m_Workhour;
	double m_Paywage;
};
int main()
{
	double Hourwage;
	int Workhour;
	cin>>Hourwage>>Workhour;
	Payroll person1(Hourwage,Workhour);
	person1.output();
	Payroll person2(person1);
	person2.output();
	return 0;
}

实验3.3

定义一个平面坐标系下的点类Point,有整型数据成员x,y坐标值。成员函数包括:(1)带默认值的构造函数,默认值均为0;(2)拷贝构造函数;(3)置x,y坐标值;(4)取x,y的坐标值,参数为两个整型量的引用,分别用于获取x,y坐标值。(5)输出函数,用于输出x,y坐标值。(6)求两个点之间距离的函数,参数是Point类的对象引用。

定义一个平面坐标系下的三角形类Triangle,数据成员为三个Point类的对象p1、p2、p3。成员函数包括:(1)带参数的构造函数,参数为整型量x1,y1,x2,y2,x3,y3,分别是三角形三个顶点的坐标。(2)带参数的构造函数,参数是三个Point类对象的引用。(3)求三角形周长。(4)求三角形面积。(5)输出三角形的三个顶点坐标、周长和面积。

定义一个普通函数:判断三个顶点坐标能否构成三角形。

main()中,从键盘输入三个点坐标,判断这三个点能否构成三角形,不能,则提示重新输入,并重新输入点坐标;能,则输出三个顶点坐标、周长和面积。

0 0

1 1

2 2

0 0

5 6

3 0

顶点坐标不正确,不能构成三角形!请重新输入坐标!

三角形三个顶点坐标为:

(0,0) (5,6) (3,0)

三角形周长为:17.1348,面积为:9

0 0

3 3

0 2

三角形三个顶点坐标为:

(0,0) (3,3) (0,2)

三角形周长为:9.40492,面积为:3

1 1

6 9

8 2

三角形三个顶点坐标为:

(1,1) (6,9) (8,2)

三角形周长为:23.7852,面积为:25.5

#include<iostream>
using namespace std;
#include<cmath>
class Point
{
public:
	Point(int x=0,int y=0):m_x(x),m_y(y){}
	Point(const Point& p): m_x(p.m_x),m_y(p.m_y){}
	void Set()
	{
		cin>>this->m_x>>this->m_y;
	}
	void Get(int& x,int& y)
	{
		m_x=x;
		m_y=y;
	}
	void Print()
	{
		cout<<"("<<m_x<<","<<m_y<<")";
	}
	double Distance(Point& p)
	{
		return sqrt((pow(double(this->m_x-p.m_x),2)+pow(double(this->m_y-p.m_y),2)));
	}
private:
	int m_x,m_y;
};
class Triangle
{
public:
	Triangle(int x1, int y1, int x2, int y2, int x3, int y3): p1(x1,y1),p2(x2,y2),p3(x3,y3){}
	Triangle(const Point& p1, const Point& p2, const Point& p3): p1(p1),p2(p2),p3(p3){}
	double Perimrter()
	{
		return p1.Distance(p2)+p1.Distance(p3)+p2.Distance(p3);
	}
	double Area()
	{
		double p=Perimrter()/2.0;
		return sqrt(p*(p-p1.Distance(p2))*(p-p1.Distance(p3))*(p-p2.Distance(p3)));
	}
	void Print()
	{
		p1.Print();
		p2.Print();
		p3.Print();
		cout<<endl<<"三角形周长为:"<<Perimrter()<<",面积为:"<<Area()<<endl;
	}
private:
	Point p1,p2,p3;
};
bool Compare(Point& p1,Point& p2,Point& p3)
{
	if(p1.Distance(p2)+p1.Distance(p3)>p2.Distance(p3)&&p2.Distance(p3)+p1.Distance(p3)>
		p1.Distance(p2)&&p2.Distance(p3)+p1.Distance(p2)>p1.Distance(p3))
	{
		cout<<"三角形三个顶点坐标为:"<<endl;
		return false;
	}
	else
	{
		cout<<"顶点坐标不正确,不能构成三角形!请重新输入坐标!"<<endl;
		return true;
	}
}
int main()
{
	Point p1,p2,p3;
	do
	{
		p1.Set();
		p2.Set();
		p3.Set();
	}
	while(Compare(p1,p2,p3));
	Triangle t(p1,p2,p3);
	t.Print();
	return 0;
}

实验3.4

使用C++中的string标准类,将5个字符串按由小到大顺序输出。

string month attack price hello

排序后的结果为:

attack hello month price string

book newspaper apple red class

排序后的结果为:

apple book class newspaper red

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<algorithm>
int main()
{
	int i;
	vector<string> str(5);
	for(i=0;i<5;i++)
	{
		cin>>str[i];
	}
	sort(str.begin(),str.end());
    cout<<"排序后的结果为:"<<endl;
	for(i=0;i<5;i++)
	{
		cout<<str[i]<<" ";
    }
	cout<<endl;
	return 0;
}

  • 55
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

꧁༺❀氯ྀൢ躅ྀൢ❀༻꧂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值