程序分析题:阅读程序后,填写程序的正确运行结果。
请写出myText1.txt文本文件中的内容。
【正确答案】:
ORIGINAL DATE:2008,JANUARY 1
CONVERTED DATE:1 JANUARY 2008
【题目解析】:
根据题目string Date("2008,January 1")可知,myFile1<<"original date: "<<Date<<endl;的内容为original date:2008,January 1
成员函数find用来在主串中检索所需字符串,成员函数substr用来返回字符串的子串。
string Year=Date.substr(0.4);返回Date字符串中从0开始返回4个字符,即Year=2008;
int k=Date.find(","); k=4; int i=Date.find(" "); i=12;
String Month=Date.substr(k+1,i-k-1);即String Month=Date.substr(5,7); 返回Date中从5开始返回7个字符,即Month=January;
同理Day=1;
string NewDate=Day+“ ”+Month+“ ”+Year;因此myFile1<<"Converted date:"<<NewDate<<endl;的内容为Converted date:1 January 2008