📄 11-08.cpp
字号:
//DISPLAY 11.8 Overloading << and >>
//Program to demonstrate the class Money.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
//Class for amounts of money in U.S. currency.
class Money
{
public:
friend Money operator +(const Money& amount1, const Money& amount2);
friend Money operator -(const Money& amount1, const Money& amount2);
friend Money operator -(const Money& amount);
friend bool operator ==(const Money& amount1, const Money& amount2);
Money(long dollars, int cents);
Money(long dollars);
Money( );
double get_value( ) const;
friend istream& operator >>(istream& ins, Money& amount);
//Overloads the >> operator so it can be used to input values of type Money.
//Notation for inputting negative amounts is as in -$100.00.
//Precondition: If ins is a file input stream, then ins has already been
//connected to a file.
friend ostream& operator <<(ostream& outs, const Money& amount);
//Overloads the << operator so it can be used to output values of type Money.
//Precedes each output value of type Money with a dollar sign.
//Precondition: If outs is a file output stream,
//then outs has already been connected to a file.
private:
long all_cents;
};
int digit_to_int(char c);
//Used in the definition of the overloaded input operator >>.
//Precondition: c is one of the digits '0' through '9'.
//Returns the integer for the digit; for example, digit_to_int('3') returns 3.
int main( )
{
Money amount;
ifstream in_stream;
ofstream out_stream;
in_stream.open("infile.dat");
if (in_stream.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}
out_stream.open("outfile.dat");
if (out_stream.fail( ))
{
cout << "Output file opening failed.\n";
exit(1);
}
in_stream >> amount;
out_stream << amount
<< " copied from the file infile.dat.\n";
cout << amount
<< " copied from the file infile.dat.\n";
in_stream.close( );
out_stream.close( );
return 0;
}
//Uses iostream, cctype, cstdlib:
istream& operator >>(istream& ins, Money& amount)
{
char one_char, decimal_point,
digit1, digit2; //digits for the amount of cents
long dollars;
int cents;
bool negative;//set to true if input is negative.
ins >> one_char;
if (one_char == '-')
{
negative = true;
ins >> one_char; //read '$'
}
else
negative = false;
//if input is legal, then one_char == '$'
ins >> dollars >> decimal_point >> digit1 >> digit2;
if ( one_char != '$' || decimal_point != '.'
|| !isdigit(digit1) || !isdigit(digit2) )
{
cout << "Error illegal form for money input\n";
exit(1);
}
cents = digit_to_int(digit1)*10 + digit_to_int(digit2);
amount.all_cents = dollars*100 + cents;
if (negative)
amount.all_cents = -amount.all_cents;
return ins;
}
int digit_to_int(char c)
{
return ( static_cast<int>(c) - static_cast<int>('0') );
}
//Uses cstdlib and iostream:
ostream& operator <<(ostream& outs, const Money& amount)
{
long positive_cents, dollars, cents;
positive_cents = labs(amount.all_cents);
dollars = positive_cents/100;
cents = positive_cents%100;
if (amount.all_cents < 0)
outs << "-$" << dollars << '.';
else
outs << "$" << dollars << '.';
if (cents < 10)
outs << '0';
outs << cents;
return outs;
}
//The definitions of the member functions and other overloaded operators go here.
//See Display 11.3, 11.4, 11.5, and 11.6 for the definitions.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -