以下是跟大家分享的代码以及运行结果:
#include <iostream>
#include <string>
struct jiegou{
std::string name;
int made;
int attemps;
float percent;
};
void display(const jiegou &ft);
void set_pc(jiegou &ft);
jiegou & accumlate(jiegou &target,const jiegou &source);
int main(){
jiegou jiegou1={"例1",11,15};
jiegou jiegou2={"例2",23,32};
jiegou jiegou3={"例3",34,56};
jiegou jiegou4={"例4",23,34};
jiegou jiegou5={"例5",43,56};
jiegou ceshi={"例6",0,0};
jiegou jiegou6;
set_pc(jiegou1);
display(jiegou1);
accumlate(ceshi,jiegou1);
display(ceshi);
display(accumlate(ceshi,jiegou2));
accumlate(accumlate(ceshi,jiegou3),jiegou4);
display(ceshi);
jiegou6=accumlate(ceshi,jiegou5);
std::cout<<"ceshi:\n";
display(ceshi);
std::cout<<"jiegou6:\n";
display(jiegou6);
set_pc(jiegou4);
accumlate(jiegou6,jiegou5)=jiegou4;
std::cout<<"jiegou6:\n";
display(jiegou6);
return 0;
}
void display(const jiegou &ft){
using std::cout;
cout<<"name:"<<ft.name<<'\n';
cout<<"made:"<<ft.made<<'\n';
cout<<"attemps:"<<ft.attemps<<'\n';
cout<<"percent:"<<ft.percent<<'\n';
}
void set_pc(jiegou &ft){
if(ft.attemps!=0)
ft.percent=100.0f*float(ft.made)/float(ft.attemps);
else
ft.percent=0;
}
jiegou & accumlate(jiegou &target,const jiegou &source){
target.attemps+=source.attemps;
target.made+=source.made;
set_pc(target);
return target;
};