C++课程期末复习全集

id:192 最贵的两本书

cpp 复制代码
#include<iostream>
#include<string>
#include<iomanip>
//#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pb push back
#define sedgcd
#define len(a,b)(a* b)/ ged(a, b)
#define all(a) a. beginO. a. end()
#define men(a, x)menset(a, x sizeof(a))
#define f(i,s,e)for(inti=s:i<=e; i++)
#define ff(i,s,e)for(intis;i>e;i-)
#define setbits(x)builtin_popcount(x)
#define zrobits(x)builtin_ctzll(x)
#define mad 1000000007
#define maxn(ll)(2e5 + 5000)
#define INF LONG LONG MAX
using namespace std;
class Book {
public:
	string name;
	string author;
	double price;
	string publisher;

	Book(string n, string a, double p, string d) {
		name = n;
		author = a;
		price = p;
		publisher = d;
	}
	friend void find(Book* book, int n, int& max1index, int& max2index);//参数使用引用,实参会发生跟形参一样的变化
	Book() :name(""), author(" "), price(0.0), publisher(" "){ }
	friend istream& operator>>(istream& is, Book& book); 
	friend ostream& operator<<(ostream& os, const Book& book);
};
void find(Book* book, int n, int& max1index, int& max2index) {  //函数类型为不返回就得用引用
	if (n == 0)
		return;
	max1index = 0;
	max2index = n>1?1:0;
	if (n > 1 && book[1].price > book[0].price)
	{
		max1index = 1;
		max2index = 0;
	}
	for (int i = 2; i < n; i++) {
		if (book[i].price > book[max1index].price) {
			max2index = max1index;
			max1index = i;
		}
		else if (book[i].price > book[max2index].price) {
			max2index = i;
		}
	}

}
istream& operator>>(istream& is,Book& book) {
	string pricestr;
	getline(is, book.name, ',');
	getline(is, book.author, ',');
	getline(is,pricestr, ',');
	book.price = stod(pricestr);
	getline(is, book.publisher);
	return is;
}
ostream& operator<<(ostream& os, const Book& book) {
	os << book.name << "\n";
	os << book.author << "\n";
	os << fixed << setprecision(2) << book.price << "\n";
	os << book.publisher;
	return os;
}

int main() {
	int t;
	cin >> t;
	string name, author, address;
	double price;
	for(int i=0;i<t;i++) {
		int n;
		cin >> n;
		Book* book = new Book[n];
		for (int j = 0; j < n; j++) {
			cin >> book[j];
		}
		int max1index, max2index;
		find(book, n, max1index, max2index);
		cout << book[max1index] << endl;
		cout << endl;
		cout << book[max2index] << endl;
		delete[]book;
	}
	return 0;
}

id:128 加密模板

cpp 复制代码
#include<iostream>
using namespace std;
template<class T>
T max(T a[], int n) {
	T max = a[0];
	for (int i = 1; i < n; i++) {
		if (a[i] > max) {
			max = a[i];
		}
	}
	return max;
}

template<class T>
class Cryption
{
private:
	T ptxt[100]; //明文
	T ctxt[100]; //密文
	T key; //密钥
	int len; //长度
public:
	Cryption(T ttk, T tt[], int n) //参数依次对应密钥、明文、长度
	{
		 
			for (int i = 0; i < n; i++) {
				ptxt[i] = tt[i];

			}
			key = ttk;
			len = n;
		
	}
	void encrypt()//加密
	{
		T max1 = max(ptxt, len);
		for (int i = 0; i < len; i++) {
			ctxt[i] = max1 - ptxt[i] + key;
		}
	}
	void print() //打印,无需改造
	{
		int i;
		for (i = 0; i < len - 1; i++)
		{
			cout << ctxt[i] << " ";
		}
		cout << ctxt[i] << endl;
	}
};

