综合实验(续)

AdvancedInheritance

描述

不同的动物既有共性也有个性。鸟类会飞,鱼会游泳。请设计类的层次结构进行表示,并通过以下测试

int main()
{
    Animal *animal;
    string type, color;
    bool Osteichthyes, daytime;
    cin >> type >> color >> Osteichthyes;
    Fish fish(type, color, Osteichthyes);
    fish.Print();
    animal = &fish;
    animal->Print();
    cin >> type >> color >> daytime;
    Bird bird(type, color, daytime);
    bird.Print();
    animal = &bird;
    animal->Print();
    return 0;
}

输入

鱼类型 鱼的颜色 是否硬骨鸟类型 鸟的颜色 是否白天活动

输出

见样例,冒号和逗号后有一个空格

输入样例 1 

chub white 1
swallow black 1

输出样例 1

type: chub, color: white, Osteichthyes: 1
type: chub, color: white, Osteichthyes: 1
type: swallow, color: black, daytime: 1
type: swallow, color: black, daytime: 1
#include<iostream>
#include<string>
using namespace std;
class Animal
{
public:
    string type, color;
    bool yes;
    virtual void Print() {}
};

class Fish :public Animal
{
public:
    int yes;
    Fish(string t, string c, bool y)
    {
        type = t;
        color = c;
        yes = y;
    }
    virtual void Print()
    {
        cout << "type: " << type << ", color: " << color << ", Osteichthyes: " << yes << endl;
    }
};

class Bird :public Animal
{
public:
    int yes;
    Bird(string t, string c, bool y)
    {
        type = t;
        color = c;
        yes = y;
    }
    virtual void Print()
    {
        cout << "type: " << type << ", color: " << color << ", daytime: " << yes << endl;
    }
};
int main()
{
    Animal* animal;
    string type, color;
    bool Osteichthyes, daytime;
    cin >> type >> color >> Osteichthyes;
    Fish fish(type, color, Osteichthyes);
    fish.Print();
    animal = &fish;
    animal->Print();
    cin >> type >> color >> daytime;
    Bird bird(type, color, daytime);
    bird.Print();
    animal = &bird;
    animal->Print();
    return 0;
}

有理数类

描述

设计一个有理数类Rational,要求对运算符“+”“-”“*”“/”和“+=”“-=”“=”“/=”进行重载,完成有理数的加减乘除以及加减乘除复合赋值运算;并且重载“<<”和“>>”操作符完成有理数的输入和输出。最后,重载“==”和“!=”比较两个有理数是否相等。

类的定义如下:


class Rational

{

private:

    int z;    //分子

    int m;    //分母

public:

    Rational(int a=0, int b=1);  //构造有理数分数,分子默认为0,分母默认为1

    Rational& yuefen(); //约分函数对分数化简

    friend Rational operator+(const Rational &r1, const Rational &r2);

    friend Rational operator-(const Rational &r1, const Rational &r2);

    friend Rational operator*(const Rational &r1, const Rational &r2);

    friend Rational operator/(const Rational &r1, const Rational &r2);

    Rational & operator+=(const Rational &r);

    Rational & operator-=(const Rational &r);

    Rational & operator*=(const Rational &r);

    Rational & operator/=(const Rational &r);

    friend bool operator==(const Rational &, const Rational &);//判断两个有理数是否相等

    friend bool operator!=(const Rational &, const Rational &);//判断两个有理数是否不等

    friend ostream & operator<<(ostream &, const Rational &);

    friend istream & operator>>(istream &, Rational &);

};

使用以下的main函数体进行测试:

int main()
{
    Rational r1, r2, r3;

    while (cin >> r1 >> r2)
    {
        cout << "r1 = " << r1 << "\n" << "r2 = " << r2 << endl;
        r3 = r1 + r2;
        cout << "r1+r2 = " << r3 << endl;
        r3 = r1 - r2;
        cout << "r1-r2 = " << r3 << endl;
        r3 = r1 * r2;
        cout << "r1*r2 = " << r3 << endl;
        r3 = r1 / r2;

        cout << "r1/r2 = " << r3 << endl;
        cout << (r1 == r2) << " " << (r1 != r2) << endl;
        cout << (r1 += r2) << endl;
        cout << (r1 -= r2) << endl;
        cout << (r1 *= r2) << endl;
        cout << (r1 /= r2) << endl;
    }

    return 0;
}

输入

输入r1,r2的值

输出

输出r1,r2在各种操作之后的值。

输入样例 1 

-4 6
2 -5

