前言:
这是我在大一时候与室友写得学生信息管理系统,现在倒腾出来差不多都忘记了。发给大家瞧瞧。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
//控制台句柄
HANDLE hOut;
/*获取光标坐标*/
void gotoxy(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
/*设置颜色*/
int color(int c)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
return 0;
}
/*链表——管理员*/
struct admins
{
char account[20];
char cipher[20];
struct admins* admin_next;
};
/*链表—学生信息*/
struct students
{
char number[20]; //学号 2018060804
char name[20]; //姓名 柴某某
struct students* next;
};
/*函数预处理*/
void face();
struct students* Create();
struct admins *Admin_create();
void input(struct students *head);
void Admin_Login(struct admins* head);
struct students *Delete(struct students *head);
struct students *insert(struct students *head);
struct students *inquire(struct students *head);
/*全局变量*/
int Admin_count, count = 0;
/**********************************************************************/
/********************** 主 函 数 *********************************/
/**********************************************************************/
main()
{
struct students* user_Head;
face();
user_Head = Create(); //学生信息初次创建
input(user_Head); //学生信息输出
user_Head = insert(user_Head); //插入数据
input(user_Head);
user_Head = Delete(user_Head); //删除数据
input(user_Head);
user_Head = inquire(user_Head);//查询数据
system("pause");
}
/*头标题*/
void face()
{
int i;
for (i = 30; i < 80; i++)
{
color(9);
gotoxy(i, 3);
printf("-");
gotoxy(i, 7);
printf("-");
}
for (i = 3; i < 8; i++)
{
color(9);
gotoxy(30, i);
printf("|");
gotoxy(79, i);
printf("|");
}
color(10);
gotoxy(40, 5);
printf(" 学 生 信 息 管 理 系 统\n");
}
/*读取输入的账户*/
struct put
{
char d[20];
char mima[20];
};
/*创建链表*/
struct students* Create()
{
int count_s;
struct students *head = NULL;
struct students *new, *end;
new = end = (struct students*)malloc(sizeof(struct students));
printf("要录入的人数\n");
scanf("%d", &count_s);
printf("存储格式\n学号 姓名\n");
for (; count < count_s;)
{
count++;
printf("第%d个人\n", count);
scanf("%s", &new->number);
scanf("%s", &new->name);
if (count == 1)
{
new->next = head;
end = new;
head = new;
}
else
{
new->next = NULL;
end->next = new;
end = new;
}
new = (struct students*)malloc(sizeof(struct students));
}
free(new);
printf("总共有%d个学生\n", count);
return head;
}
/*输出信息*/
void input(struct students *head)
{
struct students* tep;
tep = head;
printf("当前的管理员名单\n学号\t姓名\n");
while (tep != NULL)
{
printf("%s\t", tep->number);
printf("%s\n", tep->name);
tep = tep->next;
}
free(tep);
}
/*管理员登录*/
void Admin_Login(struct admins* head)
{
struct admins* temp;
struct put* p;
struct put shuru;
int i;
for (i = 0;; i++)
{
int x;
int i;
temp = head;
face();
gotoxy(40, 16);
printf("登录账户");
gotoxy(40, 9);
printf("管理员账户:");
gotoxy(40, 11);
printf("管理员密码:");
gotoxy(53, 9);
scanf("%s", shuru.d);
gotoxy(53, 11);
scanf("%s", shuru.mima);
p = &shuru;
printf("%s\n%s\n", temp->account, temp->cipher);
Sleep(2000);
for (i = 0; (temp->admin_next) == NULL;)//输入检索
{
if (strcmp(temp->account, p->d))
{
gotoxy(53, 12);
printf("\n此账户不存在\n");
}
else
{
if (strcmp(temp->cipher, p->mima))
{
gotoxy(53, 12);
printf("\n密码错误\n");
}
else
{
gotoxy(53, 12);
printf("\n登录成功,请稍后\n");
Sleep(2000);
system("cls");
x = 1;
break;
}
}
temp = temp->admin_next;
}
if (x == 1)
{
break;
}
}
}
/*创建一个新的管理员账户*/
struct admins *Admin_create()
{
struct admins *head;
struct admins *new, *end;
new = end = (struct admins*)malloc(sizeof(struct admins));
face();
head = NULL;
gotoxy(40, 16);
printf("创建账户");
gotoxy(40, 9);
printf("管理员账户:");
gotoxy(40, 11);
printf("管理员密码:");
gotoxy(53, 9);
scanf("%s", &new->account);
gotoxy(53, 11);
scanf("%s", &new->cipher);
Admin_count++;
if (Admin_count == 1)
{
new->admin_next = head;
end = new;
head = new;
}
else
{
new->admin_next = NULL;
end->admin_next = new;
end = new;
}
new = (struct admins*)malloc(sizeof(struct admins));
free(new);
system("cls");
return head;
}
/*插入信息*/
struct students *insert(struct students *head)
{
int x, right;
int i;
struct students *per, *tep, *new;
tep = head;
per = tep;
//printf("请选择插入方式\n1.头插\n2.尾插\n选择位置插入\n");
//scanf("%d", &x);
new = (struct students *)malloc(sizeof(struct students));
printf("输入插入的信息\n");
scanf("%s %s", &new->number, &new->name);
printf("请选择插入方式\n1.头插\t2.尾插\t3.选择位置插入\n请选择:");
scanf("%d", &x);
switch (x)
{
case 1:
one:
new->next = tep;
head = new;
right = 1; break;
case 2:
while (tep->next != NULL)
{
tep = tep->next;
}
tep->next = new;
new->next = NULL;
break;
case 3:
printf("选择你插入的位置\n");
scanf("%d", &x);
if (x == 1)
{
goto one;
}
for (i = 1; i < x; i++)
{
per = tep;
tep = tep->next;
}
new->next = tep;
per->next = new;
break;
default:
break;
}
return head;
}
/*删除信息*/
struct students * Delete(struct students *head)
{
struct students *per, *tep;
int adtion;
int i;
tep = head;
per = tep;
printf("请输入要删除的数据位置\n");
scanf("%d", &adtion);
if (adtion == 1) {
tep = tep->next;
head = tep;
}
else {
for (i = 1; i < adtion; i++)
{
per = tep;
tep = tep->next;
}
per->next = tep->next;
free(tep);
}
return head;
}
//学号查询
struct students *inquire(struct students *head)
{
struct students *p;
char str_number[10];
char str_name[20];
int i;
p = head;
printf("------------------\n");
printf("-----1.按学号查询-----\n");
printf("-----2.按姓名查询-----\n");
printf("-----3.退出本菜单-----\n");
printf("------------------\n");
while (1)
{
printf("请选择字菜单编号");
scanf("%d", &i);
switch (i)
{
case 1:
printf("请输入您要查询的学号:");
scanf("%s", str_number);
do {
if (strcmp(p->number, str_number) == 0)
{
printf("姓名 学号\n");
printf(" %s %s", p->number, p->name);
printf("\n\n");
free(p);
return head;
}
p = p->next;
} while (p!=NULL);
printf("数据库中没有存储您要查询的数据!\n");
printf("\n\n");
break;
case 2:
printf("请输入你要查询的姓名");
scanf("%s", str_name);
do {
if (strcmp(p->name, str_name) == 0)
{
printf("姓名 学号\n");
printf(" %s %s", p->number, p->name);
printf("\n\n");
free(p);
return head;
}
p = p->next;
} while (p!=NULL);
printf("数据库中没有存储您要查询的数据!\n");
printf("\n\n");
break;
case 3:return 0;
default:printf("请在1-3之间选择\n");
}
}
}