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
原创粉丝点击