学生管理系统

#define _CRT_SECURE_NO_WARNINGS
/*************************************************************
项 目 名:学生管理系统
作    者:零起点
编译环境:vs2015
版    本:3.0
工    能:1.输入数据
 2.查看数据
 3.修改数据
 4.删除数据
 5.插入数据
 因为我没有时间去写 6.保存数据  如果赶兴趣请您自己写写 TXT文件
**************************************************************/


#include<stdio.h>
#include<stdlib.h>
#include<string.h>


#define account_number  "123456789"//定义账号
#define password "123456"   //定义密码
//创建学生
typedef struct student
{
char Student_ID[6];//学生学号
char name[9];//学生姓名
char sex[3];//学生性别
unsigned int age;//学生年龄
float score;//入学成绩
struct student * pNext;
}NODE,*PNODE;


void main_window(void);
PNODE Create_linked_list(void);
void Output_linked_list(PNODE Student_library);
int statistics(PNODE pHew);//
void seqencing(PNODE pHew);
void delete_list(PNODE pHead);
void insert_list(PNODE pHead);
void revise(PNODE pHead);


int main(void)
{
char ch[10], ch1[7];
//简单的登录系统
printf("用户名:");
scanf("%s", ch);
printf("密  码:");
scanf("%s", ch1);
if (strcmp(ch, account_number) == 0)
if (strcmp(ch1, password) == 0)
main_window();
else
printf("密码错误!!!");
else
printf("用户和密码错误!!!");
system("pause");
return 0;
}
//进入主窗口
void main_window(void)
{
char ch;
PNODE pHead = NULL;
B:
system("mode con cols=150 lines=40");//修改窗口大小
printf("\n\n\t\t\t\t\t\t\t学生管理系统\n");
printf("\t\t\t\t\t\t┏━━━━━━━━━━━━┓\n");
printf("\t\t\t\t\t\t┃    1.输入数据          ┃\n");
printf("\t\t\t\t\t\t┃    2.查看数据          ┃\n");
printf("\t\t\t\t\t\t┃    3.修改数据          ┃\n");
printf("\t\t\t\t\t\t┃    4.删除数据          ┃\n");
printf("\t\t\t\t\t\t┃    5.插入数据          ┃\n");
//printf("\t\t\t\t\t\t┃    6.保存数据          ┃\n");
printf("\t\t\t\t\t\t┗━━━━━━━━━━━━┛\n");
printf("请输入你需要的功能:");
A:
scanf(" %c", &ch);
switch (ch)
{
case '1':pHead = Create_linked_list(); goto B; break;
case '2':Output_linked_list(pHead); goto B; break;
case '3':revise(pHead); goto B; break;
case '4':delete_list(pHead); goto B; break;
case '5':insert_list(pHead); goto B; break;
//case '6':break;
default:printf("输入错误,请从新输入:");goto A;break;
}
}
//创建链表
PNODE Create_linked_list()
{
int len = 0;
char ch;
printf("请输入学生个数:len = ");
scanf("%d", &len);
PNODE Student_library = (PNODE)malloc(sizeof(NODE));//动态分配内存
if (Student_library == NULL)//判断是否分配成功
{
printf_s("分配内存失败!");
exit(-1);
}
PNODE pTail = Student_library;//把首节点赋给 pTail
pTail->pNext = NULL;//把首节点下一个节点  赋值空
for (int i = 0; i < len; ++i)
{
char Student_ID[6];//学号
char name[9];//姓名
char sex[3];//性别
unsigned int age;//年龄
float score;//入学成绩
fflush(stdin);
printf("请输入第%d学生的学号:", i + 1);
scanf("%s", Student_ID);//临时赋值
printf("请输入第%d学生的姓名:", i + 1);
scanf("%s", name);
printf("请输入第%d学生的性别:", i + 1);
scanf("%s", sex);
printf("请输入第%d学生的年龄:", i + 1);
scanf("%d", &age);
printf("请输入第%d学生的入学成绩:", i + 1);
scanf("%f", &score);
PNODE pHew = (PNODE)malloc(sizeof(NODE));
if (pHew == NULL)
{
printf_s("分配内存失败!");
exit(-1);
}
strcpy(pHew->Student_ID, Student_ID);
strcpy(pHew->name, name);
strcpy(pHew->sex, sex);
pHew->age = age;
pHew->score = score;
pTail->pNext = pHew;
pHew->pNext = NULL;
pTail = pHew;
}
printf("Y/N是否继续:");
scanf(" %c", &ch);
if(ch=='Y'||ch=='y')
return Student_library;
}


