来自一个小白的作品
展示:
要求实现的内容
万年历练习
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<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
char Content();
int Distinguish(int year, int month);
int DayNum(int year, int month);
void OutPut(int days, int year, int month);
int main()
{
int year = 0, month = 0;
char select;
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;
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;
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;
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)
{
for (i = 0; i < k + 1; i++)
cout << ' ' << '\t';
}
k = k + 1 + 1;
n = Distinguish(year, month);
for (i = 1; i <= n; i++)
{
cout << i << '\t';
if (k % 7 == 0)
{
cout << endl;
}
k = k + 1;
}
cout << endl;
}
|