553C++笔试笔记2010

目录

1.Vehicle类 、Car类点、Brake类和Wheel类

 2.shape类 二维图形与三维图形。

 3.输入n个十进制数转换成二进制写到文件

​编辑

4.插入排序的迭代与递归实现 

 5.多项式类(要求构造,拷贝,析构,重载运算符)

重载运算符如下:

1.友元函数

2.成员函数重载

6.字符串解析


1.Vehicle类 、Car类点、Brake类和Wheel类

//Vehicle类
class Vehicle {
private:
	int speed;
public:
	Vehicle(int s = 0) :speed(s) {}

	int getSpeed()const {
		return speed;
	}
};

//Wheel类
#include<string>
using namespace std;
class Wheel {
private:
	string wheelBrand;
public:
	Wheel(string wb = "") :wheelBrand(wb) {}

	string getWheelBrand()const {
		return wheelBrand;
	}
	void setWheelBrand(string wb){
		wheelBrand=wb;
	}

};

//Brake类
#include<string>
using namespace std;
class Brake {
private:
	string brakeBrand;
public:
	Brake(string bb = "") :brakeBrand(bb) {}
	
	string getBrakeBrand(){
		return brakeBrand;
	}

	void setBrakeBrand(string bb) {
		brakeBrand = bb;
	}
};

//Car类
#include"vehicle.cpp"
#include"brake.cpp"
#include"wheel.cpp"
#include<iostream>
class Car :public Vehicle {
private:
	string carBrand;
	Wheel wheel;
	Brake brake;
public:
	Car(int s, string bBrand, string cBrand, string wBrand):Vehicle(s){
		brake.setBrakeBrand(bBrand);
		wheel.setWheelBrand(wBrand);
	/*Vehicle(s);
	Brake(bBrand);
	Wheel(wBrand);*/
	carBrand = cBrand;
	}

	void print() {
		cout << "速度为" << getSpeed() << "KM/H" << endl;
		cout << "车牌为" << carBrand  << endl;
		cout << "轮胎品牌为" << wheel.getWheelBrand() << endl;
		cout << "刹车品牌为" << brake.getBrakeBrand() << endl;
	}
}
;


//主函数
#include<iostream>
#include"car.cpp"
using namespace std;
int main() {
	Car mycar(200, "宝马", "米其林", "金麒麟");
	mycar.print();
	return 0;
}

运行结果:

 2.shape类 二维图形与三维图形。

//shape类
#ifndef SHAPE_CPP
#define SHAPE_CPP
#include<string>
using namespace std;
class Shape {
private: 
	string color;
public:
	Shape(string c=""):color(c){}

	virtual int calculate()const = 0;

	string getcolor()const {
		return color;
	}
};
#endif

//2dshape
#ifndef _2DSHAPE_CPP
#define _2DSHAPE_CPP
#include"Shape.cpp"
#include<string>

class _2dShape:public Shape {
private:
	int length;
	int wedth;
public:
	_2dShape(string c, int l, int w) :Shape(c), length(l), wedth(w) {}
	int getLength()const {
		return length;
	}
	int getWedth()const {
		return wedth;
	}
	virtual int calculate()const {
		return getWedth() * getLength();
	}
};
#endif 

//3dshape
#ifndef _3DSHAPE_CPP
#define _3DSHAPE_CPP
#include"_2dShape.cpp"
#include<string>
class _3dShape :public _2dShape {
private:
	int height;
public:
	_3dShape(string color,int height, int wedth, int length) :height(height), _2dShape(color,length, wedth) {}
	int getHeight()const {
		return height;
	}
	int calculate()const {
		return getHeight() * _2dShape::calculate();
	}
};
#endif


//main
#include<iostream>
#include"_2dShape.cpp"
#include"_3dShape.cpp"
#include<string>
using namespace std;
int main() {
	Shape* shapePtr;
	_2dShape b("red",10,5);
	cout << "b的面积为:" << b.calculate()<<endl;
	_3dShape c("red", 3, 4, 5);
	cout << "c的体积为  " << c.calculate()<<endl;
	

}

运行结果如图:

 3.输入n个十进制数转换成二进制写到文件

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

string toBinary(int d) {
	string s;
	if (d == 0)
		return"0";
	while (d) {
		if(d % 2 > 0)
			s += "1";
		else
			s += "0";
		d = d / 2;
	}
	reverse(s.begin(), s.end());
		return s;
}