void Output_linked_list(PNODE Student_library)
 {
char ch;
printf("Y/N是否按成绩排序输出:");
scanf(" %c", &ch);
if (ch == 'Y' || ch == 'y')
seqencing(Student_library);
PNODE g;
if(Student_library !=NULL)
g = Student_library->pNext;
else
g = NULL;
printf("\t\t\t┏━━━┳━━━━┳━━━┳━━━┳━━━━━┓\n");
printf("\t\t\t┃%-6s┃%-8s┃%-6s┃%-5s ┃%-8s  ┃\n", "学号", "姓名", "性别", "年龄", "入学成绩");
while (g != NULL)
{
printf("\t\t\t┣━━━╋━━━━╋━━━╋━━━╋━━━━━┫\n");
printf("\t\t\t┃%-6s┃%-8s┃%-6s┃%-5d ┃%-8.1f  ┃\n",g->Student_ID,g->name, g->sex, g->age,g->score);
g =g->pNext;
}
printf("\t\t\t┗━━━┻━━━━┻━━━┻━━━┻━━━━━┛\n");
printf("Y/N是否继续:");
scanf(" %c", &ch);
if (ch == 'Y' || ch == 'y')
return;
else
exit(-1);
return;
}


int statistics(PNODE pHew)
{
PNODE p;
int len = 0;
if (pHew != NULL)
p = pHew->pNext;
else
p = NULL;
while (p!=NULL)
{
++len;
p = p->pNext;
}
return len;
}


void seqencing(PNODE pHew)
{
int i, j;
int len = statistics(pHew);
PNODE P, Q;
for(i=0,P=pHew->pNext;i<len;++i,P->pNext)
for (j = i + 1, Q = P->pNext; j < len; ++j, Q->pNext)
{
if (P->score < Q->score)
{
char Student_ID[6];
char name[9];
char sex[3];
unsigned int age;
float score;
strcpy(Student_ID, Q->Student_ID);
strcpy(Q->Student_ID, P->Student_ID);
strcpy(P->Student_ID, Student_ID);
strcpy(name, Q->name);
strcpy(Q->name, P->name);
strcpy(P->name, name);
strcpy(sex, Q->sex);
strcpy(Q->sex, P->sex);
strcpy(P->sex, sex);
age = Q->age;
Q->age = P->age;
P->age = age;
score = Q->score;
Q->score = P->score;
P->score = score;
}
}
return;
}


void delete_list(PNODE pHead)
{
char ch;
int i = 0, len = statistics(pHead);
char Student_ID[6];
PNODE e=pHead, p = pHead;
printf("请输入输出学生的学号:");
scanf("%s", Student_ID);
while (strcmp(Student_ID,p->Student_ID)!=0&&i<=len)//找到节点的位置
{
p = p->pNext;
++i;
}
if (i < len)
{
for (int a = 1; a < i; ++a)
e = e->pNext;
printf("Y\N是否删除数据:\n");
printf("\t\t\t┏━━━┳━━━━┳━━━┳━━━┳━━━━━┓\n");
printf("\t\t\t┃%-6s┃%-8s┃%-6s┃%-5d ┃%-8.1f  ┃\n", p->Student_ID, p->name, p->sex, p->age, p->score);
printf("\t\t\t┗━━━┻━━━━┻━━━┻━━━┻━━━━━┛\n");
scanf(" %c", &ch);
if (ch == 'Y' || ch == 'y')
{
PNODE q = e->pNext;
e->pNext = e->pNext->pNext;
free(q);
q = NULL;
}
p = NULL;
return;
}
else
printf("没有查找到数据!!");
return;
}


