順序表學生成績實驗
順序表實驗
#include<iostream>
#include<string>
using namespace std;
const int MaxSize=10;
class Student
{
private:
intGrade[MaxSize];
char*name[MaxSize];
intlenght;
public:
Student(){lenght=0;}
Student(char*a[],int b[],int x);
~Student(){};
voidInsert(int i,char*n,int m);
intDelete(int i);
charLocate(char *k);
voidPrint();
};
#include<iostream>
using namespace std;
#include "SeqList.h"
Student::Student(char *a[],int b[],int x)
{
if(x>MaxSize)throw"上溢";
for(inti=0;i<x;i++)
{
name[i]=a[i];
Grade[i]=b[i];
}
lenght=x;
}
void Student::Insert(int i,char *n,int m)
{
if(lenght>=MaxSize)throw"上溢";
if(i<1||i>lenght+1)throw"位置非法";
for(intj=lenght;j>=i;j--)
{
name[j]=name[j-1];
Grade[j]=Grade[j-1];
}
name[i-1]=n;
Grade[i-1]=m;
lenght++;
}
int Student::Delete(int i)
{
if(lenght==0)throw"下溢";
if(i<1||i>lenght+1)throw"位置非法";
charg=*name[i-1];
for(intj=i;j<lenght;j++)
{
name[j-1]=name[j];
}
lenght--;
returng;
}
char Student::Locate(char *k)
{
for(inti=0;i<lenght;i++)
if(name[i]==k)cout<<i+1<<endl;
return0;
}
void Student::Print()
{
for(inti=0;i<lenght;i++)
cout<<name[i]<<""<<Grade[i]<<endl;
}
#include<iostream>
using namespace std;
#include "SeqList.h"
void main()
{
char*r[5]={"A","B","C","D","E"};
intg[5]={100,85,90,67,95};
cout<<"執行插入操作前的資料為:"<<endl;
StudentL(r,g,5);
L.Print();
try
{
L.Insert(2,"F",60);
}
catch(char*w)
{
cout<<w<<endl;
}
cout<<"執行插入操作後資料為:"<<endl;
L.Print();
cout<<"姓名為A的學生的序號為:"<<endl;
cout<<L.Locate("A")<<endl;
cout<<"執行刪除第一個學生資料的操作,刪除前資料為:"<<endl;
L.Print();
try
{
L.Delete(1);
}
catch(char*w)
{
cout<<w<<endl;
}
cout<<"刪除後資料為:"<<endl;
L.Print();
}