输出样例 1

r1 = -2/3
r2 = -2/5
r1+r2 = -16/15
r1-r2 = -4/15
r1*r2 = 4/15
r1/r2 = 5/3
0 1
-16/15
-2/3
4/15
-2/3

提示

  • Rational& yuefen();该函数原理是求得分子和分母的最大公约数gcd,然后将m和z除以gcd得到最简分数形式。求最大公约数的方法叫做 辗转相除法,具体可上网查询。
  • 观察输出,负号总是在分子前,若输入不符合该情形,需做相应处理。
  • 分数总以化简形式输出,可在所有成员函数及运算符函数内恰当位置调用yuefen

// 注意:无需提交main函数和Rational类定义,提交Rational类实现及其他相关代码
#include<iostream>
using namespace std;
class Rational

{

private:

    int z;    //分子

    int m;    //分母

public:

    Rational(int a = 0, int b = 1);  //构造有理数分数,分子默认为0,分母默认为1

    Rational& yuefen(); //约分函数对分数化简

    friend Rational operator+(const Rational& r1, const Rational& r2);

    friend Rational operator-(const Rational& r1, const Rational& r2);

    friend Rational operator*(const Rational& r1, const Rational& r2);

    friend Rational operator/(const Rational& r1, const Rational& r2);

    Rational& operator+=(const Rational& r);

    Rational& operator-=(const Rational& r);

    Rational& operator*=(const Rational& r);

    Rational& operator/=(const Rational& r);

    friend bool operator==(const Rational&, const Rational&);//判断两个有理数是否相等

    friend bool operator!=(const Rational&, const Rational&);//判断两个有理数是否不等

    friend ostream& operator<<(ostream&, const Rational&);

    friend istream& operator>>(istream&, Rational&);

};

Rational::Rational(int a, int b)
{
    z = a;
    m = b;
}
Rational& Rational::yuefen()
{
    if (z == 0)return *this;
    int x, y, r;
    if (abs(z) > abs(m)) {
        x = abs(z);
        y = abs(m);
    }
    else {
        x = abs(m);
        y = abs(z);
    }
    r = x % y;
    while (r != 0)
    {
        x = y;
        y = r;
        r = x % y;
    }
    z = z / y;
    m = m / y;
    if (m < 0 && z < 0) {
        z = abs(z);
        m = abs(m);
    }
    else if (m < 0 && z>0) {
        z = -z;
        m = abs(m);
    }
    return *this;
}
Rational operator+(const Rational& r1, const Rational& r2)
{
    Rational r;
    r.m = r1.m * r2.m;
    r.z = r1.z * r2.m + r2.z * r1.m;
    r.yuefen();
    return r;
}
Rational operator-(const Rational& r1, const Rational& r2)
{
    Rational r;
    r.z = r1.z * r2.m - r2.z * r1.m;
    r.m = r1.m * r2.m;
    r.yuefen();
    return r;
}
Rational operator*(const Rational& r1, const Rational& r2)
{
    Rational r;
    r.z = r1.z * r2.z;
    r.m = r1.m * r2.m;
    r.yuefen();
    return r;
}
Rational operator/(const Rational& r1, const Rational& r2)
{
    Rational r;
    r.z = r1.z * r2.m;
    r.m = r1.m * r2.z;
    r.yuefen();
    return r;
}
Rational& Rational::operator+=(const Rational& r)
{
    this->z = this->z * r.m + this->m * r.z;
    this->m = this->m * r.m;
    this->yuefen();
    return *this;

}

Rational& Rational::operator-=(const Rational& r)
{
    this->z = this->z * r.m - this->m * r.z;
    this->m = this->m * r.m;
    this->yuefen();
    return *this;

}

Rational& Rational::operator*=(const Rational& r)
{
    this->z = this->z * r.z;
    this->m = this->m * r.m;
    this->yuefen();
    return *this;
}

Rational& Rational::operator/=(const Rational& r)
{
    this->z = this->z * r.m;
    this->m = this->m * r.z;
    this->yuefen();
    return *this;
}

bool operator==(const Rational& r1, const Rational& r2)
{
    if ((r1.z == r2.z) && (r1.m == r2.m))
    {
        return true;
    }
    else
        return false;
}

