c++第二次试验-本月有几天

来源:互联网 发布:手机视频拼接软件 编辑:程序博客网 时间:2024/05/22 11:36
/*  一、问题及代码* 文件名称:本月有几天.cpp  * 作    者:潘维涵  * 完成日期:2017 年 13月 5 日  * 版 本 号:v1.0  * 对任务及求解方法的描述部分: * 输入描述:输入年份  * 问题描述:输入年份来显示某月的天数* 程序输出:无* 问题分析:年分为闰年与平年 * 算法设计:使用switch与if语句  */    #include<iostream>using namespace std;int main(){int iYear, iMonth;cout << "请输入年份和月份 : ";cin >> iYear >> iMonth;if (iYear % 4 == 0 && iYear % 100 != 0 || iYear % 400 == 0){switch (iMonth){case 1:cout << "本月31天" << endl; break;case 2:cout << "本月29天" << endl; break;case 3:cout << "本月31天" << endl; break;case 4:cout << "本月30天" << endl; break;case 5:cout << "本月31天" << endl; break;case 6:cout << "本月30天" << endl; break;case 7:cout << "本月31天" << endl; break;case 8:cout << "本月31天" << endl; break;case 9:cout << "本月30天" << endl; break;case 10:cout << "本月31天" << endl; break;case 11:cout << "本月30天" << endl; break;case 12:cout << "本月31天" << endl; break;}}else{switch (iMonth){case 1:cout << "本月31天" << endl; break;case 2:cout << "本月28天" << endl; break;case 3:cout << "本月31天" << endl; break;case 4:cout << "本月30天" << endl; break;case 5:cout << "本月31天" << endl; break;case 6:cout << "本月30天" << endl; break;case 7:cout << "本月31天" << endl; break;case 8:cout << "本月31天" << endl; break;case 9:cout << "本月30天" << endl; break;case 10:cout << "本月31天" << endl; break;case 11:cout << "本月30天" << endl; break;case 12:cout << "本月31天" << endl; break;}}return 0;}
二、运行结果:
三、心得体会:
灵活使用if与switch语句来解决问题
四、知识点总结:
switch后接整型常量,枚举类型或字符型

0 0
原创粉丝点击