//支持三种类型的主函数
int main()
{
	int i;
	int length; //长度
	int ik, itxt[100];
	double dk, dtxt[100];
	char ck, ctxt[100];
	//整数加密
	cin >> ik >> length;
	for (i = 0; i < length; i++)
	{
		cin >> itxt[i];
	}
	Cryption<int> ic(ik, itxt, length);//显式实例化
	ic.encrypt();
	ic.print();
	//浮点数加密
	cin >> dk >> length;
	for (i = 0; i < length; i++)
	{
		cin >> dtxt[i];
	}
	Cryption<double> dc(dk, dtxt, length);//显式实例化
	dc.encrypt();
	dc.print();
	//字符加密
	cin >> ck >> length;
	for (i = 0; i < length; i++)
	{
		cin >> ctxt[i];
	}
	Cryption<char> cc(ck, ctxt, length);//显式实例化
	cc.encrypt();
	cc.print();

	return 0;
}

id:127 加湿风扇

cpp 复制代码
#include<iostream>
using namespace std;
class Appliance {//电器
protected:
	int num;//编号
	int pp;//功率
	
public:
	Appliance(){}
	Appliance(int n, int p) { num = n; pp = p; }
	void display() {
		cout << "编号" << num << "--功率" << pp << "W" << endl;
	}
};
class Fan:virtual public Appliance {//风扇
protected:
	int direction;//风向   0定向  1 旋转
	int size;//风力
public:
	Fan(int n, int p, int d, int s) :Appliance(n, p) { direction = d; size = s; }
	void setsize(int s) {
		size = s;
	}
	void setdirection(int d) {
		direction = d;
	}

	 
};
class Humidifier :virtual public Appliance {  //加湿器
protected:
	float waternum;//水位实际值
	float watermax;//水位最大值
public:
	Humidifier(int n,int p,float wn,float wm):Appliance(n,p)
	{
		waternum = wn;
		watermax = wm;
	}
	int alarm() {
		if (waternum >= watermax * 0.5)
			return 1;
		else if (waternum <= watermax * 0.5 && waternum >= watermax * 0.1)
			return 2;
		else
			return 3;
	}

};
class HF :public Humidifier, public Fan {//加湿风扇
protected:
	int gear;//档位
public:
	HF(int n,int p,float wn, float wm, int direction, int size, int g) :Appliance(n,p),Humidifier(n,p,wn, wm), Fan(n,p,direction, size) {
		gear = g;
	}
	void setgear(int n) {
		gear = n;
		if (n == 1) {
			setdirection (0);
			setsize (1);
		}
		else if (n == 2) {
			setsize (2);
			setdirection (1);
		}
		else if (n == 3) {
			setdirection (1);
			setsize (3);
		}
	}
	void display() {

		cout << "加湿风扇--档位" << gear << endl;
		cout << "编号" << num << "--功率" << pp << "W" << endl;
		
		if (direction == 0)
			cout << "定向吹风";
		else
			cout << "旋转吹风";
		cout << "--风力" << size << "级" << endl; ;
		cout << "实际水容量" << waternum << "升--";
		switch (alarm()) {
		case 1: {
			cout << "水量正常" << endl;
			break;
		}
		case 2: {
			cout << "水量偏低" << endl;
			break;
		}
		case 3: {
			cout << "水量不足" << endl;
			break;
			}
		}
	}
}; 
int main()
{
	int t;
	cin >> t;
	while (t--) {
		int num, pp, direction, size, gear,gear0;
		float waternum, watermax;
		cin >> num >> pp >> direction >> size >> waternum >> watermax>>gear;
		cin >> gear0;
		HF fh(num,pp,waternum,watermax,direction,size,gear);
		
		fh.setgear(gear0);
		fh.display();
	}
}

id:128 计重转换

cpp 复制代码
#include<iostream>
using namespace std;
class CN; //提前声明
class EN; //提前声明

