自考新教材-p296

源程序:

#include<iostream>

#include<iomanip>

#include<string>

using namespace std;

string Months[13] = { "","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };  //西文格式的月份

int main()

{

char ch;

int year, month, day;

while ((ch = cin.peek()) != EOF) {           //取输入流的第1个字符查看

if (ch >= 'A'&&ch<'Z') {               //处理西文格式:Dec 24 2011

string sMonth;

cin >> sMonth >> day >> year;        //接收:月、日、年

//查找月份完成转换

for (month = 0; month<12 && sMonth != Months[month]; ++month);

}

else {                              //处理中文格式:2011.12.24

cin >> year;                     //接收年

cin.ignore() >> month;           //使用ignore跳过'.',然后接收月

cin.ignore() >> day;             //跳过'.',然后接收日

//以上3条语句等价于:cin>>year>>ch>>month>>ch>>day;

}

cin.ignore();                        //接收

cout << setfill('0') << setw(2) << month;  //设置填充字符'0'和输出宽度

cout << "-" << setw(2) << day << "-" << setw(4) << year << endl;

}

return 0;

}

运行结果:

原文地址:https://www.cnblogs.com/duanqibo/p/12236136.html