请编写一个函数printdate(int year,int month,int day),该函数实现将输入的3个数字转换成英语数字纪年输出的功能,如输入March9,1978,则输出1978 3 9。注意:使用switch结构实现该函数的基本功能并应该能够

admin2010-02-08  20

问题 请编写一个函数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; }

解析 本题考查的是考生对switch结构的应用。switch分支结构也是常用的选择结构,对于每个case结构,只有遇到break才会中止并且跳出switch结构,否则会一直执行到下一个break或者switch的结尾,而对于参数的预处理应该是程序健壮性的基本要求。
转载请注明原文地址:https://kaotiyun.com/show/KKID777K
0

最新回复(0)