1. 程式人生 > >連結串列-簡易學生成績管理

連結串列-簡易學生成績管理

LinkTable.h

view plaincopy to clipboardprint?
class StudentInfo  
{  
public:  
    char code[6];  
    float chinesescore;  
    float mathscore;  
    StudentInfo* next;  
    StudentInfo* prior;  
};  
 
//新增學生資訊,如果prior為NULL,則newinfo為頭結點  
StudentInfo* AddInfo(StudentInfo* prior,StudentInfo* newinfo);  
//根據編號刪除學生資訊  
bool DelStudentInfo(char* code,StudentInfo*& pHead);  
//根據編號查詢學生資訊  
StudentInfo* FindStudentInfoByCode(char* code,StudentInfo* pHead);  
//修改學生成績資訊  
void ModifyStudentInfo(StudentInfo* p);  
//從頭結點開始列印學生資訊  
void PrintStudentInfo(StudentInfo* pHead);  
//對指定的節點輸入學生資訊  
void InputInfo(StudentInfo* pInfo);  
//獲取最後一個節點  
StudentInfo* GetLastStudentInfo(StudentInfo* pHead);  
//儲存連結串列到檔案,順序儲存  
void SaveStudentInfoToFile(char* filename,StudentInfo* pHead);  
//從檔案中讀取連結串列  
void LoadStudentInfoFromFile(char* filename,StudentInfo*& pHead);  
//操作提示  
void ShowOperateInfo(void); 
class StudentInfo
{
public:
 char code[6];
 float chinesescore;
 float mathscore;
 StudentInfo* next;
 StudentInfo* prior;
};

//新增學生資訊,如果prior為NULL,則newinfo為頭結點
StudentInfo* AddInfo(StudentInfo* prior,StudentInfo* newinfo);
//根據編號刪除學生資訊
bool DelStudentInfo(char* code,StudentInfo*& pHead);
//根據編號查詢學生資訊
StudentInfo* FindStudentInfoByCode(char* code,StudentInfo* pHead);
//修改學生成績資訊
void ModifyStudentInfo(StudentInfo* p);
//從頭結點開始列印學生資訊
void PrintStudentInfo(StudentInfo* pHead);
//對指定的節點輸入學生資訊
void InputInfo(StudentInfo* pInfo);
//獲取最後一個節點
StudentInfo* GetLastStudentInfo(StudentInfo* pHead);
//儲存連結串列到檔案,順序儲存
void SaveStudentInfoToFile(char* filename,StudentInfo* pHead);
//從檔案中讀取連結串列
void LoadStudentInfoFromFile(char* filename,StudentInfo*& pHead);
//操作提示
void ShowOperateInfo(void);

LinkTable.cpp

view plaincopy to clipboardprint?
#include "LinkTable.h"  
#include <stdio.h>  
#include <string.h>  
 
//新增學生資訊,如果prior為NULL,則newinfo為頭結點  
StudentInfo* AddInfo(StudentInfo* prior,StudentInfo* newinfo)  
{  
    if (!prior)  
    {  
        newinfo->next = NULL;  
        newinfo->prior = NULL;  
    }  
    else 
    {  
        prior->next = newinfo;  
        newinfo->prior = prior;  
        newinfo->next = NULL;  
    }  
    memset(newinfo->code,0,sizeof(newinfo->code));  
    InputInfo(newinfo);  
    return newinfo;  
}  
 
//根據編號刪除學生資訊  
bool DelStudentInfo(char* code,StudentInfo*& pHead)  
{  
    StudentInfo* pDel = FindStudentInfoByCode(code,pHead);  
    if (pDel == NULL)  
        return false;  
    else 
    {  
        if (pDel == pHead)  
            pHead = pDel->next;  
        else 
        {             
            pDel->prior->next = pDel->next;  
            if (pDel->next != NULL)  
                pDel->next->prior = pDel->prior;  
        }  
        delete pDel;  
        pDel = NULL;  
        return true;  
    }  
}  
 
