上机目的:解决有关的问题
上机任务:完成有关年月的计算
/*
* Copyright (c) 2012, 烟台大学计算机学院
* All rights reserved.
* 作 者:王飞
* 完成日期:2012 年 10 月 25 日
* 版 本 号:v1.0
* 输入描述:本月有几天
* 问题描述:输入年份、月份,即可求得本月一共多少天
* 程序输出:本月一共多少天
* 问题分析:先判断是否为闰年,然后根据所输入的月份进行输出
* 算法设计:
*/
#include <iostream>
#include <Cmath>
using namespace std; //函数定义
int main()
{
int year,month;
cout<<"请输入年月:";
cin>>year>>month;
if(month>12)
{
cout<<"您输入有误,请重新输入!!!"<<endl;
}
else
{
if(month==2)
{
if(year%4==0&&year%100!=0||year%400==0) //判断是否是闰年
{
cout<<"该月有29天"<<endl;
}
else
{
cout<<"该月有28天"<<endl;
}
}
else
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) //一系列的判断条件
cout<<"该月有31天"<<endl;
else
cout<<"该月有30天"<<endl;
}
return 0;
}
输出结果:
心得体会:对于各种程序框架要熟练掌握,只有在练习中才会有收获!