C++实验03_类

C++实验03_类

实验03(01)Location类

题目描述
设计一个用来表示直角坐标系的Location类,有两个double型私有数据成员x,y;主程序中,输入相应的值,创建类Location的两个对象a和b,要求a的坐标点在第3 象限,b的坐标点在第2象限;分别采用成员函数和友元函数计算给定两个坐标点之间的距离。
【提示】类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;
}; 
输入描述
二行,每行分别为一个点的坐标 
输出描述
四行:
第一、二行分别为第一个点、第二个点的坐标
第三行为调用成员函数求出的点之间的距离
第四行为调用友元函数求出的点之间的距离 
输入样例
1 2
6 7 
输出样例
第一个点:A(1,2)
第二个点:B(6,7)
调用成员函数,Distance=7.07107
调用友元函数,Distance=7.07107
#include <iostream>
#include <cmath>

using namespace std;
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;
};
Location::Location(double a, double b)
{
    x = a;
    y = b;
}
double Location::getX()
{
    return x;
}
double Location::getY()
{
    return y;
}
double distance1(Location &a, Location &b)
{
    double dx = a.getX() - b.getX();
    double dy = a.getY() - b.getY();
    return sqrt(dx * dx + dy * dy);
}
double Location::distance1(Location &b)
{

    double dx = x - b.getX();
    double dy = y - b.getY();
    return sqrt(dx * dx + dy * dy);
}

int main()
{
    double x1, x2, y1, y2;
    cin >> x1 >> y1;
    cin >> x2 >> y2;
    Location a(x1, y1);
    Location b(x2, y2);

    cout << "第一个点:"
         << "A(" << x1 << "," << y1 << ")" << endl;
    cout << "第二个点:"
         << "B(" << x2 << "," << y2 << ")" << endl;

    cout << "调用成员函数,Distance=" << a.distance1(b) << endl;
    cout << "调用友元函数,Distance=" << distance1(a, b) << endl;
    return 0;
}

成员函数与友元函数

成员函数

double Location::distance1(Location &b)
{

	double dx = x - b.getX();
	double dy = y - b.getY();
	return sqrt(dx*dx + dy * dy);

}

使用
a.distance1(b)

友元函数

友元函数不用写作用域范围

double distance1(Location &a, Location&b)
{
	double dx = a.getX() - b.getX();
	double dy = a.getY() - b.getY();
	return sqrt(dx*dx + dy * dy);

}
使用
distance1(a, b)

实验03(02)Payroll类

题目描述
设计一个计算薪水的类Payroll,数据成员包括:单位小时工资、周工作小时、每周应付工资,其中每周应付工资=单位小时工资* 周工作小时。要求:定义构造函数、析构函数、拷贝构造函数,常成员函数output()用来输出对象的数据成员值。主函数中定义两个对象:第一个对象的单位小时工资、周工作小时由键盘输入,第二个对象定义为常对象,他的单位小时工资为第一个对象的1.5倍,周工作小时相同,输出每个对象的信息。 
输入描述
一行:第一个对象的单位小时工资、周工作小时 
输出描述
两个对象的小时工资、周工作时数、周应付工资
函数调用的相关信息 
输入样例
25 38 
输出样例
Constructor is called!
小时工资:25 周工作时数:38 周应付工资:950
Copy constructor is called!
小时工资:37.5 周工作时数:38 周应付工资:1425
Destructor is called!
#include <iostream>

using namespace std;

class Payroll
{
public:
    Payroll(double a, int b)
    {
        cout << "Constructor is called!" << endl;
        SalaryPurHour = a;
        WorkTime = b;
    }
    double GetTotalSalary()
    {
        TotalSalary = WorkTime * SalaryPurHour;
        return TotalSalary;
    }
    Payroll(const Payroll &a);
    void output()
    {
        cout << "小时工资:" << SalaryPurHour << " 周工作时数:" << WorkTime << " 周应付工资:" << GetTotalSalary() << endl;
    }
    ~Payroll()
    {
        cout << "Destructor is called!" << endl;
    }

private:
    int WorkTime;
    double SalaryPurHour;
    double TotalSalary;
};
Payroll::Payroll(const Payroll &x)
{
    cout << "Copy constructor is called!" << endl;
    SalaryPurHour = 1.5 * x.SalaryPurHour;
    WorkTime = x.WorkTime;
    TotalSalary = SalaryPurHour * WorkTime;
}
int main()
{
    int WorkTime;
    double SalaryPurHour;
    cin >> SalaryPurHour;
    cin >> WorkTime;
    Payroll A(SalaryPurHour, WorkTime);
    A.output();
    Payroll B(A);
    B.output();
    return 0;
}

