亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用C語(yǔ)言實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)

 更新時(shí)間:2021年09月08日 12:52:43   作者:機(jī)智蛋蛋  
這篇文章主要介紹了使用C語(yǔ)言實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近正在學(xué)習(xí)C語(yǔ)言,搞了一個(gè)學(xué)生成績(jī)管理系統(tǒng)的小程序,代碼粗陋,大佬勿噴。

先上圖:

主菜單

整個(gè)程序采用鏈表來(lái)管理學(xué)生成績(jī),保存學(xué)生成績(jī)用的是 文件處理函數(shù),使用 fopen(FilePath, “wb”) 將學(xué)生信息在內(nèi)存中的數(shù)據(jù)直接寫入文件,相應(yīng)的打開(kāi)程序后讀取時(shí)使用 fopen(FilePath, “rb”) 將數(shù)據(jù)直接再次還原到內(nèi)存中去。

選項(xiàng)6 是手動(dòng)保存鏈表數(shù)據(jù)及配置數(shù)據(jù)。
選項(xiàng)7 配置每次修改鏈表中的數(shù)據(jù)是否需要自動(dòng)保存。

選項(xiàng)1 進(jìn)入后,直接按照提示輸入 學(xué)生的各個(gè)信息,一行一個(gè)


選項(xiàng)3 修改學(xué)生信息,進(jìn)入后選擇以什么方式查找要修改的學(xué)生,然后輸入學(xué)生的學(xué)號(hào)或姓名,找到之后(因?yàn)榭赡艽嬖谥孛耐瑢W(xué),所以這里做了序號(hào)索引),輸入結(jié)果索引,然后根據(jù)提示將該學(xué)生的信息重新輸入一遍,達(dá)到修改的效果。

選項(xiàng)4 查詢,功能如上圖所示

選項(xiàng)5 排序,根據(jù)提示輸入條件,排序會(huì)導(dǎo)致鏈表數(shù)據(jù)改變,所以回到主菜單后會(huì)自動(dòng)保存數(shù)據(jù)到文件

選項(xiàng)8 投票系統(tǒng),輸入鏈表中存在的人名,對(duì)應(yīng)下方投票結(jié)果會(huì)實(shí)時(shí)變動(dòng),并排序,最高票數(shù)的人顏色高亮。


選項(xiàng)9 輸入后右側(cè)出現(xiàn)提示“導(dǎo)出成功”,則在相應(yīng)目錄下會(huì)出現(xiàn)student_export.txt文件,里面包含有鏈表中所有學(xué)生信息,這里采用的是 fprintf 函數(shù)輸出到文件。

鏈表排序時(shí),數(shù)據(jù)交換使用了kernel32.dll 中的內(nèi)存數(shù)據(jù)拷貝 MoveMemory 函數(shù),只需要將除鏈表中的pNext 以外的數(shù)據(jù)進(jìn)行拷貝交換即可。

下面上代碼:

// 學(xué)生管理系統(tǒng).cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "string.h"
#include "conio.h"
#include "windows.h"
#include "stdlib.h"

#define LIST_TITLE "學(xué)號(hào) 姓名 性別 語(yǔ)文 數(shù)學(xué) 英語(yǔ)\n"
#define LIST_TITLE_1 "學(xué)號(hào) 姓名 性別 語(yǔ)文 數(shù)學(xué) 英語(yǔ) 均分\n"
#define FILE_DATABASE "C:\\student_database.dat"
#define FILE_EXPORT "C:\\student_export.txt"
//顏色
enum
{
 BLACK,
 BLUE,
 GREEN,
 CYAN,
 RED,
 MAGENTA,
 BROWN,
 LIGHTGRAY,
 DARKGRAY,
 LIGHTBLUE,
 LIGHTGREEN,
 LIGHTCYAN,
 LIGHTRED,
 LIGHTMAGENTA,
 YELLOW,
 WHITE
};
//功能索引
enum
{
 Func_Add = 1,//添加學(xué)生信息
 Func_Delete,//刪除
 Func_Modify,//修改
 Func_Search,//搜索
 Func_Sort,//排序
 Func_Save,//保存
 Func_AutoSave,//自動(dòng)保存
 Func_Vote,//投票系統(tǒng)
 Func_Export,//導(dǎo)出學(xué)生信息
 Func_ExitSystem//退出系統(tǒng)
};
struct Student
{
 int num;//學(xué)號(hào)
 char name[20];//姓名
 char sex[8];//性別
 float score[3];//三門課程成績(jī)
 float averge;//平均分
 int count;//投票計(jì)數(shù)
 Student* pNext;

};
Student* G_pStuHead;//鏈表頭
bool G_autoStore = false;//自動(dòng)保存

