筆記試験問題:学生の学号、名前、性別、年齢情報を含む単鎖表を作成する.いくつかのプログラムを書いて、学生の学号によって挿入して、照会して、削除するなどの操作を実現します.

4152 ワード

以下のコードは、LZが2、3時間かけてじっくり吟味した結果、使用する知識点はfreopen()の使用、異常処理があり、参考に供する.漏れがあれば、指摘を歓迎します.
#include 
using namespace std;
//        ,       
char *err = "not find.";
//         
struct node
{
	char name[30];
	char id[20];
	unsigned short age;
	char sex;
	//           
	struct node *next;
};
//      
class Student
{
	public:
	//              
	Student()
	{
		head = new node;
		head->next = NULL;
	}
	void Create(int n);
	bool Insert(char *stuID);
	bool Delete(char *stuID);
	node* Query(char *stuID);
	int Length() const;
	void Display();
	//         
    ~Student()
    {
    	delete head;
    }
	private:
	struct node *head;
};
//   ,          
/*
void Student::Create(int n)
{
	unsigned short stu_age;
	char stu_name[30];
	char stu_id[20];
	char stu_sex;
	
	node *p;
	for(int i = 0; i < n; ++i)
	{
		p = new node;
		cout << "input the "<< i + 1 << "th " << "student's name,id,sex,age:" << endl;
	cin >> stu_name >> stu_id >> stu_sex >> stu_age;
	 strcpy(p->name ,stu_name);
	 strcpy(p->id,stu_id);
	 p->sex = stu_sex;
	 p->age = stu_age;
	 
	 p->next = head->next;
	 head->next = p;
	}
}
*/
//              
void Student::Create(int n)
{
	char stu_name[30];
	char stu_id[20];
	char stu_sex;
	unsigned short stu_age;
	
	node *p,*q;//        q 
	q = head;
	for(int i = 0; i < n; ++i)
	{
		p = new node;
		cout << "input the "<< i + 1 << "th " << "student's name,id,sex,age:" << endl;
	    cin >> stu_name >> stu_id >> stu_sex >> stu_age;
		strcpy(p->name,stu_name);
		strcpy(p->id,stu_id);
		p->age = stu_age;
		p->sex = stu_sex;
		
		p->next = q->next;
		q->next = p;
		q = p;
	}
}
//         
void Student::Display()
{
	node *p = head->next;
	while(p != NULL)
	{
		cout << p->name << '\t' << p->id << '\t' << p->sex << '\t' << p->age << endl;
		p = p->next;
	}
}

int Student::Length() const
{
	int cnt = 0;
	node *p = head->next;
	while(p != NULL)
	{
		cnt++;
		p = p->next;
	}
	return cnt;
}
//                 NULL,              
node * Student::Query(char *stuID)
{
	node *p = head->next;
	while(strcmp(p->id,stuID) != 0 && p->next != NULL)
	p = p->next;
	if(p->next == NULL)
	return NULL;
	else
	return p;
}
//            ,          
bool Student::Insert(char *stuID)
{
	node *p;
	if((p = Query(stuID)) == NULL)
	//return false;
	throw err;
	
	else
	{
		node *q = new node;
			char stu_name[30];
	        char stu_id[20];
            char stu_sex;
	        unsigned short stu_age;
	    cout << "input the student's name,id,sex,age:" << endl;
	    cin >> stu_name >> stu_id >> stu_sex >> stu_age;
		strcpy(q->name,stu_name);
		strcpy(q->id,stu_id);
		q->age = stu_age;
		q->sex = stu_sex;
		
		q->next = p->next;
		p->next = q;
		
		return true;
	}
}
//          
bool Student::Delete(char *stuID)
{
	node *p,*q;
	p = head;
	q = p->next;
	//               ,          ,     p,q 
	while(strcmp(q->id,stuID) != 0 && q->next != NULL)
	{
		p = p->next;
		q = q->next;
	}
	//                   ,     .       false   
	//           . 
	if(strcmp(q->id,stuID) != 0 && q->next == NULL)
	//return false;
	throw 1;
	//                
	else
	{
		p->next = q->next;
		delete q;
		q = 0;
		return true;
	}
}
//     
int main()
{
	//          N   ,    ,            ,    .  . 
	freopen("data.txt","r",stdin);
	Student stu;
	stu.Create(2);
	stu.Display();
	try
	{
	stu.Insert("0910312101");
	stu.Display();
	cout << "After delete..." << endl;
	stu.Delete("0910312103");
	stu.Display();
	stu.Delete("0910312104");
	stu.Display();
	}
	
	catch(char *&err)
	{
		cout << err << endl;
	}
	catch(...)
	{
		cout << "unknown error." << endl;
	}
	
	fclose(stdin);
	return 0;
}