C++自定义数据类型单元习题(一)

C++自定义数据类型单元习题

基础知识:
结构体:
把不同类型的数据组合在一起
关键字:struct
题目一:
代码如下:

/*第十三周C++作业
*任意输入两个学生的数据,计算他们的平均分。
* 36号 刘易行
* 2020年11月21日
*/ 
#include<iostream>
using namespace std;
int average(int score1, int score2)
{
	return (score1 + score2) / 2;
}
struct Student
{
	unsigned int no;
	char name[36];
	int score;
};
int main()
{
	Student st1, st2;
	cout << "请输入第一个学生的成绩" << endl;
	cin >> st1.score;
	cout << "请输入第二个学生的成绩" << endl;
	cin >> st2.score;
	cout <<"平均分为"<<average(st1.score, st2.score) << endl;;
}

题目二:
设停车费为每小时3元,请输入起始时间和结束时间,计算停车费。

/*
* 第十三周C++作业
* 设停车费为每小时3元,请输入起始时间和结束时间,计算停车费
* 36号 刘易行
* 2020年11月21日
*/
#include<iostream>
using namespace std;
struct Time             //时间结构体
{
	int hour;
	int minute;
};
double fee(int Starthour,int Endhour,int Startminute, int Endminute)     //计费函数
{
	int sumhour;
	int a,b,c;
	a = Endhour-Starthour;
	b = Endminute - Startminute;
	if (b > 30)       //分钟数若超过30min则计为一个小时
	{
		c = 1;
	}
	else
	{
		c = 0;
	}
	sumhour = a + c;
	return sumhour*3;
}
int main()
{
	Time begin, end;     //定义结构体成员
	cout << "请输入起始时间的小时(24进制)" << endl;
	cin >> begin.hour;
	cout << "请输入起始时间的分钟" << endl;
	cin >> begin.minute;
	cout << "请输入结束时间的小时(24进制)" << endl;
	cin >> end.hour;
	cout << "请输入结束时间的分钟" << endl;
	cin >> end.minute;
	cout << "停车费用为" << fee(begin.hour, end.hour, begin.minute, end.minute) << "元" << endl;
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值