C++学生信息管理系统4.0

//example6_14_student.h
#include<iostream.h>
#include<fstream.h>
#include<string.h>

class Person
{
protected:
	char *name;
	char sex;
	int age;
public:
	Person( );
	Person( char *na , char s , int a);
	~Person( );
};

class Student: virtual public Person
{
protected:
	char speciality[20];
	char number[16];
public:
	Student( );
	Student( char *na , char s, int a, char * spec, char * num);
	~Student( );
	Student & operator = (Student &); //重载赋值运算符=
	operator char * ();				  //重载转换类型运算符
	operator char ();				  //重载转换类型运算符
	operator int ();				  //重载转换类型运算符
    friend ostream & operator<<(ostream &,const Student & );  //友元函数重载输出运算符"<<"	
    friend istream & operator >> (istream &,Student &); 	  //友元函数输入重载运算符">>"
};



//example6_14_student.cpp
//Person类的函数实现
#include "example6_14_student.h"
Person::Person( )
{
	name = NULL;
}
Person::Person( char *na , char s, int a)
{
	if( na )
	{ 
		name = new char[ strlen(na) + 1 ];
		strcpy(name, na);
	}
	sex = s;
	age = a;
}
Person::~Person( )
{
	if (name)
		delete [] name;
}
//Student类的函数实现
Student::Student( )
{
	;
}
Student::Student( char *na , char s, int a, char * spec ,char * num):
 Person( na, s, a)
{
	strcpy(speciality, spec);
	strcpy(number, num);
}
Student::~Student( )
{
//	count--;
}
Student & Student::operator = (Student & stu)
{
	strcpy(name,stu.name);
	sex = stu.sex;	
    age = stu.age;
	strcpy(speciality,stu.speciality);
	strcpy(number,stu.number);
	return *this;
}
Student::operator char * ()
{
	return name;
}
Student::operator char ()
{
	return sex;
}
Student::operator int ()
{
	return age;
}
ostream & operator<<(ostream & out,const Student & stu)
{
	out << stu.name << '\t';
	out << stu.sex << '\t';	
    out << stu.age << '\t';
	out << stu.speciality << '\t';
	out << stu.number << '\n';	
	return out;
}
istream & operator>>(istream & in,Student & stu)
{
	char temp[80];
	cout << "输入学生信息:\n";
	cout << "姓名:";	in>>temp;	
	if( temp )
	{ 
		stu.name = new char[ strlen(temp) + 1 ];
		strcpy(stu.name, temp);
	}
	cout << "姓别:";	in >> stu.sex;
    cout << "年龄:";	in >> stu.age;
	cout << "专业:";	in >> stu.speciality;
	cout << "学号:";	in >> stu.number;
	return in;
}
//example6_14_interface.h
#include "example6_14_student.h"
const int N = 2;        //常量N的值可以根据实际编程需要调整
class Interface
{
protected:
	Student st[N];
	int num;			//学生信息人数
public:
	Interface( );		//学生信息系统界面
	void Browse( );		//浏览学生信息
	void Run( );		//系统启动
	void Input( );		//输入学生信息
	void Sort( );		//按年龄排序学生信息
	void Statistic( );	//按性别统计学生信息
	bool Search( );		//按姓名查询学生信息
};


//example6_14_interface.cpp
#include "example6_14_interface.h"
#include <string.h>
#include <iostream.h>
Interface::Interface( )
{
	num = 0;
}
void Display( )
{
	cout<<"\n**********0.退    出**********" << endl;
	cout << "**********1.录入信息**********" << endl;
	cout << "**********2.查询信息**********" << endl;
	cout << "**********3.浏览信息**********" << endl;
	cout << "**********4.年龄排序**********" << endl;
	cout << "**********5.统计信息**********" << endl;
}
void Interface::Run( )
{
	int choice;
	do
	{
		Display( );
		cout << "\n请输入你的选择:";
		cin >> choice;
		switch ( choice )
		{
		case 0 :
			break;
		case 1 :
			Input( );
			break;
		case 2 :
			Search( );
			break;
		case 3 :
			Browse( );
			break;
		case 4 :
			Sort( );
			break;
		case 5 :
			Statistic( );
			break;
		}
		
	} while (choice);
}
void Interface::Input( )
{
	if(num == N)
	{
		cout << "\n数据已经存满!\n";
		return;
	}
	int i = num;
	cin >> st[i];
	num ++;
}
void Interface::Browse( )
{
	cout << "\n你要查看学生数据!\n";
	if( num == 0)
	{
		cout<<"\n没有学生数据!\n";
		return;
	}
	else
	{
		cout <<"姓名"<<'\t'<<"性别"<<'\t'<< "年龄"<<'\t'
			<<"专业"<<'\t'<<"学号"<<'\n';
		for ( int i=0 ; i< num ; i++ )
			cout << st[i];
	}
}
bool Interface::Search( )
{
	char na[20];
	cout << "\n你要查找学生!" << endl;
	cout << "\n你要查找的人员姓名:";
	cin >> na;
	for ( int i=0 ; i< num ; i++ )
		if ( strcmp((char*)(st[i]), na ) == 0 )
			break;
		if ( i == num )
		{
			cout << "\n没有此人信息\n" << endl;
			return false;
		}
		else
			cout << st[i];
		return true;
}
void Interface::Sort( )
{
	cout << "\n按学生年龄排序!";
	int k;
	for ( int i=0 ; i < num - 1 ; i++ )
	{
		k=i;
		for( int j=i+1 ; j < num ; j++ )
			if( (int)(st[k]) < (int)(st[j]) )
				k = j;
			Student t = st[k];
			st[k] = st[i];
			st[i] = t;
	}
}	
void Interface::Statistic( )
{
	int m=0;
	cout << "\n按学生性别统计数据!";
	for ( int i=0 ; i < num  ; i++ )
		if(char(st[i])=='M' || char(st[i])=='m')
			m++;
		cout << "\n统计结果!男同学:" << m << "人\n";	
}
//example6_14_main.cpp
#include "example6_14_interface.h"
int main( )
{
	Interface in;
	in.Run( );
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值