码迷,mamicode.com
首页 > 其他好文 > 详细

[0301]统计图书销售信息 (运算符重载)

时间:2018-03-11 17:32:49      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:idt   color   stream   tor   int   one   open   fstream   div   

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]统计图书销售信息 (运算符重载)

标签:idt   color   stream   tor   int   one   open   fstream   div   

原文地址:https://www.cnblogs.com/shy-/p/8543888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!