C语言学生信息管理系统项目源码

时间:2023-03-08 16:23:39
C语言学生信息管理系统项目源码

#include
  //包含printf()、scanf()、gets()、puts()、getchar()函数

#include   //包含malloc()函数

#include
  //包含strcmp()、strlen()、sizeof()、strcpy()函数

#include  //包含system()、sleep()函数

#include   //包含exit()函数

#define arrSize 256

#define sleepTime 50

void countBackwards() 

{

 for (int i=3; i>=0; )

 {

  //\r表示光标重新到达行首

  printf("\r离程序退出还有%d秒",
i--);

  Sleep(sleepTime);

 }

 //函数声明

 void exitPrograms(); 

 exitPrograms();

}

void fullScreen()

{

 HWND hwnd=GetForegroundWindow();

 int x=GetSystemMetrics(SM_CXSCREEN)+300,
y=GetSystemMetrics(SM_CYSCREEN)+300;

 char setting[30];

 //设置控制台行列数

 sprintf(setting, "mode con:cols=%d lines=%d", x,
y); 

 system(setting);

 //置顶

 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, x+200,
y+200, NULL); 

 //移动

 MoveWindow(hwnd, -5, -20, x+200, y+200,
1); 

 printf("\n\n");

}

void wait()

{

 for (int i=0; i < 24; i++)

 {

  Sleep(sleepTime);printf(".");

}

 puts("\n");

}

void hello()

{

 for (int i=0; i < 5; i++)

 {

  Sleep(sleepTime);printf("X");

}

Sleep(sleepTime);printf(" ");

 Sleep(sleepTime);printf("欢");

 Sleep(sleepTime);printf("迎");

 Sleep(sleepTime);printf("来");

 Sleep(sleepTime);printf("到");

 Sleep(sleepTime);printf("学");

 Sleep(sleepTime);printf("生");

 Sleep(sleepTime);printf("信");

 Sleep(sleepTime);printf("息");

 Sleep(sleepTime);printf("管");

 Sleep(sleepTime);printf("理");

 Sleep(sleepTime);printf("系");

 Sleep(sleepTime);printf("统");

 Sleep(sleepTime);printf(" ");

for (int k=0; k < 5; k++)

 {

  Sleep(sleepTime);printf("X");

}

 puts("\n");

}

void checkUsernamePassword()

{

 char username[] = "xinke",
username0[arrSize];

 char password[] = "2016",
password0[arrSize];

 

 for(int i=1; i<4; i++)

 {

  printf("请输入用户名:");

  gets(username0);

if (strcmp(username, username0)
!= 0)

  {

   puts("用户名错误!");

printf("3次输入错误将退出程序,错误%d次!\n\n",
i);

   if (i ==
3)

   {

    printf("程序即将退出");

wait();

system("cls");

//调用倒计时函数

countBackwards();

}

  }

  else

  {

   i=3;

puts("用户名正确!\n");

for (int a=1;
a<4; a++)

   {

    printf("请输入密码:");

gets(password0);

if
(strcmp(password, password0) != 0)

    {

puts("密码错误!");

printf("3次输入错误将退出程序,错误%d次!\n\n",
a);

     if
(a == 3)

     {

puts("程序即将退出");

wait();

system("cls");

//调用倒计时函数f

countBackwards();

break;

}

}

else

{

a=3;

printf("密码正确,即将进入程序");

wait();

system("cls");

}

}

  }

 }

}

void menu()

{

 puts("::::::请输入您要选择的操作编码::::::");

 puts("1. 添加新学生的信息");

 puts("2. 根据学号查找学生信息功能");

 puts("3. 根据姓名查找学生信息功能");

 puts("4. 根据学号修改学生信息功能");

 puts("5. 根据学号删除学生信息功能");

 puts("6. 根据姓名删除学生信息功能");

 puts("7. 输入0退出");

 puts("::::::::::::::::::::::::::::::::::::");

}

struct student

{

 int
num;    //6位数字且唯一,不能添加相同学号的学生,添加的学生信息要永久保存

char
name[arrSize];  //可以重复

 char
birthday[arrSize]; //出生年月格式为1990-08-23