/************************************************************************/
/*設(shè)置字體顏色*/
/************************************************************************/
void setFontColor(int ForgC)
{
 WORD wColor;
 HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
 CONSOLE_SCREEN_BUFFER_INFO csbi;
 
 if(GetConsoleScreenBufferInfo(hOutput, &csbi))
 {
  //設(shè)置字體顏色
  wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
  SetConsoleTextAttribute(hOutput, wColor);
 }
}
/************************************************************************/
/*光標(biāo)跳轉(zhuǎn)到指定位置*/
/************************************************************************/
void gotoxy(int x, int y)
{
    // 更新光標(biāo)位置 
    COORD pos;
 
    HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(hOutput, pos);
 // 隱藏光標(biāo) 
//     CONSOLE_CURSOR_INFO cursor;
//     cursor.bVisible = FALSE;
//     cursor.dwSize = 1; //值介于1 ~ 100 之間 單元格底部為下劃線 ~ 完全填充單元格
//     SetConsoleCursorInfo(hOutput, &cursor);
 
}
/************************************************************************/
/*主菜單中指定位置打印是否自動(dòng)保存*/
/************************************************************************/
void printAutoStoreInfo()
{
 setFontColor(LIGHTGREEN);
 gotoxy(13, 10);
 printf("%s\n", G_autoStore ? "是" : "否");
 setFontColor(WHITE);
}
/************************************************************************/
/*顯示最上面的系統(tǒng)標(biāo)題*/
/************************************************************************/
void showSystemTitle()
{
 setFontColor(LIGHTGREEN);
 printf("--------------------------------------------------------\n");
 printf("\t\t歡迎進(jìn)入學(xué)生管理系統(tǒng)\n");
 printf("--------------------------------------------------------\n");
 setFontColor(WHITE);
}
/************************************************************************/
/*初始化屏幕*/
/************************************************************************/
void initScreen()
{
 system("CLS");
 showSystemTitle();
 printf("請(qǐng)輸入數(shù)字序列號(hào),選擇您要執(zhí)行的操作:\n");
 printf("1、添加學(xué)生信息\n");
 printf("2、刪除學(xué)生信息\n");
 printf("3、修改學(xué)生信息\n");
 printf("4、查詢學(xué)生信息\n");
 printf("5、排序\n");
 printf("6、保存(如打開(kāi)自動(dòng)保存,則無(wú)需手動(dòng)執(zhí)行)\n");
 printf("7、自動(dòng)保存:");
 printAutoStoreInfo();
 printf("8、投票系統(tǒng)\n");
 printf("9、導(dǎo)出學(xué)生信息\n");
 setFontColor(LIGHTRED);
 printf("10、退出學(xué)生管理系統(tǒng)\n");
 setFontColor(WHITE);

}
/************************************************************************/
/*從指定位置開(kāi)始清除指定長(zhǎng)度元素*/
/************************************************************************/
void gotodelete(int x, int y, int length)
{
 int i;
    
 for (i = 0; i < length; i++)
 {
  gotoxy(x + i, y);
  printf(" ");
 }
}
/************************************************************************/
/*清除指定位置元素*/
/************************************************************************/
void gotodelete(int x, int y)
{
 gotodelete(x, y, 1);
}
/************************************************************************/
/*投票系統(tǒng) */
/************************************************************************/
void voteSystem()
{
 bool hasFound;
 char name[20];
 int count, i, j;
 Student* pStu, *pTmp;
 Student** pStuArr;

 //初始化票數(shù)清零
 pStu = G_pStuHead->pNext;
 while(pStu != NULL)
 {
  pStu->count = 0;
  pStu = pStu->pNext;
 }

 count = 0;
 pStuArr = (Student**)malloc(4 * 100);//用于存放已經(jīng)獲得票數(shù)的同學(xué)指針
 gotoxy(0, 6);
 printf("投票結(jié)果如下:\n");
 gotoxy(0, 3);
 printf("請(qǐng)?jiān)谙路捷斎肽阆胪督o的人的姓名(輸入-1返回主菜單):\n");
 
 while (1)
 {
  gotodelete(0, 4, 20);//清空輸入行
  gotoxy(0, 4);
  scanf("%s", name);
  
  if(strcmp(name, "-1") == 0)
  {
   break;
  }
  hasFound = false;
  pStu = G_pStuHead->pNext;
  //在系統(tǒng)中查找對(duì)應(yīng)的人名
  while(pStu != NULL)
  {
   if(strcmp(pStu->name, name) == 0)
   {
    hasFound = true;
    break;
   }
   pStu = pStu->pNext;
  }
  if(! hasFound)
  {
   printf("查無(wú)此人?。?!");
   Sleep(1000);
   gotodelete(0, 5, 20);
   continue;
  }
  //找到之后,這個(gè)人所對(duì)應(yīng)的票數(shù)+1
  pStu->count++;
  for (i = 0; i < count; i++)
  {
   if(pStuArr[i] == pStu)
   {
    break;
   }
  }
  if(i == count)//說(shuō)明未找到,則添加進(jìn)候選人數(shù)組
  {
   pStuArr[count++] = pStu;
   if(count % 100 == 0)
   {
    pStuArr = (Student**)realloc(pStuArr, count + 100);
   }
  }
  //冒泡排序,票數(shù)
  for (i = 0; i < count - 1; i++)
  {
   for (j = i + 1; j < count; j++)
   {
    if(pStuArr[i]->count < pStuArr[j]->count)
    {
     pTmp = pStuArr[i];
     pStuArr[i] = pStuArr[j];
     pStuArr[j] = pTmp;
    }
   }
  }
  gotoxy(0, 7);//跳轉(zhuǎn)到打印票數(shù)的那行
  //打印票數(shù)
  for (i = 0; i < count; i++)
  {
   if(i == 0)
   {
    setFontColor(LIGHTGREEN);
   }
   else
   {
    setFontColor(WHITE);
   }
   printf("%d %s\t%d\n", pStuArr[i]->num, pStuArr[i]->name, pStuArr[i]->count);
  }

 }
 free(pStuArr);
 
}
/************************************************************************/
/*導(dǎo)出學(xué)生信息(明文) */
/************************************************************************/
bool exportStudentInfo()
{
 Student* pStu;
 FILE* fp;

 pStu = G_pStuHead->pNext;

 if((fp = fopen(FILE_EXPORT, "w")) == NULL)
 {
  return false;
 }
 while (pStu != NULL)
 {
  fprintf(fp, "%d %s  %s %.2f %.2f %.2f %.2f\n", pStu->num, pStu->name, pStu->sex, 
       pStu->score[0], pStu->score[1], pStu->score[2], pStu->averge);
  pStu = pStu->pNext;
 }
 fclose(fp);
 return true;
}
/************************************************************************/
/*保存學(xué)生信息 (以數(shù)據(jù)庫(kù)形式保存)*/
/************************************************************************/
bool saveStudentInfo()
{
 FILE *fp;
 Student *pStu;

 pStu = G_pStuHead;

 if((fp = fopen(FILE_DATABASE, "wb")) == NULL)
 {
  return false;
 }
 fwrite(&G_autoStore, sizeof(G_autoStore), 1, fp);
 while (pStu != NULL)
 {
  fwrite(pStu, sizeof(Student), 1, fp);
  pStu = pStu->pNext;
 }
 fclose(fp);
 return true;
}
/************************************************************************/
/*讀取學(xué)生信息(讀取數(shù)據(jù)庫(kù)形式的文檔) */
/************************************************************************/
bool loadStudentInfo()
{
 FILE *fp;
 int count;
 Student stu, *pStu, *pStuNew;

 count = 0;
 pStu = G_pStuHead;

 if((fp = fopen(FILE_DATABASE, "rb")) == NULL)
 {
  return false;
 }
 fread(&G_autoStore, sizeof(G_autoStore), 1, fp);//讀取是否自動(dòng)保存
 while(1)
 {
  fread(&stu, sizeof(Student), 1, fp);//讀取文檔中每個(gè)同學(xué)的數(shù)據(jù)
  if(feof(fp))//到文檔尾則跳出
  {
   break;
  }
  if(count++ > 0)//這里 > 0 是因?yàn)楸4娴臅r(shí)候會(huì)把鏈表頭保存進(jìn)去,而鏈表頭是沒(méi)有有效數(shù)據(jù)的,所以要排除掉
  {
   pStuNew = (Student*)malloc(sizeof(Student));
   MoveMemory(pStuNew, &stu, sizeof(Student) - 4);//將結(jié)構(gòu)體除指針外的所有數(shù)據(jù)拷貝進(jìn)內(nèi)存
   pStuNew->pNext = NULL;
   pStu->pNext = pStuNew;
   pStu = pStuNew;
  }
  if(stu.pNext == NULL)
  {
   break;
  }
 }
 fclose(fp);
 return true;
}
/************************************************************************/
/*學(xué)生信息排序 */
/************************************************************************/
bool sortStudentInfo()
{
 int order1, order2;
 bool swapData;
 char yesOrNo;
 Student* pStu1, *pStu2, tmpStu;

 pStu1 = G_pStuHead->pNext;
 if(pStu1 == NULL)
 {
  printf("系統(tǒng)中無(wú)學(xué)生信息!\n");
  system("pause");
  return false;
 }
 printf("輸入以下序號(hào)執(zhí)行相應(yīng)功能(輸入其他序號(hào)返回主菜單):\n");
 printf("1、根據(jù)學(xué)號(hào)進(jìn)行排序\n");
 printf("2、根據(jù)姓名排序\n");
 printf("3、根據(jù)語(yǔ)文成績(jī)排序\n");
 printf("4、根據(jù)數(shù)學(xué)成績(jī)排序\n");
 printf("5、根據(jù)英語(yǔ)成績(jī)排序\n");
 printf("6、根據(jù)平均分成績(jī)排序\n");

 scanf("%d", &order1);
 if(order1 >= 1 && order1 <= 6)
 {
  printf("請(qǐng)選擇正序OR倒序排列?(輸入其他序號(hào)返回主菜單)\n");
  printf("1、正序排列\(zhòng)n");
  printf("2、倒序排列\(zhòng)n");
  scanf("%d", &order2);
  if(order2 >= 1 && order2 <= 2)
  {
   //冒泡排序
   for ( ; pStu1->pNext != NULL; pStu1 = pStu1->pNext)
   {
    for (pStu2 = pStu1->pNext; pStu2 != NULL; pStu2 = pStu2->pNext)
    {
     swapData = false;//是否交換數(shù)據(jù)
     switch(order1)
     {
     case 1://根據(jù)學(xué)號(hào)排序
      {
       if(order2 == 1 ? (pStu1->num > pStu2->num) : (pStu1->num < pStu2->num))//三目運(yùn)算符, 判斷正序還是倒序
       {
        swapData = true;
       }
       break;
      }
     case 2://根據(jù)姓名排序
      {
       if(order2 == 1 ? (strcmp(pStu1->name, pStu2->name) > 0) : (strcmp(pStu1->name, pStu2->name) < 0))
       {
        swapData = true;
       }
       break;
      }
     case 3://根據(jù)語(yǔ)文排序
     case 4://根據(jù)數(shù)學(xué)排序
     case 5://根據(jù)英語(yǔ)排序
      {
       if(order2 == 1 ? (pStu1->score[order1 - 3] > pStu2->score[order1 - 3]) : (pStu1->score[order1 - 3] < pStu2->score[order1 - 3]))
       {
        swapData = true;
       }
       break;
      }
     case 6://根據(jù)均分排序
      {
       if(order2 == 1 ? (pStu1->averge > pStu2->averge) : (pStu1->averge < pStu2->averge))
       {
        swapData = true;
       }
       break;
      }
     }

     if(swapData)
     {
      //交換內(nèi)存數(shù)據(jù),只需要將除pNext指針外的結(jié)構(gòu)體數(shù)據(jù)拷貝交換即可
      MoveMemory(&tmpStu, pStu1, sizeof(Student) - 4);
      MoveMemory(pStu1, pStu2, sizeof(Student) - 4);
      MoveMemory(pStu2, &tmpStu, sizeof(Student) - 4);
     }
    }
   }
   printf("排序完成,是否顯示?Y/N\n");
   getchar();//過(guò)濾掉輸入時(shí)的換行符
   scanf("%c", &yesOrNo);
   if(yesOrNo == 'Y' || yesOrNo == 'y')
   {
    pStu1 = G_pStuHead->pNext;
    setFontColor(LIGHTGREEN);
    printf(LIST_TITLE_1);//顯示列表標(biāo)題頭
    setFontColor(WHITE);
    //打印排序后的各個(gè)學(xué)生信息
    while(pStu1 != NULL)
    {
     printf("%d %s  %s %.2f %.2f %.2f %.2f\n", pStu1->num, pStu1->name, pStu1->sex, 
       pStu1->score[0], pStu1->score[1], pStu1->score[2], pStu1->averge);
     pStu1 = pStu1->pNext;
    }
    system("pause");
   }
   return true;

  }
 }
 return false;
}
/************************************************************************/
/*查詢學(xué)生信息 */
/************************************************************************/
void searchStudentInfo()
{
 bool hasFound;
 int order, stuID, count, i, min, max;
 float score;
 char name[20];
 Student* pStu;
 Student** pStuArr;

 pStuArr = NULL;
 
 while (1)
 {
  system("CLS");
  showSystemTitle();

  if(pStuArr != NULL)//如果再次查詢,這里需要判斷,將上一輪查詢的學(xué)生信息指針數(shù)組清空
  {
   free(pStuArr);
  }
  count = 0;
  stuID = 0;
  hasFound = false;
  pStu = G_pStuHead->pNext;
  pStuArr = (Student**)malloc(4 * 100);//初始化查詢到后存放的學(xué)生信息指針數(shù)組

  printf("輸入以下序號(hào)執(zhí)行相應(yīng)功能(輸入其他序號(hào)返回主菜單):\n");
  printf("1、輸入學(xué)號(hào)查詢信息\n");
  printf("2、輸入姓名查詢信息\n");
  printf("3、輸入語(yǔ)文成績(jī)范圍查詢信息\n");
  printf("4、輸入數(shù)學(xué)成績(jī)范圍查詢信息\n");
  printf("5、輸入英語(yǔ)成績(jī)范圍查詢信息\n");
  printf("6、輸入平均分范圍查詢信息\n");
  printf("7、列出所有學(xué)生信息\n");

  scanf("%d", &order);
  switch(order)
  {
  case 1://根據(jù)學(xué)號(hào)查詢
   {
    printf("請(qǐng)輸入要查詢的學(xué)生學(xué)號(hào):");
    scanf("%d", &stuID);
    while (pStu != NULL)
    {
     if(pStu->num == stuID)
     {
      hasFound = true;
      break;
     }
     pStu = pStu->pNext;
    }
    if(hasFound)//
    {
     setFontColor(LIGHTGREEN);
     printf(LIST_TITLE_1);
     setFontColor(WHITE);
     printf("%d %s  %s %.2f %.2f %.2f %.2f\n", pStu->num, pStu->name, pStu->sex, 
       pStu->score[0], pStu->score[1], pStu->score[2], pStu->averge);
    }
    
    break;
   }
  case 2://根據(jù)姓名查詢
   {
    printf("請(qǐng)輸入要查詢的學(xué)生姓名:");
    scanf("%s", name);
    while (pStu != NULL)
    {
     if(strcmp(pStu->name, name) == 0)
     {
      hasFound = true;
      pStuArr[count++] = pStu;
      if(count % 100 == 0)
      {
       pStuArr = (Student**)realloc(pStuArr, count + 100);
      }
     }
     pStu = pStu->pNext;
    }
    if(hasFound)
    {
     setFontColor(LIGHTGREEN);
     printf(LIST_TITLE_1);
     setFontColor(WHITE);
     for (i = 0; i < count; i++)
     {
      printf("%d %s  %s %.2f %.2f %.2f %.2f\n", pStuArr[i]->num, pStuArr[i]->name, pStuArr[i]->sex, 
        pStuArr[i]->score[0], pStuArr[i]->score[1], pStuArr[i]->score[2], pStuArr[i]->averge);
     }

    }
    break;
   }
  case 3://根據(jù)語(yǔ)文成績(jī)范圍查詢
  case 4://根據(jù)數(shù)學(xué)成績(jī)范圍查詢
  case 5://根據(jù)英語(yǔ)成績(jī)范圍查詢
  case 6://根據(jù)平均分范圍查詢
   {
    char *subjectStrArr[4] = {"語(yǔ)文", "數(shù)學(xué)", "英語(yǔ)", "平均"};
    printf("請(qǐng)輸入要查詢的%s成績(jī)范圍:", subjectStrArr[order - 3]);
    scanf("%d %d", &min, &max);
    while (pStu != NULL)
    {
     if(order < 6)// 3 ~ 5
     {
      score = pStu->score[order - 3];
     }
     else  //order = 6
     {
      score = pStu->averge;
     }
     if(score >= min && score <= max)
     {
      //找到符合條件的學(xué)生信息,則加入到指針數(shù)組中去
      hasFound = true;
      pStuArr[count++] = pStu;
      if(count % 100 == 0)
      {
       pStuArr = (Student**)realloc(pStuArr, count + 100);
      }
     }
     pStu = pStu->pNext;
    }
    if(hasFound)
    {
     setFontColor(LIGHTGREEN);
     printf(LIST_TITLE_1);
     setFontColor(WHITE);
     //打印指針數(shù)組中的學(xué)生信息
     for (i = 0; i < count; i++)
     {
      printf("%d %s  %s %.2f %.2f %.2f %.2f\n", pStuArr[i]->num, pStuArr[i]->name, pStuArr[i]->sex, 
       pStuArr[i]->score[0], pStuArr[i]->score[1], pStuArr[i]->score[2], pStuArr[i]->averge);
     }
     
    }
    break;
   }
  case 7://列出所有學(xué)生信息
   {
    hasFound = true;
    setFontColor(LIGHTGREEN);
    printf(LIST_TITLE_1);
    setFontColor(WHITE);
    while(pStu != NULL)
    {
     printf("%d %s  %s %.2f %.2f %.2f %.2f\n", pStu->num, pStu->name, pStu->sex, 
       pStu->score[0], pStu->score[1], pStu->score[2], pStu->averge);
     pStu = pStu->pNext;
    }
    break;
   }
  default:
   {
    goto lab_search;
   }
  }
  if(! hasFound)
  {
   printf("未能找到相應(yīng)的學(xué)生信息!\n");
  }
  system("pause");
 }
lab_search:
 free(pStuArr);
}
/************************************************************************/
/*刪除學(xué)生信息 */
/************************************************************************/
bool deleteStudentInfo()
{
 char yesOrNo;
 int stuID;
 bool hasFound;
 Student* pStu, *pStu1;

 hasFound = false;
 pStu = G_pStuHead->pNext;
 pStu1 = G_pStuHead;

 printf("請(qǐng)輸入欲刪除的學(xué)生學(xué)號(hào):");
 scanf("%d", &stuID);
 while (pStu != NULL)
 {
  if(pStu->num == stuID)
  {
   hasFound = true;
   break;
  }
  pStu1 = pStu;
  pStu = pStu->pNext;
 }
 if(hasFound)
 {
  printf("找到此學(xué)生的信息如下:\n");
  setFontColor(LIGHTGREEN);
  printf(LIST_TITLE_1);
  setFontColor(WHITE);
  printf("%d %s  %s %.2f %.2f %.2f %.2f\n", pStu->num, pStu->name, pStu->sex, 
    pStu->score[0], pStu->score[1], pStu->score[2], pStu->averge);
  printf("是否刪除?Y/N");
  getchar();//過(guò)濾掉輸入時(shí)的換行符
  scanf("%c", &yesOrNo);
  if(yesOrNo == 'y' || yesOrNo == 'Y')
  {
   pStu1->pNext = pStu->pNext;
   free(pStu);
   printf("已刪除\n");
  }
  else
  {
   hasFound = false;
  }
 }
 else
 {
  printf("未找到對(duì)應(yīng)學(xué)生的信息\n");
 }
 system("pause");
 return hasFound;
}
/************************************************************************/
/*修改學(xué)生信息 */
/************************************************************************/
bool modifyStudentInfo()
{
 int order, count, i;
 int stuID;
 char name[20];
 char yesOrNo;
 bool hasModify;
 Student* pStu;
 Student** pStuArr;

 hasModify = false;
 count = 0;
 pStu = G_pStuHead->pNext;
 pStuArr = (Student**)malloc(4 * 100);//用于存放查找到的學(xué)生信息指針,這里定義指針數(shù)組是防止查詢姓名出現(xiàn)重名

 printf("請(qǐng)輸入以下序號(hào),選擇對(duì)應(yīng)功能(1或2,否則返回上級(jí)菜單)\n");
 printf("1、輸入學(xué)號(hào)查找學(xué)生\n");
 printf("2、輸入姓名查找學(xué)生\n");
 scanf("%d", &order);

 if(order == 1)
 {
  printf("請(qǐng)輸入要修改的學(xué)生學(xué)號(hào):\n");
  scanf("%d", &stuID);
  while(pStu != NULL)
  {
   if(pStu->num == stuID)
   {
    pStuArr[count++] = pStu;
    break;
   }
   pStu = pStu->pNext;
  }
 }
 else if(order == 2)
 {
  printf("請(qǐng)輸入要修改的學(xué)生姓名:\n");
  scanf("%s", name);
  while(pStu != NULL)
  {
   if(strcmp(pStu->name, name) == 0)
   {
    pStuArr[count++] = pStu;
    if(count % 100 == 0)//如果數(shù)組存放滿了,則再次申請(qǐng)內(nèi)存
    {
     pStuArr = (Student**)realloc(pStuArr, count + 100);
    }
   }
   pStu = pStu->pNext;
  }
 }
 else
 {
  return false;
 }
 if(count == 0)
 {
  printf("未能找到任何信息,是否繼續(xù)修改?Y/N");
  getchar();//過(guò)濾掉輸入時(shí)的換行符
  scanf("%c", &yesOrNo);
  if(yesOrNo == 'y' || yesOrNo == 'Y')
  {
   system("CLS");
   showSystemTitle();
   return modifyStudentInfo();
  }
 }
 else
 {
  printf("為您查找到%d個(gè)學(xué)生信息:\n   ", count);
  setFontColor(LIGHTGREEN);
  printf(LIST_TITLE);
  setFontColor(WHITE);
  for (i = 0; i < count; i++)
  {
   printf("%d、%d %s  %s %.2f %.2f %.2f\n", i + 1, pStuArr[i]->num, pStuArr[i]->name, pStuArr[i]->sex, 
    pStuArr[i]->score[0], pStuArr[i]->score[1], pStuArr[i]->score[2]);
  }

  printf("請(qǐng)輸入您要修改的信息序號(hào)(1~%d),其他數(shù)字返回主菜單\n", count);
  scanf("%d", &order);
  if(order >= 1 && order <= count)
  {
   printf("請(qǐng)依次輸入\n");
   setFontColor(LIGHTGREEN);
   printf(LIST_TITLE);
   setFontColor(WHITE);
   pStu = pStuArr[order - 1];
   scanf("%d %s %s %f %f %f", &pStu->num, pStu->name, pStu->sex, &pStu->score[0], &pStu->score[1], &pStu->score[2]);
   pStu->averge = (pStu->score[0] + pStu->score[1] + pStu->score[2]) / 3;
   hasModify = true;
  }
 }
 free(pStuArr);
 return hasModify;
}
/************************************************************************/
/*檢測(cè)學(xué)號(hào)是否存在*/
/************************************************************************/
bool checkStuIDExist(int stuID)
{
 Student* pStu;

 pStu = G_pStuHead->pNext;

 while(pStu != NULL)
 {
  if(pStu->num == stuID)
  {
   return true;
  }
  pStu = pStu->pNext;
 }
 return false;
}
/************************************************************************/
/*添加學(xué)生信息 */
/************************************************************************/
bool addStudentInfo()
{
 printf("輸入-1回車,返回上級(jí)菜單\n");
 setFontColor(LIGHTGREEN);
 printf(LIST_TITLE);
 setFontColor(WHITE);

 char c;
 bool hasAdd = false;
 Student* pStu = G_pStuHead;
 Student* pStuNew;

 while (pStu->pNext != NULL)
 {
  pStu = pStu->pNext;
 }

 while(1)
 {
  pStuNew = (Student*)malloc(sizeof(Student));
  scanf("%d", &pStuNew->num);
  if(pStuNew->num == -1)//輸入-1返回主菜單
  {
   while ((c = getchar()) != EOF && c != '\n');//不停地使用getchar()獲取緩沖中字符,直到獲取的c是“\n”或文件結(jié)尾符EOF為止
   free(pStuNew);
   return hasAdd;
  }
  else if(checkStuIDExist(pStuNew->num))
  {
   while ((c = getchar()) != EOF && c != '\n');//不停地使用getchar()獲取緩沖中字符,直到獲取的c是“\n”或文件結(jié)尾符EOF為止
   printf("該學(xué)號(hào)已存在,請(qǐng)重新輸入!\n");
   free(pStuNew);
   continue;
  }
  hasAdd = true;
  scanf("%s %s %f %f %f", pStuNew->name, pStuNew->sex, &pStuNew->score[0], 
   &pStuNew->score[1], &pStuNew->score[2]);
  pStuNew->averge = (pStuNew->score[0] + pStuNew->score[1] + pStuNew->score[2]) / 3;
  pStuNew->pNext = NULL;
  pStu->pNext = pStuNew;
  pStu = pStuNew;
 }

 return hasAdd;
}
/************************************************************************/
/*根據(jù)指令序號(hào)執(zhí)行對(duì)應(yīng)功能  */
/************************************************************************/
bool orderToExecute(int order)
{
 bool succ;

 succ = false;
 if(order != Func_Save && order != Func_AutoSave && order!= Func_Export)
 {
  system("CLS");
  showSystemTitle();
 }

 switch (order)
 {
 case Func_Add://添加
  {
   succ = addStudentInfo();
   break;
  }
 case Func_Delete://刪除
  {
   succ = deleteStudentInfo();
   break;
  }
 case Func_Modify://修改
  {
   succ = modifyStudentInfo();
   break;
  }
 case Func_Search://搜索
  {
   searchStudentInfo();
   break;
  }
 case Func_Sort://排序
  {
   succ = sortStudentInfo();
   break;
  }
 case Func_Save://保存
  {
   succ = saveStudentInfo();
   if(succ)
   {
    gotoxy(42, Func_Save + 3);
    setFontColor(LIGHTGREEN);
    printf("保存成功!");
    setFontColor(WHITE);
    gotodelete(0, Func_ExitSystem + 4, 2);
    gotoxy(0, Func_ExitSystem + 4);
   }
   return false;
  }
 case Func_AutoSave://設(shè)置自動(dòng)保存
  {
   G_autoStore = ! G_autoStore;
   printAutoStoreInfo();
   orderToExecute(Func_Save);//保存配置
   break;
  }
 case Func_Vote://投票系統(tǒng)
  {
   voteSystem();
   break;
  }
 case Func_Export://導(dǎo)出所有學(xué)生信息(明文)
  {
   succ = exportStudentInfo();
   gotoxy(17, Func_Export + 3);
   setFontColor(LIGHTGREEN);
   if(succ)
   {
    printf("導(dǎo)出成功!");
   }
   else
   {
    printf("導(dǎo)出失??!");
   }
   setFontColor(WHITE);
   gotodelete(0, Func_ExitSystem + 4, 2);
   gotoxy(0, Func_ExitSystem + 4);
   return false;
  }
 default:
  {
   
   break;
  }
 }
 return succ;
}

