实验3:顺序表

#include <iostream>
using namespace std;
const int maxsize = 7;

template <class datatype>
class scorelist
{
private:
	datatype stu[maxsize];
	int length;
public:
	scorelist()
	{
		length = 0;
	}
	~scorelist(){}
	scorelist(datatype a[], int n);
	double locate(int i);
	void insert(int i, datatype x);
	void printlist();
};

template<class datatype>
scorelist<datatype>::scorelist(datatype a[],int n)
{
	int i;
	if (n > maxsize)
		throw"参数非法";
	for (i = 0; i < n; i++)
		stu[i] = a[i];
	length=n;
}

template<class datatype>
double scorelist<datatype>::locate(int i)     //按位查找成绩
{
	if (i<1 && i>length)
		throw"查找位置非法";
	else return stu[i - 1];
}

template<class datatype>
void scorelist<datatype>::insert(int i, datatype x)           //插入成绩
{
	if (length >= maxsize)
		throw"上溢";
	if (i<1 || i>length + 1)
		throw"位置非法";
	for (int j = length; j >= i; j--)
		stu[j] = stu[j - 1];
	stu[i - 1] = x;
	length++;
}


template<class datatype>
void scorelist<datatype>::printlist()
{
	for(int i=0;i<length;i++)
		cout<<stu[i]<<" ";
	cout<<endl;
}

void main()
{
	double stud[5] = { 98.6,85.7,65.9,70.2,73.4};
	scorelist<double>student(stud,5);
	student.printlist();
	cout<<"插入新成绩后:"<<endl;
	try
	{
		student.insert(3,98.6);
	}
	catch(char *s)
	{
		cout<<s<<endl;
	}
	student.printlist();
	cout<<"第三个成绩为:"<<endl;
	cout<<student.locate(3)<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值