bool operator!=(const Rational& r1, const Rational& r2)
{
    if ((r1.z != r2.z) || (r1.m != r2.m))
    {
        return true;
    }
    else
        return false;
}
ostream& operator<<(ostream& os, const Rational& Ra)
{
    os << Ra.z << "/" << Ra.m;

    return os;
}
istream& operator>>(istream& is, Rational& Ra)
{
    is >> Ra.z >> Ra.m;
    Ra.yuefen();
    return is;
}
int main()
{
    Rational r1, r2, r3;

    while (cin >> r1 >> r2)
    {
        cout << "r1 = " << r1 << "\n" << "r2 = " << r2 << endl;
        r3 = r1 + r2;
        cout << "r1+r2 = " << r3 << endl;
        r3 = r1 - r2;
        cout << "r1-r2 = " << r3 << endl;
        r3 = r1 * r2;
        cout << "r1*r2 = " << r3 << endl;
        r3 = r1 / r2;

        cout << "r1/r2 = " << r3 << endl;
        cout << (r1 == r2) << " " << (r1 != r2) << endl;
        cout << (r1 += r2) << endl;
        cout << (r1 -= r2) << endl;
        cout << (r1 *= r2) << endl;
        cout << (r1 /= r2) << endl;
    }

    return 0;
}

Line

描述

表示点和线是几何学的基础。请实现模板类的点(Point2)以及线段(Line2),并计算线段长度 Line2::Length();完成以上类,并通过以下测试

int main() 
{
    Point2<double> pt1(1.0, 1.0);
    Point2<double> pt2(3.0, 4.0);
    Line2<double> line(pt1, pt2);
    cout << line.Length() << endl;

    int x1,y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    Line2<int> nLine(Point2<int>(x1, y1), Point2<int>(x2, y2));
    cout << nLine.Length()<< endl;

    return 0;
}

输入

两个点的x,y坐标

输出

参考样例输出

输入样例 1 

1 2 3 4

输出样例 1

3.60555
2

提示

数学函数头文件为<cmath>

#include<iostream>
#include<cmath>
#include<string>
using namespace std;
template<typename T>
class Point2
{
private:
    T x;
    T y;
public:
    Point2(T a, T b) :x(a), y(b) {}
    T getX() { return x; }
    T getY() { return y; }
};
template<typename T>
class Line2 {
private:
    Point2<T> p1;
    Point2<T> p2;
public:
    Line2(Point2<T> a, Point2<T> b) :p1(a), p2(b) {}
    T Length()
    {
        return sqrt(abs((p1.getX() - p2.getX()) * (p1.getX() - p2.getX()) + (p1.getY() - p2.getY()) * (p1.getY() - p2.getY())));
    }
};
int main()
{
    Point2<double> pt1(1.0, 1.0);
    Point2<double> pt2(3.0, 4.0);
    Line2<double> line(pt1, pt2);
    cout << line.Length() << endl;

    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    Line2<int> nLine(Point2<int>(x1, y1), Point2<int>(x2, y2));
    cout << nLine.Length() << endl;

    return 0;
}

铁轨

描述

某城市有一个火车站,铁轨铺设如图所示。有n节车厢从A方向驶入车站,按进站顺序编号为1~n。你的任务是让它们按照某种特定的顺序进入B方向的铁轨并驶出车站。为了重组车厢,你可以借助中转站C。这是一个可以停放任意多节车厢的车站,但由于末端封闭,驶入C的车厢必须按照相反的顺序驶出C。对于每个车厢,一旦从A移入C,就不能再回到A了;一旦从C移入B,就不能回到C了。换句话说,在任一时刻,只有两种选择:A->C和C->B。
请编程判断:按给定的出站顺序,火车能否出站。

输入

输入包含多组测试数据,每组数据的第一行是一个正整数n(1<n<1000),第二行是第1个~第n个这n个整数的一个全排列。

输出

对于每一组测试数据,如果能按要求完成车厢重组,请输出“Yes”,否则输出“No”,每组输出占一行。

输入样例 1 

5
1 2 3 4 5
5
5 4 1 2 3
6
6 5 4 3 2 1

输出样例 1

Yes
No
Yes

提示

#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
int n, rail[1000];
int main()
{
	while (scanf("%d", &n) == 1)
	{
		stack<int> s;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &rail[i]);
		}
		int flag, a, b;
		flag = a = b = 1;
		while (b <= n)
		{
			if (a == rail[b])
			{
				a++;
				b++;
			}
			else if (!s.empty() && s.top() == rail[b])
			{
				s.pop();
				b++;
			}
			else if (a <= n)
				s.push(a++);
			else
			{
				flag = 0;
				break;
			}
		}
		printf("%s\n", flag ? "Yes" : "No");
	}
	return 0;
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值