您的位置:首页 > 其它

简单实现了一个学生成绩信息管理系统

2015-08-11 19:04 1276 查看
要求:

1.该程序以链表的形式存储信息;

2.系统实现添加,查找,修改,删除,遍历操作的基本功能;

总结:

1.最大的问题没有建立一个管理系统的类,把各个功能当做管理类的成员函数,通过调用函数来操作,有待进一步改进!!!!

//************************************学生信息管理系统********************

#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<fstream>
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(
4000
)
{
cout<<"请输入第"<<Count<<"个学生的信息:"<<endl;
cout<<"              姓名 =";
cin>>name;
cout<<"              学号 = ";
cin>>num;
cout<<"              成绩 =";
cin>>score;
w = Count;
Count++;

}

void stu::show()
{
cout<<"第"<<w<<"个学生的信息为:"<<endl;
cout<<"              姓名 ="<<name<<endl
<<"              学号 ="<<num<<endl
<<"              成绩 ="<<score<<endl;

}

//------------------------创建输入第一个信息----------------------------------------

stu *create()
{
stu *head, *tail, *New;

cout<<"按任意键开始输入:"<<endl;
getch();

New = new stu;
head = tail = New;
tail->next = 0;
system("cls");
cout<<"录入完成!!!按任意键进入系统···"<<endl;
getch();
system("cls");
return head;

}

//-------------------------查找修改模块-------------------------------------------------------------
stu *chazhao(stu *head)
{

fflush(stdin);
cout<<"按学号查找选:1  姓名查找选:2: "<<endl
<<"输入查找方式 = ";
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<<endl;

cout<<"是否修改数据?Yes 1 No 0"<<endl;

int chance;
cin>>chance;

if (chance)
{
cout<<"新学号为:";
cin>>p->num;
cout<<"修改姓名为:";
cin>>p->name;
cout<<"修改成绩为:";
cin>>p->score;
cout<<"修改成功!"<<endl;
cout<<"按任意键继续···"<<endl;
getch();
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<<endl;

cout<<"是否修改数据?Yes 1 No 0"<<endl;

int chance;
cin>>chance;

if (chance)
{
cout<<"新学号为:";
cin>>p->num;
cout<<"修改姓名为:";
cin>>p->name;
cout<<"修改成绩为:";
cin>>p->score;
cout<<"修改成功!"<<endl;
cout<<"按任意键继续···"<<endl;
getch();
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<<endl;
}
cout<<"按任意键继续···"<<endl;
getch();
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<<"添加成功!!"<<endl;
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<<"添加成功!!"<<endl;
cout<<"按任意键继续···"<<endl;
getch();
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<<"添加成功!!"<<endl;
cout<<"按任意键继续···"<<endl;
getch();
return head;

}

}

//----------------------------删除模块---------------------------------

stu *shanchu(stu *head)
{
//  system("cls");
fflush(stdin);
cout<<"删除操作:"<<endl
<<"按学号查找选:1  按姓名查找选:2 "<<endl
<<"输入查找方式 = ";
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<<endl;

cout<<"是否删除数据?Yes 1,No 0"<<endl;

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<<"删除成功!最后一个"<<endl;
cout<<"按任意键继续···"<<endl;
getch();
return head;
}
else if (p == head)         //删除的第一个节点
{
stu *node = p->next;
delete p;
stu::Count--;
cout<<"删除成功!第一个节点"<<endl;
cout<<"按任意键继续···"<<endl;
getch();
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<<"删除成功!中间节点"<<endl;
cout<<"按任意键继续···"<<endl;
getch();
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<<endl;

cout<<"是否删除数据?Yes 1,No 0"<<endl;

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<<"删除成功!最后一个"<<endl;
cout<<"按任意键继续···"<<endl;
getch();
return head;
}
else if (p == head)         //删除的第一个节点
{
stu *node = p->next;
delete p;
stu::Count--;
cout<<"删除成功!第一个节点"<<endl;
cout<<"按任意键继续···"<<endl;
getch();
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<<"删除成功!中间节点"<<endl;
cout<<"按任意键继续···"<<endl;
getch();
return head;

}
}
else
{
return head;
}
}
}
return head;
}
}
}

//-----------------------------主函数-------------------------------------
int main()
{
stu *Head ;
int begin;
cout<<"                              "
<<"欢迎进入新系统"<<endl<<"按任意键进入···"<<endl;
int y = getch();
system("cls");
Head = create();

int x;
x = getchar();
while(x != 0)
{

cout<<"                                      "<<"菜单"<<endl
<<"                           1.查询修改          2.添加信息 "<<endl
<<"                           3.删除信息          4.遍历学生信息 "<<endl
<<"                           5.统计学生总个数    6.退出系统"<<endl
<<"                                  选择操作: ";

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<<"学生个数为:"<<stu::Count-1<<endl;
break;
case 6:
return 0;

}
}
system("pause");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  函数 管理 链表