程序实例
①用类模板实现学生成绩管理系统👇
将要操作的所有对象构成一个链表,链表中的每个结点元素就是一个对象。定义一个类模板Linklist,数据成员*head表示指向链表的头指针,链表中每个结点元素包含数据域data和指针域next,数据域data是T类型,指针next指向链表中下一个结点元素。
成员函数Inser_Linklist表示插入一个结点元素;成员函数Get_Linklist表示返回第i个结点元素的地址;成员函数Del_Linklist表示删除第i个结点元素;成员函数Print_Linklist表示输出链表中结点元素的值。
#include <iostream>
using namespace std;
template <class T>
struct Node
{
T data;
Node *next;
};
template <class T>
class Linklist
{
private:
Node<T> *head;
Node<T> *r;
public:
Linklist();
int Insert_linklist();
Node<T>* Get_linklist(int i);
int Del_linklist(int i);
void Print_linklist();
~Linklist();
};
class Stu
{
private:
long no;
char name[10];
float score;
public:
Stu();
void Print();
};
template <class T>
Linklist<T>::Linklist()
{
head = NULL;
r = head;
}
template <class T>
int Linklist<T>::Insert_linklist()
{
Node<T> *s = new Node<T>;
if(s)
{
if(head)
{
r->next = s;
r = s;
}
else
{
head = s;
r = s;
}
}
if(r)
r->next = NULL;
return 1;
}
template <class T>
void Linklist<T>::Print_linklist()
{
Node<T> *p;
p = head;
while(p)
{
p->data.Print();
p = p->next;
}
cout <<endl;
}
template <class T>
Node<T> * Linklist<T>::Get_linklist(int i)
{
Node<T> *p = head;
int j = 1;
while(p != NULL && j < i)
{
p = p->next;
j++;
}
if(j == i)
return p;
else
return NULL;
}
template <class T>
int Linklist<T>::Del_linklist(int i)
{
Node<T> *p, *s;
if(i == 1)
{
p = head;
head = head->next;
delete p;
return 1;
}
else
{
p = Get_linklist(i-1);
if(p == NULL)
{
cout << "Error!" <<endl;
return -1;
}
else if(p->next == NULL)
{
cout << "NULL!" <<endl;
return 1;
}
else
{
s = p->next;
p->next = s->next;
if(!p->next)
r = p;
delete s;
return 1;
}
}
}
template <class T>
Linklist<T>::~Linklist()
{
delete head;
}
void menu()
{
cout <<endl;
cout << "1......Insert:" <<endl;
cout << "2......Delete:" <<endl;
cout << "3......Display:" <<endl;
cout << "4......Exit:" <<endl;
cout << "Choice:" <<endl;
}
Stu::Stu()
{
cout << "Input number,name,score:" <<endl;
cin >>no>>name>>score;
}
void Stu::Print()
{
cout << no << " " << name << " " << score <<endl;
}
int main()
{
Linklist<Stu> L;
int n,m = 1;
while(m)
{
menu();
cin >> n;
switch(n)
{
case 1:
{
int success;
success = L.Insert_linklist();
if(success == 1)
cout << "Insert successfully" <<endl;
else
cout << "Insert failure" <<endl;
break;
}
case 2:
{
int i,success;
cout << "Input sitution" <<endl;
cin >> i;
success = L.Del_linklist(i);
if(success == 1)
cout << "Delete successfully" <<endl;
else
cout << "Delete failure" <<endl;
break;
}
case 3:
{
cout << "Infomation:" <<endl;
L.Print_linklist();
break;
}
case 0: m = 0;
}
}
return 0;
}
类Stu是类模板要实例化的一个具体类
②用类模板实现银行办公系统👇
#include <iostream>
using namespace std;
template <class T>
struct Qnode
{
T data;
Qnode *next;
};
template <class T>
class Lqueue
{
private:
Qnode<T> *front,*rear;
public:
Lqueue();
void In_lqueue();
int Empty_lqueue();
int Out_lqueue();
void Print_lqueue();
~Lqueue();
};
class Customer
{
private:
int account;
int amount;
public:
Customer();
void Print();
};
template <class T>
Lqueue<T>::Lqueue()
{
rear = NULL;
front = NULL ;
}
template <class T>
void Lqueue<T>::Print_lqueue()
{
Qnode<T> *p;
p = front;
while(p != NULL)
{
p->data.Print();
p = p->next;
}
cout << endl <<endl;
}
template <class T>
void Lqueue<T>::In_lqueue()
{
Qnode<T> *p;
p = new Qnode<T>;
p->next = NULL;
if(front == 0)
{
front = p;
rear = p;
}
else
{
rear->next = p;
rear = p;
}
}
template <class T>
int Lqueue<T>::Empty_lqueue()
{
if(front == NULL && rear == NULL)
return 1;
else
return 0;
}
template <class T>
int Lqueue<T>::Out_lqueue()
{
Qnode<T> *p;
if(Empty_lqueue() == 1)
return 0;
else
{
p = front;
front = p->next;
delete p;
if(front == NULL)
rear = front;
return 1;
}
}
template <class T>
Lqueue<T>::~Lqueue()
{
delete rear;
}
void menu()
{
cout <<endl;
cout << "1......Push" <<endl;
cout << "2......Show" <<endl;
cout << "3......Pop" <<endl;
cout << "0......Exit" <<endl;
cout << "Choice:" <<endl;
}
Customer::Customer()
{
cout << "Input account and amount:" <<endl;
cin >>account>>amount;
}
void Customer::Print()
{
cout << account << " " << amount <<endl;
}
int main()
{
Lqueue<Customer> L;
int n,m = 1;
while(m)
{
menu();
cin >> n;
switch(n)
{
case 1:
{
L.In_lqueue();
cout << endl << "Element of Queue:" <<endl;
L.Print_lqueue();
break;
}
case 2:
{
int flag;
flag = L.Empty_lqueue();
if(flag != 1)
{
cout << "Element of Queue:" <<endl;
L.Print_lqueue();
}
else
cout << "NULL" <<endl;
break;
}
case 3:
{
int flag;
flag = L.Out_lqueue();
if(flag == 1)
{
cout << endl << "Element of Queue:" <<endl;
L.Print_lqueue();
}
else
cout << "Empty!" <<endl;
break;
}
case 0: m = 0;
}
}
return 0;
}
类Customer是类模板Lqueue所实例化的一个具体类