void insert_list(PNODE pHead)
{
char Student_ID[6];
int i = 0,len = statistics(pHead);
PNODE p = pHead;
printf("请输入插入学生前的学号:");
scanf("%s", Student_ID);
while (strcmp(Student_ID, p->Student_ID) != 0 && i < len)
{
p->pNext;
i++;
}
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if (pNew == NULL)
{
printf("内存分配失败!\n");
exit(-1);
}
printf("请输入学生学号:");
scanf("%s", pNew->Student_ID);
printf("请输入学生姓名:");
scanf("%s",pNew->name );
printf("请输入学生性别:");
scanf("%s",pNew->sex );
printf("请输入学生年龄:");
scanf("%d", &pNew->age);
printf("请输入学生入学成绩:");
scanf("%f", &pNew->score);
PNODE q = p->pNext;
p->pNext = pNew;
pNew->pNext = q;
return;
}


void revise(PNODE pHead)
{
char Student_ID[6];
PNODE p = pHead;
printf("请输入学生学号:");
scanf(" %s", Student_ID);
while (strcmp(Student_ID,p->Student_ID)!=0)
p=p->pNext;
printf("请输入学生学号:");
scanf("%s", p->Student_ID);
printf("请输入学生姓名:");
scanf("%s", p->name);
printf("请输入学生性别:");
scanf("%s", p->sex);
printf("请输入学生年龄:");
scanf("%d", &p->age);
printf("请输入学生入学成绩:");
scanf("%f", &p->score);
return;
}
#include <stdio.h> #include "string.h" int N,i; FILE *fp; struct student {char num[10]; char name[8]; char sex[5]; int age; char addr[15]; int score; }stu[100]; void input() /*输入学生数据的资料*/ {printf("Input the student data %d:\n",i+1); /*输入一个学生的数据*/ printf("NO.:"); scanf("%s",stu[i].num); /*输入号数*/ printf("name:"); scanf("%s",stu[i].name); /*姓名*/ printf("sex:"); scanf("%s",stu[i].sex); /*性别*/ printf("age:"); scanf("%d",&stu[i].age); /*年龄*/ printf("address:"); scanf("%s",stu[i].addr); /*家庭地址*/ printf("score:"); scanf("%d",&stu[i].score); /*分数*/ printf("\n"); } void add() /*在ouru的文件后添加学生数据*/ { if((fp=fopen("ouru","ab"))==NULL) /*以读写的二进制形式打开ouru文件*/ {printf("Cann't open the file\n"); return;} printf("How many data do you want:"); /*输入需要添加几个学生的数据和具体的资料*/ scanf("%d",&N); for(i=0;i<N;i++) /*向ouru文件中加入添加学生的数据的个数*/ input(); for(i=0;i<N;i++) if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) printf("File error\n"); fclose(fp); } void save() /*保存学生的数据到ouru文件中*/ { int i; if((fp=fopen("ouru","wb"))==NULL) /*打开ouru文件*/ {printf("Cann't open the file\n"); return;} for(i=0;i<N;i++) /*把学生的数据写入到ouru 文件中*/ if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) printf("File write error\n"); fclose(fp); } void insert() /*在ouru文件中插入学生的数据*/ {char positions[10]; int a,b; if((fp=fopen("ouru","r+"))==NULL) /*以读写打开ouru文件*/ {printf("Can not open file!"); return;} for(i=0;(fread(&stu[i],sizeof(struct student),1,fp))!=0;i++); /*读取文件并计算文件中有多少个学生的数据*/ printf("Which position do you want to insert:"); scanf("%s",positions); for(a=0;a<i;a++) /*确定插入的位置*/ if(strcmp(positions,stu[a].num)==0)break; a=a+1; fseek(fp,sizeof(struct student)*(a+1),0); /*将指针指向要插入位置的后一位*/ for(b=a;b<i;b++) fwrite(&stu[b],sizeof(struct student),1,fp); /*将插入后的学生数据向后移动一位*/ i=a; input(); fseek(fp,sizeof(struct student)*a,0); /*将指针指向要插入的位置*/ fwrite(&stu[a],sizeof(struct student),1,fp); /*将插入的数据保存到ouru文件里*/ fclose(fp);} void change() /*修改ouru文件中的某个学生的数据*/ { char positions[10]; if((fp=fopen("ouru","rb+"))==NULL) /*以读写打开二进制ouru文件*/ {printf("Can not open the file\n"); return;} printf("What data do you want to modify:"); /*输入要修改学生的号码*/ scanf("%s",positions); for(i=0;(fread(&stu[i],sizeof(struct student),1,fp))!=0;i++) /*找出要修改的数据*/ if(strcmp(stu[i].num,positions)==0) {fseek(fp,sizeof(struct student)*i,0); /*将指针指向要修改的资料*/ input(); fwrite(&stu[i],sizeof(struct student),1,fp); /*将修改的数据保存*/ break;} fclose(fp); } void del() /*删除ouru中的某个学生的数据*/ {int a,b,f; char positions[10]; if((fp=fopen("ouru","rb+"))==NULL) /*以读写打开ouru文件*/ {printf("Can not open file!"); return;} printf("Input the deleted number:"); /*输入要删除的学生号码*/ scanf("%s",positions); for(i=0;fread(&stu[i],sizeof(struct student),1,fp)!=0;i++); /*查找文件中有多少个学生的数据*/ f=i; for(a=0;a<i;a++) /*找出要删除的学生的数据并把后面的数据全都向前移一位*/ {if(strcmp(positions,stu[a].num)==0) { for(b=a;b<i;b++) {strcpy(stu[b].num,stu[b+1].num); strcpy(stu[b].name,stu[b+1].name); strcpy(stu[b].sex,stu[b+1].sex); stu[b].age=stu[b+1].age; strcpy(stu[b].addr,stu[b+1].addr); stu[b].score=stu[b+1].score;} f=f-1; break;} } fclose(fp); fopen("ouru","w"); /*将删除后的结果保存到ouru文件里*/ for(a=0;a<f;a++) fwrite(&stu[a],sizeof(struct student),1,fp); fclose(fp);} void examine() /*查看ouru文件中的资料*/ {int i; if((fp=fopen("ouru","r"))==NULL) /*以只读的方式打开ouru文件*/ {printf("Cannot open the file\n"); return;} printf("\n"); printf("num name sex age addr score\n"); /*读取各个学生数据的同时把它们显示出来*/ for(i=0;(fread(&stu[i],sizeof(struct student),1,fp))!=0;i++) {printf("%-10s%-10s%-8s%-9d%-17s%d\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].age,stu[i].addr,stu[i].score);} printf("\n"); fclose(fp); } main() {int choose; /*显示主界面*/ printf("********************************************************************************\n"); printf("--------------------------------------------------------------------------------\n\n"); printf(" Welcome to the student system\n"); printf(" Producer:ouru\tRegistration number:03011123\n"); printf("\n\n--------------------------------------------------------------------------------\n"); printf("********************************************************************************\n"); printf("Please the choice:\n"); printf("\t\t\t1. Input the student data\n"); printf("\t\t\t2. Add the new studetn data\n"); printf("\t\t\t3. Change a data\n"); printf("\t\t\t4. Insert a new data\n"); printf("\t\t\t5. Delete the student data\n"); printf("\t\t\t6. Manifestation student data\n"); printf("\t\t\t7. Main menu\n"); printf("\t\t\t8. exit\n\n\n"); scanf("%d",&choose); /*选择想要操作的选项*/ while(choose!=8) /*当选择8的时候不再循环并退出*/ { switch(choose) {case 1:printf("Please input the student total:"); /*当choose为下面的数时执行相应的功能*/ scanf("%d",&N); for(i=0;i<N;i++) input(); save();break; case 2:add();break; case 3:change();break; case 4:insert();break; case 5:del();break; case 6:examine();break; case 7:printf("\n\n\nPlease the choice:\n"); printf("\t\t\t1. Input the student data\n"); printf("\t\t\t2. Add the new studetn data\n"); printf("\t\t\t3. Change a data\n"); printf("\t\t\t4. Insert a new data\n"); printf("\t\t\t5. Delete the student data\n"); printf("\t\t\t6. Manifestation student data\n"); printf("\t\t\t7. Main menu\n"); printf("\t\t\t8. exit\n\n\n");break; default :printf("error\n");} scanf("%d",&choose); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值