《c++编程思想第2卷》第四章练习题4-15答案
来源:互联网 发布:sublime text 3 java 编辑:程序博客网 时间:2024/05/16 04:29
最近在看c++编程思想第2卷,今天刚刚做完了第4章输入输出流的练习题,觉得第15题有点意思,特写到博文里,以便以后查阅和学习。
题目如下:(意思是解析文件,并按格式输出)
15. Suppose you are given line-oriented data in a file formatted as follows:
//: C04:Exercise15.txt
Australia
5E56,7667230284,Langler,Tyson,31.2147,0.00042117361
2B97,7586701,Oneill,Zeke,553.429,0.0074673053156065
4D75,7907252710,Nickerson,Kelly,761.612,0.010276276
9F2,6882945012,Hartenbach,Neil,47.9637,0.0006471644
Austria
480F,7187262472,Oneill,Dee,264.012,0.00356226040013
1B65,4754732628,Haney,Kim,7.33843,0.000099015948475
DA1,1954960784,Pascente,Lester,56.5452,0.0007629529
3F18,1839715659,Elsea,Chelsy,801.901,0.010819887645
Belgium
BDF,5993489554,Oneill,Meredith,283.404,0.0038239127
5AC6,6612945602,Parisienne,Biff,557.74,0.0075254727
6AD,6477082,Pennington,Lizanne,31.0807,0.0004193544
4D0E,7861652688,Sisca,Francis,704.751,0.00950906238
Bahamas
37D8,6837424208,Parisienne,Samson,396.104,0.0053445
5E98,6384069,Willis,Pam,90.4257,0.00122009564059246
1462,1288616408,Stover,Hazal,583.939,0.007878970561
5FF3,8028775718,Stromstedt,Bunk,39.8712,0.000537974
1095,3737212,Stover,Denny,3.05387,0.000041205248883
7428,2019381883,Parisienne,Shane,363.272,0.00490155
///:~
The heading of each section is a region, and every line under that heading is a seller in that region. Each comma-separated field represents the data about each seller. The first field in a line is the SELLER_ID which unfortunately was written out in hexadecimal format. The second is the PHONE_NUMBER (notice that some are missing area codes). LAST_NAME and FIRST_NAME then follow. TOTAL_SALES is the second to the last column. The last column is the decimal amount of the total sales that the seller represents for the company. You are to format the data on the terminal window so that an executive can easily interpret the trends. Sample output is given below.
Australia
---------------------------------
*Last Name* *First Name* *ID* *Phone* *Sales* *Percent*
Langler Tyson 24150 766-723-0284 31.24 4.21E-02
Oneill Zeke 11159 XXX-758-6701 553.43 7.47E-01
(etc.)
本人实现代码为:
/* * 415.cpp * * Created on: Aug 14, 2015 * Author: zyl */#include <fstream>#include <string>#include <iostream>#include <iomanip>#include <sstream>#include "../require.h"using namespace std;class Sales_man {size_t id;string last_name, first_name;string phone;double sales;double percent;public:enum {NAME_LENGTH = 12, ID_LENGTH = 5, PHONE_LENGTH = 12,SALES_LENGTH = 8, PERCENT_LENGTH = 7};friend ostream& operator<<(ostream& os, const Sales_man& sales_man);friend istream& operator>>(istream& is, Sales_man& sales_man);};ostream& operator<<(ostream& os, const Sales_man& sales_man) {char fillc = os.fill(' ');streamsize prec = os.precision();os << left << setw(Sales_man::NAME_LENGTH) << sales_man.last_name << " " << setw(Sales_man::NAME_LENGTH) << sales_man.first_name << " " << setw(Sales_man::ID_LENGTH) << sales_man.id << " " << setw(Sales_man::PHONE_LENGTH) << sales_man.phone << " " << right << setw(Sales_man::SALES_LENGTH) << setprecision(2) << sales_man.sales << " " << right << setw(Sales_man::PERCENT_LENGTH) << setprecision(2) << scientific << sales_man.percent << left;os.fill(fillc);os.precision(prec);return os;}istream& operator>>(istream& is, Sales_man& sales_man) {char comma;is >> hex >> sales_man.id;is >> comma;char phone[255];is.get(phone, sizeof(phone), ',');// add '-' into phonesales_man.phone = phone;sales_man.phone.insert(3, "-");sales_man.phone.insert(7, "-");is >> comma;char lastname[255];is.get(lastname, sizeof(lastname), ',');sales_man.last_name = lastname;is >> comma;is.get(lastname, sizeof(lastname), ',');sales_man.first_name = lastname;is >> comma;is >> sales_man.sales;is >> comma;is >> sales_man.percent;return is;}bool startsWith(const string&line, const string& start) {size_t pos = line.find(start);if (pos == 0) {return true;}return false;}void printTitle(ostream& os) {char fillc = os.fill(' ');streamsize prec = os.precision();os << left << setw(Sales_man::NAME_LENGTH) << "*Last Name*" <<" " << setw(Sales_man::NAME_LENGTH) << "*First Name*" <<" " << setw(Sales_man::ID_LENGTH) << "*ID*"<< " " << setw(Sales_man::PHONE_LENGTH) << "*Phone*" <<" " << right << setw(Sales_man::SALES_LENGTH) << "*Sales*" << " " << right << setw(Sales_man::PERCENT_LENGTH) << "*Percent*" <<" " << left;os.fill(fillc);os.precision(prec);}int main() {ifstream ifs("Exercise15.txt");assure(ifs);string line;while (getline(ifs, line)) {// jump the comment lineif (startsWith(line, "//")) {continue;}istringstream is(line);size_t pos = line.find(",");if (pos == string::npos) {//it's country linecout << " " << line << endl;cout << " --------------------" << endl;printTitle(cout);cout << endl << endl;} else {Sales_man man;is >> man;cout << man << endl;}}}
- 《c++编程思想第2卷》第四章练习题4-15答案
- java编程思想(第四版)第二章练习题-2
- 读书笔记《C++编程思想》第1卷 第2章
- Java编程思想 第4版 练习题 / 第2章 一切都是对象 / 练习4
- Java编程思想 第4版 练习题 / 第2章 一切都是对象 / 练习1
- Java编程思想 第4版 练习题 / 第2章 一切都是对象 / 练习3
- Java编程思想 第4版 练习题 / 第2章 一切都是对象 / 练习5
- Java编程思想 第4版 练习题 / 第2章 一切都是对象 / 练习6
- Java编程思想 第4版 练习题 / 第2章 一切都是对象 / 练习8
- Java编程思想 第4版 练习题 / 第2章 一切都是对象 / 练习9
- Java编程思想 第4版 练习题 / 第2章 一切都是对象 / 练习11
- Java编程思想 第4版 练习题 / 第3章 操作符 / 练习2
- <<C++编程思想 第2卷>>读后感
- C++ 编程思想 第一卷课后练习题
- C和指针第四章编程练习题小结2
- java编程思想(第四版)第二章练习题-3
- java编程思想(第四版)第二章练习题-9
- java编程思想(第四版)第二章练习题-10
- 一个极度简单的写Excel文件实现
- quartz源码解析(一)
- eclipse中j2ee(struts2)部署及相关问题释疑
- FragmentTabHost+RadioGroup(RadioButton)创建底部导航菜单(默认顶部_隐藏)
- Android Studio快捷键的设置
- 《c++编程思想第2卷》第四章练习题4-15答案
- HDU 5090 Game with Pearls【贪心||模拟||二分匹配】
- sqlserver 数据库修复
- 扩展kmp算法
- mysql 自定义函数一直返回0
- IOS与javascript的交互
- POJ 2752 Seek the Name, Seek the Fame(KMP)
- Jqgrid 底部合计功能
- Android震动vibrator系统开发全过程