C++综合实验 继承 静态数据成员 运算符重载 栈

2:Composite

// 注意:无需提交main函数
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
class CPU {
private:
	string cpuType0;
	double frequency0;
public:
	CPU(string a, double b) {
		cpuType0 = a;
		frequency0 = b;
	}
	CPU() {

	}
	void print1() {
		cout << "CPU type: " << cpuType0 << ", CPU frequency: "<<frequency0<<" GHz"<<endl;
	}
};
class Disk {
private:
	string diskType0; double capacity;
public:
	Disk(string c, double d) {
		diskType0 = c;
		capacity = d;
	}
	Disk() {

	}
	void print2() {
		cout << "disk type: " << diskType0 << ", disk capacity: " << capacity << " T " << endl;
	}
};
class Computer :virtual public CPU, virtual public Disk {
protected:
	int C3;
public:
	CPU cpu;
	Disk disk;
	Computer(string a1, double a2, string b2, double b3, int c1) : CPU(a1, a2), Disk(b2, b3) {
		C3 = c1;
	}
	Computer(CPU cpu, Disk disk)
	{
		this->cpu = cpu;
		this->disk = disk;		
	}
	void Print() {
		cout << "The computer has a cpu and a disk."<<endl;
		cpu.print1();
		disk.print2();
	}
};
int main()
{
    string cpuType, diskType;
    double frequency, capacity;
    cin >> cpuType >> frequency >> diskType >> capacity;
    CPU cpu(cpuType, frequency);
    Disk disk(diskType, capacity);
    Computer computer(cpu, disk);

    computer.Print();
    return 0;
}

在这里插入图片描述

3:静态数据成员

就是要注意在主函数中访问静态成员时,应先在主函数外面初始化静态成员

int Student::count = 0;

在这里插入图片描述

// 注意:无需提交main函数和Student类定义,提交Student类实现及其他相关代码
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
    int age;   //年龄
    string name;  //姓名
public:
    static int count; //静态成员,表示学生人数
    Student(int a, string n);
    Student();
    ~Student();
    void Print();
};

int Student::count = 0;
Student::Student(int a, string n)
{
	name = n;
	age = a;
	count++;
}

Student::Student()
{
	name = "NoName";
	age = 0;
	count++;
}

Student::~Student()
{
	count--;
}

void Student::Print()
{
	cout << "Name=" << name << ", age=" << age << endl;
}

int main()
{
    cout<<"count="<<Student::count<<endl;
    Student s1, *p=new Student(23,"ZhangHong");
    s1.Print();
    p->Print();
    delete p;
    s1.Print();
    Student Stu[4];
    cout<<"count="<<Student::count<<endl;
    return 0;
}

4:Ellipse

设椭圆的长轴为a,短轴为b,注意长轴可能是x轴,也可能是y轴
椭圆的面积计算 :PI* a* b
椭圆的离心率计算:e=c/a; 其中c=sqrt(a * a -b * b)//此处假设a>b,否则调换a,b
判断一个点和椭圆的关系:

在平面上的任意点P和某椭圆的关系就这么三种;当点在椭圆内将点的坐标带入椭圆的方程根据椭圆的定义:椭圆上的点到两焦点的距离的和为2a, 根据点到点的距离方程就可列出椭圆的标准公式;若某点位于椭圆内,则该点到两焦点的距离的和就小于2a,若某点位于椭圆外,则该点到两焦点的距离的和就大于2a,

所以我们需要找到两个焦点,然后求给定点到焦点的距离,注意,焦点可能在x轴,也可能在y轴
在这里插入图片描述

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
const double PI = acos(-1.0);
class Point
{
private:
	double x, y;
public:
	Point(double a = 0, double b = 0) :x(a), y(b) {}
	~Point() {}
	double getX() const { return x; }
	double getY() const { return y; }
	void setX(double a) { x = a; }
	void setY(double b) { y = b; }
};

class Ellipse
{
public:
	Point pAxis;
	Ellipse(double x = 0, double y = 0) :pAxis(x, y) { }
	~Ellipse() { }
	Ellipse(const Ellipse&);
	Ellipse transposition();    // 转置(交换长短半轴)
	double area();            // 面积
	double eccentricity();        // 离心率
	string position(const Point);    // 位置关系
};

int main()
{	
	double a, b;
	while (1)
	{
		cin >> a >> b;
		Ellipse d1(a, b), d2(d1), d3 = d2.transposition();
		cout << d1.area() << endl;	
		cout << d2.eccentricity() << endl;
		cin >> a >> b;
		Point p(a, b);
		cout << d3.position(p) << endl;
	}
	return 0;
}

