关闭

学生信息表管理

168人阅读 评论(0) 收藏 举报
分类:
#include <iostream>
using namespace std;

struct Student
{
	char number[10];
	char name[20];
	double score;
	friend ostream &operator<<(ostream &out,Student &stu);
};

ostream &operator<<(ostream &out,Student &stu)
{
	out<<"("<<stu.number<<","<<stu.name<<","<<stu.score<<")";
	return out;
}
#include <iostream>
using namespace std;

template<class T>
class SeqList
{
public:
	SeqList(int size=64);
	SeqList(T value[],int n);
	~SeqList();

	bool isEmpty();
	int length();
	T get(int i);
	bool set(int i,T x);
	friend ostream &operator<<<T>(ostream &out,SeqList<T> &list);
	void insert(int i,T x);
	void insert(T x);
	bool remove(int i,T &old);
	void clear();

private:
	T *element;
	int size;
	int len;
};

template<class T>
SeqList<T>::SeqList(int size)
{
	this->size=size<64?64:size;
	this->element=new T[this->size];
	this->len=0;
}

template<class T>
SeqList<T>::SeqList(T value[],int n)
{
	if (n>0)
	{
		this->element=new T[2*n];
		this->size=2*n;
		this->len=n;

		for(int i=0;i <n;i ++)
		{
			this->element[i]=value[i];
		}
	}
}

template<class T>
SeqList<T>::~SeqList()
{
	delete []this->element;
}

template<class T>
bool SeqList<T>::isEmpty()
{
	return len==0;
}

template<class T>
int SeqList<T>::length()
{
	return len;
}

template<class T>
T SeqList<T>::get(int i)
{
	return element[i];
	/*if (i >=0&&i<len)
	{
	return element[i];
	}
	return "false";*/
}

template<class T>
bool SeqList<T>::set(int i,T x)
{
	if (i>=0&&i<len)
	{
		element[i]=x;
		return true;
	}
	return false;
}

template<class T>
ostream &operator<<(ostream &out,SeqList<T> &list)
{
	out<<"(";
	if (list.len>0)
	{
		out<<list.element[0];
		for (int i=1;i<list.len;i++)
		{
			out<<","<<list.element[i];
		}
	}
	out<<")\n";
	return out;
}

template<class T>
void SeqList<T>::insert(int i,T x)
{
	if (len==size)
	{
		T *temp=element;
		element=new T[size*2];
		for (int i=0;i<size;i ++)
		{
			element[i]=temp[i];
		}
		size*=2;
	}
	if (i<0)
	{
		i=0;
	}
	if (i>len)
	{
		i=len;
	}
	for (int j=len-1;j>=i;j--)
	{
		element[j+1]=element[j];
	}
	element[i]=x;
	len++;
}

template<class T>
void SeqList<T>::insert(T x)
{
	insert(len,x);
}

template<class T>
bool SeqList<T>::remove(int i,T &old)
{
	if (len>0&&i>=0&&i<len)
	{
		old=element[i];
		for (int j=i;j<len;j++)
		{
			element[j]=element[j+1];
		}
		len--;
		return true;
	}
	return false;
}

template<class T>
void SeqList<T>::clear()
{
	len=0;
}

#include "SeqList.h"
#include "Student.h"
#include <iostream>
using namespace std;

double average(SeqList<Student> &stulist)
{
	int n=stulist.length();
	if (n>0)
	{
		double sum=0;
		for (int i=0;i<n;i ++)
		{
			sum+=stulist.get(i).score;
		}
		return sum/n;
	}
	return 0.0;
}

void printGrade(SeqList<Student> &stulist)
{
	int n=stulist.length();
	if (n>0)
	{
		int grade[5]={0};
		for (int i=0;i<n;i++)
		{
			double x=stulist.get(i).score;
			if (x>=90&&x<=100)
			{
				grade[0]++;
			}
			else if (x>=80&&x<=90)
			{
				grade[1]++;
			}
			else if (x>=70&&x<80)
			{
				grade[2]++;
			}
			else if (x>=60&&x<70)
			{
				grade[3]++;
			}
			else
			{
				grade[4]++;
			}
		}
		cout<<"优秀"<<grade[0]<<",良好"<<grade[1]<<",中等" <<grade[2]<<",及格"<<grade[3]<<",不及格"<<grade[4]<<endl;
	}
}

int main()
{
	Student group[]={{"20208301","王红",80},{"20208302","张明",75},{"20208303","李强",90},{"20208304","郑小春",80},{"20208305","陈鑫诺",60},{"20208306","吴宁",65}};
	SeqList<Student> stulist(group,6);
	Student stu={"20208307","崔小兵",70};
	stulist.insert(stu);
	cout<<"学生成绩表("<<stulist.length()<<"人):\n";
	cout<<stulist;
	cout<<"平均成绩"<<average(stulist)<<endl;
	if (stulist.remove(0,stu))
	{
		cout<<"删除了"<<stu.name<<endl;
	}
	system("pause");
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1817次
    • 积分:162
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:1篇
    • 译文:2篇
    • 评论:0条