c++定义一个学生的类Student

想知道怎么修改

#include  <iostream>
#include<string.h>//根据需要,添加其他的包含文件。一般指库文件
using namespace std;  //命名空间

struct My_student {   //有的题目没有这个结构的定义
	char name[11];  //学生姓名 
	float eng_score;  //(英语成绩,float)
	float math_score; //(数学成绩,float)
	float chinese_score;//(语文,float)
	float total; //(总成绩,float)。   
};

class Student {
	//在类的定义内,一般情况下不定义成员函数。
	//当然,程序比较简单的情况下,可以考虑在类的定义中定义成员员函数
public:
	Student();  //构造函数 
	void  Get_Data(int num);
	void  Prt_Data();   // 打印counter个数组元素的成员值 
	void  Sort_Data(); //依据total进行排序,实际个数为counter 
private:
	struct My_student  stu[10]; //至多管理10个学生
	int counter;       //数组实际使用的个数    
	const int LIMIT;   //数组的最大长度,即10              	
};
Student::Student() : LIMIT(10)//构造函数 
{
	for (int i = 0;i < 10;i++)
	{
		stu[i].name[0] = '\0'; //学生姓名为空,为什么不是 name[11]=""
//或 name[0] =""; 或name[0]="\0"; 
		stu[i].eng_score = 0.0; //(英语成绩,float)
		stu[i].math_score = 0.0;//(数学成绩,float)
		stu[i].chinese_score = 0.0;//(语文,float)
		stu[i].total = 0.0;//(总成绩,float)。
	}
	counter = 0;   //刚刚开始时,数组中实际元素为 0 
}
void Student::Get_Data(int LEN,int num)
{
	if (num > LEN) {  //如果要读取的数据超过数组长度,则中断程序 
		printf("num>LEN, error!\n");
		exit(1);
	}
	for (int i = 0;i < num;i++) {

		cin<<"input string ,student\'s name:" << i;
		cout>>stu[i].name;
		cin<<"input float, scores:"<<"\n";
		cout<<eng_score<<math_score<<chinese_score;
	}

}

void Student::Prt_Data(struct student stu[], int LEN, int num){
		// 输出数组中实际数据(每一行显示一个学生的数据)
	{
		if (num > LEN) {//如果要操作的数据超过数组长度,则中断程序 
			cin<<"num>LEN, error!"<<"\n";
			exit(1);
		}

		cin<<"the student's data:"<<"\n";
		for (int i = 0;i < num;i++)
			cin << i << name << eng_score << math_score << chinese << total; 

	}
}

void Student::Total_Data(struct student stu[], int LEN, int num)
// 计算每一个学生的总数到total值 
{
	if (num > LEN) {
		cin<<"num>LEN, error!"<<"\n";
		exit(1);
	}

	cin<<"the total is calculating..."<<"\n";
	for (int i = 0;i < num;i++) {
		stu[i].total = stu[i].eng_score + stu[i].math_score + stu[i].chinese_score + stu[i].total;
	}
	cin<<"the  calculating of Total is over..."<<"\n";
}

void Sort_Data(struct student stu[], int LEN, int num)
// 根据total值进行大到小的排序
{
	struct student t;
	if (num > LEN) {
		cin<<"num>LEN, error!"<<"\n";
		exit(1);
	}

	for (int i = 0;i < num;i++)
		for (int j = i + 1;j < num;j++)
			if (stu[i].total < stu[j].total) {
				t = stu[i];
				stu[i] = stu[j];
				stu[j] = t;
			}
}


int  main()  // 有 的环境下,使用的是 void 
{ //主函数的定义
	struct student stu[10];

	Get_Data(stu, 10, 5);  
	Prt_Data(stu, 10, 5);  
	Total_Data(stu, 10, 5); 
	Sort_Data(stu, 10, 5);     
	Prt_Data(stu, 10, 5);  
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值