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

📄 studentsys.cpp

📁 学习c++的ppt
💻 CPP
字号:
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <math.h>

struct student  {
      int       num;
	  char      name[10];
      float     math, eng, comp, avg;

      struct    student   *next;
};   // 学生节点定义

struct student  *head;
const int PLEN = sizeof(struct student);
int n = 0;

void loadInfo(void)
{     fstream fi("student.dat", ios::in | ios::binary | ios::nocreate);
      if(!fi) {
		  return;
		  cout << "文件 student.dat 打开失败 !" << endl;
		  exit(1);
	  }
	  
	  fi.unsetf(ios::skipws);  
	  head = NULL;
	  if( fi.eof() ) return;
	  struct student *p1, *p2;
      n = 0;
	  int pcc = 0;
	  while( !fi.eof())  {
           n ++;
		   p1 = new  struct student; 
		   fi.read((char *)p1, PLEN);
		   pcc = fi.gcount();
		   if(pcc < PLEN)  {
			   delete p1;
			   break;
		   }
		   
		   if(n == 1)  head = p1;
           else p2->next = p1;
           p2 = p1;
      }
	  p2->next = NULL;
	  fi.close();
}

void saveInfo(void)
{     if(head == NULL) return;
      ofstream fo("student.dat", ios::out | ios::binary);
      if(!fo) {
		  cout << "文件 student.dat 不能建立 !" << endl;
		  exit(1);
	  }
	  struct student *p;
	  p = head;
	  while( p != NULL )  {
           fo.write((char *)p, PLEN);
           p = p->next;
		   
      }
	  fo.close();
}

void createInfo(void)
{     struct student *p1, *p2;
      cout << endl << "请依次输入每个学生的学号 姓名 数学 英语 计算机 ... " << endl;
      p1 = p2 = new  struct student; 
      cin >> p1->num >> p1->name >> p1->math >> p1->eng >> p1->comp;
	  p1->avg = ceil((p1->math + p1->eng + p1->comp) / 3);
      head = NULL;
	  n = 0;
      while(p1->num != 0)  {
           n ++;
           if(n == 1)  head = p1;
           else p2->next = p1;
           p2 = p1;
           p1 = new  struct student;
           cin >> p1->num >> p1->name >> p1->math >> p1->eng >> p1->comp;
		   p1->avg = ceil((p1->math + p1->eng + p1->comp) / 3);
     }
     p2->next = NULL;
}

void printInfo()
{  struct student *p = head;
   if(head != NULL) {
	    cout << endl << "全部学生数据如下:" << endl;
        cout << endl << "学号    姓名    数学    英语  计算机  平均分" << endl;
        cout << "--------------------------------------------" << endl;
        do  {
			 cout.setf(ios::left, ios::adjustfield);
             cout << setw(8) << p->num << setw(8) << p->name;
			 cout.setf(ios::right, ios::adjustfield);
			 cout << setw(4) << p->math << setw(8) << p->eng << setw(8) << p->comp << setw(8) << p->avg << endl;
             p = p->next;
        } while(p != NULL);
   }
   else
	   cout << endl << "数据库中没有学生档案 !请按菜单 7 新建学生档案库 。" << endl;
 }

void deleteStudent()
{   struct student *p1, *p2;
    int num;
    if(head == NULL)  {
        cout << endl << "数据库中没有学生档案 !" << endl;
        return ;
	}
    cout << endl << "请输入需要删除的学号:";
	cin >> num;
    p1 = head;
    while(p1->num != num && p1->next != NULL){
        p2 = p1;
        p1 = p1->next;
	}
    if(p1->num == num)  {
        if(p1 == head) head = p1->next;  // 删除首结点    
        else p2->next = p1->next;    // 删除中间或尾结点  
        cout << endl << "学号为:" << num << " 的学生已被删除 !" << endl;
        delete p1;
	} 
	else  cout << endl << "找不到学号为:" << num << " 的学生 !" << endl;
}

void addStudent()
{    struct student *p0, *p1, *p2;
     p1 = head; p0 = new struct student;
	 cout << endl << "请输入新增加学生的学号 姓名 数学 英语 计算机 ... " << endl;
     cin >> p0->num >> p0->name >> p0->math >> p0->eng >> p0->comp;
	 p0->avg = ceil((p0->math + p0->eng + p0->comp) / 3);
     if(head == NULL)  {
         head = p0;  
		 p0->next = NULL; 
	 }
     while((p0->num > p1->num) && (p1->next != NULL))  {
         p2 = p1;  
		 p1 = p1->next;
	 }
     if(p0->num <= p1->num)  {  
         if(head == p1)  head = p0;  //  插在头部  //
         else p2->next = p0;  // 插在 p2和p1之间  //
         p0->next = p1;
	 } else {  //  插到最后  //
         p1->next = p0;  
		 p0->next = NULL; 
	 } 
	 cout << endl << "数据已被追加到数据库 !" << endl;
}