//根據編號查詢學生資訊  
StudentInfo* FindStudentInfoByCode(char* code,StudentInfo* pHead)  
{  
    StudentInfo* p = pHead;  
    while (p != NULL)  
    {  
        if (strcmp(p->code,code) == 0)  
        {  
            return p;  
        }  
        p = p->next;  
    }  
    return NULL;  
}  
//修改學生成績資訊  
void ModifyStudentInfo(StudentInfo* p)  
{  
    printf("%s號學生當前資訊:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);  
    printf("%s","請輸入修改後的語文成績:");  
    scanf("%f",&p->chinesescore);  
    printf("%s","請輸入修改後的數學成績:");  
    scanf("%f",&p->mathscore);  
    printf("修改完畢,%s號學生最新資訊:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);  
}  
 
//從頭結點開始列印學生資訊  
void PrintStudentInfo(StudentInfo* pHead)  
{  
    StudentInfo* p = pHead;  
    while (p!=NULL)  
    {  
        printf("%s號學生:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);  
        p = p->next;  
    }  
}  
 
//對指定的節點輸入學生資訊  
void InputInfo(StudentInfo* pInfo)  
{  
    printf("%s","請輸入學生編號:");  
    scanf("%s",pInfo->code);  
    printf("%s","請輸入語文成績:");  
    scanf("%f",&pInfo->chinesescore);  
    printf("%s","請輸入數學成績:");  
    scanf("%f",&pInfo->mathscore);  
}  
 
//獲取最後一個節點  
StudentInfo* GetLastStudentInfo(StudentInfo* pHead)  
{  
    if (pHead == NULL)  
        return NULL;  
    StudentInfo* p = pHead;  
    while (p)  
    {  
        if (p->next == NULL)  
            return p;  
        p = p->next;  
    }  
}  
 
//儲存連結串列到檔案,順序儲存  
void SaveStudentInfoToFile(char* filename,StudentInfo* pHead)  
{  
    FILE* f;  
    f= fopen(filename,"w");  
    StudentInfo* p = pHead;  
    while (p)  
    {  
        fwrite(p->code,sizeof(char),sizeof(p->code),f);  
        fwrite(&p->chinesescore,sizeof(float),1,f);  
        fwrite(&p->mathscore,sizeof(float),1,f);  
        //fwrite("/n",sizeof(char),1,f);  
        p=p->next;  
    }  
    //fwrite("/0",sizeof(char),1,f);  
    fclose(f);  
}  
 
//從檔案中讀取連結串列  
void LoadStudentInfoFromFile(char* filename,StudentInfo*& pHead)  
{  
    FILE* f;  
    f= fopen(filename,"r");  
    if (f == NULL)  
        return;  
    fseek(f, 0, SEEK_SET);   
    StudentInfo* p = NULL;  
    StudentInfo* newp = NULL;  
    int c;  
    //c = fgetc(f);  
    while (!feof(f))  
    {  
        //因為不能完全依賴feof判斷檔案是否到結尾,所以用fgetc的返回值來判斷,  
        //當讀取到結尾時,feof實際並沒有獲得到達結尾的資訊,所以這裡用fgetc繼續讀取一個字元,來判斷是否結束  
        //如果不是-1(結尾),那麼回退一個檔案位置,如果是-1,退出  
        c = fgetc(f);  
        if (c != -1)  
            fseek(f, -1, SEEK_CUR);  
        else 
            return;  
        if (pHead == NULL)  
        {  
            pHead = new StudentInfo;  
            pHead->next = NULL;  
            pHead->prior = NULL;  
            memset(pHead->code,0,sizeof(pHead->code));  
            fread(pHead->code,sizeof(char),sizeof(pHead->code),f);  
            fread(&pHead->chinesescore,sizeof(float),1,f);  
            fread(&pHead->mathscore,sizeof(float),1,f);  
            p = pHead;  
        }  
        else 
        {  
            newp = new StudentInfo;  
            newp->next = NULL;  
            newp->prior = NULL;  
            newp->prior = p;  
            memset(newp->code,0,sizeof(newp->code));  
            fread(newp->code,sizeof(char),sizeof(newp->code),f);  
            fread(&newp->chinesescore,sizeof(float),1,f);  
            fread(&newp->mathscore,sizeof(float),1,f);  
            p->next = newp;  
            p = newp;  
        }     
          
    }  
    fclose(f);  
}  
 
//操作提示  
void ShowOperateInfo(void)  
{  
    printf("%s/n","1:增加資訊");  
    printf("%s/n","2:刪除資訊");  
    printf("%s/n","3:查詢資訊");  
    printf("%s/n","4:修改資訊");  
    printf("%s/n","5:儲存資訊");  
    printf("%s/n","9:列印資訊");  
    printf("%s/n","0:結束");  

#include "LinkTable.h"
#include <stdio.h>
#include <string.h>

//新增學生資訊,如果prior為NULL,則newinfo為頭結點
StudentInfo* AddInfo(StudentInfo* prior,StudentInfo* newinfo)
{
 if (!prior)
 {
  newinfo->next = NULL;
  newinfo->prior = NULL;
 }
 else
 {
  prior->next = newinfo;
  newinfo->prior = prior;
  newinfo->next = NULL;
 }
 memset(newinfo->code,0,sizeof(newinfo->code));
 InputInfo(newinfo);
 return newinfo;
}

//根據編號刪除學生資訊
bool DelStudentInfo(char* code,StudentInfo*& pHead)
{
 StudentInfo* pDel = FindStudentInfoByCode(code,pHead);
 if (pDel == NULL)
  return false;
 else
 {
  if (pDel == pHead)
   pHead = pDel->next;
  else
  {   
   pDel->prior->next = pDel->next;
   if (pDel->next != NULL)
    pDel->next->prior = pDel->prior;
  }
  delete pDel;
  pDel = NULL;
  return true;
 }
}

//根據編號查詢學生資訊
StudentInfo* FindStudentInfoByCode(char* code,StudentInfo* pHead)
{
 StudentInfo* p = pHead;
 while (p != NULL)
 {
  if (strcmp(p->code,code) == 0)
  {
   return p;
  }
  p = p->next;
 }
 return NULL;
}
//修改學生成績資訊
void ModifyStudentInfo(StudentInfo* p)
{
 printf("%s號學生當前資訊:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);
 printf("%s","請輸入修改後的語文成績:");
 scanf("%f",&p->chinesescore);
 printf("%s","請輸入修改後的數學成績:");
 scanf("%f",&p->mathscore);
 printf("修改完畢,%s號學生最新資訊:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);
}

//從頭結點開始列印學生資訊
void PrintStudentInfo(StudentInfo* pHead)
{
 StudentInfo* p = pHead;
 while (p!=NULL)
 {
  printf("%s號學生:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);
  p = p->next;
 }
}

//對指定的節點輸入學生資訊
void InputInfo(StudentInfo* pInfo)
{
 printf("%s","請輸入學生編號:");
 scanf("%s",pInfo->code);
 printf("%s","請輸入語文成績:");
 scanf("%f",&pInfo->chinesescore);
 printf("%s","請輸入數學成績:");
 scanf("%f",&pInfo->mathscore);
}

//獲取最後一個節點
StudentInfo* GetLastStudentInfo(StudentInfo* pHead)
{
 if (pHead == NULL)
  return NULL;
 StudentInfo* p = pHead;
 while (p)
 {
  if (p->next == NULL)
   return p;
  p = p->next;
 }
}

//儲存連結串列到檔案,順序儲存
void SaveStudentInfoToFile(char* filename,StudentInfo* pHead)
{
 FILE* f;
 f= fopen(filename,"w");
 StudentInfo* p = pHead;
 while (p)
 {
  fwrite(p->code,sizeof(char),sizeof(p->code),f);
  fwrite(&p->chinesescore,sizeof(float),1,f);
  fwrite(&p->mathscore,sizeof(float),1,f);
  //fwrite("/n",sizeof(char),1,f);
  p=p->next;
 }
 //fwrite("/0",sizeof(char),1,f);
 fclose(f);
}

//從檔案中讀取連結串列
void LoadStudentInfoFromFile(char* filename,StudentInfo*& pHead)
{
 FILE* f;
 f= fopen(filename,"r");
 if (f == NULL)
  return;
 fseek(f, 0, SEEK_SET);
 StudentInfo* p = NULL;
 StudentInfo* newp = NULL;
 int c;
 //c = fgetc(f);
 while (!feof(f))
 {
  //因為不能完全依賴feof判斷檔案是否到結尾,所以用fgetc的返回值來判斷,
  //當讀取到結尾時,feof實際並沒有獲得到達結尾的資訊,所以這裡用fgetc繼續讀取一個字元,來判斷是否結束
  //如果不是-1(結尾),那麼回退一個檔案位置,如果是-1,退出
  c = fgetc(f);
  if (c != -1)
   fseek(f, -1, SEEK_CUR);
  else
   return;
  if (pHead == NULL)
  {
   pHead = new StudentInfo;
   pHead->next = NULL;
   pHead->prior = NULL;
   memset(pHead->code,0,sizeof(pHead->code));
   fread(pHead->code,sizeof(char),sizeof(pHead->code),f);
   fread(&pHead->chinesescore,sizeof(float),1,f);
   fread(&pHead->mathscore,sizeof(float),1,f);
   p = pHead;
  }
  else
  {
   newp = new StudentInfo;
   newp->next = NULL;
   newp->prior = NULL;
   newp->prior = p;
   memset(newp->code,0,sizeof(newp->code));
   fread(newp->code,sizeof(char),sizeof(newp->code),f);
   fread(&newp->chinesescore,sizeof(float),1,f);
   fread(&newp->mathscore,sizeof(float),1,f);
   p->next = newp;
   p = newp;
  } 
  
 }
 fclose(f);
}

//操作提示
void ShowOperateInfo(void)
{
 printf("%s/n","1:增加資訊");
 printf("%s/n","2:刪除資訊");
 printf("%s/n","3:查詢資訊");
 printf("%s/n","4:修改資訊");
 printf("%s/n","5:儲存資訊");
 printf("%s/n","9:列印資訊");
 printf("%s/n","0:結束");
}
 

Main方法

view plaincopy to clipboardprint?
#include "LinkTable.h"  
 
int main()  
{  
    ShowOperateInfo();  
    int i;  
    //curInfo當前節點,用來新增節點用  
    StudentInfo* curInfo = new StudentInfo;  
    StudentInfo* pHead = NULL;    
    LoadStudentInfoFromFile("e://link.txt",pHead);  
    //將curInfo指向最後一個節點  
    if (pHead != NULL)  
        curInfo = GetLastStudentInfo(pHead);  
    char code[10]={0};  
    while (true)  
    {  
        printf("%s","請輸入操作號:");  
        scanf("%d",&i);  
        switch (i)  
        {  
            //退出  
        case 0:  
            exit(0);  
            //新增  
        case 1:  
            if (pHead == NULL)  
            {  
                pHead = new StudentInfo;      
                curInfo = pHead = AddInfo(NULL,pHead);  
            }  
            else 
            {  
                StudentInfo* newInfo = new StudentInfo;  
                curInfo = AddInfo(curInfo,newInfo);  
            }  
            break;  
            //刪除  
        case 2:           
            printf("%s","請輸入要刪除的學生編號:");  
            scanf("%s",code);  
            DelStudentInfo(code,pHead);  
            curInfo = GetLastStudentInfo(pHead);  
            break;  
            //查詢  
        case 3:  
            printf("%s/n","請輸入要查詢的學生編號:");  
            scanf("%s",code);  
            {  
                StudentInfo* p = FindStudentInfoByCode(code,pHead);  
                if (p == NULL)  
                    printf("編號%s不存在",code);  
                else 
                {  
                    printf("%s號學生:語文:%f,數學:%f/n",p->code,p->chinesescore,p->mathscore);  
                }  
            }  
            break;  
            //修改  
        case 4:  
            printf("%s/n","請輸入要修改的學生編號:");  
            scanf("%s",code);  
            {  
                StudentInfo* p = FindStudentInfoByCode(code,pHead);  
                if (p == NULL)  
                    printf("編號%s不存在",code);  
                else 
                {  
                    ModifyStudentInfo(p);  
                }  
            }  
            break;  
        case 5:  
            SaveStudentInfoToFile("e://link.txt",pHead);  
            break;  
            //列印  
        case 9:  
            PrintStudentInfo(pHead);  
            break;  
        default:  
            exit(0);  
        }  
    }