int main(int argc, char* argv[])
{
 int order;
 bool succ;

 system("title 學(xué)生管理系統(tǒng) by 機(jī)智蛋");
 order = 0;
 succ = false;
 G_pStuHead = (Student*)malloc(sizeof(Student));
 G_pStuHead->pNext = NULL;

 loadStudentInfo();

 while(1)
 {
  if(order != Func_Save && order != Func_AutoSave && order != Func_Export)//當(dāng)輸入這幾個(gè)指令時(shí)不需要初始化屏幕
  {
   initScreen();
  }
  if(succ && order != Func_Save && G_autoStore)//執(zhí)行自動(dòng)保存
  {
   orderToExecute(Func_Save);
  }
  succ = false;

  do 
  {
   scanf("%d", &order);
   if(order >= Func_Add && order <= Func_ExitSystem)
   {
    break;
   }
   else
   {
    printf("指令錯(cuò)誤,請(qǐng)重新輸入\n");
   }
  } while (1);
  
  if(order == Func_ExitSystem)//退出
  {
   printf("歡迎下次繼續(xù)使用,拜拜~~\n");
   return 0;
  }

  succ = orderToExecute(order);//執(zhí)行功能

  
 }
 
 return 0;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語(yǔ)言中變量與其內(nèi)存地址對(duì)應(yīng)的入門知識(shí)簡(jiǎn)單講解

    C語(yǔ)言中變量與其內(nèi)存地址對(duì)應(yīng)的入門知識(shí)簡(jiǎn)單講解

    這篇文章主要介紹了C語(yǔ)言中變量與其內(nèi)存地址對(duì)應(yīng)的入門知識(shí)簡(jiǎn)單講解,同時(shí)這也是掌握指針部分知識(shí)的基礎(chǔ),需要的朋友可以參考下
    2015-12-12
  • 深入分析C中不安全的sprintf與strcpy

    深入分析C中不安全的sprintf與strcpy

    本篇文章是對(duì)C中不安全的sprintf與strcpy函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 最新評(píng)論