c++课本第三章课后习题

 

1. 

#include<iostream>
#include<Windows.h>
using namespace std;
class Box
{
public:
	Box()
	{
		length = 0; width = 0; height = 0;
	}
	void clength()
	{
		float i;
		cout << "请输入长方体的长度" << endl;
		cin >> i;
		length = i;
		cout << "长方体的长度已修改为:" << length << endl;
	}
	void cwidth()
	{
		float i;
		cout << "请输入长方体的宽度" << endl;
		cin >> i;
		width = i;
		cout << "长方体的宽度已修改为:" << width << endl;
	}
	void cheight()
	{
		float i;
		cout << "请输入长方体的高度" << endl;
		cin >> i;
		height = i;
		cout << "长方体的高度已修改为:" << height << endl;
	}
	void area()
	{
		float i;
		i = ((length*width) + (length*height) + (width*height)) * 2;
		cout << "长方体的表面积为:" << i << endl;
	}
	void volume()
	{
		float i;
		i = length*width*height;
		cout << "长方体的体积为:" << i << endl;
	}
private:
	float length, width, height;
};
int main()
{
	Box box1;
	box1.clength();
	box1.cwidth();
	box1.cheight();
	box1.area();
	box1.volume();
	system("pause");
	return 0;
}

2.
 

#include<iostream>
#include<Windows.h>
#include<string>
using namespace std;

class Account
{
public:
	Account()
	{
		id = "000000";
		balance = 0;
		cout << "账户ID为:" << id << endl;
		cout << "账户密码为:" << balance << endl;
	}
	void deposit()
	{
		int i;
		cout << "请输入您要存的金额:";
		cin >> i;
		balance = i;
		cout << "您账户的余额为:" << balance << endl;
	}
	void withdrawal()
	{
		int i;
		if (balance == 0)
		{
			cout << "该账户余额为0,请及时充值!" << endl;
		}
		else
		{
			int i;
			cout << "请输入要取的数目:";
			cin >> i;
			balance -= i;
			if (balance < 0)
			{
				cout << "账户余额不足,无法满足需要取的数量!" << endl;
			}
			else
			{
				cout <<"取完后的账户余额为:"<< balance << endl;
			}
		}
	}
private:
	string id;
	int balance;
};
int main()
{
	Account x1;
	x1.deposit();
	x1.withdrawal();
	system("pause");
	return 0;
}

3.

#include<iostream>
#include<Windows.h>
#include<string>
using namespace std;

class airCondition
{
public:
	airCondition()
	{
		brand = "格力";
		colour = "白色";
		power = "2匹";
		swch = "off";
		tem = 25;
		cout << "品牌:" << brand << endl;
		cout << "颜色:" << colour << endl;
		cout << "功率:" << power << endl;
		cout << "开关状态为:" << swch << endl;
		cout << "温度为:" << tem << endl;
	}
	void cut()
	{
		swch = "on";
		cout << "空调已打开!" << endl;
		cout << "目前空调状态为:" << swch << endl;
	}
	void temperature()
	{
		tem = 20;
		cout << "空调温度已改为:" << tem << endl;
	}
private:
	string brand, colour, swch, power;
	int tem;
};
int main()
{
	airCondition a1;
	a1.cut();
	a1.temperature();
	system("pause");
	return 0;
}

4.

#include<iostream>
#include<Windows.h>
#include<string>
using namespace std;

class student
{
public:
	student()
	{
		num = 0;
		name = "张三";
		score = 100;
	}
	void stu()
	{
		int m,n,a,b,t;	
		int obj[10];	//成绩存放的的数组
		cout << "请输入学生总数:";
		cin >> m;
		for (n = 0; n <m; n++)
		{
			cout << "第" << n+1 << "个学生学号为:";
			cin >> num;
			cout << "第" << n+1 << "个学生的姓名为:";
			cin >> name;
			cout << "第" << n+1 << "个学生的成绩为:";
			cin >> score;
			obj[n] = score;
		}
		cout << "从第一个学生到最后一个学生的成绩为:" << endl;
		for (n = 0; n < m; n++)
		{
			cout << obj[n] << " ";
		}
		cout << endl;
		cout << "总的学生成绩从大到小为:" << endl;
		for (b = 0; b < m; b++)
		{
			for (a = 0; a < m - b - 1; a++)
			{
				if (obj[a]<obj[a + 1])
				{
					t = obj[a];
					obj[a] = obj[a + 1];
					obj[a + 1] = t;
				}
			}
		}
		for (a = 0; a < m; a++)
		{
			cout << obj[a] << " ";
		}
	}
private:
	int num;
	string  name;
	float score;
};

int main()
{
	student obj;
	obj.stu();
	system("pause");
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值