C語言實現班級學生成績管理系統
阿新 • • 發佈:2018-11-11
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Student//學生結構
{
char Name[10];//姓名
int No;//學號
float Score[5];//5克的成績
float Total;//總分
};
typedef struct Node//結點結構
{
struct Student st;
struct Node *pNext;
} NODE, *PNODE;
/*
NODE等價於struct Student st
PNODE等價於struct Node *pNext
*/
PNODE InputStudent(void)//輸入學生資訊
{
int len,i;//學生的人數
NODE stu;//學生結構
PNODE pHead = (PNODE)malloc(sizeof(NODE));//定義一個頭結點並且為頭結點分配記憶體
PNODE pTail = pHead;//定義一個指向頭結點的指標
pTail->pNext = NULL;//清空指標域
printf("請輸入學生的人數:");
scanf("%d",&len);
for(i=0; i<len; i++)
{
system("cls");//清屏
printf("請輸入第%d個學生的姓名:", i+1);
scanf("%s", stu.st.Name);
printf("請輸入第%d個學生的學號:", i+1);
scanf("%d", &stu.st.No);
printf("請輸入第%d個學生的語文成績:", i+1);
scanf("%f", &stu.st.Score[0]);
printf("請輸入第%d個學生的數學成績:", i+1);
scanf("%f", &stu.st.Score[1]);
printf("請輸入第%d個學生的英語成績:", i+1);
scanf("%f", &stu.st.Score[2]);
printf("請輸入第%d個學生的物理成績:", i+1);
scanf("%f", &stu.st.Score[3]);
printf("請輸入第%d個學生的化學成績:", i+1);
scanf("%f", &stu.st.Score[4]);
stu.st.Total = stu.st.Score[0] + stu.st.Score[1] + stu.st.Score[2] + stu.st.Score[3] + stu.st.Score[4];//計算總分
PNODE pNew = (PNODE)malloc(sizeof(NODE));//為新節點分配記憶體
pNew->st = stu.st;//初始化結點的資料域
pTail->pNext = pNew;//將新結點掛到老結點後
pNew->pNext = NULL;//清空新結點的指標域
pTail = pNew;//將pTail移到新結點上
}
return pHead;
}
void OutputStudent(PNODE pHead)//輸出學生資訊
{
PNODE p = pHead->pNext; //定義一個指標用於遍歷學生資訊
printf("姓名 學號 語文 數學 英語 物理 化學 總分\n");
while(NULL != p)
{
printf("%s %d %-4.f %-4.f %-4.f %-4.f %-4.f %-4.f\n", p->st.Name,p->st.No, p->st.Score[0], p->st.Score[1], p->st.Score[2], p->st.Score[3], p->st.Score[4], p->st.Total);
p = p->pNext;
}
}
void SearchStudent(PNODE pHead)//查詢學生資訊
{
char Name[10];
printf("請輸入你需要查詢的學生的姓名:");
scanf("%s",Name);
PNODE p = pHead->pNext;
while(NULL != p)
{
if(0 == strcmp(Name,p->st.Name))
{
printf("%s %d %-4.f %-4.f %-4.f %-4.f %-4.f %-4.f\n", p->st.Name,p->st.No, p->st.Score[0], p->st.Score[1], p->st.Score[2], p->st.Score[3], p->st.Score[4], p->st.Total);
}
p = p->pNext;
}
}
void ChangeStudent(PNODE pHead)//修改學生資訊
{
char Name[10];
printf("請輸入你需要修改的學生的姓名:");
scanf("%s",&Name);
PNODE p = pHead->pNext;//定義一個指標用於遍歷學生資訊
while(NULL != p)
{
if(0 == strcmp(Name,p->st.Name))
{
printf("修改前的學生的資訊:");
printf("姓名 學號 語文 數學 英語 物理 化學 總分\n");
printf("%s %d %-4.f %-4.f %-4.f %-4.f %-4.f %-4.f\n", p->st.Name,p->st.No, p->st.Score[0], p->st.Score[1], p->st.Score[2], p->st.Score[3], p->st.Score[4], p->st.Total);
system("pause");
system("cls");
printf("請輸入新的學生的姓名:");
scanf("%s",&p->st.Name);
printf("請輸入新的學生的學號:");
scanf("%d",&p->st.No);
printf("請輸入新的學生的語文:");
scanf("%f",&p->st.Score[0]);
printf("請輸入新的學生的數學:");
scanf("%f",&p->st.Score[1]);
printf("請輸入新的學生的英語:");
scanf("%f",&p->st.Score[2]);
printf("請輸入新的學生的物理:");
scanf("%f",&p->st.Score[3]);
printf("請輸入新的學生的化學:");
scanf("%f",&p->st.Score[4]);
p->st.Total = p->st.Score[0] + p->st.Score[1] + p->st.Score[2] + p->st.Score[3] + p->st.Score[4];
break;
}
p = p->pNext;
}
}
void DeleteStudent(PNODE pHead)//刪除學生資訊
{
PNODE p = pHead;
int a;
int i=0;
printf("請輸入你需要刪除的學生編號:");
scanf("%d",&a);
while(NULL == p->pNext)
{
p = p->pNext;
i++;
}
PNODE q = p->pNext;
p->pNext = q->pNext;
free(q);
printf("你已經成功刪除了第%d個學生的資訊!\n",a);
}
void InsertStudent(PNODE pHead)//新增學生資訊
{
PNODE p = pHead;
PNODE s;
int a;//插入結點的位置
int i=0;//計數器
struct Student stu;//學生結構
printf("請輸入你想插入的位置:");
scanf("%d",&a);
while(p->pNext != NULL && i<a)
{
p = p->pNext;
i++;
}
if(NULL == p || i>a)
{
printf("插入結點的位置不存在!\n");
return;
}
printf("請輸入學生的姓名:");
scanf("%s",stu.Name);
printf("請輸入學生的學號:");
scanf("%d",&stu.No);
printf("請輸入學生的語文:");
scanf("%f",&stu.Score[0]);
printf("請輸入學生的數學:");
scanf("%f",&stu.Score[1]);
printf("請輸入學生的英語:");
scanf("%f",&stu.Score[2]);
printf("請輸入學生的物理:");
scanf("%f",&stu.Score[3]);
printf("請輸入學生的化學:");
scanf("%f",&stu.Score[4]);
stu.Total = stu.Score[0] + stu.Score[1] + stu.Score[2] + stu.Score[3] + stu.Score[4];
s = (PNODE)malloc(sizeof(NODE));//申請填裝結點
s->st = stu;
s->pNext = p->pNext;
p->pNext = s;
}
void ScortByTotal(PNODE pHead)//對總分進行排序
{
PNODE p, q;//定義兩個指標,進行遍歷
NODE temp; //用於交換
for(p = pHead->pNext; p != NULL; p = p->pNext)
{
for(q = pHead->pNext; q != NULL; q= q->pNext)
{
if(p->st.Total < q->st.Total)//當前一個學生的總分小於後一個學生的總分時
{
temp.st = p->st;//將p的資料賦值給temp
p->st = q->st;//將q的資料賦值給p
q->st = temp.st;//將temp的資料賦值給q
}
}
}
}
void menu()
{
printf("\t\t|**********歡迎進入學生資訊管理系統**********|\n");
printf("\t\t| 1,錄入學生資訊 |\n");
printf("\t\t| 2,顯示學生資訊 |\n");
printf("\t\t| 3,查詢學生資訊 |\n");
printf("\t\t| 4,修改學生資訊 |\n");
printf("\t\t| 5,新增學生資訊 |\n");
printf("\t\t| 6,刪除學生資訊 |\n");
printf("\t\t| 7,按總分排名次 |\n");
printf("\t\t| 0,退出 |\n");
printf("\t\t|********************************************|\n");
printf("請輸入你所選的選項(0--7:");
}
int main()
{
PNODE pHead = NULL;//定義一個指標
int a;
while(1)
{
menu();
scanf("%d",&a);
switch(a)
{
case 1:
pHead = InputStudent();
system("pause");
system("cls");
break;
case 2:
OutputStudent(pHead);
system("pause");
system("cls");
break;
case 3:
SearchStudent(pHead);
system("pause");
system("cls");
break;
case 4:
ChangeStudent(pHead);
system("pause");
system("cls");
break;
case 5:
InsertStudent(pHead);
system("pause");
system("cls");
break;
case 6:
DeleteStudent(pHead);
system("pause");
system("cls");
break;
case 7:
ScortByTotal(pHead);
OutputStudent(pHead);
system("pause");
system("cls");
break;
case 0:
exit(-1);
}
}
return 0;
}
#include <stdlib.h>
#include <math.h>
struct Student//學生結構
{
char Name[10];//姓名
int No;//學號
float Score[5];//5克的成績
float Total;//總分
};
typedef struct Node//結點結構
{
struct Student st;
struct Node *pNext;
} NODE, *PNODE;
/*
NODE等價於struct Student st
PNODE等價於struct Node *pNext
*/
PNODE InputStudent(void)//輸入學生資訊
{
int len,i;//學生的人數
NODE stu;//學生結構
PNODE pHead = (PNODE)malloc(sizeof(NODE));//定義一個頭結點並且為頭結點分配記憶體
PNODE pTail = pHead;//定義一個指向頭結點的指標
pTail->pNext = NULL;//清空指標域
printf("請輸入學生的人數:");
scanf("%d",&len);
for(i=0; i<len; i++)
{
system("cls");//清屏
printf("請輸入第%d個學生的姓名:", i+1);
scanf("%s", stu.st.Name);
printf("請輸入第%d個學生的學號:", i+1);
scanf("%d", &stu.st.No);
printf("請輸入第%d個學生的語文成績:", i+1);
scanf("%f", &stu.st.Score[0]);
printf("請輸入第%d個學生的數學成績:", i+1);
scanf("%f", &stu.st.Score[1]);
printf("請輸入第%d個學生的英語成績:", i+1);
scanf("%f", &stu.st.Score[2]);
printf("請輸入第%d個學生的物理成績:", i+1);
scanf("%f", &stu.st.Score[3]);
printf("請輸入第%d個學生的化學成績:", i+1);
scanf("%f", &stu.st.Score[4]);
stu.st.Total = stu.st.Score[0] + stu.st.Score[1] + stu.st.Score[2] + stu.st.Score[3] + stu.st.Score[4];//計算總分
PNODE pNew = (PNODE)malloc(sizeof(NODE));//為新節點分配記憶體
pNew->st = stu.st;//初始化結點的資料域
pTail->pNext = pNew;//將新結點掛到老結點後
pNew->pNext = NULL;//清空新結點的指標域
pTail = pNew;//將pTail移到新結點上
}
return pHead;
}
void OutputStudent(PNODE pHead)//輸出學生資訊
{
PNODE p = pHead->pNext; //定義一個指標用於遍歷學生資訊
printf("姓名 學號 語文 數學 英語 物理 化學 總分\n");
while(NULL != p)
{
printf("%s %d %-4.f %-4.f %-4.f %-4.f %-4.f %-4.f\n", p->st.Name,p->st.No, p->st.Score[0], p->st.Score[1], p->st.Score[2], p->st.Score[3], p->st.Score[4], p->st.Total);
p = p->pNext;
}
}
void SearchStudent(PNODE pHead)//查詢學生資訊
{
char Name[10];
printf("請輸入你需要查詢的學生的姓名:");
scanf("%s",Name);
PNODE p = pHead->pNext;
while(NULL != p)
{
if(0 == strcmp(Name,p->st.Name))
{
printf("%s %d %-4.f %-4.f %-4.f %-4.f %-4.f %-4.f\n", p->st.Name,p->st.No, p->st.Score[0], p->st.Score[1], p->st.Score[2], p->st.Score[3], p->st.Score[4], p->st.Total);
}
p = p->pNext;
}
}
void ChangeStudent(PNODE pHead)//修改學生資訊
{
char Name[10];
printf("請輸入你需要修改的學生的姓名:");
scanf("%s",&Name);
PNODE p = pHead->pNext;//定義一個指標用於遍歷學生資訊
while(NULL != p)
{
if(0 == strcmp(Name,p->st.Name))
{
printf("修改前的學生的資訊:");
printf("姓名 學號 語文 數學 英語 物理 化學 總分\n");
printf("%s %d %-4.f %-4.f %-4.f %-4.f %-4.f %-4.f\n", p->st.Name,p->st.No, p->st.Score[0], p->st.Score[1], p->st.Score[2], p->st.Score[3], p->st.Score[4], p->st.Total);
system("pause");
system("cls");
printf("請輸入新的學生的姓名:");
scanf("%s",&p->st.Name);
printf("請輸入新的學生的學號:");
scanf("%d",&p->st.No);
printf("請輸入新的學生的語文:");
scanf("%f",&p->st.Score[0]);
printf("請輸入新的學生的數學:");
scanf("%f",&p->st.Score[1]);
printf("請輸入新的學生的英語:");
scanf("%f",&p->st.Score[2]);
printf("請輸入新的學生的物理:");
scanf("%f",&p->st.Score[3]);
printf("請輸入新的學生的化學:");
scanf("%f",&p->st.Score[4]);
p->st.Total = p->st.Score[0] + p->st.Score[1] + p->st.Score[2] + p->st.Score[3] + p->st.Score[4];
break;
}
p = p->pNext;
}
}
void DeleteStudent(PNODE pHead)//刪除學生資訊
{
PNODE p = pHead;
int a;
int i=0;
printf("請輸入你需要刪除的學生編號:");
scanf("%d",&a);
while(NULL == p->pNext)
{
p = p->pNext;
i++;
}
PNODE q = p->pNext;
p->pNext = q->pNext;
free(q);
printf("你已經成功刪除了第%d個學生的資訊!\n",a);
}
void InsertStudent(PNODE pHead)//新增學生資訊
{
PNODE p = pHead;
PNODE s;
int a;//插入結點的位置
int i=0;//計數器
struct Student stu;//學生結構
printf("請輸入你想插入的位置:");
scanf("%d",&a);
while(p->pNext != NULL && i<a)
{
p = p->pNext;
i++;
}
if(NULL == p || i>a)
{
printf("插入結點的位置不存在!\n");
return;
}
printf("請輸入學生的姓名:");
scanf("%s",stu.Name);
printf("請輸入學生的學號:");
scanf("%d",&stu.No);
printf("請輸入學生的語文:");
scanf("%f",&stu.Score[0]);
printf("請輸入學生的數學:");
scanf("%f",&stu.Score[1]);
printf("請輸入學生的英語:");
scanf("%f",&stu.Score[2]);
printf("請輸入學生的物理:");
scanf("%f",&stu.Score[3]);
printf("請輸入學生的化學:");
scanf("%f",&stu.Score[4]);
stu.Total = stu.Score[0] + stu.Score[1] + stu.Score[2] + stu.Score[3] + stu.Score[4];
s = (PNODE)malloc(sizeof(NODE));//申請填裝結點
s->st = stu;
s->pNext = p->pNext;
p->pNext = s;
}
void ScortByTotal(PNODE pHead)//對總分進行排序
{
PNODE p, q;//定義兩個指標,進行遍歷
NODE temp; //用於交換
for(p = pHead->pNext; p != NULL; p = p->pNext)
{
for(q = pHead->pNext; q != NULL; q= q->pNext)
{
if(p->st.Total < q->st.Total)//當前一個學生的總分小於後一個學生的總分時
{
temp.st = p->st;//將p的資料賦值給temp
p->st = q->st;//將q的資料賦值給p
q->st = temp.st;//將temp的資料賦值給q
}
}
}
}
void menu()
{
printf("\t\t|**********歡迎進入學生資訊管理系統**********|\n");
printf("\t\t| 1,錄入學生資訊 |\n");
printf("\t\t| 2,顯示學生資訊 |\n");
printf("\t\t| 3,查詢學生資訊 |\n");
printf("\t\t| 4,修改學生資訊 |\n");
printf("\t\t| 5,新增學生資訊 |\n");
printf("\t\t| 6,刪除學生資訊 |\n");
printf("\t\t| 7,按總分排名次 |\n");
printf("\t\t| 0,退出 |\n");
printf("\t\t|********************************************|\n");
printf("請輸入你所選的選項(0--7:");
}
int main()
{
PNODE pHead = NULL;//定義一個指標
int a;
while(1)
{
menu();
scanf("%d",&a);
switch(a)
{
case 1:
pHead = InputStudent();
system("pause");
system("cls");
break;
case 2:
OutputStudent(pHead);
system("pause");
system("cls");
break;
case 3:
SearchStudent(pHead);
system("pause");
system("cls");
break;
case 4:
ChangeStudent(pHead);
system("pause");
system("cls");
break;
case 5:
InsertStudent(pHead);
system("pause");
system("cls");
break;
case 6:
DeleteStudent(pHead);
system("pause");
system("cls");
break;
case 7:
ScortByTotal(pHead);
OutputStudent(pHead);
system("pause");
system("cls");
break;
case 0:
exit(-1);
}
}
return 0;
}