 char
sex[arrSize];  //性别

 char
adress[arrSize]; //家庭地址

 char
phone[arrSize]; //联系方式为11位电话号码或是0710-9999999的格式

struct student
*next;   

};

static struct student *head = NULL;

struct student *createStudent(int num, char name[arrSize], char
birthday[arrSize], char sex[arrSize], char adress[arrSize], char
phone[arrSize], struct student *next)

{

 struct student *tmp;

 

 tmp = (struct student*)malloc(sizeof(struct
student));

 tmp->num = num;

 strcpy(tmp->name, name);

 strcpy(tmp->birthday, birthday);

 strcpy(tmp->sex, sex);

 strcpy(tmp->adress, adress);

 strcpy(tmp->phone, phone);

tmp->next = next;

return tmp;

}

void addStudent(int num, char name[arrSize], char
birthday[arrSize], char sex[arrSize], char adress[arrSize], char
phone[arrSize])

{

 struct student *tmp = head->next;

 struct student *tmp1 = head;

 struct student *newnode = createStudent(num,
name, birthday, sex, adress, phone, NULL);

 

 while (tmp != NULL)

 {

  if (tmp->num >=
num)

  {

   newnode->next
= tmp;

   tmp1->next
= newnode;

   break;

  }

  tmp1 = tmp1->next;

  tmp = tmp->next;

 }

 if (tmp == NULL)

 {

  tmp1->next = newnode;

 }

}

void findStudentFromNum(int num)

{

 struct student *tmp = head->next;

 int flag1;

 while (tmp != NULL)

 {

  if (tmp->num == num)

  {

   flag1 =
1;

   system("cls");

puts("恭喜您!找到了该学生,信息如下:\n学号\t姓名\t出生日期\t性别\t家庭地址\t联系方式");

printf("%d\t%s\t%s\t%s\t%s\t\t%s\n\n",
tmp->num, tmp->name, tmp->birthday, tmp->sex,
tmp->adress, tmp->phone);

   break;

  }

  tmp = tmp->next;

 } 

 if (flag1 != 1)

 {

  puts("系统中不存在该学生的信息!");

 }

}

void findStudentFromName(char name[arrSize])

{

 struct student *tmp = head->next;

 int flag2;

 while (tmp != NULL)

 {

  if (strcmp(tmp->name, name)
== 0)

  {

   flag2 =
1;

   system("cls");

puts("恭喜您!找到了该学生,信息如下:\n学号\t姓名\t出生日期\t性别\t家庭地址\t联系方式");

printf("%d\t%s\t%s\t%s\t%s\t\t%s\n\n",
tmp->num, tmp->name, tmp->birthday, tmp->sex,
tmp->adress, tmp->phone);

   break;

  }

  tmp = tmp->next;

 } 

 if (flag2 != 1)

 {

  puts("系统中不存在该学生的信息!");

 }

}

void modifyStudentFromNum(int num, char name[arrSize], char
birthday[arrSize], char sex[arrSize], char adress[arrSize], char
phone[arrSize])

{

 struct student *tmp = head->next;

 

 while (tmp != NULL)

 {

  if (tmp->num == num)

  {

   strcpy(tmp->name,
name);

   strcpy(tmp->birthday,
birthday);

   strcpy(tmp->sex,
sex);

   strcpy(tmp->adress,
adress);

   strcpy(tmp->phone,
phone);

   break;

  }

  tmp = tmp->next;

 } 

}

void deleteStudentFromNum(int num)

{

 struct student *tmp = head->next;

 struct student *tmp1 = head;

 

 while (tmp != NULL)

 {

  if (tmp->num == num)

  {

   tmp1->next
= tmp->next;

   free(tmp);

break;

  }

  tmp = tmp->next;

  tmp1 = tmp1->next;

 }

}

void deleteStudentFromName(char name[arrSize])

{

 struct student *tmp = head->next;

 struct student *tmp1 = head;

 

 while (tmp != NULL)

 {

  if (strcmp(tmp->name, name)
== 0)

  {

   tmp1->next
= tmp->next;

   free(tmp);

break;

  }

  tmp = tmp->next;

  tmp1 = tmp1->next;

 }

}

