第七章 7.6节练习

练习7.56

什么是类的静态成员?他有何有点?静态成员与普通成员有何区别?

解答:

【引用】与类本身直接相关的成员。


练习7.57

编写你自己的Account类。

解答:

参考书中实现吧。


练习7.58

下面的静态数据成员的声明和定义有错误吗?请解释原因。

// example.h
class Example{
public:
  static double rate = 6.5;
  static const int vecSize = 20;
  static vector<double> vec(vecSize);
};

//example,c
#include "example.h"
double Example::rate;
vector<double> Example::vec;

解答:

以下是clang3.4给出的错误提示,相较VS2013和gcc4.8.2的提示来说更好理解一些。

test.cc:8:19: error: non-const static data member must be initialized out of
      line
    static double rate = 6.3;
                  ^      ~~~
test.cc:10:35: error: unknown type name 'vecSize'
        static vector<double> vec(vecSize);
                                  ^
test.cc:14:21: error: definition or redeclaration of 'rate' not allowed inside a
      function
    double Example::rate;
           ~~~~~~~~~^
test.cc:15:31: error: definition or redeclaration of 'vec' not allowed inside a
      function
      vector<double> Example::vec;
                     ~~~~~~~~~^
当使用为rate添加const标识时,其会将const和double联系在一起,作为一个新的类型。

所以,这里rate需要在类外定义。


不大清楚这里为什么要输入类型名称。

不过,应该清楚怎么改。将但参数和括号去掉,在类外去定义这个成员。

