目錄
功能展示
界面展示?
所有功能模塊:
功能1:菜單模塊(顯示功能菜單)
功能2:增加學生信息
功能3:輸出學生信息(查看所有學習信息)
功能4:修改學生信息
功能5:刪除學生信息
功能6:查詢單個學生信息
功能7:排序?qū)W習信息(按照學號升序排序)
功能8:退出管理系統(tǒng)
?心得分享?
過程梳理
個人總結(jié)
學生管理系統(tǒng)(完整代碼)
main.cpp?
student.h
student.cpp
功能展示
界面展示?
?
所有功能模塊:
功能1:菜單模塊(顯示功能菜單)
功能2:增加學生信息
功能3:輸出學生信息(查看所有學習信息)
功能4:修改學生信息
第一步:修改信息
第二步:查詢驗證
功能5:刪除學生信息
第一步:刪除信息
第一步:查詢驗證
功能6:查詢單個學生信息
- 按照學號查詢:
- 按照姓名查詢:
- 查詢?yōu)榭眨?/strong>
功能7:排序?qū)W習信息(按照學號升序排序)
在排序之前,先增加幾條數(shù)據(jù):
執(zhí)行排序:
驗證結(jié)果(該數(shù)據(jù)無法驗證是否執(zhí)行頭排序)
再插入一條比第一條小的數(shù)據(jù)(用于執(zhí)行頭排序):
執(zhí)行(頭)排序:
驗證結(jié)果:
功能8:退出管理系統(tǒng)
?心得分享?
過程梳理
- 首先要根據(jù)需求分析,將各個功能模塊羅列出來
- 數(shù)據(jù)建模,對數(shù)據(jù)進行封裝,創(chuàng)建一個student結(jié)構體,添加我們需要的各種數(shù)據(jù)。
?????
文章來源:http://www.zghlxwxcb.cn/news/detail-486295.html
- 創(chuàng)建一個可以循環(huán)輸入命令的菜單,并且可以退出,還可以檢查輸入的指令是否匹配。
- 然后一個個功能模塊實現(xiàn)就可以了。但要注意實現(xiàn)功能的順序。(寫功能也要有順序,這樣可以事半功倍)
- 第一個功能,肯定就是添加了,每當需要添加的時候,就需要申請一塊內(nèi)存,創(chuàng)建一個student結(jié)構體,并賦予相應的數(shù)據(jù)。
- 第二個功能,就是打印全部數(shù)據(jù)。
- 第三個功能,實現(xiàn)單個數(shù)據(jù)的查詢。
- 第四個功能,實現(xiàn)單個數(shù)據(jù)的刪除。(刪除和修改都差不多)
- 第五個功能,實現(xiàn)單個數(shù)據(jù)的修改。
- 第六個功能,也就是最難的功能——鏈表排序
- 最后,對細節(jié)進行處理。
個人總結(jié)
- 當思維混亂的時候,不妨畫圖來理解。
- 寫循環(huán)時,一定要明確跳出的邏輯和循環(huán)的次數(shù)和迭代方法。
- 在實現(xiàn)功能的時候,還需要考慮特殊情況!邊界條件的處理尤其重要。(在排序的時候,一定要考慮多個因素)
- 總體難度不大,但是需要一步一步去實現(xiàn),更多的是考驗對細節(jié)的處理,后面又看了別人的實現(xiàn)方法,發(fā)現(xiàn)自己的思維方式還是比較呆板,別人幾行就可以搞定的,自己還是用了各種if分支和for循環(huán)才搞定,還得加強自己的思維能力,多刷算法!
學生管理系統(tǒng)(完整代碼)
?文章來源地址http://www.zghlxwxcb.cn/news/detail-486295.html
main.cpp?
# include "student.h"
int main(){
StudentNode* s = NULL;
int command;
while (true)
{
myMenu(); // 展示菜單
scanf("%d", &command); // 輸入
switch (command)
{
case 1:// 增加學生信息
AddStudent(&s);
break;
case 2:// 刪除學生信息
DeleteStudent(&s);
break;
case 3:// 修改學生信息
UpdateStudent(&s);
break;
case 4:// 查詢學生信息 (可按照姓名和學號)
SearchStudent(&s);
break;
case 5:// 輸出學生信息 (打印所有學生信息)
MyPrint(s);
break;
case 6:// 排序?qū)W生信息 (按照學號升序排序)
MySort(&s);
break;
case 0:// 退出管理系統(tǒng)
exit();
break;
default : //輸入有誤
error();
break;
}
system("pause");
}
}
?student.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int STDateTypeOne;
typedef char STDateTypeTow;
// 學生
typedef struct StudentNode
{
int ID; // 學號
int Score; // 排名
char Name[50]; // 姓名
char Sex[10]; // 性別
char Profession[50]; // 專業(yè)
char Phone[20]; // 電話號碼
char Q[20]; // QQ號
struct StudentNode* next; // 指針域
}StudentNode;
// 主菜單界面
void myMenu();
// 退出系統(tǒng)
void exit();
// 命令有誤
void error();
// ----------------------------------CRUD模塊
void AddStudent(StudentNode** s); // 添加
void DeleteStudent(StudentNode** s); // 刪除
void UpdateStudent(StudentNode** s); // 更新
void SearchStudent(StudentNode** s); // 查詢
// ----------------------------------其他模塊
void MyPrint(StudentNode* s); // 輸出學生信息 (打印所有學生信息)
void MySort(StudentNode** s); // 排序?qū)W生信息 (按照學號升序排序)
?student.cpp
#include "student.h"
// 字符判斷
int MyStrcmp(const char* str1, const char* str2)
{
assert(NULL != str1);
assert(NULL != str2);//防御性編程
while (*(unsigned char*)str1 == *(unsigned char*)str2)
{
if (*(unsigned char*)str1 != '\0')
{
return 0;//當*(unsigned char*)str1 ==*(unsigned char*)str2==‘\0'時兩個字符串完全相等
}
str1++; //比較下個字符
str2++;
}
//*(unsigned char*)str1 與*(unsigned char*)str2的差值與返回值正負匹配
return *(unsigned char*)str1 - *(unsigned char*)str2;
}
// 菜單
void myMenu(){
system("cls");
printf("****************************************************************\n");
printf("*********** \033[34m學生信息管理系統(tǒng) \033[30m ***********\n");
printf("----------------------------------------------------------------\n");
printf("*********** \033[32m 1 \033[30m---- 增加學生信息 ***********\n");
printf("*********** \033[32m 2 \033[30m---- 刪除學生信息 ***********\n");
printf("*********** \033[32m 3 \033[30m---- 修改學生信息 ***********\n");
printf("*********** \033[32m 4 \033[30m---- 查詢學生信息 ***********\n");
printf("*********** \033[32m 5 \033[30m---- 輸出學生信息 ***********\n");
printf("*********** \033[32m 6 \033[30m---- 排序?qū)W生信息 ***********\n");
printf("*********** \033[32m 0 \033[30m---- 退出管理系統(tǒng) ***********\n");
printf("****************************************************************\n");
printf("\033[31m");
printf("請選擇想要實現(xiàn)的功能(數(shù)字):");
}
// 退出管理系統(tǒng)
void exit()
{
system("cls");
printf("歡迎下次使用學生信息管理系統(tǒng)!\n");
exit(0); // 結(jié)束程序
}
// 提示輸入錯誤
void error(){
printf("請輸入對應指令!\n");
}
// ----------------------------------CRUD模塊
void AddStudent(StudentNode** s){
int ID; // 學號
int Score; // 排名
char Name[50]; // 姓名
char Sex[10]; // 性別
char Profession[50]; // 專業(yè)
char Phone[20]; // 電話號碼
char Q[20]; // QQ號
// 創(chuàng)建新節(jié)點
StudentNode* newnode = (StudentNode*)malloc(sizeof(StudentNode)); //將申請的空間---強轉(zhuǎn)為 指針變量
// 輸入數(shù)據(jù)
printf("請輸入新增學生的相關信息:\n");
printf("學號:");
scanf("%d", &newnode->ID); // 輸入
printf("排名:");
scanf("%d", &newnode->Score); // 輸入
printf("姓名:");
scanf("%s", &newnode->Name); // 輸入
printf("性別: ");
scanf("%s", &newnode->Sex); // 輸入
printf("專業(yè): ");
scanf("%s", &newnode->Profession); // 輸入
printf("電話號碼:");
scanf("%s", &newnode->Phone); // 輸入
printf("QQ號: ");
scanf("%s", &newnode->Q); // 輸入
newnode->next = NULL;
// 尾插
if(*s == NULL){
*s = newnode;
}
else{
printf("添加中...\n");
//找到尾節(jié)點
StudentNode* tp = *s;
StudentNode* tmp;
while(tp->next != NULL){
tmp = tp->next;
tp = tmp;
}
tp->next = newnode;
}
printf("添加成功!\n");
}
void DeleteStudent(StudentNode** s){
//判斷鏈表是不是空
if(*s == NULL){
printf("當前還沒有任何數(shù)據(jù)!\n ");
return;
}
// 展示所以數(shù)據(jù)
MyPrint(*s);
int id;
// 選擇刪除的數(shù)據(jù)
printf("您要刪除第幾條信息: ");
scanf("%d", &id); // 輸入
// 進行刪除
StudentNode* tp = *s;
StudentNode* flag= *s;
// 刪頭節(jié)點
if(id == 1){
StudentNode* p = (*s)->next;
free(*s);
*s = p;
printf("刪除成功!\n");
return;
}
for(int i = 1; i< id; i++){
flag = tp;
tp = tp->next;
}
flag->next = tp->next;
free(tp);
printf("刪除成功!\n");
}
void UpdateStudent(StudentNode** s){
//判斷鏈表是不是空
if(*s == NULL){
printf("當前還沒有任何數(shù)據(jù)!\n ");
return;
}
// 展示所以數(shù)據(jù)
MyPrint(*s);
int id;
// 選擇修改的信息
printf("您要修改第幾條信息: ");
scanf("%d", &id); // 輸入
// 進行修改
StudentNode* tp = *s;
StudentNode* flag= *s;
for(int i = 1; i< id; i++){
tp = tp->next;
}
// 請輸入您要修改的數(shù)據(jù)
printf("請輸入您要修改的數(shù)據(jù) :\n");
printf("學號:");
scanf("%d", &tp->ID); // 輸入
printf("排名:");
scanf("%d", &tp->Score); // 輸入
printf("姓名:");
scanf("%s", &tp->Name); // 輸入
printf("性別: ");
scanf("%s", &tp->Sex); // 輸入
printf("專業(yè): ");
scanf("%s", &tp->Profession); // 輸入
printf("電話號碼:");
scanf("%s", &tp->Phone); // 輸入
printf("QQ號: ");
scanf("%s", &tp->Q); // 輸入
// 修改成功!
printf("修改成功! \n");
}
void SearchStudent(StudentNode** s){
//判斷鏈表是不是空
if(*s == NULL){
printf("當前還沒有任何數(shù)據(jù)!\n ");
return;
}
StudentNode* p = *s;
int choice;
int myid; // 學號
char myname[50]; // 姓名
// 選擇查詢方法
printf("請選擇查詢方式(0:學號 / 1:姓名) : ");
scanf("%d", &choice); // 輸入
// 學號查詢
if(choice == 0){
printf("請輸入查詢的學號 : ");
scanf("%d", &myid); // 輸入
while(1){
// 邊界判斷
if(p->next == NULL){
if(p->ID == myid){
printf("__________________________________________");
printf("_______________________________________\n");
printf("|%d\t|%d\t|%s\t|%s\t|%s\t\t|%s\t\t|%s\t\t|\n",
p->ID, p->Score, p->Name, p->Sex, p->Profession, p->Phone, p->Q);
}else{
printf("您所查找的學生不存在!\n");
}
break;
}
// 判斷學號
if(p->ID == myid){
printf("__________________________________________");
printf("_______________________________________\n");
printf("|%d\t|%d\t|%s\t|%s\t|%s\t\t|%s\t\t|%s\t\t|\n",
p->ID, p->Score, p->Name, p->Sex, p->Profession, p->Phone, p->Q);
break;
}
// 繼續(xù)遍歷
p = p->next;
}
return;
}
if(choice == 1){
printf("請輸入查詢的姓名 : ");
scanf("%s", &myname); // 輸入
while(1){
// 邊界判斷
if(p->next == NULL){
// 判斷姓名
int i = MyStrcmp(p->Name, myname);
if(i == 0){
printf("|學號\t|排名\t|姓名\t|性別\t|專業(yè)\t\t|電話\t\t|QQ\t\t|\n");
printf("__________________________________________");
printf("_______________________________________\n");
printf("|%d\t|%d\t|%s\t|%s\t|%s\t\t|%s\t\t|%s\t\t|\n",
p->ID, p->Score, p->Name, p->Sex, p->Profession, p->Phone, p->Q);
break;
}
else{
printf("您所查找的學生不存在!\n");
}
break;
}
// 判斷姓名
int i = MyStrcmp(p->Name, myname);
if(i == 0){
printf("|學號\t|排名\t|姓名\t|性別\t|專業(yè)\t\t|電話\t\t|QQ\t\t|\n");
printf("__________________________________________");
printf("_______________________________________\n");
printf("|%d\t|%d\t|%s\t|%s\t|%s\t\t|%s\t\t|%s\t\t|\n",
p->ID, p->Score, p->Name, p->Sex, p->Profession, p->Phone, p->Q);
break;
}
// 繼續(xù)遍歷
p = p->next;
}
return;
}
printf("請輸入正常的指令!\n");
}
// ----------------------------------其他模塊
void MyPrint(StudentNode* s){
system("cls");
StudentNode* p = s;
if(!p){
printf("暫無任何信息!\n");
return;
}
printf("|學號\t|排名\t|姓名\t|性別\t|專業(yè)\t\t|電話\t\t|QQ\t\t|\n");
while (p != NULL)
{
printf("__________________________________________");
printf("_______________________________________\n");
printf("|%d\t|%d\t|%s\t|%s\t|%s\t\t|%s\t\t|%s\t\t|\n",
p->ID, p->Score, p->Name, p->Sex, p->Profession, p->Phone, p->Q);
p = p->next;
}
}
//=============冒泡排序====================升序
void MySort(StudentNode** s){
StudentNode* p = *s;
StudentNode* temp;
int lenth = 0;
// 判斷特殊情況 長度為1
if((p -> next == NULL))
{
printf("長度為1,無需排序!\n");
return;
}
// 判斷特殊情況 長度為2
if((p -> next -> next == NULL))
{
if(p->ID < p->next->ID){
temp = p; // 保存頭節(jié)點
*s = (*s)->next; // 頭節(jié)點換為下一個節(jié)點
(*s)->next = temp;
(*s)->next->next = NULL;
}
printf("排序完成! \n");
return;
}
// 獲取長度
while(1) {
lenth++;
if(p->next == NULL){
// 退出
break;
}
p = p->next;
}
printf("長度為%d !\n", lenth);
// 冒泡排序
StudentNode* head = *s;
StudentNode* pre = *s; // 當前
StudentNode* cur = (*s)->next; // 當前 +1
StudentNode* next = (*s)->next->next; // 當前 + 2
StudentNode* end = NULL;
for (int i = lenth; i >= 0; i--) {
pre = head;
cur = pre->next;
next = cur->next;
while(next != NULL) {
if (cur->ID > next->ID) {
cur->next = next->next;
pre->next = next;
next->next = cur;
next = cur->next;
pre = pre->next;
}
else {
pre = pre->next;
cur = cur->next;
next = next->next;
}
}
}
// 頭結(jié)點 排序
head = *s;
cur = *s; // 當前
// cur到尾巴
while(cur->next != NULL){
// 大于上一個,小于下一個
if(head->ID > cur->ID && head->ID < cur->next->ID ){
// 頭節(jié)點換為下一個節(jié)點
*s = (*s)->next;
// 插入 head
temp = cur->next;
cur->next = head;
head->next = temp;
printf("頭排序完成!\n");
printf("排序完成!\n");
return;
}
cur = cur->next; // 往下走
}
// 單獨比較尾巴
if(head->ID > cur->ID){
// 頭節(jié)點換為下一個節(jié)點
*s = (*s)->next;
cur->next = head;
head->next = NULL;
printf("頭排序完成!\n");
}
printf("排序完成!\n");
}
到了這里,關于C語言——學生信息管理系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!