You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.0 KiB
42 lines
1.0 KiB
#include <iostream>
|
|
#include <fstream>
|
|
#include <string>
|
|
|
|
class Money
|
|
{
|
|
private:
|
|
int yuan, jiao, fen;
|
|
|
|
public:
|
|
Money(int _yuan = 0, int _jiao = 0, int _fen = 0)
|
|
: yuan{_yuan}, jiao{_jiao}, fen{_fen}
|
|
{
|
|
}
|
|
friend std::ostream &operator<<(std::ostream &os, const Money &m)
|
|
{
|
|
if (&os == &std::cout)
|
|
return os << m.yuan << "元" << m.jiao << "角" << m.fen << "分\n";
|
|
else
|
|
return os << m.yuan << ' ' << m.jiao << ' ' << m.fen << '\n';
|
|
}
|
|
friend std::istream &operator>>(std::istream &is, Money &m)
|
|
{
|
|
if (&is == &std::cin)
|
|
std::cout << "请输入元角分的数值,以空格分开:";
|
|
return is >> m.yuan >> m.jiao >> m.fen;
|
|
}
|
|
};
|
|
|
|
int main()
|
|
{
|
|
std::ofstream ofile{"money.txt"};
|
|
Money m{}, n{};
|
|
std::cin >> m; // 从键盘输入m
|
|
ofile << m; // m写入文件
|
|
ofile.close();
|
|
std::ifstream ifile{"money.txt"};
|
|
ifile >> n; // 从文件读入n
|
|
std::cout << n; // 输出n
|
|
ifile.close();
|
|
return 0;
|
|
} |