Experiment 3 Indirect addressing realizes simple processing of students' grades

The code is implemented as follows:
#include<iostream>
using namespace std;
const int max=20;
struct node
{
float data;
};
class student
{
node *a[max];
 int length;
public:
	student();
	student(float b[],int n);
	~student(){}
	int getlength(){return length;}
	float get(int i);
	int locate(float x);
	void insert(float x,int i);
	void dele(int i);
	void print();
};
student::student()
{length=0;
  for(int i=0;i<max;i++)
{a[i]=new node;
a[i]=NULL;}
}

student::student(float b[],int n)
{length=n;
for(int i=0;i<n;i++)
{a[i]=new node;
a[i]->data=NULL;}
for(i=0;i<n;i++)
{
	a[i]=new node;  
  a[i]->data=b[i];
}
}

float student::get(int i){ //Bitwise search
	if(i>length||i<=0) throw "Finding position error";
 return a[i-1]->data;
}

int student::locate(float x) //Find by value
{
for(int i=0;i<length;i++)
{if(a[i]->data==x)  return i+1;}
return 0; //The search fails when the return value is 0
}

void student::insert(float x,int i) //insert operation
{
if(i>max||i<1||i>length+1) throw"插入失败";
 for(int j=length;j>=i;j--)  
 {
	 a[j]=a[j-1];
}
	   length++;
	   a[i-1]=new node;
a[i-1]->data=x;
}
void student::dele(int i) //Delete operation
{  
    if(length==0)
		throw"下溢";  
    if(i<1||i>length)
		throw"位置";  
for(int j=i;j<length;j++)
a[j-1]=a[j];
length--;
}
void student::print() // Traverse the output
{               
	cout<<"The student's grade is:";
	for(int i=0;i<length;i++)
		cout<<a[i]->data<<"   ";
        cout<<endl;
}
void main() //main function
{
	float score[5]={78,79,80,81,84.5};
	student s1,s(score,5);
	cout<<"*******Total"<<s.getlength()<<"Student grades*******"<<endl;
	s.print();
	try{
		cout<<"***Find the third student's grade:";
	cout<<s.get(3)<<endl;}
	catch(char *s)
	{cout<<s<<endl;}
	cout<<"***Finding a score of 50 is the first"<<s.locate(50)<<"bit\n";
	cout<<"***Finding a score of 80 is the first"<<s.locate(79)<<"bit\n";
	cout<<"***Insert 86 points to the fourth place"<<endl;
try{
	s.insert(86,4);}
catch(char *s)
	{
	cout<<s<<endl;
}
cout<<"*******Total"<<s.getlength()<<"Student grades*******"<<endl;
s.print();                                   
cout<<"***Delete the third student's grade\n";
try{
	s.dele(3);}
catch(char *s)
	{
	cout<<s<<endl;
}
s.print();
}

Running result graph:


Experiment impression: Originally, the insertion and deletion functions were missing, and I thought it was almost finished. It turned out that these two functions dragged me down for hours. Fortunately, I finally found my problem by searching for information. The implementation of the data structure code is very important. Some of them have clear ideas, but the results of the written programs are not what they want. You should practice and read more books.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325611308&siteId=291194637