2017年复试程序设计题
2018-03-02 15:27
295 查看
2017年
题目
格式转换,从一个文件中读取日期07/21/2016,转换为以下格式July 21,2016并输出到屏幕上方法一:
#include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; int main(){ ifstream is("/Users/.../MyDate.txt"); if (!is){ cerr << "File cannot be opened!" << endl; exit(EXIT_FAILURE); } string myMonth[13] = {"","Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec"}; string line; int month; int day; int year; while (is >> line) { month = stoi(line.substr(0,2)); day = stoi(line.substr(3,2)); year = stoi(line.substr(6,4)); cout << setw(4) << left << setfill(' ') << myMonth[month] << " "; cout << setw(2) << right << setfill('0') << day << "," << year << endl; } return 0; }
运行结果:
值得注意的是,setfill(‘0’)函数设置填充字符过后是会一直生效的,所以要在输出月份之前添加一句setfill(’ ‘)以保证格式统一。
方法二:
当然也可以这么玩:#include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; int main(){ ifstream is("/Users/.../MyDate.txt"); if (!is) { cerr << "File cannot be opened!" << endl; exit(EXIT_FAILURE); } string temp; string mon[13] = {"","Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec"}; int month,day,year; size_t Pos_1,Pos_2; while (is >> temp) { Pos_1 = temp.find("/"); Pos_2 = temp.find_last_of("/"); month = stoi(temp.substr(0,Pos_1)); day = stoi(temp.substr(Pos_1 + 1,Pos_2 - Pos_1 - 1)); year = stoi(temp.substr(Pos_2 + 1)); cout << setw(4) << left << setfill(' ') << mon[month] << " " << setw(2) << right << setfill('0') << day << "," << year << endl; } return 0; }
法二更加通用化。
相关文章推荐
- 2017年吉林大学软件学院学硕高级程序设计复试题回忆
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem B : Coach
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem J : Raising Bacteria
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛—D
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛 Problem E: qwb和李主席 (折半枚举!!!!n堆数分成最接近的两堆!!
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛—H
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛 D: qwb与神奇的序列 [矩阵]【数学】
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛—A qwb与支教
- 2017年上海金马五校程序设计竞赛 A : STEED Cards 暴力
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛 Problem G: qwb去面试(数学问题)
- 2017年上海金马五校程序设计竞赛(网上资格赛)Problem E : A No-story Kingdom
- 2017年浙江中医药大学程序设计竞赛 C、寻找zcmu
- 2017年山东省第八届ACM大学生程序设计竞赛 C fireworks(sdut 3895) 逆元求组合数
- 2017年浙江工业大学大学生程序设计迎新赛预赛
- 2017年北理工研究生复试总结
- 2017年浙江中医药大学大学生程序设计竞赛(重现赛)-H剪纸
- 2017年浙江工业大学大学生程序设计迎新赛决赛 F-栗酱的不等式 [KMP]
- 2017年浙江工业大学大学生程序设计迎新赛决赛—网络同步赛 I 栗酱和仙剑【模拟】
- 2017年浙江工业大学大学生程序设计迎新赛决赛 F 栗酱的不等式
- 【2017年浙江工业大学大学生程序设计迎新赛决赛】 F 栗酱的不等式【二分枚举】