C++第8周项目 数组和指针 【项目4-游戏中的角色类增强版】 (复习新版)
2016-05-16 12:57
399 查看
/*
*copyright(c) 2016,烟台大学计算机学院
*All rights reserved
*文件名称:test.cpp
*作者:谭泽纯
*版本:v6.0
*
*问题描述:
【项目3-指向学生类的指针】参考解答
设计一个学生类Student,数据成员包括学号(num)和成绩(score),成员函数根据需要自行设计(建议配备需要的set、get函数,以及必要的输入或输出,给出的代码中也可以找到需要成员函数的线索)。在main函数中,要做到
建立一个对象数组,通过初始化,设置5个学生的数据;
用指针指向数组首元素,输出第1、3、5个学生的信息;
设计一个函数int max(Student *arr);,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并返回值其学号。
下面给出部分程序,在此基础上完成项目:
#include<iostream>
using namespace std;
class Student
{
public:
Student(int nu,double sc):num(nu),score(sc){}
void shuchu()
{
cout<<num<<":"<<score<<endl;
}
double getscire()
{
return score;
}
private:
int num; //学号
double score; //成绩
};
//max函数返回arr指向的对象数组中的最高成绩者的学号(max并不是成员函数,而是普通函数)
int max(Student *arr)
{
int i;
int max1=0;
for(i=1;i<5;i++)
if(arr[i].getscire()>arr[max1].getscire())
max1=i;
return i;
}
int main()
{
int i;
Student stud[5]=
{
Student(101,78.5),Student(102,85.5),Student(103,100),
Student(104,98.5),Student(105,95.5)
};
for(i=0;i<5;i++)
{
stud[i].shuchu();
i++;
}
cout<<"5个学生中成绩最高者的学号为: "<<max(stud);//调用函数显示最高成绩
return 0;
}
*copyright(c) 2016,烟台大学计算机学院
*All rights reserved
*文件名称:test.cpp
*作者:谭泽纯
*版本:v6.0
*
*问题描述:
【项目3-指向学生类的指针】参考解答
设计一个学生类Student,数据成员包括学号(num)和成绩(score),成员函数根据需要自行设计(建议配备需要的set、get函数,以及必要的输入或输出,给出的代码中也可以找到需要成员函数的线索)。在main函数中,要做到
建立一个对象数组,通过初始化,设置5个学生的数据;
用指针指向数组首元素,输出第1、3、5个学生的信息;
设计一个函数int max(Student *arr);,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并返回值其学号。
下面给出部分程序,在此基础上完成项目:
<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">Student</span> {</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">public</span>: …… <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">private</span>: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> num; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//学号 </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> score; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//成绩 </span> }; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//max函数返回arr指向的对象数组中的最高成绩者的学号(max并不是成员函数,而是普通函数) </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> max(Student *arr); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main() { Student stud[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>]= { Student(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">101</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">78.5</span>),Student(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">102</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">85.5</span>),Student(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">103</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>), Student(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">104</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">98.5</span>),Student(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">105</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">95.5</span>) }; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//输出第1、3、5个学生的信息(用循环语句) </span> …… <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//输出成绩最高者的学号 </span> cout<<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"5个学生中成绩最高者的学号为: "</span><<max(stud);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//调用函数显示最高成绩 </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; } </code>*/
#include<iostream>
using namespace std;
class Student
{
public:
Student(int nu,double sc):num(nu),score(sc){}
void shuchu()
{
cout<<num<<":"<<score<<endl;
}
double getscire()
{
return score;
}
private:
int num; //学号
double score; //成绩
};
//max函数返回arr指向的对象数组中的最高成绩者的学号(max并不是成员函数,而是普通函数)
int max(Student *arr)
{
int i;
int max1=0;
for(i=1;i<5;i++)
if(arr[i].getscire()>arr[max1].getscire())
max1=i;
return i;
}
int main()
{
int i;
Student stud[5]=
{
Student(101,78.5),Student(102,85.5),Student(103,100),
Student(104,98.5),Student(105,95.5)
};
for(i=0;i<5;i++)
{
stud[i].shuchu();
i++;
}
cout<<"5个学生中成绩最高者的学号为: "<<max(stud);//调用函数显示最高成绩
return 0;
}
相关文章推荐
- 自己写的哈夫曼树代码,有兴趣的朋友可以看看(C++)
- C++第8周项目 数组和指针 【项目2-用对象数组操作长方柱类】(复习新版)
- C++第8周项目 数组和指针 【项目1 - 数组作数据成员】(复习新版)
- 关于C++输出中文乱码的解决方案
- 【C语言】终端一检测到输入后直接输出,不需要按下enter键
- 【C语言】获取终端的显示区的大小
- 【C语言】统计字符数
- C++作业5
- C++的回调机制
- Sublime Text 3 搭建C++开发环境与插件
- 设计模式之简单工厂模式(C++实现)
- 宏函数
- 浅析C/C++中动态链接库的创建和调用
- C# 调用C++结构体
- 汉诺塔-递归
- C++指针与地址引用
- C语言—动态库建立
- windows c++ 使用锁来控制线程访问
- 一元线性回归模型与最小二乘法及其C++实现
- 【C++】判断vector是否为另一个vector的子集