int main() {
	int n, m;
	ofstream os;
	os.open("D:/vs/Project1/Project1/file.txt");
	if (!os) {
		cerr << "File cannot be opened!\n" << endl;
		exit(EXIT_FAILURE);
	}
	cout << "请输入十进制数的个数" << endl;
	cin >> n;
	while (n > 0) {
		cout << "请输入" ;
		cin >> m;
		os << toBinary(m) << endl;
		n--;
	}
	return 0;
}










4.插入排序的迭代与递归实现 

//insert1
#include<iostream>
#include"Insert2.cpp"
using namespace std;
template<class T1>
void Insert1(T1 a[], int n) {
	T1 temp;
	int j;
	for (int i = 1; i < n; i++) {
		temp = a[i];
		j = i - 1;
		while (j >= 0 && temp < a[j]) {
			a[j + 1] = a[j];
			j--;
		}
		a[j + 1] = temp;
	}
}

int main() {
	int A[7] = { 1,2,3,4,5,6,7 };
	double B[3] = { 1.2,2.3,1.4 };
	Insert1(A, sizeof(A) / sizeof(*A));//其实这里这么写我也不太清楚
	for (auto const item : A)
		cout << item << " ";
	Insert2(B, sizeof(B) / sizeof(*B)-1);
	for (auto const item : B)
		cout << item << " ";
	return 0;
}

//insert2
#include<iostream>
using namespace std;

template<class T2>
void Insert2(T2 a[], int n) {
	if (n > 0) {
		Insert2(a, n - 1);
			int i = n-1;
	T2 temp = a[n];
	while (i >= 0 && temp < a[i]) {
		a[i + 1] = a[i];
		i--;
	}
	a[i + 1] = temp;
	}
	return;

}

 5.多项式类(要求构造,拷贝,析构,重载运算符)

#include<vector>
#include<iostream>
using namespace std;
class Poly {
private:
	int xiangshu;
	vector<double> xishu;
public:
	Poly();
	Poly(vector<double>, int);
	Poly(const Poly&);
	~Poly();
	void setXishu(int, double);
	double getXishu(int) const;
	void print()const;
};

Poly::Poly() {
	xiangshu = 0;
}
Poly::Poly(vector<double> a, int b) {
	xishu.assign(a.begin(), a.end());
	xiangshu = b;
}
Poly::~Poly() {
	xishu.shrink_to_fit();
}

void Poly::setXishu(int a, double b) {
	xishu[a] = b;
}

double Poly::getXishu(int a)const {
	return xishu[a];
}

void Poly::print()const {
	cout << "Poly's xishu :";
		for (int i = 0; i < xishu.size(); i++) {
			cout << xishu[i] << ends;
	}	
		cout << endl<<"Poly's xiangshu:" << xiangshu;
}


int main() {
	vector<double> v1 = { 1,2,3,4,5,6 };
	Poly p1(v1, 6);
	p1.print();
}

运行结果:

重载运算符如下:

1.友元函数

Poly operator+(const Poly& p1,const Poly& p2) {
	if (p1.xiangshu > p2.xiangshu) {
		Poly p3(p1);
		int i;
		for (i = 0; i < p2.xiangshu; i++) {
			p3.xishu[i] = p1.xishu[i] + p2.xishu[i];
		}	return p3;

	}
	else {
		Poly p3(p2);
		int i;
		for (i = 0; i < p1.xiangshu; i++) {
			p3.xishu[i] = p1.xishu[i] + p2.xishu[i];
		}return p3;
	}
}
int main() {
	vector<double> v1 = { 1,2,3,4,5,6 };
	Poly p1(v1, 6);
	p1.print();
	vector<double> v2 = { 1,2,3,4,5,6,6,7,8,9 };
	Poly p2(v2, 9);
	p2.print();
	Poly p3 = p1 + p2;
	p3.print();

}

2.成员函数重载

Poly Poly::operator+(const Poly& p1) {
	if (this->xiangshu > p1.xiangshu) {
		
		for (int i = 0; i < p1.xiangshu; i++) {
			this->xishu[i] += p1.xishu[i];
			this->xiangshu = this->xiangshu;
		}
		return *this;
	}
	else {
		Poly p2(p1);
		for (int i = 0; i < this->xiangshu; i++) {
			p2.xishu[i] += this->xishu[i];
		}
		return p2;
	}
}

6.字符串解析

那题比较简单,这里就略过了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值