* Copyright (c++) 2016 烟台大学计算机与控制工程学院
* All right reserved.
* 文件名称:stu.cpp
* 作 者: 王雪松
* 完成日期:2016年12月31日
* 版 本 号:vc6.0
源文件
stu.h
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
# include "conio.h"
# define N sizeof(struct student) //测结构体变量的大小//
typedef struct student
{
char number[20];
char name[20];
char sex[20];
char Tel[20];
char college[20];
char add[20];
int score;//高考成绩
struct student *next;
} stu; //为结构体命名//
stu *creatlink( stu * &head); //创建链表信息//
stu *delet(stu *p0); //删除信息//
stu *modify(stu *h); //修改信息//
void findNum(stu *h); //查询信息//
void findName(stu *h); //查询信息//
void print(stu *p); //显示全部信息//
stu *sort(stu *head); //冒泡排序
int MainMenu();
int FindMenu();
int ManageMenu();
int StudentMenu();
void findNum(stu *h);
void findName(stu *h);
stu.cpp
#include <malloc.h>
#include "stu.h"
stu *creatlink( stu * &head) //尾插法创建链表
{
int score;
stu *p1, *p2;
int i = 1;
char choice1;
head = p2 = (stu *)malloc(N); //p2始终指向尾节点,开始时指向头结点
head->next = NULL;
choice1 = 'y';
for (i = 1; choice1 == 'y' || choice1 == 'Y'; i++)
{
p1 = (stu *)malloc(N);
printf("*******************************************************************************\n\n");
printf("请输入第%d个学生的信息:\n", i);
printf("学号:");
scanf("%s", p1->number);
printf("\n姓名:");
scanf("%s", p1->name);
printf("\n性别:");
scanf("%s", p1->sex);
printf("\n手机号:");
scanf("%s", p1->Tel);
printf("\n学院:");
scanf("%s", p1->college);
printf("\n籍贯:");
scanf("%s", p1->add);
printf("\n高考成绩:");
scanf("%d", &score);
p1->score= score;
p2->next = p1; // 将*p1插入*p2后
p2 = p1;
printf("\n是否继续?(Y/N):");
choice1 = getch();
printf("\n");
}
p2->next = NULL;
return(head);
}
stu *delet(stu *head) //删除信息
{
char num[20];
stu *p=head ,*q=p->next ;
char choice2;
printf("\n请输入要删除学生的学号:");
scanf("%s", num);
while (strcmp(q->number, num) != 0&& q!= NULL ) //找到要删除的学生的信息
{
p=q;
q= q->next;
}
if(q==NULL) //未找到该信息
{
printf("输入错误:\n");
printf("\n按任意键结束!");
getch();
}
else
{
//q=p->next;
printf("你要删除的信息如下,请确认是否删除:\n");
printf("\n学号:%s 姓名:%s 性别:%s 手机号:%s 学院:%s 籍贯:%s\n\n", q->number, q->name, q->sex, q->Tel, q->college, q->add);
printf("\n是Y,否N:"); //显示要删除的学生的信息//
choice2 = getch();
if (choice2 == 'Y' || choice2 == 'y')
{
p->next = q->next;
free(q);
printf("\n删除成功!");
}
printf("\n按任意键结束!");
getch();
return head;
}
}
stu *modify(stu *h) //修改信息
{
int score;
char num[20];
stu *p;
char choice;
printf("\n请输入要修改学生信息的学号:");
scanf("%s", num);
for (p = h; strcmp(p->number, num) != 0; p = p->next); //找到要删除的学生的信息
while (1)
{
printf("请输入修改后学生的信息:\n");
printf("学号:");
scanf("%s", p->number);
printf("\n姓名:");
scanf("%s", p->name);
printf("\n性别:");
scanf("%s", p->sex);
printf("\n手机号:");
scanf("%s", p->Tel);
printf("\n学院:");
scanf("%s", p->college);
printf("\n籍贯:");
scanf("%s", p->add);
printf("\n高考成绩:");
scanf("%d", &score);
p->score= score;
printf("修改后学生信息如下:\n");
printf("\n学号:%s 姓名:%s 性别:%s 手机号:%s 学院:%s 籍贯:%s 高考成绩:%d\n\n", p->number, p->name, p->sex, p->Tel, p->college, p->add,p->score);
printf("\n确认?是Y否N:");
choice = getch();
if (choice == 'y' || choice == 'Y') break;
}
printf("\n修改成功,按任意键继续!");
getch();
return h;
}
void findNum(stu *h) //按学号查询信息
{
char num[20];
stu *p;
printf("\n请输入你要查找的学生的学号:");
scanf("%s", num);
for (p = h; strcmp(p->number, num) != 0; p = p->next);
printf("你查找的学生信息如下:\n");
printf("\n学号:%s 姓名:%s 性别:%s 手机号:%s 学院:%s 籍贯:%s 高考成绩:%d\n\n", p->number, p->name, p->sex, p->Tel, p->college, p->add, p->score);
printf("按任意键继续!");
getch();
}
void findName(stu *h) //按姓名查询信息
{
char name[20];
stu *p;
printf("\n请输入你要查找的学生的姓名:");
scanf("%s", name);
for (p = h; strcmp(p->name, name) != 0; p = p->next);
printf("你查找的学生信息如下:\n");
printf("\n学号:%s 姓名:%s 性别:%s 手机号:%s 学院:%s 籍贯:%s 高考成绩:%d\n\n", p->number, p->name, p->sex, p->Tel, p->college, p->add, p->score);
printf("按任意键继续!");
getch();
}
void print(stu *p) //显示全部信息
{
p=p->next;
while (p != NULL)
{
printf("\n学号%s 姓名%s 性别%s 手机号%s college%s 籍贯%s 高考成绩%d\n", p->number, p->name, p->sex, p->Tel, p->college, p->add,p->score);
p = p->next;
}
printf("\n按任意键继续!");
getch();
}
stu *sort(stu *head) //按高考成绩冒泡排序
{
stu* p1;
stu* p2;
char number[20];
char name[20];
char sex[20];
char Tel[20];
char college[20];
char add[20];
int score;//高考成绩
p1 = head;
while (p1!=NULL)
{
p2 = p1->next;
while (p2!=NULL)
{
if (p2->score<p1->score)
{
//完成数值交换
strcpy(number, p2->number);
strcpy(name, p2->name);
strcpy(sex, p2->sex);
strcpy(Tel, p2->Tel);
strcpy(college, p2->college);
strcpy(add, p2->add);
score= p2->score;
strcpy(p2->number, p1->number);
strcpy(p2->name, p1->name);
strcpy(p2->sex, p1->sex);
strcpy(p2->Tel, p1->Tel);
strcpy(p2->college, p1->college);
strcpy(p2->add, p1->add);
p2->score= p1->score;
strcpy(p1->number, number);
strcpy(p1->name, name);
strcpy(p1->sex, sex);
strcpy(p1->Tel, Tel);
strcpy(p1->college, college);
strcpy(p1->add, add);
p1->score= score;
}
p2 = p2->next;//指向下一个节点
}
p1 = p1->next;//指向下一个节点
}
print(head);
printf("学生信息排序完毕,按任意键继续!");
getch();
return(head); /*返回头指针*/
}
//============================系统主菜单==========================
int MainMenu()
{
int ok;
system("cls");
printf("=========================================================================\n");
printf(" 欢迎来到学生信息管理系统主界面! \n");
printf("=========================================================================\n");
printf(" 1.管理员 \n");
printf(" 2.学生 \n");
printf(" 0.退出 \n");
scanf("%d", &ok);
return ok;
}
//============================查询主菜单==========================
int FindMenu()
{
int ok;
system("cls");
printf("=========================================================================\n");
printf(" 1.按学号 \n");
printf(" 2.按姓名 \n");
printf(" 0.退出 \n");
scanf("%d", &ok);
return ok;
}
//============================管理员菜单==========================
int ManageMenu()
{
int choice;
system("cls");
printf("=========================================================================\n");
printf(" 1. 添加学生信息 \n");
printf(" 2. 删除学生信息 \n");
printf(" 3. 修改学生信息 \n");
printf(" 4. 查询学生信息 \n");
printf(" 5. 排序显示信息 \n");
printf(" 0. 返 回 \n");
printf("请输入你要执行的操作代码:");
scanf("%d", &choice);
return choice;
}
//============================管理员菜单==========================
int StudentMenu()
{
int choice;
system("cls");
printf("=========================================================================\n");
printf(" 1. 查询学生信息 \n");
printf(" 0. 返 回 \n");
printf("请输入你要执行的操作代码:");
scanf("%d", &choice);
return choice;
}
main.cpp
#include"stu.h"
int main()
{
int nChoose1;
int nChoose2;
int nChoose3;
int nChoose4;
int nEnd = 1;
int nEnd2 = 1;
int nEnd3 = 1;
char sName[10];
char sPassword[10];
int i; //choice用于选择对数据执行操作,i循环,ok主界面选择//
stu *head;
head = NULL;
while (nEnd)
{
nChoose1 = MainMenu();
switch (nChoose1)
{
case 0:
nEnd = 0;
break;
case 1:
printf("=========================================================================\n");
printf("请输入账号:\n");
scanf("%s", sName);
printf("=========================================================================\n");
printf("请输入密码:\n");
scanf("%s", sPassword);
if (strcmp(sName,"admin")!=0)
{
printf("账号错误!\n");
}
else
{
if (strcmp(sPassword,"123456")!=0)
{
printf("密码错误!\n");
}
else
{
nEnd2 = 1;
while (nEnd2)
{
nChoose2 = ManageMenu();
switch (nChoose2)
{
case 1:
head = creatlink(head);
break;
case 2:
head = delet(head);
break;
case 3:
head = modify(head);
break;
case 4:
nChoose4 = FindMenu();
switch (nChoose4)
{
case 1:
findNum(head);
break;
case 2:
findName(head);
break;
default:
printf("=========================================================================\n");
printf(" 错误的选项号 \n");
printf("=========================================================================\n");
break;
}
break;
case 5:
head = sort(head);
break;
case 0:
nEnd2 = 0;
break;
default:
printf("=========================================================================\n");
printf(" 错误的选项号 \n");
printf("=========================================================================\n");
break;
}
}
}
}
break;
case 2:
nEnd3 = 1;
while (nEnd3)
{
nChoose3 = StudentMenu();
switch (nChoose3)
{
case 0:
nEnd3 = 0;
break;
case 1:
nChoose4 = FindMenu();
switch (nChoose4)
{
case 1:
findNum(head);
break;
case 2:
findName(head);
break;
default:
printf("=========================================================================\n");
printf(" 错误的选项号 \n");
printf("=========================================================================\n");
break;
}
break;
default:
printf("=========================================================================\n");
printf(" 错误的选项号 \n");
printf("=========================================================================\n");
break;
}
}
break;
default:
printf("=========================================================================\n");
printf(" 错误的选项号 \n");
printf("=========================================================================\n");
break;
}
}
}
运行结果
主菜单
管理员菜单
学生菜单
添加学生信息
删除学生信息
修改学生信息
查询菜单
按学号查找
按姓名查找
按高考成绩排序