- 来自一个小白的作品
- 要求实现的内容
展示:
万年历练习
1. 教学目标:
1)初步掌握图形界面设计与键盘交互(用户交互)。
2)教师采用模块填充方式教学。
3)教学计划4个课时。
2.要求
1)设计函数
int Distinguish(int year,int month);/*判断月份的类型,确定此月的天数*/
2)设计函数
float DayNum(int year,int month)
计算从公元1月1日到所输入的时间的前一个月的最后一天的总天数*
要求:所输入年内的月天数调用Distinguish(int year,int month)函数获取;
3)设计函数
OutPut(float days,int year,int month)
输出显示界面
调用函数DayNum(int year,int month),Distinguish
4)设计函数char Content();/*输出一目录,提示用户选择后序的 *** 作,并返回用户的选择*/
界面菜单的设计,接收用户输入。
5)主程序:
(1)调用函数 OutPut(DayNum(year,month),year,month)显示界面
(2)调用函数 Content()显示菜单
(3)根据用户输入执行相应 *** 作。
> 下一个月
< 上一个月
? 自定义年月
# 退出
源码:
#include
#include
#include
using namespace std;
char Content();//打印菜单
int Distinguish(int year, int month);/*判断月份的类型,确定此月的天数*/
int DayNum(int year, int month);// 计算从公元1月1日到所输入的时间的前一个月的最后一天的总天数
void OutPut(int days, int year, int month);
int main()
{
int year = 0, month = 0;
char select;//用char记录选择
l:cout << "!欢迎使用万年历!" << endl;
cout << "please input the year and month you want to see:";
cin >> year;
cin >> month;
OutPut(DayNum(year, month), year, month);
m:select = Content();//将返回的字符接收
switch (select)
{
case '>':
month = month + 1;
if (month >= 13)
{
year = year + 1;
month = month % 12;
}
system("cls"); //清屏
cout << year << "年" << month << "月\n";
OutPut(DayNum(year, month), year, month);
goto m;//18行定义
case '<':
month = month - 1;
if (month <= 0)
{
year = year - 1;
month = month + 12;
}
system("cls"); //清屏
cout << year << "年" << month << "月\n";
OutPut(DayNum(year, month), year, month);
goto m;//18行定义
case '?':
system("cls"); //清屏
goto l;
case '#':
system("cls");
cout << "已退出,谢谢使用!" << endl;
break;
}
return 0;
}
int Distinguish(int year, int month)//判断并返回所查月份的天数
{
int days = 0;
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
if (month == 2)
{
days = 29;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
days = 30;
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
days = 31;
}
}
else
{
if (month == 2)
{
days = 28;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
days = 30;
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
days = 31;
}
}
return days;
}
char Content()
{
char select;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
cout << "$$$$$$$ > nest month $$$$$$$" << endl;
cout << "$$$$$$$ < last month $$$$$$$" << endl;
cout << "$$$$$$$ ? resert date $$$$$$" << endl;
cout << "$$$$$$$$$$ # quit $$$$$$$$$$" << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
cout << "Please input your select: ";
cin >> select;
return select;
}
int DayNum(int year, int month)
{
int days = 0;
int pingyear = 0, runyear = 0;
for (int i = 1; i <= year - 1; i++)
{
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
{
runyear++;
}
}
pingyear = year - 1 - runyear;//记得减1,因为我们计算的是全年到元年的整天数
days = pingyear * 365 + runyear * 366;
for (int i = 1; i <= month - 1; i++)//再加上输入的年的零散的几个月的天数
{
days += Distinguish(year, i);
}
return days;
}
void OutPut(int days, int year, int month)
{
int i, k = 0, n;
cout << "日" << '\t' << "一" << '\t' << "二" << '\t' << "三" << '\t' << "四" << '\t' << "五" << '\t' << "六" << '\t' << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
k = days % 7;
if (k != 6)//表示第一行开头需要有k+1个空,k+1表示上个月所占空位置数。
{
for (i = 0; i < k + 1; i++)
cout << ' ' << '\t';
}
k = k + 1 + 1;//调整k值,用于接下来的输出计数。此时,k代表下一个要输出的位置
n = Distinguish(year, month);
for (i = 1; i <= n; i++)
{
cout << i << '\t';
if (k % 7 == 0) //每行七个
{
cout << endl;
}
k = k + 1;
}
cout << endl;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)