信管117118李志荣数据结构实验三---顺序表实现

//sequence_list.cpp
#include<iostream>
#include<string>
using namespace std;
const int n = 5;

struct Information
{	
	int number;
	string name;
	int score;
	int exist=0;   //判断该数据是否需要输出
};
class Sequence_list
{
	
public:
	Sequence_list() { length = 0; }
	void Set();
	void Delete();
	void Seek();
	void Print();
	void Insert();
private:
	int length;
	Information pupil[n];
};
int main()
{
	Sequence_list Student;
	cout << "MENU";
	int use = 0;
	while (use != 6)
	{
		cout << "\n1.SET\n2.Delete\n3.Seek\n4.Print\n5.Insert\n6.Exit\nNow input a number to use function:";
		cin >> use;
		switch (use)
		{
		case 1:Student.Set(); break;
		case 2:Student.Delete(); break;
		case 3:Student.Seek(); break;
		case 4:Student.Print(); break;
		case 5:Student.Insert(); break;
		default:
			break;
		}
	}
	return 0;
}

void Sequence_list::Set()
{
	if (length >= n) throw "overflow";
	cout << "Enter the student information(name,number,score) : ";
	cin >> pupil[length].name >> pupil[length].number >> pupil[length].score;
	pupil[length].exist = 1;
	cout << "Done\n";	
	length++;
}

void Sequence_list::Delete()
{
	if (length == 0) throw"no data you can delete";
	cout << "Enter the number of student you want to delete:";
	int delete_data;
	cin >> delete_data;
	for (int i=0;i<n;i++)
	{
		if (delete_data == pupil[i].number) { cout << "The student information is destroied"; pupil[i].exist = 0; length--; break; }
		if (i + 1 >= n) throw "No match was found ";
	}
}

void Sequence_list::Seek()
{
	if (length == 0) throw "no data";
	cout << "Enter the number of student you want to seek;";
	int seek_data;
	cin >> seek_data;
	for (int i = 0; i<n; i++)
	{
		if (seek_data == pupil[i].number) { 
			cout << "The student information is :\nname:"<< pupil[i].name
				<<"   number:"<< pupil[i].number << "   score:" << pupil[i].score;
			break;
		}
		if (i + 1 >= n) throw "No match was found ";
	}
}

void Sequence_list::Print()
{
	for(int i=0;i<n;i++)
	{if(pupil[i].exist!=0) cout << "The student information is :\nname:" << pupil[i].name << "   number:" << pupil[i].number << "   score:" << pupil[i].score<<endl;}
}

void Sequence_list::Insert()
{
	if (length >= n) throw "overflow";
	cout << "Enter the location you want to insert:";
	int insert_location;
	cin >> insert_location;
	if (insert_location > n) throw "wrong location";
	if (insert_location > length) 
	{
		cout << "Enter the student information:"; 
		cin >> pupil[insert_location].name >> pupil[insert_location].number >> pupil[insert_location].score;
		
	}
	else
	{
			for (int i=length;i>=insert_location;i--)
		{pupil[i] = pupil[i - 1];}	
			cout << "Enter the student information:";
			cin >> pupil[insert_location-1].name >> pupil[insert_location-1].number >> pupil[insert_location-1].score;
	}
	pupil[insert_location].exist = 1;
	length++;
}

猜你喜欢

转载自blog.csdn.net/rumple49/article/details/80149470