void printList()

{

 struct student *tmp;

 tmp = head->next;

puts("学号\t姓名\t出生日期\t性别\t家庭地址\t联系方式");

 while (tmp)

 {

  printf("%d\t%s\t%s\t%s\t%s\t\t%s\n",
tmp->num, tmp->name, tmp->birthday, tmp->sex,
tmp->adress, tmp->phone);

  tmp = tmp->next;

 }

}

void freeList()

{

 struct student *tmp = head;

 struct student *tmp1;

 

 while (tmp)

 {

  tmp1 = tmp;

  tmp = tmp->next;

  free(tmp1);

 } 

}

void exitPrograms()

{

 system("cls");

 printf("请输入0退出程序:");

 exit(getch());

}

int x;

int checkNumStrlen()  

{

 

 while(1)

 {

  char num[arrSize];

  printf("请输入要添加的学生学号:");

  scanf("%s", num);

  if (strlen(num) ==
6)  

  {

   x =
atoi(num);

   //函数声明

   int
checkNumNoSame(); 

   checkNumNoSame();

return
x;

  }

  else

  { 

   puts("学号必须为6位数!\n");

}

 }

}

int
checkNumNoSame()



 struct student *tmp = head->next;

while(1)

 {

  if (tmp == NULL)

  {

   return
x;

  }

  if (tmp->num == x)

  {

   printf("已存在该学号,请重新输入!\n\n");

checkNumStrlen();

break;

  }

  tmp=tmp->next;

 }

}

int main(void)

