C++第3次作业

时间:2022-03-08 03:00:03

项目二:本月有几天?

#include<iostream>
using namespace std;
int main()
{
	int y, m, d;
	cout << "请输入年份和月份:" << endl;
	cin >> y >> m;
	if (m == 4|| m == 6|| m == 9|| m == 11)
		d = 30;
	else if (m == 2)
	{
		if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0)
			d = 29;
		else d = 28;
	}
	else d = 31;
	cout << "该月的天数为:" << d << endl;
	return 0;
}


项目三:定期存款利息计算器

#include<iostream>
using namespace std;
int main()
{
	int y;
	double x, z, p, w;
	cout << "请输入存款金额:";
	cin >> x;
	cout << "=========存款期限及利率=========" << endl;
	cout << "1.三个月" << '\n' << "2.六个月" << '\n' << "3.一年" << '\n' << "4.两年" << '\n' << "5.三年" << '\n' << "6.五年" << endl;
	cout << "请输入存款期限的代号:";
	cin >> y;
	switch (y)
	{
	case 1:z = 0.031*0.25; break;
	case 2:z = 0.033*0.5; break;
	case 3:z = 0.035 * 1; break;
	case 4:z = 0.044 * 2; break;
	case 5:z = 0.050 * 3; break;
	case 6:z = 0.055 * 5; break;
	default:cout << "error\n";
	}
	p = x*z, w = x + p;
	cout << "到期利息为" << p<<"元"<<",";
	cout << "本息合计为" << w <<"元"<<"。"<< endl;
	cout << "感谢您的使用,欢迎下次光临!" << endl;
	return 0;
}


项目四:多分数段函数求值

#include<iostream>
using namespace std;
int main()
{
	double x, y;
	cout << "请输入x=";
	cin >> x;
	if (x < 2)
		y = x;
	else if (x >= 2 && x < 6)
		y = x*x + 1;
	else if (x >= 6 && x < 10)
		y = sqrt(x + 1);
	else y = 1 / (x + 1);
	cout << "y=" << y;
	return 0;
}