拷贝构造函数

Payroll::Payroll(const Payroll &x)
{
    cout << "Copy constructor is called!" << endl;
    SalaryPurHour = 1.5 * x.SalaryPurHour;
    WorkTime = x.WorkTime;
    TotalSalary = SalaryPurHour * WorkTime;
}

实验03(03)组合类:Triangle类与Point类

题目描述
定义一个平面坐标系下的点类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
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
    Point(int a = 0, int b = 0)
    {
        x = a;
        y = b;
    }
    Point(const Point &a) //拷贝构造函数
    {
        x = a.x;
        y = a.y;
    }
    void input()
    {
        cin >> x >> y;
    }
    int getx(int &x)
    {
        return x;
    }
    int gety(int &y)
    {
        return y;
    }
    void output()
    {
        cout << "(" << x << "," << y << ")";
    }
    double distance(Point &a)
    {
        double dx = x - a.x, dy = y - a.y;
        return sqrt(dx * dx + dy * dy);
    }

private:
    int x, y;
};
class Triangle
{
public:
    Triangle(int x1, int y1, int x2, int y2, int x3, int y3)
    {
        A = (x1, y1);
        B = (x2, y2);
        C = (x3, y3);
    }
    Triangle(Point &a, Point &b, Point &c)
    {
        A = a;
        B = b;
        C = c;
    }
    double Len()
    {
        return A.distance(B) + B.distance(C) + C.distance(A);
    }
    double Area()
    {
        double s = Len() / 2.0;
        return sqrt(s * (s - A.distance(B)) * (s - B.distance(C)) * (s - C.distance(A)));
    }
    void output()
    {
        cout << endl
             << "三角形周长为:" << Len() << ",面积为:" << Area();
    }

private:
    Point A, B, C;
};
bool ifTriangle(Point &p1, Point &p2, Point &p3)
{
here:
    if ((p1.distance(p2) + p2.distance(p3) > p3.distance(p1)) && (p1.distance(p2) + p3.distance(p1) > p2.distance(p3)) && (p2.distance(p3) + p3.distance(p1) > p1.distance(p2)))
    {
        cout << "三角形三个顶点坐标为:" << endl;
        p1.output();
        p2.output();
        p3.output();
        Triangle T(p1, p2, p3);
        T.output();
    }
    else
    {
        cout << "顶点坐标不正确,不能构成三角形!请重新输入坐标!" << endl;
        return false;
    }
}
int main()
{
    Point A, B, C;
here:
    A.input();
    B.input();
    C.input();
    if (ifTriangle(A, B, C) == false)
    {
        goto here;
    }
    return 0;
}

判断是否是三角形

两边之和大于第三边
或者(两边之差小于第三边)
但是要列出3个
比如
a+b>c&&a+c>b&&b+c>a

if ((p1.distance(p2) + p2.distance(p3) > p3.distance(p1)) && (p1.distance(p2) + p3.distance(p1) > p2.distance(p3)) && (p2.distance(p3) + p3.distance(p1) > p1.distance(p2)))

实验03(04)标准类string的使用

题目描述
使用C++中的string标准类,将5个字符串按由小到大顺序输出(注意:使用string类中封装的成员函数)。
说明:查阅C++类库中的string类,了解类中的成员函数和运算符。 
输入描述
五个字符串 
输出描述
按由小到大排列的5个字符串 
输入样例
string month attack price hello 
输出样例
排序后的结果为:
attack hello month price string
#include <iostream>
#include <string>

using namespace std;
int main()
{
    int i;
    string str[5];
    void sort(string s[]);
    for (i = 0; i < 5; i++)
        cin >> str[i];
    sort(str);
    cout << "排序后的结果为:" << endl;
    for (int i = 0; i < 5; i++)
        cout << str[i] << endl;

    return 0;
}
void sort(string s[])
{
    string temp;
    for (int i = 0; i < 4; i++) //使用冒泡法排序
    {
        for (int j = 0; j < 4 - i; j++)
        {
            if (s[j] > s[j + 1])
            {
                temp = s[j];
                s[j] = s[j + 1];
                s[j + 1] = temp;
            }
        }
    }
}
  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

bmNkotc2AECynaY6

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

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

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

打赏作者

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

抵扣说明:

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

余额充值