【C++】引用作为函数形参的练习

复制代码
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 struct free_throws
 6 {
 7     string name;
 8     int made;
 9     int attempts;
10     float percent;
11 };
12 
13 void set_pc(free_throws &ft);
14 void display(const free_throws &ft);
15 free_throws & accumlate(free_throws &target,const free_throws &source);
16 int main()
17 {
18     free_throws one = {"Rick",13,14};
19     free_throws two = {"jack",10,16};
20     free_throws three = {"Rose",11,13};
21     free_throws four = {"Mike",12,17};
22     free_throws team = {"Class 6",0,0};
23     set_pc(one);
24     display(one);
25     accumlate(accumlate(accumlate(accumlate(team,one),two),three),four);
26     display(team);
27     return 0;
28 }
29 
30 void set_pc(free_throws &ft)
31 {
32     if(ft.attempts != 0)
33         ft.percent = 100.0 * float(ft.made) / float(ft.attempts);
34     else
35         ft.attempts = 0;
36 }
37 
38 void display(const free_throws &ft)
39 {
40     cout << "Name: " << ft.name << endl;
41     cout << "Made: " << ft.made << "\t";
42     cout << "Attempt: " << ft.attempts << "\t";
43     cout << "Percent: " << ft.percent << endl;
44 }
45 
46 free_throws & accumlate(free_throws &target,const free_throws &source)
47 {
48     target.attempts += source.attempts;
49     target.made += source.made;
50     set_pc(target);
51     return target;
52 }
复制代码

 

 

posted @   老年新手工程师  阅读(41)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示