类实现输出最高成绩
来源:互联网 发布:曲婉婷jar of love知乎 编辑:程序博客网 时间:2024/06/08 09:04
/*试定义一个处理学生信息的类 Student。该类包含学号、成绩和姓名等数据成员(学号不能相同)以及若干成员函数,另外定义一外全局函数 max(),返回 n 个学生成绩最高者。具体要求如下:(1)私有数据成员 int num,score; num 存放学号,score 存放成绩 char name[9]; name 存放学生的姓名(2)公有成员函数 构造函数;将学号、成绩设置为 0,姓名设置为空 int check_num(Student str[],int n); 检查学号是否已经在 str 中存在,返回 1,否则返回 0。 void Set(int id,char *na,int sc);为数据成员赋值。 int get_score();返回学生成绩。 void print();输出学生的学号、姓名和成绩。(3) Student max(Student *s,int n);全局函数,求得并返回 s 所指向的 n 个学生中成绩最高者。(4) 在主函数中完成对该类的测试,主函数中定义一个长度为 3 的 Student 类的对象数组 s,依次键入学号、成绩、姓名;注意,在输入每一个学生的信息后,要判断所输入的学号是否已存在,如果存在,则要求重新输入该学生的信息。最后调用全局函数 max(),得到成绩最高的学生信息并输出。*/#include<iostream>using namespace std;class Student{private: int num, score; char name[9];public: Student(); int check_num(Student str[], int n); void Set(int id, char *na, int sc); int get_score(); void print();};Student::Student(){ char *ptr = name; num = 0; score = 0; ptr = NULL;}int Student::check_num(Student str[],int n ){ int flag = 1; for (int i = 0; i < sizeof(str) / sizeof(Student); i++) { if (n == str[i].num) flag = 0; } return flag;}void Student::Set(int id, char *na, int sc){ char *ptr = name; num = id; score = sc; while (*na) { *ptr = *na; ptr++; na++; } ptr = '\0';}int Student::get_score(){ return score;}void Student::print(){ char *ptr = name; cout << "姓名为" ; while (*ptr) cout << *ptr++; cout << endl; cout << "学号为" << num << endl; cout << "成绩为" << score << endl; cout << endl;}Student max(Student *s, int n){ Student temp = *s; for (int i = 0; i < n; i++) { if (temp.get_score() < s->get_score()) temp = *s; s++; } return temp;}int main(){ Student stu[3]; char a[]="sun" ; stu[0].Set(1620, a, 80); //先判断再决定是否插入!!! char b[]="wang"; if (stu[1].check_num(stu, 1621)) stu[1].Set(1621, b, 120); else cout << "重新输入!" << endl; char c[]="zhang"; if (stu[2].check_num(stu, 1622)) stu[2].Set(1622, c, 1120); else cout << "重新输入!" << endl; max(stu, 3).print(); system("pause"); return 0;}
阅读全文
0 0
- 类实现输出最高成绩
- 十六周任务二:成绩排序,输出最高成绩
- 输出各班各科成绩最高的同学?输出各科成绩最高的同学?
- 37. 输入3个学生4门课的成绩,完成maxaver函数实现如下功能:输出平均成绩最高的学生的所有成绩。
- 输出学生成绩。本题要求编写程序,根据输入学生的成绩,统计并输出学生的平均成绩、最高成绩和最低成绩。建议使用动态内存分配来实现。
- week 12 输入姓名与成绩,输出,最高
- 第14周项目3-多科成绩输出最高、最低、平均成绩
- C#中使用数组存储5个学生成绩,计算并输出最高成绩和平均值。
- 请用一条sql语句实现求最高成绩,
- 从键盘上输入学生成绩,统计并输出最高成绩和最低成绩,当输入负数时结束输入。
- 输入5个学生的姓名和成绩,顺序输出这五个学生的姓名和成绩,并输出最高成绩的姓名和成绩
- 输入n(n<46)个学生的姓名和成绩,顺序输出这n个学生的姓名和成绩,并输出最高成绩的姓名和成绩。
- 最高成绩,最低成绩,平均成绩处理
- 第五周任务4(找出个学生中成绩最高者,并输出其学号和成绩)
- c++:指向学生类的指针:求最高成绩
- 第13周项目6 例1.4 输出最高成绩及学号
- 第14周项目3-增加保存姓名的数组输出最高成绩的名单
- 动态数组实现输出全班平均成绩
- 17.5.28C组总结
- Springboot状态码总结
- spring事件机制_http://enjiex.iteye.com/blog/1070094
- iptables简单应用
- 【剑指offer】面试题 24:反转链表
- 类实现输出最高成绩
- 游戏开发中的人工智能(四):群聚
- SPOJ 28286 BLMIRINA
- 前端笔记——jQuery
- 40. Combination Sum II
- 【HTML】- 固定边框滚动
- 17.4.15C组总结
- php 生成验证码,缩略图,及水印图
- java web开发者常用的一些软件和工具