实验目的
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
三..实验内容
1.建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。-----------------用顺序表来实现。
#include<iostream>
using namespace std;
const int N= 50;
class stu
{
private:
double score[N];
int num;
public:
stu(double Score[], int n);
void add(int i,double x);
double Delete(int i);
double research(int i);
};
stu::stu(double Score[], int n)
{
if (n > N) throw"参数非法";
int i;
for (i = 0; i < n; i++)
{
score[i] = Score[i];
}
num = n;
}
void stu::add(int i, double x)
{
int j;
if (i > N) throw "上溢";
if (i < 1||i>num+1) throw "位置";
for (j = num; j >= i; j--)
{
score[j] = score[j - 1];
}
score[i - 1] = x;
}
double stu::Delete(int i)
{
int j;
if (num == 0) throw"下溢";
if (i<1 || i>num) throw"位置";
double x= score[i - 1];
for (j = i; j < num; j++)
{
score[j - 1] = score[j];
}
num--;
return x;
}
double stu::research(int i)
{
if (i<1 || i>num) throw"查找位置非法";
else return score[i - 1];
}
int main()
{
double Score[N]={98,99,97,96,65,59 };
stu student(Score, 6);
student.add(2, 100);
cout << student.research(2) << endl;
student.Delete(2);
cout << student.research(2) << endl;
}