C++ 万年历、生肖判断、计算第几天

1、输入一个年、月、日并计算它是本年度第几天

2、输入任何一年份,给出该年出生人的属性,如1945年出生人的属性为“鸡”

3、显示输入2000-2099年的任何一年的某一月的月历,如下图

C++ 万年历、生肖判断、计算第几天_第1张图片

problem1

#include 
using namespace std;
int main()
{
	int year, month, day, num;
	bool runnian;	//是否为闰年标志位
	cout << "请输入一个年份:";
	cin >> year;
	cout << "请输入月份:";
	cin >> month;
	cout << "请输入日期:";
	cin >> day;
	if (year % 100 == 0)
	{
		if (year % 400 == 0)
			runnian = true;
		else
			runnian = false;
	}
	else
	{
		if (year % 4 == 0)
		{
			runnian = true;
		}
		else
			runnian = false;
	}

	switch (month)
	{
	case 1:  num = day; break;
	case 2:	 num = 31 + day; break;
	case 3:	 num = 59 + day; break;
	case 4:  num = 90 + day; break;
	case 5:  num = 120 + day; break;
	case 6:  num = 151 + day; break;
	case 7:  num = 182 + day; break;
	case 8:  num = 212 + day; break;
	case 9:  num = 243 + day; break;
	case 10: num = 274 + day; break;
	case 11: num = 304 + day; break;
	case 12: num = 335 + day; break;
	default: break;
	}

	if (month >= 3)
	{
		if (runnian == true)
			num++;
	}
	cout << "这是这一年的第" << num << "天";
	return 0;
}

problem 2

#include 
using namespace std;
int main()
{
	char p[12][4] = { "鼠","牛","虎","兔","龙","蛇","马","羊","鸡","猴","狗","猪" };
	int year0 = 2008;   //2008年是鼠年,作为基准
	int year, n;
	cout << "请输入一个年份";
	cin >> year;
	n = year - year0;
	n = n % 12;
	if (n < 0)
		n += 12;   //求余时,n可能是负数,如果是负数则加12 
	cout << year << "年的人属相为:" << p[n];
	return 0;
}

problem3

#include 
using namespace std;
int year, month, day, he;
int run[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
int ping[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
//这点到基本点的求和
//判断闰年和平年
int leap(int year)
{
	int four, hundred, fourhundred;
	four = year % 4;
	hundred = year % 100;
	fourhundred = year % 400;
	if (four == 0 && hundred == 0 && fourhundred == 0)
		return 1;
	else if (four == 0 && hundred != 0 && fourhundred != 0)
		return 1;
	else return 0;
}
int qiuhe(int year, int month, int day, int run[], int ping[])
{
	//19000101 星期一
	int i, sum = 0;
	for (i = 1900; i < year; i++)
	{
		int m;//用来标签是闰年还是平年  m==1,就是闰年
		m = leap(i);
		if (m == 1)
		{
			sum = sum + 366;
		}
		else
		{
			sum = sum + 365;
		}
	}
	//加月份的天数
	for (i = 0; i < month - 1; i++)
	{
		if (leap(year) == 1)
			sum = sum + run[i];
		else
			sum = sum + ping[i];
	}
	return sum;
}


void muban()
{
	char weekday[7][10] = { "星期一\t","星期二\t","星期三\t","星期四\t","星期五\t","星期六\t","星期天" };
	int i;
	for (i = 0; i < 7; i++)
		cout << weekday[i];
	cout << endl;
}

void shibiePrint(int sum, int year, int month)
{
	int yushu, temp, i;
	yushu = sum % 7;//得到多余的天数,然后在后一个月份用空格表示出来
	for (i = 0; i < yushu; i++)
		cout << "\t";//输出空格
	temp = 7 - yushu;
	if (leap(year) == 1)
	{
		for (i = 1; i <= run[month - 1]; i++)//打印31天
		{
			cout << i<< "\t";
			if (i == temp || (i - temp) % 7 == 0)
				cout << endl;
		}
		cout << endl;
	}
	else
	{
		for (i = 1; i <= ping[month - 1]; i++)//打印30天
		{
			cout << i << "\t";
			if (i == temp || (i - temp) % 7 == 0)
				cout << endl;
		}
		cout << endl;
	}
}

int main()
{
	cout << "请输入一个年份";
	cin >> year;
	cout << "请输入月份";
	cin >> month;
	cout << "请输入日期";
	cin >> day;
	he = qiuhe(year, month, day, run, ping);//调用函数求和
	cout << "\t\t\t"<< year<<"年"<

 

你可能感兴趣的:(C++)