C++期中模拟题-答案

#include <iostream>
#include <cstring>
using namespace std;

class robot
{
	string name;
	char type;
	int blood, rank, hurt, defense;
public:
	robot(string n, char t, int r)
	{
		name = n;
		type = t;
		rank = r;
		switch (type)
		{
		case 'N':
			blood = 5 * rank;
			hurt = 5 * rank;
			defense = 5 * rank;
			break;
		case 'A':
			blood = 5 * rank;
			hurt = 10 * rank;
			defense = 5 * rank;
			break;
		case 'D':
			blood = 5 * rank;
			hurt = 5 * rank;
			defense = 10 * rank;
			break;
		case 'H':
			blood = 50 * rank;
			hurt = 5 * rank;
			defense = 5 * rank;
			break;
		}
	}
	friend bool turn(robot* ro, char turntype);
	void print()
	{
		cout << name << "--" << type << "--" << rank << "--" << blood << "--" << hurt << "--" << defense << endl;
	}
};

//全局函数bool
bool turn(robot* ro, char turntype)
{
	if (ro->type == turntype)
	{
		return false;
	}
	else
	{
		ro->type = turntype;
		switch (turntype)
		{
		case 'N':
			ro->blood = 5 * ro->rank;
			ro->hurt = 5 * ro->rank;
			ro->defense = 5 * ro->rank;
			break;
		case 'A':
			ro->blood = 5 * ro->rank;
			ro->hurt = 10 * ro->rank;
			ro->defense = 5 * ro->rank;
			break;
		case 'D':
			ro->blood = 5 * ro->rank;
			ro->hurt = 5 * ro->rank;
			ro->defense = 10 * ro->rank;
			break;
		case 'H':
			ro->blood = 50 * ro->rank;
			ro->hurt = 5 * ro->rank;
			ro->defense = 5 * ro->rank;
			break;
		}
		return true;
	}
}

int main()
{
	int t, sum = 0;
	cin >> t;
	while (t--)
	{
		string name;
		char type, turntype;
		int rank;
		cin >> name >> type >> rank >> turntype;
		robot r(name, type, rank);
		robot* p;
		p = &r; //important!!引用
		sum += turn(p, turntype);//若成立sum++,若不成立sum = sum
		p->print();
	}
	cout << "The number of robot transform is " << sum << endl;
}
#include <iostream>
using namespace std;

class phone
{
	int number, state;
	char type;
	string name;
public:
	phone(int n, int s, char t, string m_name)
	{
		number = n;
		state = s;
		type = t;
		name = m_name;
		cout << number << " constructed." << endl;
	}
	void print()
	{
		cout << "Phone=" << number << "--Type=" << type << "--State=";
		if (state == 1)
			cout << "use";
		else if (state == 0)
			cout << "unuse";
		cout << "--Owner=" << name << endl;
	}
	bool check(int n)
	{
		if (number == n)
			return 1;
		else
			return 0;
	}
	~phone()
	{
		cout << number << " destructed." << endl;
	}
};

int main()
{
	int number, state;
	char type;
	string name;
	cin >> number >> type >> state >> name;
	phone p1(number, state, type, name);    //调用构造函数
	cin >> number >> type >> state >> name;
	phone p2(number, state, type, name);
	cin >> number >> type >> state >> name;
	phone p3(number, state, type, name);
	int t;
	cin >> t;
	while (t--)
	{
		cin >> number;
		int flag = 0;
		flag += p1.check(number);
		if (p1.check(number))
			p1.print();
		flag += p2.check(number);
		if (p2.check(number))
			p2.print();
		flag += p3.check(number);
		if (p3.check(number))
			p3.print();         //从p3到p1调用析构函数
		if (flag == 0)
			cout << "wrong number." << endl;
	}
}
#include <iostream>
using namespace std;

class BankAccount
{
private:
    long number;
    char type;
    double sum;
    double rate;
public:
    BankAccount(long n, char t, double s, double r)
    {
        number = n;
        type = t;
        sum = s;
        rate = r;
    }
    BankAccount(const BankAccount& ba)
    {
        number = ba.number + 50000000;
        type = ba.type;
        sum = ba.sum;
        rate = 0.015;
    }
    void countInterest()
    {
        double interest = sum * rate;
        sum += interest;
        cout << "Account=" << number << "--sum=" << sum << endl;
    }
    void query()
    {
        cout << "Account=" << number << "--";
        if (type == 'P')
        {
            cout << "Person--";
        }
        else
        {
            cout << "Enterprise--";
        }
        cout << "sum=" << sum << "--rate=" << rate << endl;
    }
};

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        long n;
        char t;
        double s;
        cin >> n >> t >> s;
        BankAccount ba1(n, t, s, 0.005);
        BankAccount ba2(ba1);
        char op1, op2;
        cin >> op1 >> op2;
        if (op1 == 'C')
        {
            ba1.countInterest();
        }
        else
        {
            ba1.query();
        }
        if (op2 == 'C')
        {
            ba2.countInterest();
        }
        else
        {
            ba2.query();
        }
    }
    return 0;
}


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

class TV {
private:
    int id;
    int volume;
    int channel;
    string mode;
    static int tvCount;
    static int dvdCount;

public:
    TV(int i, int c, string m, int v) :id(i), channel(c), mode(m), volume(v) {
        tvCount++;
    }

    void setMode(string m, int c = 0) {
        if (mode == "TV" && m == "DVD") {
            channel = 99;
            dvdCount++;
            tvCount--;
        }
        else if (mode == "DVD" && m == "TV") {
            channel = c;
            tvCount++;
            dvdCount--;
        }
        else if (m == "TV") {
            channel = c;
        }

        mode = m;
    }

    void adjustVolume(int v) {
        volume += v;

        if (volume > 100) {
            volume = 100;
        }
        else if (volume < 0) {
            volume = 0;
        }
    }

    void print() {
        cout << "第" << id << "号电视机--" << mode << "模式--频道" << channel << "--音量" << volume << endl;
    }

    static int getTvCount() {
        return tvCount;
    }

    static int getDvdCount() {
        return dvdCount;
    }

};

int TV::tvCount = 0;
int TV::dvdCount = 0;

void control(TV& tv, string m, int v, int c) {
    tv.setMode(m, c);
    tv.adjustVolume(v);
    tv.print();
}

int main() {
    int n, t;
    cin >> n >> t;

    TV** tvs = new TV * [n];
    for (int i = 0; i < n; i++) {
        tvs[i] = new TV(i + 1, i + 1, "TV", 50);
    }

    while (t--) {
        int i, k, x, v;
        cin >> i >> k >> x >> v;
        control(*tvs[i - 1], (k == 1) ? "TV" : "DVD", v, x);
    }

    cout << "播放电视的电视机数量为" << TV::getTvCount() << endl;
    cout << "播放DVD的电视机数量为" << TV::getDvdCount() << endl;

    for (int i = 0; i < n; i++) {
        delete tvs[i];
    }
    delete[] tvs;

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值