c語言編寫學生管理系統
昨天我一高中同學讓我寫一個學生管理系統,說怎麼簡單怎麼來,其實管理系統是每個學程式設計的人必須會的程式碼,下面分享給大家。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define number 50
#define LEN sizeof(struct address)
struct address
{
char name[20];
char num[20];
char cj[20];
struct address *next;
};
struct address *head;
static int n=0;
void scan(struct address *g)
{
printf("請輸入:姓名:");
scanf("%s",g->name);
printf("請輸入:學號:");
scanf("%s",g->num);
printf("請輸入:成績:");
scanf("%s",g->cj);
}
int menu_select()
{
int choice;
printf("*********************************************************************\n");
printf(" 學生管理系統 \n");
printf("*********************************************************************\n");
printf(" 1:學生資訊錄入 \n");
printf(" 2:分段人數 \n");
printf(" 3:資訊查詢 \n");
printf(" 4:按成績排序 \n");
printf(" 5:系統退出 \n");
printf("請輸入您的選擇:");
scanf("%d",&choice);
return choice;
}
void look(struct address *head)
{
struct address *p;
p=head;
printf("*********************************************************************\n");
printf("姓名\t學號\t成績\n");
printf("*********************************************************************\n");
if(head!=NULL)
{
while(p!=NULL)
{
printf("%s\t%s\t%s\n",p->name,p->num,p->cj);
p=p->next;
}
}
}
struct address* add()
{
int temp;
struct address *p1,*p2;
p2=p1=(struct address *)malloc(LEN);
head=NULL;
while(1)
{
n=n+1;
printf("第%d個人.\n",n);
if(n==1) head=p1;
else p2->next=p1;
scan(p1);
p2=p1;
p1=(struct address *)malloc(LEN);
printf("如果你想退出,請輸入1;如果你想繼續,請輸入2:");
scanf("%d",&temp);
if(temp==1) break;
}
p2->next=NULL;
look(head);
return head;
}
void find(struct address *head)
{
struct address *p;
char good[20];
p=head;
printf("請輸入所查詢的人的姓名:");
scanf("%s",good);
if(head!=NULL)
{
while(p!=NULL)
{
if(strcmp(good,p->name)==0)
{
printf("*********************************************************************\n");
printf("姓名\t學號\t成績\n");
printf("%s\t%s\t%s\n",p->name,p->num,p->cj);
printf("*********************************************************************\n");
}
p=p->next;
}
}
}
struct address *sort()
{
struct address *a,*p,*p1,*p2,*p3,*p4;
int f=0,h;
p2=p1=(struct address *)malloc(LEN);
a=(struct address *)malloc(LEN);
p4=p=head;
while(f<n)
{
f=f+1;
if(f==1) head=p1;
else p2->next=p1;
p=p4;
h=0;
while(p!=NULL)
{
h=h+1;
p3=p;
p=p->next;
if(h==1)
{*a=*p3;}
if(strcmp(a->cj,p3->cj)>0)
{
*a=*p3;
}
}
p=p4;
while(p!=NULL)
{
p3=p;
p=p->next;
if(strcmp(a->cj,p4->cj)==0)
{
p4=p;
break;
}
if(strcmp(a->cj,p->cj)==0)
{
p3->next=p->next;
break;
}
}
*p1=*a;
p2=p1;
p1=(struct address *)malloc(LEN);
}
p2->next=NULL;
look(head);
return head;
}
void set_grade(struct address *head)
{
struct address *p;
p=head;
int i,a=0,b=0,c=0,d=0,e=0;
if(head!=NULL)
{
while(p!=NULL)
{
if(strcmp(p->cj,"90")>=0)
{
a++;
}
else if(strcmp(p->cj,"80")>=0)
{
b++;
}
else if(strcmp(p->cj,"70")>=0)
{
c++;
}
else if(strcmp(p->cj,"60")>=0)
{
d++;
}
else
{
e++;
}
p=p->next;
}
}
printf("a:%d\nb:%d\nc:%d\nd:%d\ne:%d\n",a,b,c,d,e);
}
int main()
{
while(1)
{
switch(menu_select())
{
case 1:add();break;//錄入
case 2:set_grade(head);break;//分段
case 3:find(head);break;//查詢
case 4:sort();break;//排序
case 5:exit(0);
}
}
return 0;
}
因為他要求的功能比較少,所以只做了這些,如果需要更多功能的,我之前寫的管理系統功能更全面可以用做參考