Ellipse::Ellipse(const Ellipse &e)
{	
		this->pAxis.setX(e.pAxis.getX());
		this->pAxis.setY(e.pAxis.getY());
}

Ellipse Ellipse::transposition()
{
	double temp=this->pAxis.getX();
	double temp2 = this->pAxis.getY();
	Ellipse d(temp2, temp);
	return d;
}

double Ellipse::area()
{
	return PI * this->pAxis.getX()*this->pAxis.getY();
}

double Ellipse::eccentricity()
{
	double c = 0.0;
	if (this->pAxis.getX() > this->pAxis.getY())
	{
		c = sqrt(this->pAxis.getX()*this->pAxis.getX() - this->pAxis.getY()*this->pAxis.getY());
		return  (double)c /(double) this->pAxis.getX();
	}
	else
	{
		c = sqrt(this->pAxis.getY()*this->pAxis.getY()- this->pAxis.getX()*this->pAxis.getX());
		return  (double)c / (double)this->pAxis.getY();
	}
	
}

string Ellipse::position(const Point p)
{
	
	double c = 0.0;
	double jiaoju;
	Point q1,q2;
	if (this->pAxis.getX() > this->pAxis.getY())
	{
		c = sqrt(this->pAxis.getX()*this->pAxis.getX() - this->pAxis.getY()*this->pAxis.getY());
		q1.setX(c);	q1.setY(0);
		q2.setX(-c);q2.setY(0);
		jiaoju = 2 * this->pAxis.getX();
	}
	else
	{
		c = sqrt(this->pAxis.getY()*this->pAxis.getY() - this->pAxis.getX()*this->pAxis.getX());
		q1.setX(0);	q1.setY(c);
		q2.setX(0); q2.setY(-c);
		jiaoju = 2 * this->pAxis.getY();
	}
	

	double sum1 = sqrt((p.getX() - q1.getX())*(p.getX() - q1.getX()) + (p.getY() - q1.getY())*(p.getY() - q1.getY()));
	double sum2 = sqrt((p.getX() - q2.getX())*(p.getX() - q2.getX()) + (p.getY() - q2.getY())*(p.getY() - q2.getY()));
	
	
	if (sum1 + sum2 == jiaoju)
	{

		return "at";
	}
	else if (sum1 + sum2 < jiaoju)
	{
		return "inside";
	}
	else
	{		
		return "outside";
	}
	
}

在这里插入图片描述

5:AdvancedInheritance

// 注意:无需提交main函数
#include<iostream>
#include<string>
using namespace std;

class Animal {
public:
	string type;
	string color;
	Animal() {

	}
	Animal(string type, string color) {
		this->type = type;
		this->color = color;
	}
	virtual void Print()
	{

	}
};
class Fish :virtual public Animal {
public:
bool  Osteichthyes;
	Fish(string type,string color, bool  Osteichthyes) :Animal(type, color) {
		this->type = type;
		this->color = color;
		this->Osteichthyes = Osteichthyes;
	}
	virtual void Print()
	{
		cout << "type: " << type << ", color: " << color << ", Osteichthyes: " << Osteichthyes << endl;
	}
};

class Bird:virtual public Animal {
public:
	bool   daytime;
	Bird(string type, string color, bool  daytime) :Animal(type, color) {
		this->type = type;
		this->color = color;
		this->daytime = daytime;
	}
	virtual void Print()
	{
		cout << "type: " << type << ", color: " << color << ", daytime: " << daytime << 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;
}

在这里插入图片描述

6:有理数类

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

类的定义如下:

#include<iostream>
#include<string>
#include<cmath>
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 &);

};

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

	while (1)
		
	{
		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;
}

Rational::Rational(int a, int b)
{

	this->z = a;
	this->m = b;
}

int gcd(int p, int q)
{
	/*if (p < 0) p = -p;
	if (q < 0) q = -q;*/
	if (q == 0)
	{
		return p;
	}
	else {
		int r = p % q;
		return gcd(q, r);
	}
}
Rational & Rational::yuefen()
{
	// TODO: 在此处插入 return 语句
	int a = gcd(this->m, this->z);
	this->m = this->m / a;
	this->z = this->z/ a;
	Rational r(this->z, this->m);
	return r;
}

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

	return *this;
}

Rational & Rational::operator-=(const Rational & r3)
{
	// TODO: 在此处插入 return 语句
	Rational r;
	r.m =this->m * r3.m;
	r.z = this->z*r3.m - this->m*r3.z;
	r = r.yuefen();
	this->m = r.m;
	this->z = r.z;

	return *this;
}