{

 fullScreen();    //调用全屏函数

system("color
0C");   //背景为黑色,前景为淡红色

checkUsernamePassword(); //验证用户名和密码

printf("系统正在初始化,请稍候");

 wait();      //稍候效果......

system("cls");

 

 system("color 0A");

 for (int i=50000; i>=0; i--)

 {

  printf("%d", rand());

  Sleep(0.000001);

 }

 system("cls");

hello();    //动态显示欢迎信息

system("color
0A"); //背景为黑色,前景为淡绿色

menu(); //打印菜单栏

 

 struct student *stu1;

 struct student *stu2;

 struct student *stu3;

 struct student *stu4;

 struct student *tmp;

stu1 = createStudent(201601, "Alan",
"1997-09-15", "女", "墨西哥", "13871745572", NULL);

 stu2 = createStudent(201602, "Bobo",
"1994-08-25", "男", "亚马逊", "0710-1452637", NULL);

 stu3 = createStudent(201603, "John",
"1996-02-23", "男", "莫斯科", "13476320757", NULL);

 stu4 = createStudent(201604, "Cici",
"1995-11-12", "女", "太平洋", "0710-4567894", NULL);

 

 

 head = (struct student*)malloc(sizeof(struct
student));

 head->num = -1;

 head->next = NULL;

head->next = stu1;

 stu1->next = stu2;

 stu2->next = stu3;

 stu3->next = stu4;

 stu4->next = NULL;

 

 

 FILE *fp;

 struct student tmps;

 struct student *tmp_head;

 fp = fopen("student.txt", "rt");

 if (fp == NULL)

 {

  perror("fopen");

  return -1;

 }

 tmp_head = head;

 memset(&tmps, 0, sizeof(struct
student));

 while (fread(&tmps, sizeof(struct student),
1, fp) > 0)

 {

  tmp_head->next =
createStudent(tmps.num, tmps.name, tmps.birthday, tmps.sex,
tmps.adress, tmps.phone, NULL);

  tmp_head =
tmp_head->next;

  memset(&tmps, 0,
sizeof(struct student));

 }

 fclose(fp);

  

 int num;

 char temp[arrSize] =
{0}; //用于以字符串形式接收的学号成员转换为int类型的中间变量

 char name[arrSize]={0}, birthday[arrSize]={0},
sex[arrSize]={0}, adress[arrSize]={0}, phone[arrSize]={0};

int choice = 0;

 while(1)

 {

  ret:

  printf("\n请选择1-7:");

  fflush(stdin);

  scanf("%d", &choice);

  fflush(stdin);

  switch(choice)

  {

   case 1:

    puts("请分别输入要添加的学生的信息");

num=checkNumStrlen();

printf("请输入要添加的学生姓名:");

fflush(stdin);

scanf("%s",
name);

ret1: //用于跳转语句1

printf("请输入要添加的学生出生日期:");

scanf("%s",
birthday);

if
((birthday[4] != '-') || (birthday[7] != '-'))

    {

puts("您输入的出生日期格式有误,请按照:1990-09-20的格式输入!\n");

goto
ret1;

    }

ret2: //用于跳转语句2

while
(1)

    {

printf("请输入要添加的学生性别:");

scanf("%s",
sex);

     if
((strcmp(sex, "男") == 0) || (strcmp(sex, "女") == 0))

     {

break;

}

else

{

puts("您输入的性别格式有误,请输入男或女!\n");

goto
ret2;

     }

}

printf("请输入要添加的学生家庭地址:");

scanf("%s",
adress);

ret3: //用于跳转语句3

while
(1)

    {

printf("请输入要添加的学生联系方式:");

scanf("%s",
phone);

     if
((strlen(phone) == 11) || ((strlen(phone) == 12) &&
(phone[4] == '-')))

     {

break;

}

else

{

puts("您输入的联系方式格式有误,请按照:11位数字或者0710-9999999的格式输入!\n");

goto
ret3;

     }

}

system("cls");

printf("系统正在添加学生信息,请稍候");wait();

addStudent(num,
name, birthday, sex, adress, phone);

    Sleep(100);

puts("\n恭喜您!信息添加成功!\n");

printList();

puts("\n");

menu();

break;

   

   case 2:

    printf("请输入要查找的学生学号:");

scanf("%d",
&num);

    system("cls");

printf("系统正在查找学生信息,请稍候");wait();

findStudentFromNum(num);

printf("\n");

printList();

puts("\n");

menu();

break;

case
3:

    printf("请输入要查找的学生姓名:");

scanf("%s",
name);

    system("cls");

printf("系统正在查找学生信息,请稍候");wait();

findStudentFromName(name);

printf("\n");

printList();

puts("\n");

menu();

break;

case
4:

    printf("请输入要修改的学生学号:");

scanf("%d",
&num);

    printf("请输入要修改的学生姓名:");

scanf("%s",
name);

    printf("请输入要修改的学生出生日期:");

scanf("%s",
birthday);

    printf("请输入要修改的学生性别:");

scanf("%s",
sex);

    printf("请输入要修改的学生家庭地址:");

scanf("%s",
adress);

    printf("请输入要修改的学生联系方式:");

scanf("%s",
phone);

    system("cls");

printf("系统正在修改学生信息,请稍候");

wait();

modifyStudentFromNum(num,
name, birthday, sex, adress, phone);

    Sleep(100);

puts("\n恭喜您!信息修改成功!\n");

printList();

puts("\n");

menu();

break;

case
5:

    printf("请输入要删除的学生学号:");

scanf("%d",
&num);

    puts("\n");

system("cls");

printf("系统正在删除学生信息,请稍候");

wait();

deleteStudentFromNum(num);

Sleep(100);

puts("\n恭喜您!信息删除成功!\n");

printList();

puts("\n");

menu();

break;

  

   case 6:

    printf("请输入要删除的学生姓名:");

scanf("%s",
name);

    system("cls");

printf("系统正在删除学生信息,请稍候");

wait();

deleteStudentFromName(name);

Sleep(100);

puts("\n恭喜您!信息删除成功!\n");

printList();

puts("\n");

menu();

break;

   

   case 7:

fp
= fopen("student.txt", "wt");

    if
(fp == NULL)

    {

perror("fopen");

return
-1;

    }

tmp_head
= head->next;

    while
(tmp_head)

    {

fwrite(tmp_head,
sizeof(struct student), 1, fp);

     tmp_head
= tmp_head->next;

    }

fclose(fp);

exitPrograms();

break;

default:

puts("您输入的操作编号不在本系统服务区范围,请输入数字1-7!");

goto
ret;

   break;

  } 

 }

freeList();

 

 return 0;

}