(2014.12.03更新,这里要求输入一个类型,是因为C++将其解析为一个函数声明,所以让你输入类型以确定这是一个正确的函数声明。这就是一个名为“C++’s most vexing parse”(最令人头痛的语法解析)的东西, 有兴趣的同学可以去 http://en.wikipedia.org/wiki/Most_vexing_parse 或 http://qiezhuifeng.diandian.com/post/2012-08-27/40038339477 这里看看。)


在C文件中,重定义了类中的变量,所以会报错。


小科同学学习了数组,认真进行编程练习,顺利完成了老师布置的20道课外编程题后,他想起了科比20年的职业生涯数据,查阅数据如下: No Season Age TRB AST PTS 1 1996-97 18 1.9 1.3 7.6 2 1997-98 19 3.1 2.5 15.4 3 1998-99 20 5.3 3.8 19.9 4 1999-00 21 6.3 4.9 22.5 5 2000-01 22 5.9 5 28.5 6 2001-02 23 5.5 5.5 25.2 7 2002-03 24 6.9 5.9 30 8 2003-04 25 5.5 5.1 24 9 2004-05 26 5.9 6 27.6 10 2005-06 27 5.3 4.5 35.4 11 2006-07 28 5.7 5.4 31.6 12 2007-08 29 6.3 5.4 28.3 13 2008-09 30 5.2 4.9 26.8 14 2009-10 31 5.4 5 27 15 2010-11 32 5.1 4.7 25.3 16 2011-12 33 5.4 4.6 27.9 17 2012-13 34 5.6 6 27.3 18 2013-14 35 4.3 6.3 13.8 19 2014-15 36 5.7 5.6 22.3 20 2015-16 37 3.7 2.8 17.6 Career 5.2 4.7 25 请你帮助小科分析一下,职业生涯的数据需要你根据给定的数据在主函数里初始化,输入两个不同的赛季序号a和b(均已满足 1<=a<=20,1<=b<=20),输出范围内的最高得分和次高得分以及对应的赛季名、年龄、得分、助攻、篮板数据。 给定原始数据格式 {1,"1996-97",18,1.9,1.3,7.6}, {2,"1997-98",19,3.1,2.5,15.4}, {3,"1998-99",20,5.3,3.8,19.9}, {4,"1999-00",21,6.3,4.9,22.5}, {5,"2000-01",22,5.9,5,28.5}, {6,"2001-02",23,5.5,5.5,25.2}, {7,"2002-03",24,6.9,5.9,30}, {8,"2003-04",25,5.5,5.1,24}, {9,"2004-05",26,5.9,6,27.6}, {10,"2005-06",27,5.3,4.5,35.4}, {11,"2006-07",28,5.7,5.4,31.6}, {12,"2007-08",29,6.3,5.4,28.3}, {13,"2008-09",30,5.2,4.9,26.8}, {14,"2009-10",31,5.4,5,27}, {15,"2010-11",32,5.1,4.7,25.3}, {16,"2011-12",33,5.4,4.6,27.9}, {17,"2012-13",34,5.6,6,27.3}, {18,"2013-14",35,4.3,6.3,13.8}, {19,"2014-15",36,5.7,5.6,22.3}, {20,"2015-16",37,3.7,2.8,17.6}
最新发布
05-28
好的,我可以帮你解决这个问题。你可以把数据存储在一个结构体数组中,然后编写一个函数来查找指定范围内的最高得分和次高得分以及对应的赛季名、年龄、得分、助攻、篮板数据。以下是一个可能的代码实现: ```c #include <stdio.h> #include <string.h> #define MAX_SEASONS 20 typedef struct { int no; char season[8]; int age; float trb; float ast; float pts; } SeasonData; void find_max_scores(SeasonData *data, int a, int b, float *max1, float *max2) { int i; char max1_season[8], max2_season[8]; int max1_age, max2_age; float max1_trb, max1_ast, max1_pts; float max2_trb, max2_ast, max2_pts; // 初始化 *max1 = data[a-1].pts; strcpy(max1_season, data[a-1].season); max1_age = data[a-1].age; max1_trb = data[a-1].trb; max1_ast = data[a-1].ast; max1_pts = data[a-1].pts; *max2 = -1.0; strcpy(max2_season, ""); max2_age = 0; max2_trb = 0.0; max2_ast = 0.0; max2_pts = 0.0; // 查找最高得分和次高得分 for (i = a; i <= b; i++) { if (data[i-1].pts > *max1) { *max2 = *max1; strcpy(max2_season, max1_season); max2_age = max1_age; max2_trb = max1_trb; max2_ast = max1_ast; max2_pts = max1_pts; *max1 = data[i-1].pts; strcpy(max1_season, data[i-1].season); max1_age = data[i-1].age; max1_trb = data[i-1].trb; max1_ast = data[i-1].ast; max1_pts = data[i-1].pts; } else if (data[i-1].pts > *max2) { *max2 = data[i-1].pts; strcpy(max2_season, data[i-1].season); max2_age = data[i-1].age; max2_trb = data[i-1].trb; max2_ast = data[i-1].ast; max2_pts = data[i-1].pts; } } // 输出结果 printf("最高得分:%.1f(%s,年龄:%d,篮板:%g,助攻:%g)\n", *max1, max1_season, max1_age, max1_trb, max1_ast); printf("次高得分:%.1f(%s,年龄:%d,篮板:%g,助攻:%g)\n", *max2, max2_season, max2_age, max2_trb, max2_ast); } int main() { SeasonData data[MAX_SEASONS] = { {1,"1996-97",18,1.9,1.3,7.6}, {2,"1997-98",19,3.1,2.5,15.4}, {3,"1998-99",20,5.3,3.8,19.9}, {4,"1999-00",21,6.3,4.9,22.5}, {5,"2000-01",22,5.9,5,28.5}, {6,"2001-02",23,5.5,5.5,25.2}, {7,"2002-03",24,6.9,5.9,30}, {8,"2003-04",25,5.5,5.1,24}, {9,"2004-05",26,5.9,6,27.6}, {10,"2005-06",27,5.3,4.5,35.4}, {11,"2006-07",28,5.7,5.4,31.6}, {12,"2007-08",29,6.3,5.4,28.3}, {13,"2008-09",30,5.2,4.9,26.8}, {14,"2009-10",31,5.4,5,27}, {15,"2010-11",32,5.1,4.7,25.3}, {16,"2011-12",33,5.4,4.6,27.9}, {17,"2012-13",34,5.6,6,27.3}, {18,"2013-14",35,4.3,6.3,13.8}, {19,"2014-15",36,5.7,5.6,22.3}, {20,"2015-16",37,3.7,2.8,17.6} }; int a, b; float max1, max2; printf("请输入赛季序号范围(1-20):"); scanf("%d %d", &a, &b); find_max_scores(data, a, b, &max1, &max2); return 0; } ``` 这个程序首先定义了一个结构体 `SeasonData` 来存储每个赛季的数据。然后在 `main` 函数中初始化了这个结构体数组 `data`。接着调用了 `find_max_scores` 函数来查找指定范围内的最高得分和次高得分。这个函数接受一个指向 `data` 数组的指针,以及两个整数 `a` 和 `b` 来指定查找范围。函数使用两个 `float` 类型的指针来返回最高得分和次高得分。函数内部使用一个循环来遍历指定范围内的所有赛季,查找最高得分和次高得分,并记录相应的赛季名、年龄、得分、助攻、篮板数据。最后输出结果。 注意,这个程序在输入赛季序号范围时,要求用户输入两个整数,用空格隔开。例如,如果要查找第 5 到第 10 个赛季的数据,用户应该输入“5 10”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值