Rational & Rational::operator*=(const Rational & r3)
{
	// TODO: 在此处插入 return 语句
	Rational r;
	r.m = this->m * r3.m;
	r.z = this->z * r3.z;
	r = r.yuefen();
	this->m = r.m;
	this->z = r.z;

	return *this;
}

Rational & Rational::operator/=(const Rational & r2)
{
	// TODO: 在此处插入 return 语句
	Rational r;
	r.m = this->m * r2.z;
	r.z = this->z * r2.m;
	r = r.yuefen();
	this->m = r.m;
	this->z = r.z;

	return *this;
}

Rational operator+(const Rational & r1, const Rational & r2)
{
	Rational r;
	r.m = r1.m * r2.m;
	r.z = r1.z*r2.m + r1.m*r2.z;
	r = r.yuefen();
	return r;
}

Rational operator-(const Rational & r1, const Rational & r2)
{
	Rational r;
	r.m = r1.m * r2.m;
	r.z = r1.z*r2.m - r1.m*r2.z;
	r = r.yuefen();
	return r;
}

Rational operator*(const Rational & r1, const Rational & r2)
{
	Rational r;
	r.m = r1.m * r2.m;
	r.z = r1.z * r2.z;
	r = r.yuefen();
	return r;
}

Rational operator/(const Rational & r1, const Rational & r2)
{
	Rational r;
	r.m = r1.m * r2.z;
	r.z = r1.z * r2.m;
	r = r.yuefen();
	return r;
}

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

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

ostream & operator<<(ostream &output, const Rational &r1)
{
	// TODO: 在此处插入 return 语句	
    Rational r = r1;
    r = r.yuefen();
	if (r.z == 0)//等于0
	{
		output << r.z;
		return output;
	}

	if (r.z*r.m < 0)//有一个小于0
	{
		if (r.m <0)//分母小于0
		{
			output  << -r.z << "/" << -r.m;
		}
		else//分子小于0
		{
			output  << r.z << "/" << r.m;
		}
	
	}
	/*else if(r.z*r.m == 0)
	{
		if (r.m < 0)
		{
			output << "-" << -r.z << "/" << -r.m;
		}
		else
		{
			output << "-" << r.z << "/" << r.m;
		}
	}*/
	else if (r.m < 0 && r.z <= 0)//都小于0
	{
		output << -r.z << "/" <<-r.m;
	}	
	else//都不小于0
	{
		output << r.z << "/" << r.m;
	}
	
	return output;
}

istream & operator>>(istream &input, Rational &r)
{
	// TODO: 在此处插入 return 语句
	input >> r.z >> r.m;
	return input;
}

在这里插入图片描述

7:Line

// 注意:main函数已经存在,无需提交。仅提交Point2和Line2定义及实现
#include<iostream>
#include<string>
#include<cmath>
using namespace std;

template<class T> 
class Point2{

public:
	T x;
	T y;
	Point2()
	{
	}
	Point2(T x, T y)
	{
		this->x = x;
		this->y = y;
	}
};

template<class S>
class Line2 {

public:
	Point2<S> pt1;
	Point2<S> pt2;
	
	Line2(Point2<S> pt1, Point2 <S>pt2)
	{
		this->pt1 = pt1;
		this->pt2 = pt2;
	}
	
	S Length()
	{
      
		return sqrt((pt1.x - pt2.x)*(pt1.x - pt2.x) + (pt1.y - pt2.y)*(pt1.y - pt2.y));
	}

};
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;
}

在这里插入图片描述

8:铁轨

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

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include  <vector>
#include <stack>
#include <stdio.h>
#include <string.h>
using namespace std;
class Solution {
public:
	bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
		if (pushed.size() != popped.size()) return false;
		stack<int> stk;
		int i = 0;
		for (auto x : pushed) {
			stk.push(x);
			while (!stk.empty() && stk.top() == popped[i]) {
				stk.pop();
				i++;
			}
		}
		return stk.empty();
	}
};
int main()
{
	int n = 0;
	char s[1000];
	while (cin >> n)
	{	
	int a = 0;
	vector<int>pushed;
	vector<int>popped;
	Solution solution;	
	for (int i = 1; i <= n; i++)
	{
		pushed.push_back(i);
		cin >> a;
		popped.push_back(a);
	}
	if (solution.validateStackSequences(pushed, popped))
	{
		cout << "Yes" << endl;
	}
	else
	{
		cout << "No" << endl;
	}
	}

	//system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值