// 抽象类
class Weight
{
protected:
	char kind[20]; //计重类型
	int gram; // 克
public:
	Weight(const char tk[] = "no name", int tg = 0)
	{
		int h = sizeof(tk);
		for (int i = 0; i < h; i++) {
			kind[i] = tk[i];
		}
		gram = tg;
	}
	virtual void print(ostream& out) = 0; // 输出不同类型的计重信息  纯虚函数
};

// 中国计重
class CN : public Weight
{
private:
	int jin, liang, qian;
public:
	CN(int i, int j, int k, int l, const char a[]) :Weight(a, l) {
		jin = i;
		liang = j;
		qian = k;

	}
	void Convert(int k) {
		gram = k % 5;
		jin = k / 500;
		liang = k % 500 / 50;
		qian = k % 50 / 5;
	}
	virtual void print(ostream& out) {
		out << "中国计重:" << jin << "斤" << liang << "两" << qian << "钱" << gram << "克" << endl;
	}

	CN& operator=(EN& e1);
};

// 英国计重
class EN : public Weight
{
private:
	int bang, angsi, dalan;
public:
	EN(int i, int j, int k, int l,const char a[]) :Weight(a, l) {
		bang = i;
		angsi = j;
		dalan = k;
	}
	void Convert(int k) {
		gram = k % 2;	
		bang = k / 512;
		angsi = k % 512 / 32;
		dalan = k % 512 % 32 / 2;
	}
	int getGram() {
		return gram + bang * 512 + angsi * 32 + dalan * 2;
	}
	virtual void print(ostream& out) {
		out << "英国计重:" << bang << "磅" << angsi << "盎司" << dalan << "打兰" << gram << "克" << endl;
	}
};
CN& CN::operator=(EN& e1) {
	int all_gram;
	all_gram = e1.getGram();
	this->Convert(all_gram);
	return *this;
}
ostream& operator<<(ostream& out, Weight& W1) {
	W1.print(out);
	return out;
}
// 以全局函数方式重载输出运算符,代码3-5行....自行编写
// 重载函数包含两个参数:ostream流对象、Weight类对象,参数可以是对象或对象引用
// 重载函数必须调用参数Weight对象的print方法

// 主函数
int main()
{
	int tw;
	// 创建一个中国计重类对象cn
	// 构造参数对应斤、两、钱、克、类型,其中克和类型是对应基类属性gram和kind
	CN cn(0, 0, 0, 0, "中国计重");
	cin >> tw;
	cn.Convert(tw); // 把输入的克数转成中国计重
	cout << cn;

	// 创建英国计重类对象en
	// 构造参数对应磅、盎司、打兰、克、类型,其中克和类型是对应基类属性gram和kind
	EN en(0, 0, 0, 0, "英国计重");
	cin >> tw;
	en.Convert(tw); // 把输入的克数转成英国计重
	cout << en;
	cn = en; // 把英国计重转成中国计重
	cout << cn;
	return 0;
}

id:187 集合运算(运算符重载)

