C++ Primer 5th 课后习题8.10、8.11、8.13
来源:互联网 发布:突变体软件 编辑:程序博客网 时间:2024/05/17 00:03
//习题8.10int main(){ ifstream in("1"); if (!in) { cerr << "无法打开文件" << endl; } string line; vector<string> words; while (getline(in, line)) words.push_back(line); in.close(); for (auto i : words) { string word; istringstream ist(i); while (ist >> word) cout << word << endl; } return 0;}//习题8.11struct PersonInfor{ string name; vector<string> phones;};int main(){ vector<PersonInfor> people; string line, na; istringstream record; while (getline(cin, line)) { PersonInfor info; record.clear();//notice重复使用字符串流时每次都要clear record.str(line); record >> info.name; while (record >> na) info.phones.push_back(na); people.push_back(info); }}//习题8.13电话号码程序--出错原因:main函数读取的文件有问题//忘了将文件放在exe同名文件夹,而是放在了main函数的文件夹//运行时用命令行打开,如text.exe 1.txt即可打开。struct PersonInfo{ string name; vector<string> phones;};string format(const string &s) { return s; }bool valid(const string &s) { //此处简单返回true,后续再改 return true;}//int main()int main(int argc, char *argv[]){ string line, word; vector<PersonInfo> people; istringstream record; if (argc != 2) { cerr << "请给出文件名" << endl; return -1; } ifstream in(argv[1]); if (!in) { cerr << "无法打开文件" << endl; return -1; } while (getline(in, line)) { PersonInfo info; record.clear(); record.str(line); record >> info.name; while (record >> word) info.phones.push_back(word); people.push_back(info); } ostringstream os; for (const auto &entry : people) { ostringstream formatted, badNums; for (const auto &nums : entry.phones) { if (!valid(nums)) { badNums << " " << nums; } else formatted << " " << format(nums);//???????????????? if (badNums.str().empty()) os << entry.name << " " << formatted.str() << endl; else cerr << "input error : " << entry.name << " invalid number(s) " << badNums.str() << endl; } cout << os.str() << endl; }}
0 0
- C++ Primer 5th 课后习题8.10、8.11、8.13
- C++Primer Chapter3课后习题
- C++primer第5版课后练习习题答案 9.4
- C++primer第5版课后练习习题答案9.5
- C++primer第5版课后练习习题答案9.16
- C++primer第5版课后练习习题答案 9.20
- C++primer第5版课后练习习题答案9.26
- C++primer第5版课后练习习题答案9.28
- C++primer第5版课后练习习题答案9.47
- C++primer第5版课后练习习题答案9.51
- C++primer第5版课后练习习题答案9.52
- C++primer第5版课后练习习题答案7.23
- Primer C 4th(第5章习题)
- 《C Primer Plus》第一章 概览 课后习题
- c primer plus第十一章课后习题
- c++primer第六章课后习题
- c++primer 第一章课后习题 编程代码
- C++ primer 课后习题
- 不得不提的volatile及指令重排序(happen-before)
- 第七周能否构成三角形
- hrbust 1758 Minimum Window Substring【尺取法】好题
- 389. Find the Difference 难度:easy
- Local Feature Detection and Extraction
- C++ Primer 5th 课后习题8.10、8.11、8.13
- 数组
- C#32课的主要内容
- UVA10763--ForeignExchange
- 无头单链表的所有操作(包含面试题)
- 迭代, 瀑布, 到底差別在那?
- 【罗兰のJava学习笔记】第六天,初识面向对象
- Git 基础
- Looper详解--Android消息机制之4