第八周 C++实践 分支程序设计

/*
* Copyright (c) 2016,北京建筑大学测绘学院
* All rights reserved.
* 文件名称:第八周项目.cpp
* 作    者:书写未来的code
* 完成日期:2016 年 1 月 30 日
* 版 本 号:v1.0
*
* 标题:第8周项目
*/

//第8周项目6:本月有几天?
#include "stdafx.h"
#include 
using namespace std;
int main()
{
	bool leap = true;
	int year,month,day;
	cout << "请输入年份和月份:";
	cin >> year >> month;
	//判断是否为闰年
	if (year % 4 != 0)
	{
		leap = false;
	}
	else if (year % 100 != 0)
	{
		leap = true;
	}
	else if (year % 400 == 0)
	{
		leap = true;
	}
	else
	{
		leap = false;
	}
	//判断所在月中某一个月有多少天
	switch (month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		day = 31;
		break;
	case 2:
		if (leap)
		{
			day = 29;
		}
		else
		{
			day = 28;
		}
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		day = 30;
	default:
		break;
	}
	cout << year << "年中" << month << "月有" << day << "天。" << endl;
	return 0;
}




//第8周项目5:定期存款利息计算器
#include "stdafx.h"
#include 
#include 
using namespace std;
int main()
{
	double dSalary, interest, rate, date, sum;
	int k;
	cout << "欢迎使用利息计算器!" << endl;
	cout << "请输入存款金额:";
	cin >> dSalary;
	cout << "======存款期限======" << endl;
	cout << "1. 3个月\n2. 6个月\n3. 一年\n4. 二年\n5. 三年\n6. 五年\n";
	cout << "请输入存款期限的代号:";
	cin >> k;
	switch (k)
	{
	case 1:
		date = 0.25; rate = 0.031; break;
	case 2:
		date = 0.5; rate = 0.033; break;
	case 3:
		date = 1; rate = 0.035; break;
	case 4:
		date = 2; rate = 0.044; break;
	case 5:
		date = 3; rate = 0.05; break;
	case 6:
		date = 5; rate = 0.055; break;
	default:
		break;
	}
	interest = dSalary*rate*date;
	sum = dSalary + interest;
	cout << "到期利息为" << interest << "元," << "本息合计共" << sum << "元。" << endl;
	cout << "感谢您的使用,欢迎下次光临!" << endl;
	return 0;
}


//第8周项目4:个人所得税计算器
#include "stdafx.h"
#include 
#include 
using namespace std;
int main()
{
	double dSalary, dTax = 0, dNetIncome = 0;
	double rate;
	double delt,k;
	cout << "请输入您本月的收入总额(元):";
	cin >> dSalary;
	// 下面求解应缴个人所得税dTax和税后收入dNetIncome
	delt = dSalary - 3500;
	if (delt <= 1500)
	{
		rate = 0.03; k = 0;
	}
	else if (delt <= 4500)
	{
		rate = 0.1; k = 105;
	}
	else if (delt <= 9000)
	{
		rate = 0.1; k = 105;
	}
	else if (delt <= 35000)
	{
		rate = 0.25; k = 1005;
	}
	else if (delt <= 55000)
	{
		rate = 0.3; k = 2755;
	} 
	else if (delt <= 80000)
	{
		rate = 0.35; k = 5505;
	}
	else
	{
		rate = 0.45;
		k = 5505;
	}
	dTax = (dSalary - 3500)*rate - k;
	dNetIncome = dSalary - dTax;
	cout << "您本月应缴个人所得税 " << dTax << " 元,税后收入是 " << dNetIncome << " 元。\n";
	cout << "依法纳税,共享繁荣。谢谢使用!\n" << endl;
	return 0;
}

//第8周项目3:多分段函数求值,请用if~else~语句的嵌套,实现这个多分支的程序;
#include "stdafx.h"
#include 
#include 
using namespace std;
int main()
{
	double x, y;
	cout << "Input x:";
	cin >> x;
	int t;
	t = (x < 2) + (x < 6) + (x < 10);
	switch (t)
	{
	case 3:
		y = x;
		break;
	case 2:
		y = x*x + 1;
		break;
	case 1:
		y = sqrt(x + 1);
		break;
	case 0:
		y = 1 / (x + 1);
		break;
	default:
		break;
	}
	cout << "y = " << y << endl;
	return 0;
}


//第8周项目3:多分段函数求值,请用if~else~语句的嵌套,实现这个多分支的程序;
#include "stdafx.h"
#include 
#include 
using namespace std;
int main()
{
	double x, y;
	cout << "Input 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 << endl;
	return 0;
}

//第8周项目2:加班费,要不要?
#include "stdafx.h"
#include 
using namespace std;
int main()
{
	double rate, hour,salary;
	cout << "Input rate,hour:";
	cin >> rate >> hour;
	if (hour <= 40)
	{
		salary = rate*hour;
	}
	else
	{
		salary = rate*1.5*(hour-40) + rate*40;
	}
	cout << "salary = " << salary << endl << endl;
	return 0;
}


//第8周项目1:求分段函数的值
#include "stdafx.h"
#include 
using namespace std;
int  main()
{
	int x, y;
	cout << "请输入x的值:";
	cin >> x;
	if (x >= 1)
	{
		y = x - 1;
	}
	else
	{
		y = 1 - x;
	}
	cout << "y = " << y << endl;
	return 0;
}



//阅读下面的两段程序,用“人脑”运行写出输出结果,再在计算机或手机上运行程序,对比自己写出的结果,进行反思。
#include "stdafx.h"
#include 
using namespace std;

int  main()
{
	int a = 1, b = 2, c = 3;
	if (a <= c)
		if (b == c)
			cout << "a = " << a << endl;
		else
			cout << "b = " << b << endl;
	cout << "c = " << c << endl;
	return 0;
}



#include "stdafx.h"
#include 
using namespace std;
int main()
{
	int i = 10, j, m = 0, n = 0;
	j = i % 3;
	switch (j)
	{
	case 0: m++;
		break;
	case 1:
	case 2: n++;//这句执行完,n的值就会加1。
		break;
	default: cout << "i=" << i << endl;
	}
	cout << "m = " << m << ",n = " << n << endl;
	return 0;
}


你可能感兴趣的:(C++基础知识)