cpp 复制代码
#include<iostream>
using namespace std;
class CSet
{
public:
	int* data;
	int n;
public:
	CSet operator+(CSet& s);
	CSet operator-(CSet& s);
	CSet operator*(CSet& s);
	CSet();
	CSet(int* p, int x);
	void setdata(int* p, int x);
	void display();
};
CSet::CSet(int* p, int x)
{
	data = p; n = x;
}
CSet::CSet()
{
	int x;
	cin >> x;
	n = x;
	data = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> *(data + i);
	}
}
void CSet::display()
{
	if (n == 1) { cout << data[0]; }
	else {
		
			for (int i = 0; i < n - 1; i++)
			{

				cout << *(data + i) << " ";
			}

			cout << *(data + n-1);
		}
}
CSet CSet::operator*(CSet& s)
{
	int* p = new int[n + s.n];
	int sum = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < s.n; j++)
		{
			if (s.data[j] == data[i])
			{
				p[sum] = data[i];
				sum++;
				break;
			}
		}
	}
	CSet m(p, sum);
	return m;
}
CSet CSet::operator-(CSet& s)
{
	int* p = new int[n + s.n]; int x = 0;
	for (int i = 0; i < n; i++)
	{
		int sum = 0;
		for (int j = 0; j < s.n; j++)
		{
			if (s.data[j] == data[i])
				sum++;
		}
		if (sum == 0)//属于A而不属于B
		{
			p[x] = data[i];
			x++;
		}
	}
	CSet m(p, x);
	return m;
}
CSet CSet::operator+(CSet& s)
{
	int* p = new int[n + s.n];
	int x = s.n + n;
	for (int i = 0; i < n; i++)
	{
		*(p + i) = *(data + i);
	}
	for (int i = 0; i < s.n; i++)
	{
		*(p + n + i) = *(s.data + i);
	}
	for (int i = 0; i < x; i++)
		for (int j = 0; j < x; j++)
		{
			if (i != j)
			{
				if (*(p + i) == *(p + j))
				{
					for (int k = j; k < x; k++)
					{
						*(p + k) = *(p + k + 1);
					}
					x--;
				}
			}
		}
	CSet m(p, x);
	return m;
}
int main()
{
	int a; cin >> a;
	
	while(a--)
	{
		CSet A, B;
		CSet m = A - B, n = B - A;
		cout << "A:"; A.display(); cout << endl;
		cout << "B:"; B.display(); cout << endl;
		cout << "A+B:"; (A + B).display(); cout << endl;
		cout << "A*B:"; (A * B).display(); cout << endl;
		cout << "(A-B)+(B-A):"; (m + n).display();

		if (a != 0) {
			cout << endl; cout << endl;
		}
			
		
	}
	return 0;
}

id:181 宠物的生长(虚函数和多态)

cpp 复制代码
#include<iostream>
#include<iomanip>
using namespace  std;
class CDate {
private:
	int year;
	int day;
	int month;
public:
	CDate(int y, int m, int d) {
		year = y;
		day = d;
		month = m;
	}
	CDate() {}
	int getsumofday() {
		int sum = day;
		int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0))
			a[2]++;
		for (int i = 1; i < month; i++) {
			sum += a[i];
		}
		int days = sum + year * 365;
		for (int i = 1; i < year; i++) {
			if ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0))
				days += 1;
		}
		return days;
	}
};
class Pet {
protected:
	string name;//姓名
	float length;//身长
	float weight;//体重
	CDate current;//开始记录时间
public:
	Pet() {}
	Pet(string n, float l, float w, int y, int m, int d) :current(y, m, d)
	{
		name = n;
		length = l;
		weight = w;
	}
	virtual void disply(CDate day) = 0;//输出目标日期时宠物的身长和体重
};
class Cat :public Pet {
public:
	Cat() {}
	Cat(string n, float l, float w, int y, int m, int d) :Pet(n, l, w, y, m, d) { }
	virtual void disply(CDate day) {
		int d1 = day.getsumofday();
		int d2 = current.getsumofday();
		if (d1 > d2) {
			cout << "error" << endl;
		}
		else {
			int d = d2 - d1;
			length += d * 0.1;
			weight += d * 0.2;
			cout << name << "afer" << d << "days:" << "length=" << fixed << setprecision(2) << length << ",weight=" << weight << endl;
		}
	}
};
class Dog :public Pet {
public:
	Dog() {}
	Dog(string n, float l, float w, int y, int m, int d) :Pet(n, l, w, y, m, d) { }
	virtual void disply(CDate day) {
		int d1 = day.getsumofday();
		int d2 = current.getsumofday();
		if (d1 > d2) {
			cout << "error" << endl;
		}
		else {
			int d = d2 - d1;
			length += d * 0.2;
			weight += d * 0.1;
			cout << name << "afer" << d << "days:" << "length=" << fixed << setprecision(2) << length << ",weight=" << weight << endl;
		}
	}
};
int main() {
	int t;
	cin >> t;
	int y, m, d;
	cin >> y >> m >> d;
	CDate day(y, m, d);
	while (t--) {
		Pet* p = nullptr;
		int type;
		string n; float l, w;
		cin >> type >> n >> l >> w >> y >> m >> d;
		if (type == 1)
			p = new Cat(n, l, w, y, m, d);
		else
			p = new Dog(n, l, w, y, m, d);
		if (p)
			p->disply(day);
		delete p;

	}
	return 0;
}

