A-A+

请编写一个函数printdate(int year int month int day) 该

2022-08-05 23:30:59 问答库 阅读 173 次

问题详情

请编写一个函数printdate(int year,int month,int day),该函数实现将输入的3个数字转换成英语数字纪年输出的功能,如输入March9,1978,则输出1978 3 9。注意:使用switch结构实现该函数的基本功能并应该能够判断错误的输入。部分源程序已存在文件test40_2.cpp中。请勿修改主函数main和其他函数中的任何内容,仅在函数printdate的花括号中填写若干语句。
源程序文件rest40_2.cpp清单如下:
include<iostream.h>
void printdate(int year, int month, int day)
{
}
void main()
{
printdate(1978,3,9);
}请帮忙给出正确答案和分析,谢谢!

参考答案

正确答案:void printdate(int year int month int day){if(year<0||month<1||month>12||day<1||day>31){cout<<"ERROR";return;}switch(month){case 1:cout<<"January";break;case 2:cout<<"February";break;case 3:cout<<"March";break;case 4:eout<<"April";break;case 5:cout<<"May";break;case 6:cout<<"June";break;case 7:cout<<"July";break;case 8:cout<<"Auguest";break;case 9:cout<<"September";break;case 10:cout<<"October";break;case 11:cout<<"November";break;case 12:cout<<"December";break;}cout<<" "<<day<<""<<year<<endl;}
void printdate(int year, int month, int day){if(year<0||month<1||month>12||day<1||day>31){cout<<"ERROR";return;}switch(month){case 1:cout<<"January";break;case 2:cout<<"February";break;case 3:cout<<"March";break;case 4:eout<<"April";break;case 5:cout<<"May";break;case 6:cout<<"June";break;case 7:cout<<"July";break;case 8:cout<<"Auguest";break;case 9:cout<<"September";break;case 10:cout<<"October";break;case 11:cout<<"November";break;case 12:cout<<"December";break;}cout<<" "<<day<<","<<year<<endl;} 解析:本题考查的是考生对switch结构的应用。switch分支结构也是常用的选择结构,对于每个case结构,只有遇到break才会中止并且跳出switch结构,否则会一直执行到下一个break或者switch的结尾,而对于参数的预处理应该是程序健壮性的基本要求。

考点: