⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test3.cpp

📁 以 一 定 的 格 式 来 对 09 年 日 历 的打 印
💻 CPP
字号:
// test3.cpp : Defines the entry point for the console application.
//

/************************************************************************/
/* 描  述:2009-1-15作业				                                */
/* 日  期:2009-1-15                                                    */
/* 所有权:NeuSoft                                                      */
/* 作  者:王立力                                                       */
/************************************************************************/

#include "stdafx.h"

void test1();//作业1:输入一个数据并打印从1到该数据
void test2();//作业2:输入年月日输出该日期在年中的第几天
void test3();//作业3:打印2009年的日历

void Print(int n, int *daylast);					/*打印n天的日历,n表示某个月份的天数,daylast表示第多少天*/
int isrunyuan(int year);						/*该函数用于判断是否是闰年*/

int main(int argc, char* argv[])
{
	printf("\n作业1:输入一个数据并打印从1到该数据:\n");
	test1();

	printf("\n作业2:输入年月日输出该日期在年中的第几天: \n");
	test2();

	printf("\n打印2009年的日历: \n");
	test3();

	return 0;
}

void test1()//作业1:输入一个数据并打印从1到该数据
{
	int n = 0;

	printf("请输入一个数据:");
	scanf("%d", &n);//输入循环数据

	for (int i=1; i <= n; i++)
	{
		printf("%d\n", i);
	}
}

int isrunyuan(int year)
{
	if (year % 100 == 0)								/*能被100整除*/
	{
		if (year % 400 == 0)							/*能被400整除*/
		{
			return 1;
		}
		else											/*不能被400整除*/
		{
			return 0;
		}
	}
	else
	{
		if (year % 4 == 0)								/*能被4整除*/
		{
			return 1;
		}
		else											/*不能被4整除*/
		{
			return 0;
		}
	}
}

void test2()//作业2:输入年月日输出该日期在年中的第几天
{
	int y = 0;
	int m = 0;
	int d = 0;
	int daylast = 0;

	printf("请输入一个日期格式为(2009-1-15):");
	scanf("%d-%d-%d", &y, &m, &d);//要求必须按指定格式输入
	
	for (int i=1; i <= m; i++)//一个月份一个月份的累加
	{
		switch(i)//不同的月份添加不同的天数
		{
		case 1:
			daylast += d;
			break;
		case 2:
			daylast += 31;
			break;
		case 3:
			if (isrunyuan(y) == 1)//判断是闰年的话天数为29天,否则为28天
				daylast += 29;
			else
				daylast += 28;
			break;
		case 4:
			daylast += 31;
			break;
		case 5:
			daylast += 30;
			break;
		case 6:
			daylast += 31;
			break;
		case 7:
			daylast += 30;
			break;
		case 8:
			daylast += 31;
			break;
		case 9:
			daylast += 31;
			break;
		case 10:
			daylast += 30;
			break;
		case 11:
			daylast += 31;
			break;
		case 12:
			daylast += 30;
			break;
		default:
			printf("输入月份有误!");
			exit(0);
		}
	}

	printf("\n%d-%d-%d是该年中的第%d天\n", y, m, d, daylast);
}

void Print(int n, int *daylast)/*打印n天的日历,n表示某个月份的天数,daylast表示第多少天并累加*/
{
	for (int j=1; j <= n; j++)
	{
		if ((*daylast)%7 == 0)//七天一换行
			printf("\n");

		printf("\t%d", j);//打印日

		(*daylast) ++;//第几天累加
	}
}

void test3()//作业3:打印2009年的日历
{
	int daylast = 4;
	int j = 0;
	int k = 0;
	
	for (int i=1; i <= 12; i++)//一个月份一个月份的累加
	{
		printf("\n\t%d月份:\n", i);
		printf("\t日\t一\t二\t三\t四\t五\t六\n");

		for (j=0; j<daylast%7; j++)//显示每月前面的tab符
		{
			printf("\t");
		}

		switch(i)//不同的月份添加不同的天数
		{
		case 1:
			Print(31, &daylast);
			break;
		case 2:		
			Print(28, &daylast);
			break;
		case 3:	
			Print(31, &daylast);
			break;
		case 4:
			Print(30, &daylast);
			break;
		case 5:
			Print(31, &daylast);
			break;
		case 6:
			Print(30, &daylast);
			break;
		case 7:
			Print(31, &daylast);
			break;
		case 8:
			Print(31, &daylast);
			break;
		case 9:
			Print(30, &daylast);
			break;
		case 10:
			Print(31, &daylast);
			break;
		case 11:
			Print(30, &daylast);
			break;
		case 12:
			Print(31, &daylast);
			break;
		}
		printf("\n");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -