要求:
1.该程序以链表的形式存储信息;
2.系统实现添加,查找,修改,删除,遍历操作的基本功能;
总结:
1.最大的问题没有建立一个管理系统的类,把各个功能当做管理类的成员函数,通过调用函数来操作,有待进一步改进!!!!
//************************************学生信息管理系统********************
#include
#include
#include
#include
#include
#include
using namespace std;
class stu
{
public:
stu (); //构造函数
void show(); //显示学生信息
char num[20];
char name[20];
char score[10];
public:
stu *next;
static int Count ; //记录创建个数
int w ; //存储创建次序
};
int stu :: Count = 1 ;
stu::stu()
{
cout<<"请输入第"<"个学生的信息:"<cout<<" 姓名 =";
cin>>name;
cout<<" 学号 = ";
cin>>num;
cout<<" 成绩 =";
cin>>score;
w = Count;
Count++;
}
void stu::show()
{
cout<<"第"<"个学生的信息为:"<cout<<" 姓名 ="<" 学号 ="<" 成绩 ="<//------------------------创建输入第一个信息----------------------------------------
stu *create()
{
stu *head, *tail, *New;
cout<<"按任意键开始输入:"<new stu;
head = tail = New;
tail->next = 0;
system("cls");
cout<<"录入完成!!!按任意键进入系统···"<"cls");
return head;
}
//-------------------------查找修改模块-------------------------------------------------------------
stu *chazhao(stu *head)
{
fflush(stdin);
cout<<"按学号查找选:1 姓名查找选:2: "<"输入查找方式 = ";
int chance;
cin>>chance;
switch (chance)
{
case 1:
{ char Num[20];
cout<<"要查找学生的学号为:";
cin>>Num;
stu *p = head;
for (p ; p ; p = p->next)
{
if (strcmp(p->num , Num) == 0)
{
p->show();
cout<cout<<"是否修改数据?Yes 1 No 0"<int chance;
cin>>chance;
if (chance)
{
cout<<"新学号为:";
cin>>p->num;
cout<<"修改姓名为:";
cin>>p->name;
cout<<"修改成绩为:";
cin>>p->score;
cout<<"修改成功!"<cout<<"按任意键继续···"<return head;
}
else
{
return head;
}
}
}
return head;
}
case 2:
{
char Name[20];
cout<<"要查找学生的姓名为:";
cin>>Name;
stu *p = head;
for ( ; p ; p = p->next)
{
if (strcmp(p->name,Name) == 0)
{
p->show();
cout<cout<<"是否修改数据?Yes 1 No 0"<int chance;
cin>>chance;
if (chance)
{
cout<<"新学号为:";
cin>>p->num;
cout<<"修改姓名为:";
cin>>p->name;
cout<<"修改成绩为:";
cin>>p->score;
cout<<"修改成功!"<cout<<"按任意键继续···"<return head;
}
else
{
return head;
}
}
}
return head;
}
}
}
//------------------------------------遍历信息----------------------------
stu *bianli(stu *head)
{
stu *p = head; int i = 1;
for (; p ;p = p->next)
{
p->show();
cout<cout<<"按任意键继续···"<return head;
}
//------------------------------------添加信息---------------------------------
stu *tianjia(stu *head)
{
int flag;
cout<<"插入到第几个位置:";
fflush(stdin);
cin>>flag;
stu *p = head;
if (flag == 1)
{
stu *New = new stu;
New->next = head;
cout<<"添加成功!!"<return New;
}
else
{
int i = 1;
while(i < flag - 1)
{
p = p->next;
++i;
}
stu *New = new stu;
New->next = p->next;
p->next = New;
cout<<"添加成功!!"<cout<<"按任意键继续···"<return head;
}
for ( int i = 1 ;i == (flag - 1); p = p->next,++i)
{
stu *New = new stu;
New->next = p->next;
p->next = New;
cout<<"添加成功!!"<cout<<"按任意键继续···"<return head;
}
}
//----------------------------删除模块---------------------------------
stu *shanchu(stu *head)
{
// system("cls");
fflush(stdin);
cout<<"删除操作:"<"按学号查找选:1 按姓名查找选:2 "<"输入查找方式 = ";
int chance;
cin>>chance;
switch (chance)
{
case 1:
{
char Num[20];
cout<<"要删除学生的学号为:";
cin>>Num;
stu *p = head;
int count = 1;
for (p ; p ;p = p->next, count++)
{
if (strcmp(p->num , Num) == 0)
{
p->show();
cout<cout<<"是否删除数据?Yes 1,No 0"<int chance;
cin>>chance;
if (chance)
{
if (p->next == 0) //删除的为最后一个节点
{
int i = 1;
p= head;
while (i < count - 1)
{
p = p->next;
i++;
}
// delete p->next;
p->next = NULL;
stu::Count--;
cout<<"删除成功!最后一个"<cout<<"按任意键继续···"<return head;
}
else if (p == head) //删除的第一个节点
{
stu *node = p->next;
delete p;
stu::Count--;
cout<<"删除成功!第一个节点"<cout<<"按任意键继续···"<return node;
}
else //删除中间节点
{
stu *node = p->next;
strcpy(p->num ,node->num);
strcpy(p->name ,node->name);
strcpy(p->score,node->score);
p->w =node->w;
p->next = node->next;
delete node;
stu::Count--;
cout<<"删除成功!中间节点"<cout<<"按任意键继续···"<return head;
}
}
else
{
return head;
}
}
}
return head;
}
case 2:
{
char Name[20];
cout<<"要删除学生的学生姓名为:";
cin>>Name;
stu *p = head;
int count = 1;
for (p ; p ; p = p->next, count++)
{
if (strcmp(p->name ,Name) == 0)
{
p->show();
cout<cout<<"是否删除数据?Yes 1,No 0"<int chance;
cin>>chance;
if (chance)
{
if (p->next == 0) //删除的为最后一个节点
{
int i = 1;
p= head;
while (i < count - 1)
{
p = p->next;
i++;
}
// delete p->next;
p->next = NULL;
stu::Count--;
cout<<"删除成功!最后一个"<cout<<"按任意键继续···"<return head;
}
else if (p == head) //删除的第一个节点
{
stu *node = p->next;
delete p;
stu::Count--;
cout<<"删除成功!第一个节点"<cout<<"按任意键继续···"<return node;
}
else //删除中间节点
{
stu *node = p->next;
strcpy(p->num ,node->num);
strcpy(p->name ,node->name);
strcpy(p->score,node->score);
p->next = node->next;
delete node;
stu::Count--;
cout<<"删除成功!中间节点"<cout<<"按任意键继续···"<return head;
}
}
else
{
return head;
}
}
}
return head;
}
}
}
//-----------------------------主函数-------------------------------------
int main()
{
stu *Head ;
int begin;
cout<<" "
<<"欢迎进入新系统"<"按任意键进入···"<int y = getch();
system("cls");
Head = create();
int x;
x = getchar();
while(x != 0)
{
cout<<" "<<"菜单"<" 1.查询修改 2.添加信息 "<" 3.删除信息 4.遍历学生信息 "<" 5.统计学生总个数 6.退出系统"<" 选择操作: ";
cin>>x;
fflush(stdin);
switch(x)
{
case 1:
Head = chazhao(Head);
break;
case 2:
Head = tianjia(Head);
break;
case 3:
Head = shanchu(Head);
break;
case 4:
Head = bianli(Head);
break;
case 5:
cout<<"学生个数为:"<1<break;
case 6:
return 0;
}
}
system("pause");
}