[0301]统计图书销售信息 (运算符重载)
Posted shylocks
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[0301]统计图书销售信息 (运算符重载)相关的知识,希望对你有一定的参考价值。
Description:
Code:
#include<iostream> #include<cstring> #include<fstream> using namespace std; struct Sales_Item{ string name; int sum; double value; Sales_Item operator +(const Sales_Item &others){ Sales_Item tmp; tmp.sum=this->sum+others.sum; tmp.value=(this->sum*this->value+others.sum*others.value)/tmp.sum; tmp.name=this->name; return tmp; } }; istream &operator >>(istream &i,Sales_Item &tmp){ i>>tmp.name>>tmp.sum>>tmp.value; return i; } ostream &operator <<(ostream &o,const Sales_Item &tmp){ o<<tmp.name<<" "<<tmp.sum<<" "<<tmp.sum*tmp.value<<" "<<(int)tmp.value<<endl; return o; } int main() { ifstream fin; ofstream fout; fin.open("Homework1.in"); fout.open("Homework1.out"); Sales_Item item1,item2; fin>>item1>>item2; fout<<item1+item2<<endl; fin.close(); fout.close(); return 0; }
Runshot:
以上是关于[0301]统计图书销售信息 (运算符重载)的主要内容,如果未能解决你的问题,请参考以下文章
Python入门-6面向对象编程:10特殊方法和运算符重载-特殊属性