2016/5/21 1002. format the book list again~
来源:互联网 发布:极度恐慌网络 编辑:程序博客网 时间:2024/06/08 19:26
比起上一题来这一题就比较麻烦了。首先题目给了一个类,那么输出的时候就必须是要重载<<运算符了。同时看到题目的主函数里用了leftform和rightform两个自定义的符号。那么换个思路来,就可以自己定义两个类A,B,它们有对象leftform和rightform,然后再分别重载它们的<<就好了。但是要注意到题目里的默认输出是定点右对齐,并不需要保持小数位数,而下面两种输出都是控制为两位小数。那么在默认的输出里重载了左对齐setf后还需要unsetf,同时在两个自定义类里需要注意控制小数位数。
#include <iostream>#include <string>#include <iomanip>using namespace std;class Book{<span style="white-space:pre"></span>string name;<span style="white-space:pre"></span>string code;<span style="white-space:pre"></span>double cost;public:<span style="white-space:pre"></span>Book(string s, string c, double co) :name(s), code(c), cost(co) {}<span style="white-space:pre"></span>string getName()<span style="white-space:pre"></span>{<span style="white-space:pre"></span>return name;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>string getCode()<span style="white-space:pre"></span>{<span style="white-space:pre"></span>return code;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>double getCost()<span style="white-space:pre"></span>{<span style="white-space:pre"></span>return cost;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>friend ostream& operator <<(ostream& out, Book& b)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>out.setf(ios::showpoint);<span style="white-space:pre"></span>out << setiosflags(ios::fixed);<span style="white-space:pre"></span>out << setw(15) << b.getName() << setw(15) << b.getCode() << setw(15) << b.getCost() << endl;<span style="white-space:pre"></span>out.unsetf(ios::showpoint);<span style="white-space:pre"></span>out.unsetf(ios::fixed);<span style="white-space:pre"></span>return out;<span style="white-space:pre"></span>}};ostream& leftform (ostream& out){<span style="white-space:pre"></span>out << "left :";<span style="white-space:pre"></span>out.setf(ios_base::left);<span style="white-space:pre"></span>out << setiosflags(ios::fixed);<span style="white-space:pre"></span>out << fixed << setprecision(2);<span style="white-space:pre"></span>return out;}ostream& rightform(ostream& out){<span style="white-space:pre"></span>out << "right:";<span style="white-space:pre"></span>out.setf(ios_base::right);<span style="white-space:pre"></span>out << fixed << setprecision(2);<span style="white-space:pre"></span>return out;}int main() {<span style="white-space:pre"></span>int N;<span style="white-space:pre"></span>string name;<span style="white-space:pre"></span>string code;<span style="white-space:pre"></span>double cost;<span style="white-space:pre"></span>cin >> N;<span style="white-space:pre"></span>int i = 0;<span style="white-space:pre"></span>Book *books[3];<span style="white-space:pre"></span>for (i = 0; i<N; i++) {<span style="white-space:pre"></span>cin >> name >> code >> cost;<span style="white-space:pre"></span>books[i] = new Book(name, code, cost);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>for (i = 0; i<N; i++) {<span style="white-space:pre"></span>cout << "-----:" << *books[i];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>for (i = 0; i<N; i++) {<span style="white-space:pre"></span>cout << leftform << *books[i];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>for (i = 0; i<N; i++) {<span style="white-space:pre"></span>cout << rightform << *books[i];<span style="white-space:pre"></span>}}
0 0
- 2016/5/21 1002. format the book list again~
- 2016/5/21 1002. format the book list again~
- 2016/5/21 1001.Format the book list
- 2016/5/21 1001.Format the book list
- The Book List 2016北京网赛
- The Book List
- HihoCoder1383 The Book List
- zoj 2727 List the Book
- hiho 1383 The Book List
- hihoCoder 1383 : The Book List
- hihocoder 1384 the book list
- 北京赛区(2016)网络赛 A题 The Book List
- hihoCoder #1383 : The Book List 【2016北京网赛】模拟
- The Definitive C++ Book Guide and List
- The Definitive C++ Book Guide and List
- hihocoder1383 The Book List 字典树
- The Definitive C++ Book Guide and List
- BOOK LIST
- android fragments
- 时间序列处理工具选择
- TCP与UDP的区别与选择
- C#网络程序设计1-2:委托实现单线程
- LeetCode Everyday: 347. Top K Frequent Elements
- 2016/5/21 1002. format the book list again~
- Derby安装步骤及使用教程
- 3n+1数列问题 5月22号
- poj1018(枚举)
- 《Python 核心编程》 学习记录
- C# 带权重的随机 与 不带权重的随机
- Linux常用命令--网络命令、关机重启命令
- HDU 5690 (分治 || 循环节)
- C语言函数内static关键字详解