⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stuinformation.cpp

📁 学生成绩信息管理软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:

/*******************************************************/
/*                                                     */
/*                                              */
/*                    19/05/2007                       */
/*                                                     */                                        
/*******************************************************/        
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>//使用system("cls")
#define TRUE 1
#define FALSE 0
#define MYDB "StuInformation"
typedef struct student
{
   int no;
    char name[12];
    int mathScore;
    int programScore;
    int sumScore;
   struct student *next;
}node;

void MainMenu(void);
void NEWMenu(void);
void AddMenu(void);
void DelMenu(void);
void SortMenu(void);
void QueryMenu(void);

int fileExist(void);
int writeCheckByNo(node *head,int No);
node* getStu(node *q,int i);
node* getRecords(void);

void delByNo(void);
void sortByMath(void);
void sortByProgram(void);
void sortBySum(void);

void queryByNo();
void queryByName();
void queryByMath();
void queryByProgram();
void queryBySum();

void printStu(node *head);
void writeBackFile(node *head);
void freeNode(node *head);

void clrscr( void )//清屏函数
{
	system("cls");
}

void main()
{
	int ch;
   while(1)
    {
        system("cls");
        MainMenu();
        fflush(stdin);
         printf("\nPlease select 1~6 :");
         scanf("%d",&ch);
         switch(ch)
         {
             case 1:NEWMenu();break;
             case 2:AddMenu();break;
             case 3:DelMenu();break;
             case 4:SortMenu();break;
             case 5:QueryMenu();break;
             case 6:/*-----Exit studentDbSystem-----*/
             exit(0);
             default: break;
         }
     }
 }
 
 void MainMenu()//主选菜单
 {
     clrscr();//清理屏幕
	 cprintf(" 屏幕已经清理过了!"); 
     printf("\n_____________________________________\n");
     printf("\n欢迎使用学生信息系统\n");
	 printf("\n  祝你使用愉快\n");
     printf("\n_____________________________________\n");
     printf("\n1.新建数据\n");
     printf("\n2.添加数据\n");
     printf("\n3.删除数据\n");
     printf("\n4.排序\n");
     printf("\n5.查询\n");
     printf("\n6.退出\n");
     printf("\n_____________________________________\n");
 }
 
 void NEWMenu()//新建数据
 {
     int i=0;
     node *head,*p,*q;
     char ch;
     FILE *fp;
     head=(node*)malloc(sizeof(node));
     p=head;
     clrscr();
     fflush(stdin);
     printf("\n_________________________________\n");
     printf("\n新建数据\n");
     printf("\n_________________________________\n");
     
     if (fileExist())//if the file exist,pleaese add new or else create new file
     {
         printf("\n文件已经存在,是否添加新信息?(y/n)");
         ch=getchar();
         if((ch=='y')||(ch=='Y'))
             AddMenu();
         else
             return;
     }
     else
     {
         fp=fopen(MYDB,"wb+");
         while (1)
         {
             p=getStu(p,i);
             i++;
             fflush(stdin);
             printf("\n是否继续输入新信息?(y/n)");
             ch=getchar();
             if(ch=='n'||ch=='N')
             {
                 p->next=NULL;
                 break;
             }
             q=(node*)malloc(sizeof(node));
             p->next=q;
             p=p->next;
         }
         p=head;
         while(p!=NULL)
         {   
			 int No;
			 p=getStu(p,i);
             No=p->no;
			 int checkFlag;
             checkFlag=writeCheckByNo(head,No);//int writeCheckByNo(node *p,int No)判断学号是否重复
             if(checkFlag==0)
             {
                 printf("学生学号已经存在!\n");
                 printf("请重新输入.\n");
                 getchar();
                 free(p);
                 continue;
             }
             fwrite(p,sizeof(node),1,fp);//write to file
             p=p->next;
         }
         p=head;
         clrscr();
         printf("\n\n________________________________________________________");
         printf("\n%-6s%-16s%-12s%-16s%-12s","学号","姓名","数学成绩","程序设计成绩","总成绩");
         while(p!=NULL)
         {
             printf("\n%-6d%-16s%-12d%-16d%-12d",p->no,p->name,p->mathScore,p->programScore,p->sumScore);
             printf("\n\n_______________________________________________________\n");
             p=p->next;
          }
         fclose(fp);
         printf("\nThree are%d records now.",i);
         printf("\n信息存储成功!");
         getche();
         freeNode(head);
     }
 }
 
 node* getStu(node *q,int i)//给结点赋初值
 {
     clrscr();
     printf("\n________________________");
     printf("\n正在记录学生信息......");
     printf("\n________________________");
     if (i==0)
         printf("\n请输入学生信息:");
     else
         printf("\nPlease input the %d th student information:",++i);
     printf("\n请输入学号(no):");
     scanf("%d",&q->no);
     fflush(stdin);//清空标准输入的缓冲区
     printf("\n请输入学生姓名(name):");
     scanf("%s",q->name);
     fflush(stdin);
     printf("\n请输入学生数学成绩(mathscore):");
     scanf("%d",&q->mathScore);
     fflush(stdin);
     printf("\n请输入学生程序设计成绩(programscore):");
     scanf("%d",&q->programScore);
     fflush(stdin);
     q->sumScore=(q->mathScore)+(q->programScore);
     return q;
 }
 
 void AddMenu()//新建学生信息
 {
     FILE *fp;
     char ch;
     node *p,*head;
     int No;
     int checkFlag;
     int i=0;
 
     if (fileExist())
     {
         fp=fopen(MYDB,"ab+");
         while(1)//while start
         {
             fflush(stdin);
             p=(node*)malloc(sizeof(node));
             if(i==0)
             {
                 head=getRecords();
             }
                p=getStu(p,i);
             No=p->no;
             checkFlag=writeCheckByNo(head,No);//int writeCheckByNo(node *p,int No)
             if(checkFlag==0)
             {
                 printf("学生学号已经存在!\n");
                 printf("请重新输入.\n");
                 getchar();
                 free(p);
                 continue;
             }
             i++;
                fwrite(p,sizeof(node),1,fp);
             free(p);
                printf("\n添加信息成功!");
                fflush(stdin);
                printf("\n是否继续输入新信息??(Y/N)");
             ch=getchar();
             if((ch=='N')||(ch=='n'))
                {
                      fclose(fp);
                 head=getRecords();
                 if(fileExist())
                 {
                     fopen(MYDB,"rb+");
                     printStu(head);
                     fclose(fp);
                 }
                      break;
             }
         }//end of while
     }
     else
     {
         printf("\n文件不存在!");
     }
     getchar();
     freeNode(head);
     return;
 }
 
 void DelMenu()//学生信息删除
 {
     int doWhich;
     while(1)
     {
     system("cls");
     fflush(stdin);
     printf("\n________________________\n");
     printf("\n欢迎使用删除系统\n");
     printf("\n_______________________\n");
     printf("\n1.删除学生信息\n");
     printf("\n2.退出");
     printf("\n________________________\n");
     printf("\n请选择操作(1/2):\n");
     scanf("%d",&doWhich);
     if (doWhich==1)
          delByNo();
     else
         return;
     getchar();
     }
 }
 void delByNo()//按学号删除
 {
      node *head,*p,*q;
      FILE *fp;
      int No;
      if(fileExist())
          head=getRecords();
      else
          return;
      fflush(stdin);
      p=head;
      q=p;
      printf("请输入你要删除信息的学生的学号: ");
      scanf("%d",&No);
      printf("\nNo=%d\n",No);
      if(head->no==No)
      {
          p=head;
          head=head->next;
          free(p);
          writeBackFile(head);
          printf("学号为No=%d 的学生的信息已经删除!",No);
          return;
      }
      while(p!=NULL)
      {
          q=p->next;
          if((q->no)==No)
          {
              printf("学生已找到!\n");
              p->next=q->next;
              free(q);
              writeBackFile(head);
              printf("学号为 No = %d 的学生的信息已经删除!",No);
              printStu(head);
              printf("\n\n\n\n\nPress any key to continue.");
              getchar();
              return;
          }
          else
              p=p->next;
      }
      printf("此学生不存在,请重新选择你的操作");
      getchar();
      freeNode(head);
      return;
 
 }
 
 void SortMenu()//学生信息排序
 {
   int doWhich;
   while(1)
   {
     clrscr();
     fflush(stdin);
     printf("\n_____________________\n");
     printf("\n学生信息排序\n");
     printf("\n______________________\n");
     printf("\n1.按数学成绩排序\n");
     printf("\n2.按程序设计成绩排序\n");
     printf("\n3.按总成绩排序\n");
     printf("\n4.返回主菜单\n");
     printf("\n______________________\n");
     printf("\n请输入你的选择:\n");
     scanf("%d",&doWhich);
     switch (doWhich)
     {
       case 1:sortByMath();break;
       case 2:sortByProgram();break;
       case 3:sortBySum();break;
       default:return;
     }
   }
 }
 
 node* getRecords()
 {
     FILE *fp;
     node *p,*q,*head;
     if (fileExist())
     {
        fp=fopen(MYDB,"rb");
        head=(node*)malloc(sizeof(node));
        p=q=head;
        fread(p,sizeof(node),1,fp);
        while (!feof(fp))
        {
            q=(node*)malloc(sizeof(node));
            p->next=q;
            p=p->next;
            fread(p,sizeof(node),1,fp);
        }
         p->next=NULL;
        fclose(fp);
        return head;
     }
     else
         return NULL;
 }
 
 void sortByMath()//按数学成绩排序
 {
     FILE *fp;
     node *head,*p,*q,*temp;
     clrscr();
     if(fileExist())
         fp=(fopen(MYDB,"rb+"));
     else
         return;
     head=getRecords();
     p=head;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -