C語言鏈表與文件之學生成績管理系統
先上圖
想要一起學習C 的可以加裙二四八八九四四三零,有很多大神一起學習交流,有資源,然後可以訂閱轉發一下
頭文件
#ifndef SCORE_H
#define SCORE_H
#include
#include
#include
#include
typedef struct student
{
int num;
char name[20];
char ginder;
int grade;
int Chinese;
int Math;
int English;
int Computer;
int Total;
int Aver;
struct student *next;
}LIST;
int CreatLList(LIST *p,int n); //創建鏈表並輸入學生信息
int Read(LIST *p); //從文件讀取學生信息
int Write(LIST *p); //將學生信息寫入文件
int stu_edit(LIST *head); //學生信息編輯菜單
LIST *FindLList(LIST *p, int num); //查找學生信息
int InsertLList(LIST *p, int iPos); //插入學生信息
void OutputLList(LIST *p); //輸出學生信息
int DeleteLList(LIST *p, int iPos); //刪除學生信息
void FreeList(LIST *p); //釋放鏈表
想要一起學習C 的可以加裙二四八八九四四三零,有很多大神一起學習交流,有資源,然後可以訂閱轉發一下
#endif
主函數(菜單)
#include"score.h"
int main(void)
{
int iChoice;
int flag = 1;
int n; //學生人數
LIST *head=(LIST *)malloc(sizeof(LIST)); //接收頭結點
head->next = NULL;
while (1)
{
system("cls");
printf(" 成績管理程序菜單
");
printf(" 0.從文件讀取學生信息
");
printf(" 1.學生信息輸入
");
printf(" 2.學生信息管理
");
printf(" 3.學生信息輸出
");
printf(" 4.退出
");
printf("
請輸入你的選擇:");
flag=scanf("%d", &iChoice);
if (flag == 0)
break;
switch (iChoice)
{
case 0:
free(head);
Read(head);
getche();
break;
case 1:printf("學生人數:");
scanf("%d", &n);
printf("輸入學生信息:
");
CreatLList(head,n);
Write(head);
break;
case 2:
if (head)
stu_edit(head);
else
printf("Can"t informations
");
getche();
Write(head);
break;
case 3:OutputLList(head);
getche();
break;
case 4:FreeList(head);
return 0;
break;
default:
break;
}
}
return 0;
}
各功能函數
#include"score.h"
int CreatLList(LIST *p,int n)
{
int i;
LIST *pnew;
if (n == 0)
return 1;
for (i = 0; i
{
pnew = (LIST *)malloc(sizeof(LIST));
printf("請輸入學號 姓名 性別 班級 語文 數學 英語 計算機
");
scanf("%d %s %c %d %d %d %d %d", &pnew->num, pnew->name, &pnew->ginder, &pnew->grade, &pnew->Chinese, &pnew->Math, &pnew->English, &pnew->Computer);
pnew->Total = pnew->Chinese + pnew->Math + pnew->English + pnew->Computer;
pnew->Aver = (pnew->Total) / 4;
p->next = pnew;
p = pnew;
}
pnew->next = NULL;
return 0;
}
int Write(LIST *p)
{
FILE *fp;
if ((fp = fopen("informations.dat", "wb")) == NULL)
return 1;
p = p->next;
while (p)
{
fwrite(p, sizeof(LIST), 1, fp);
p = p->next;
}
fclose(fp);
return 0;
}
int Read(LIST *p)
{
LIST *pnew;
FILE *fp;
int i;
if ((fp = fopen("informations.dat", "rb")) == NULL)
return 1;
fseek(fp, 0, 2);
i = ftell(fp)/sizeof(LIST);
rewind(fp);
while(i--)
{
pnew = (LIST *)malloc(sizeof(LIST));
fread(pnew, sizeof(LIST), 1, fp);
p->next = pnew;
p = pnew;
}
pnew->next = NULL;
fclose(fp);
return 0;
}
int stu_edit(LIST *head)
{
int iChoice;
int flag;
//1.
int num;
int iPos;
LIST *pfind;
while (1)
{
system("cls");
printf("1.查詢學生信息
");
printf("2.修改學生信息
");
printf("3.插入新學生
");
printf("4.刪除學生
");
printf("5.返回上一級
");
printf("輸入你的選擇:");
flag = scanf("%d", &iChoice);
if (flag == 0)
return 0;
switch (iChoice)
{
case 1:printf("請輸入要查找學生的學號:");
scanf("%d", &num);
FindLList(head, num);
getche();
break;
case 2:printf("請輸入要修改學生的學號:");
scanf("%d", &num);
pfind = FindLList(head, num);
printf("
請修改信息:");
scanf("%d %s %c %d %d %d %d %d", &pfind->num, pfind->name, &pfind->ginder, &pfind->grade, &pfind->Chinese, &pfind->Math, &pfind->English, &pfind->Computer);
pfind->Total = pfind->Chinese + pfind->Math + pfind->English + pfind->Computer;
pfind->Aver = (pfind->Total) / 4;
break;
case 3:printf("輸入插入位置:");
scanf("%d", &iPos);
InsertLList(head,iPos);
getche();
break;
case 4:printf("輸入刪除位置:");想要一起學習C 的可以加裙二四八八九四四三零,有很多大神一起學習交流,有資源,然後可以訂閱轉發一下
scanf("%d", &iPos);
DeleteLList(head, iPos);
getche();
break;
case 5:
return 0;
break;
}
}
return 0;
}
LIST *FindLList(LIST *p, int num)
{
while (p)
{
if (p->num == num)
break;
p = p->next;
}
if (p==NULL)
printf("Can"t Find!
");
else
{
printf("學號 姓名 性別 班級 語文 數學 英語 計算機 總分 平均分
");
printf("%d %s %c %d %d %d %d %d %d %d ", p->num, p->name, p->ginder, p->grade, p->Chinese, p->Math, p->English, p->Computer, p->Total, p->Aver);
}
return p;
}
int InsertLList(LIST *p,int iPos)
{
LIST *pnew;
int i;
if (iPos == 0)
{
printf("錯誤!
");
return 1;
}
for (i = 1; i
{
p = p->next;
}
if (p->next == NULL)
{
printf("Can"t Find the list!
");
return 1;
}
pnew = (LIST *)malloc(sizeof(LIST));
scanf("%d %s %c %d %d %d %d %d", &pnew->num, pnew->name, &pnew->ginder, &pnew->grade, &pnew->Chinese, &pnew->Math, &pnew->English, &pnew->Computer);
pnew->Total = pnew->Chinese + pnew->Math + pnew->English + pnew->Computer;
pnew->Aver = (pnew->Total) / 4;
pnew->next = p->next;
p->next = pnew;
return 0;
}
int DeleteLList(LIST *p, int iPos)
{
LIST *pfree;
int i;
if (iPos == 0)
{
printf("Can"t delete!
");
return 1;
}
for (i = 1; i
{
p = p->next;
}
if (p->next == NULL)
{
printf("Can"t find the list!
");
return 1;
}
pfree = p->next;
p->next = pfree->next;
free(pfree);
return 0;
}
void OutputLList(LIST *p)
{
printf("學號 姓名 性別 班級 語文 數學 英語 計算機 總分 平均分
");
while (p->next)
{
p = p->next;
printf("%d %s %c %d %d %d %d %d %d %d
", p->num, p->name, p->ginder, p->grade, p->Chinese, p->Math, p->English, p->Computer, p->Total, p->Aver);
}
}
void FreeList(LIST *p)
{
LIST *pfree=p->next;
while (pfree)
{
p = p->next;
free(pfree);
pfree = p;
}
}
經過多次調試,應該沒有bug了哈哈,
喜歡就收藏了吧
想要一起學習C 的可以加裙二四八八九四四三零,有很多大神一起學習交流,有資源,然後可以訂閱轉發一下
※五分鐘學會炫酷C語言實現動態順序表
※編程語言的選擇重不重要!
※C程序解析:C語言代碼中的每一行都是什麼意思?
※C語言篇之淺析程序的靈魂演算法
※C語言編程之C語言遞歸
TAG:C加加 |
※晨科文件管理系統,助力文件管理集中化
※漫談文件系統
※Log結構文件系統的設計與實現
※大勢至區域網共享文件管理系統全面強化共享文件管理
※Linux之索引式文件系統
※iOS 鈴聲文件製作
※Hadoop文件系統元數據管理機制
※如何在手機端構建完美的文件整理系統?
※一鍵生成文件夾中所有文件列表到Excel電子表格
※系統小技巧:跟蹤監視系統或文件的細微變化
※嚇人技術會是它?華為發布新的文件系統
※C 語言獲取任意文件的縮略圖
※區塊鏈里程碑!迅雷發布「迅雷鏈」文件系統
※交換機的密碼恢復命令操作與TFTP文件管理
※文件存儲CFS
※文件系統目錄結構及常用操作命令
※為什麼總理說「中歐聯合聲明不是一般的表態性文件」
※文件數據IO操作
※CSV配置文件的優化策略
※PHP 生成 CSV 文件