學生資訊管理系統(增強版)
咳咳,上一次寫的鏈接 點擊可至"乞丐版"
增強版僅針對自己,之前寫過一個“乞丐版”的學生資訊管理系統,由于對函式模塊化思想不夠了解,寫的比較拉跨,這次來做一個提高吧!
這次就來簡單說一說函式吧!
文章目錄
- 學生資訊管理系統(增強版)
- 一、資訊錄入
- 二、學號、姓名查詢
- 三、修改資訊
- 四、成績排名
- 五、列印資訊
- 六、完整代碼展示
一、資訊錄入
void InputStuData()
{
int n;
printf("請輸入錄入學生的人數:\n");
while (scanf("%d", &n) != 1)
{
printf("輸入有誤!請重新輸入:\n");
fflush(stdin);
}
printf("人數錄入完畢,請錄入%d位學生的資訊:\n", n);
printf("以學號、姓名、專業和語、數、外三門課成績為順序\n");
printf("例如:E02135292 張三 計科 85 79 94\n");
while (n --)
{
Stu * temp = (Stu *)malloc(sizeof(Stu));
if (temp == NULL)
{
printf("記憶體分配出錯!\n");
exit(0);
}
else
{
scanf("%s %s %s", &temp->nums, &temp->name, &temp->major);
scanf("%f %f %f", &temp->score[0], &temp->score[1], &temp->score[2]);
temp->next = NULL;
if (head == NULL)//剛開始頭節點為空
{
head = temp;
tail = temp;
}
else
{
tail->next = temp;
tail = temp;
}
}
}
return;
}
二、學號、姓名查詢
Stu * SearchStudent()
{
char str[20], ch;
Repeat:
printf("請輸入要尋找(正確的)學號或者姓名按'#'退出\n");
scanf("%s", str);
getchar();
if (str[0] == '#') return NULL;
Stu * temp = head;
if (head == NULL)
{
printf("未錄入信息!\n");
return NULL;
}
else
{
while (temp)
{
if (!strcmp(temp->nums, str) || !strcmp(temp->name, str))
break;
temp = temp->next;
}
}
if (temp == NULL)
{
printf("未找到相關資訊!\n");
goto Repeat;
}
else
{
printf("查找成功,該同學資訊如下:\n");
printf("%10s %10s %10s",temp->nums,temp->name,temp->major);
printf("%10.2f %10.2f %10.2f\n",temp->score[0],temp->score[1],temp->score[2]);
}
printf("是否確認查詢無誤?(y or no)\n");
scanf("%c", &ch);
fflush(stdin);
if (ch != 'y' && ch != 'Y')
goto Repeat;
printf("已確認該名同學的資訊!\n");
return temp;
}
該函式回傳一個結構體指標,即為查找位置的指標
三、修改資訊
void ModifyStuData()
{
char str[20], ch;
Stu * temp = SearchStudent();
if (temp == NULL) printf("未指定修改內容位置!\n");
else
{
int num;
Repeat:
printf("請選擇需要修改的資訊:\n");
printf("1. 學號 2. 姓名 3. 專業\n");
printf("4. 語文成績 5. 數學成績 6. 英語成績\n");
while (scanf("%d", &num) != 1 || num > 6 || num < 1)
{
printf("選擇無效,請重新輸入范圍內的數字:\n");
fflush(stdin);
}
switch (num)
{
case 1:
printf("請輸入正確的學號:\n");
scanf("%s", temp->nums); break;
case 2:
printf("請輸入正確的名字:\n");
scanf("%s", temp->name); break;
case 3:
printf("請輸入正確的專業:\n");
scanf("%s", temp->major); break;
case 4:
printf("請輸入正確的語文成績:\n");
scanf("%f", &temp->score[0]); break;
case 5:
printf("請輸入正確的數學成績:\n");
scanf("%f", &temp->score[1]); break;
case 6:
printf("請輸入正確的英語成績:\n");
scanf("%f", &temp->score[2]); break;
}
getchar();
printf("資訊更改后如下:\n");
printf("%10s %10s %10s",temp->nums,temp->name,temp->major);
printf("%10.2f %10.2f %10.2f\n",temp->score[0],temp->score[1],temp->score[2]);
printf("是否確認無誤?(y or n)\n");
scanf("%c", &ch);
fflush(stdin);
if (ch != 'y' && ch != 'Y') goto Repeat;
printf("您已確認,更改完成!\n");
}
return;
}
修改資訊需要用到上面查找函式,找到定位后,根據選項進行更改
四、成績排名
void RankingList()
{
int i;
printf("請選擇學科進行成績排名:\n");
printf("1. 語文 2. 數學 3. 英語\n");
while (scanf("%d", &i) && i > 3 && i < 1)
printf("請輸入范圍內的學科!\n");
fflush(stdin);
Stu * pre, * p, * end = NULL;
pre = p = head;
while (p->next != end)
{
p = head->next;
while (p != end)
{
if (pre->score[i-1] > p->score[i-1])
{
SwapStuNode(pre, p);
Stu * temp = pre;
pre = p;
p = temp;
}
pre = pre->next;
p = p->next;
}
end = pre;
pre = p = head;
}
return;
}
void SwapStuNode(Stu * p, Stu * q)
{
if (p == q || p == NULL || q == NULL)
return;
if (p == head)
{
if (q == tail)
{
tail->next = p;
p->next = NULL;
head = tail;
tail = head->next;
}
else
{
p->next = q->next;
q->next = p;
head = q;
}
}
else
{
if (q == tail)
{
Stu * pt = head;
while (pt->next != p) pt = pt->next;
pt->next = q;
q->next = p;
p->next = NULL;
tail = p;
}
else
{
Stu * pt = head;
while (pt->next != p) pt = pt->next;
pt->next = q;
p->next = q->next;
q->next = p;
}
}
return;
}
這里的排名實際上用到是冒泡排序,這里為了好理解,用了最笨的方法,實際上鏈表排序方法多樣,本人曾經總結過單鏈表冒泡、插入排序的實作和優化,給出鏈接單鏈表冒泡、插入排序的實作和優化
五、列印資訊
void PrintStuList()
{
Stu * phead = head;
if (phead == NULL) printf("暫無學生資訊!\n");
while (phead)
{
printf("%10s %10s %10s", phead->nums, phead->name, phead->major);
printf("%10.2f %10.2f %10.2f\n",phead->score[0],phead->score[1],phead->score[2]);
phead = phead -> next;
}
return;
}
從頭開始,一直到尾部,尾部指標域為空
六、完整代碼展示
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct Stu
{
char nums[10];
char name[20], major[20];
float score[3];
struct Stu * next;
}Stu;
Stu * head = NULL;
Stu * tail = NULL;
void MainMenuSelect();//主選擇選單
void InputStuData();//錄入資訊
Stu * SearchStudent();//學號查詢、姓名查詢
void ModifyStuData();//修改資訊
void DeleteStuData();//洗掉資訊
void RankingList();//成績排名
void PrintStuList();//列印鏈表
void SwapStuNode(Stu * p, Stu * q);//交換節點
int main()
{
int num; char ch;
do
{
MainMenuSelect();
printf("請選擇一個操作:\n");
while (scanf("%d", &num) != 1 || num < 0 || num > 6 )
{
printf("您的輸入不正確,請重新輸入:\n");
fflush(stdin);
}
switch (num)
{
case 0:
printf("謝謝使用,再見!\n");
return 0;
case 1:
InputStuData(); break;
case 2:
SearchStudent(); break;
case 3:
ModifyStuData(); break;
case 4:
DeleteStuData(); break;
case 5:
RankingList(); break;
case 6:
PrintStuList(); break;
}
printf("本次任務結束,回主選單或退出程式:(y or n)\n");
scanf(" %c", &ch);
} while (ch == 'Y' || ch == 'y');
printf("謝謝使用!再見\n");
return 0;
}
void MainMenuSelect()
{
printf("---------------主選單-------------\n");
printf("1. 錄入學生資訊 2. 查詢學生資訊\n");
printf("3. 修改學生資訊 4. 洗掉學生資訊\n");
printf("5. 學生成績排名 6. 列印學生資訊\n");
printf("-----------0. 退出系統------------\n");
return;
}
void InputStuData()
{
int n;
printf("請輸入錄入學生的人數:\n");
while (scanf("%d", &n) != 1)
{
printf("輸入有誤!請重新輸入:\n");
fflush(stdin);
}
printf("人數錄入完畢,請錄入%d位學生的資訊:\n", n);
printf("以學號、姓名、專業和語、數、外三門課成績為順序\n");
printf("例如:E02135292 張三 計科 85 79 94\n");
while (n --)
{
Stu * temp = (Stu *)malloc(sizeof(Stu));
if (temp == NULL)
{
printf("記憶體分配出錯!\n");
exit(0);
}
else
{
scanf("%s %s %s", &temp->nums, &temp->name, &temp->major);
scanf("%f %f %f", &temp->score[0], &temp->score[1], &temp->score[2]);
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
}
else
{
tail->next = temp;
tail = temp;
}
}
}
return;
}
Stu * SearchStudent()
{
char str[20], ch;
Repeat:
printf("請輸入要尋找(正確的)學號或者姓名按'#'退出\n");
scanf("%s", str);
getchar();
if (str[0] == '#') return NULL;
Stu * temp = head;
if (head == NULL)
{
printf("未錄入資訊!\n");
return NULL;
}
else
{
while (temp)
{
if (!strcmp(temp->nums, str) || !strcmp(temp->name, str))
break;
temp = temp->next;
}
}
if (temp == NULL)
{
printf("未找到相關資訊!\n");
goto Repeat;
}
else
{
printf("查找成功,該同學資訊如下:\n");
printf("%10s %10s %10s",temp->nums,temp->name,temp->major);
printf("%10.2f %10.2f %10.2f\n",temp->score[0],temp->score[1],temp->score[2]);
}
printf("是否確認查詢無誤?(y or no)\n");
scanf("%c", &ch);
fflush(stdin);
if (ch != 'y' && ch != 'Y')
goto Repeat;
printf("已確認該名同學的資訊!\n");
return temp;
}
void ModifyStuData()
{
char str[20], ch;
Stu * temp = SearchStudent();
if (temp == NULL) printf("未指定修改內容位置!\n");
else
{
int num;
Repeat:
printf("請選擇需要修改的資訊:\n");
printf("1. 學號 2. 姓名 3. 專業\n");
printf("4. 語文成績 5. 數學成績 6. 英語成績\n");
while (scanf("%d", &num) != 1 || num > 6 || num < 1)
{
printf("選擇無效,請重新輸入范圍內的數字:\n");
fflush(stdin);
}
switch (num)
{
case 1:
printf("請輸入正確的學號:\n");
scanf("%s", temp->nums); break;
case 2:
printf("請輸入正確的名字:\n");
scanf("%s", temp->name); break;
case 3:
printf("請輸入正確的專業:\n");
scanf("%s", temp->major); break;
case 4:
printf("請輸入正確的語文成績:\n");
scanf("%f", &temp->score[0]); break;
case 5:
printf("請輸入正確的數學成績:\n");
scanf("%f", &temp->score[1]); break;
case 6:
printf("請輸入正確的英語成績:\n");
scanf("%f", &temp->score[2]); break;
}
getchar();
printf("資訊更改后如下:\n");
printf("%10s %10s %10s",temp->nums,temp->name,temp->major);
printf("%10.2f %10.2f %10.2f\n",temp->score[0],temp->score[1],temp->score[2]);
printf("是否確認無誤?(y or n)\n");
scanf("%c", &ch);
fflush(stdin);
if (ch != 'y' && ch != 'Y') goto Repeat;
printf("您已確認,更改完成!\n");
}
return;
}
void DeleteStuData()
{
if (head == NULL)
{
printf("還未錄入資訊!\n");
return;
}
Stu * temp = SearchStudent();
if (temp == NULL)
printf("未找到相關資訊!\n");
else if (head == tail)
{
free(head);
head = NULL;
tail = NULL;
printf("洗掉后已沒有任何學生資訊!\n");
}
else if (temp == head)
{
head = head->next;
free(temp);
printf("已完成洗掉操作!\n ");
}
else if (temp == tail)
{
Stu * pt = head;
while (pt->next->next) pt = pt->next;
free(tail);
tail = pt;
tail -> next = NULL;
printf("已完成洗掉操作!\n ");
}
else
{
Stu * pt = head;
while (pt->next != temp) pt = pt->next;
pt->next = temp->next;
free(temp);
printf("已完成洗掉操作!\n ");
}
return;
}
void RankingList()
{
int i;
printf("請選擇學科進行成績排名:\n");
printf("1. 語文 2. 數學 3. 英語\n");
while (scanf("%d", &i) && i > 3 && i < 1)
printf("請輸入范圍內的學科!\n");
fflush(stdin);
Stu * pre, * p, * end = NULL;
pre = p = head;
while (p->next != end)
{
p = head->next;
while (p != end)
{
if (pre->score[i-1] > p->score[i-1])
{
SwapStuNode(pre, p);
Stu * temp = pre;
pre = p;
p = temp;
}
pre = pre->next;
p = p->next;
}
end = pre;
pre = p = head;
}
return;
}
void SwapStuNode(Stu * p, Stu * q)
{
if (p == q || p == NULL || q == NULL)
return;
if (p == head)
{
if (q == tail)
{
tail->next = p;
p->next = NULL;
head = tail;
tail = head->next;
}
else
{
p->next = q->next;
q->next = p;
head = q;
}
}
else
{
if (q == tail)
{
Stu * pt = head;
while (pt->next != p) pt = pt->next;
pt->next = q;
q->next = p;
p->next = NULL;
tail = p;
}
else
{
Stu * pt = head;
while (pt->next != p) pt = pt->next;
pt->next = q;
p->next = q->next;
q->next = p;
}
}
return;
}
void PrintStuList()
{
Stu * phead = head;
if (phead == NULL) printf("暫無學生資訊!\n");
while (phead)
{
printf("%10s %10s %10s", phead->nums, phead->name, phead->major);
printf("%10.2f %10.2f %10.2f\n",phead->score[0],phead->score[1],phead->score[2]);
phead = phead -> next;
}
return;
}
謝謝觀看,如有錯誤還請指正!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/264866.html
標籤:其他
上一篇:11. 含k個3的數
下一篇:多種方式實作strlen
