2016/5/21 1002. format the book list again~

比起上一题来这一题就比较麻烦了。首先题目给了一个类,那么输出的时候就必须是要重载<<运算符了。同时看到题目的主函数里用了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>}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值