问题及代码:
/*
*Copyright (c) 2014,烟台大学计算机学院
*ALL right reserved
*文件名;test。cpp
*作者;李莉
*完成日期2014年10月20日
*版本号v1.0
*
*问题描述:输入年月,计算并输出这个月的天数
*输入描述:输入年份year和月份month
*程序输出:得到这个月的天数
*/
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int year,month,t;
cout<<"请输入年份和月份:"<<endl;
cin>>year>>month;
if ((year%100!=0&&year%4==0)||(year%400==0))
{
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
t=31;
else if (month==4||month==6||month==9||month==11)
t=30;
else
t=29;
}
else
{
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
t=31;
else if (month==4||month==6||month==9||month==11)
t=30;
else
t=28;
}
cout<<"本月天数为:"<<t<<endl;
return 0;
}
运行结果:
知识点总结:本来是用switch语句做的,后来又用if else语句做了一遍,自己感觉用if else语句不如用switch语句简单,而且用if else语句做的这个无法排除错误的月份
心得体会:到达目的地的方法是不同的看,别人可能不会在乎你是步行到达,还是开车到达,但是程序员不一样,程序员不仅要到达目的地,还要高效率的到达目的地