void searchStudent()
{
	struct student *p;
    int num;
    if(head == NULL)  {
        cout << endl << "数据库中没有学生档案 !" << endl;
        return ;
	}
    cout << endl << "请输入需要查询的学号:";
	cin >> num;
	p = head;
	while(p != NULL && p->num != num)  p = p->next;
    if(p != NULL)  {
		cout << endl << "学号为:" << num << " 学生资料:" << endl << endl;
		cout << "学号    姓名    数学    英语  计算机  平均分" << endl;
        cout << "--------------------------------------------" << endl;
	    cout.setf(ios::left, ios::adjustfield);
        cout << setw(8) << p->num << setw(8) << p->name;
		cout.setf(ios::right, ios::adjustfield);
		cout << setw(4) << p->math << setw(8) << p->eng << setw(8) << p->comp << setw(8) << p->avg << endl;
    }
	else 
		cout << endl << "找不到学号为:" << num << " 的学生 !" << endl;
}

void scoreSort()
{
   struct student *p = head;
   float maxscore = 0;
   if(head == NULL) {
	   cout << endl << "数据库中没有学生档案 !请按菜单 7 新建学生档案库 。" << endl;
       return;
   }
   cout << endl << "学生成绩排行榜:" << endl;
   cout << endl << "名次  学号    姓名    数学    英语  计算机  平均分" << endl;
   cout << "--------------------------------------------------" << endl;

   float sval[50], tv;
   struct student *studp[50], *tp;
   int ncc = 0;
   do  {
	   sval[ncc]= p->avg;
	   studp[ncc ++] = p;
	   p = p->next;
   } while(p != NULL);
   
   int i,j, k;
   for (i = 0; i < ncc-1; i++){           
	   k = i;	            
	   for(j = i+1; j < ncc; j++)        
	       if(sval[j] > sval[k]) k = j;
	   if(k != i)  {
		   tv = sval[i];
		   sval[i] = sval[k]; 
		   sval[k] = tv;

		   tp = studp[i];
		   studp[i] = studp[k];
		   studp[k] = tp;
	   }
   }	

   for(i = 0; i < ncc; i ++) {
	   tp = studp[i];
       cout.setf(ios::left, ios::adjustfield);
       cout << " " << setw(5) << i+1 << setw(8) << tp->num << setw(8) << tp->name;
	   cout.setf(ios::right, ios::adjustfield);
	   cout << setw(4) << tp->math << setw(8) << tp->eng << setw(8) << tp->comp << setw(8) << tp->avg << endl;
   }    
}

void menu_display() 
{   
	cout << endl;
    cout << "          学生成绩管理系统  " << endl;
	cout << " -------------------------------------" << endl;
	cout << "       1.  显示所有学生资料  " << endl;
	cout << "       2.  按编号查询学生    " << endl;
	cout << "       3.  增加学生资料      " << endl;
	cout << "       4.  删除某个学生      " << endl;
	cout << "       5.  装入学生资料      " << endl;
	cout << "       6.  保存学生资料      " << endl;
	cout << "       7.  新建学生档案      " << endl;
	cout << "       8.  学生成绩排行榜      " << endl;
	cout << "       0.  退出系统          " << endl;
	cout << " -------------------------------------" << endl;
}

void anykey()
{   
	cout << endl << "按任意键继续 ......";
	cin.get();
	cin.get();
	cout << endl;
}

void menu_select()
{   int c;

    while(1) {
        menu_display();
		cout << "请选择:";
		cin >> c;
        switch(c) {
		    case 1: printInfo();
				    anykey();
					break;
			case 2: searchStudent();
				    anykey();
					break;
			case 3: addStudent();
				    anykey();
					break;
			case 4: deleteStudent();
				    anykey();
					break;
			case 5: loadInfo();
				    anykey();
					break;
			case 6: saveInfo();
				    anykey();
					break;
			case 7: createInfo();
				    anykey();
					break;
		    case 8: scoreSort();
				    anykey();
					break;
			case 0: cout << endl;
				    return;
		}
	}
}

void main()
{     head = NULL;
      loadInfo();
	  menu_select();
	  saveInfo();
}

⌨️ 快捷键说明

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