id:300 OOP双人决斗(多重继承)

cpp 复制代码
#include<iostream>
using namespace std;

class Node2D {
protected:
    string location;
public:
    Node2D(string l) : location(l) {}
};
class Body : virtual public Node2D {
protected:
    int maxhealth;
    int health;
    int defense;
public:
    Body(string l, int h, int d) : Node2D(l), maxhealth(h), health(h), defense(d) {}
};
class Weapon : virtual public Node2D {
protected:
    string name1;
    int damage;
public:
    Weapon(string l, string n, int d) : Node2D(l), name1(n), damage(d) {}
};
class Player : public Body, public Weapon {
protected:
    string name;
public:
    Player(string l, int h, int d, string n, int dd, string nn)
        : Node2D(l), Body(l, h, d), Weapon(l, n, dd), name(nn) {}
    void decrease(Player& other) {
        if (other.health > 0||health>0) {
            int damageDealt = damage - other.defense;
            damageDealt = damageDealt < 0 ? 0 : damageDealt;
            other.health -= damageDealt;
            cout << name << " deal " << damageDealt << " damage to " << other.name << endl;
            if(other.health > 0 && health > 0)
                 cout << other.name << " still have " << other.health << " health\n"<< endl;
        }
    }
    void attack(Player& other) {
        while (health > 0 && other.health > 0) {
            decrease(other);
            if (other.health <= 0) {
                cout << name << " defeated " <<other.name<< " by "<< name1 << " in "<< location <<endl;
                break;
            }
            other.decrease(*this);
            if (health <= 0) {
                cout << other.name << " defeated " << name << " by " << other.name1 << " in " << location;
                break;
            }
        }
    }
};

int main() {
    string place;
    cin >> place;
    string n1, n2, n3, n4;
    int health1, health2, defense1, defense2, damage1, damage2;

    cin >> n1 >> health1 >> defense1 >> n3 >> damage1;
    cin >> n2 >> health2 >> defense2 >> n4 >> damage2;

    Player p1(place, health1, defense1, n3, damage1, n1);
    Player p2(place, health2, defense2, n4, damage2, n2);

    p1.attack(p2);
    return 0;
}
相关推荐
晓纪同学25 分钟前
QT-简单视觉框架代码
开发语言·qt
威桑26 分钟前
Qt SizePolicy详解:minimum 与 minimumExpanding 的区别
开发语言·qt·扩张策略
飞飞-躺着更舒服29 分钟前
【QT】实现电子飞行显示器(简易版)
开发语言·qt
明月看潮生35 分钟前
青少年编程与数学 02-004 Go语言Web编程 16课题、并发编程
开发语言·青少年编程·并发编程·编程与数学·goweb
明月看潮生38 分钟前
青少年编程与数学 02-004 Go语言Web编程 17课题、静态文件
开发语言·青少年编程·编程与数学·goweb
Java Fans40 分钟前
C# 中串口读取问题及解决方案
开发语言·c#
盛派网络小助手1 小时前
微信 SDK 更新 Sample,NCF 文档和模板更新,更多更新日志,欢迎解锁
开发语言·人工智能·后端·架构·c#
Chinese Red Guest1 小时前
python
开发语言·python·pygame
一棵星2 小时前
Java模拟Mqtt客户端连接Mqtt Broker
java·开发语言
xiaoshiguang32 小时前
LeetCode:222.完全二叉树节点的数量
算法·leetcode