c++學生管理系統(STL)

      哥小白剛剛涉及一些STL想鞏固之前學的C++和STL,
     c++第一次用類,複製構造函數和操作符重載,STL寫學生管理系統,,可惜沒用繼承多態(不知道哪裏可以用 - _-)
      寫這麼長代碼,想了很久沒思路,看了看別人代碼思路,自己也寫了這個簡單的管理系統
      自己用到了C++11新語法,vs2010以下編譯器不會通過
     STL的sort和(Greate<T>())均不會用,於是百度其用法,感謝百度,感謝度娘
    *******************************************************************
                time:2015 1月13日 0:15分
                代碼:343行
                                                       by:隱無影

     *******************************************************************


#include<iostream>
#include<fstream>//文件輸入輸出流頭文件
#include<list>//鏈表
#include<string>
#include<functional>//適配器
#include <algorithm>//算法
#include<windows.h>
using namespace std;
class Student//定義student類
{
public:
 string name;
 string ID;
 int grade;
 Student() //無參構造函數  //無初始化
 {  
  grade = 0;
 }
 Student(string &pname, string &pid, int &pgrade)//有參構造函數
 {
  this->name = pname;
  this->ID = pid;
  this->grade = pgrade;
 }
 Student(const Student &st1)//拷貝構造函數
 {
  this->name = st1.name;
  this->ID = st1.ID;
  this->grade = st1.grade;
 }
 Student &operator=(const Student &st1)//重載=
 {  
  this->name = st1.name;
  this->ID = st1.ID;
  this->grade = st1.grade;
 }
 bool operator==(const Student &st1)const//重載==
 {
  return (grade == st1.grade) ? 1 : 0;
 }
 bool operator<(const Student &st1)const
 {
  return (grade <st1.grade) ? 1 : 0;
 }
 bool operator>(const Student &st1)const
 {
  return (grade>st1.grade) ? 1 : 0;
 }
public:
 void print()
 {
  cout << "name  :" << name << endl;
  cout << "ID    :" << ID << endl;
  cout << "grade :" << grade << endl;
  cout << "-----------" << endl;
 
 }
};
list<Student> mylist;//學生鏈表
void print(list<Student> &zlist)
{
 auto it = zlist.begin();//c++11先語法
 for (it; it != zlist.end(); ++it)
 {
  it->print();
  
 }
}
void show()//主菜單
{
 cout << "+++++++++++++++++++++++++++++++++++++++++"<<endl;
 cout << "+++++++++++1.無影搜索+++++++++++" << endl;
 cout << "+++++++++++2.小曾排序+++++++++++" << endl;
 cout << "+++++++++++3.新童鞋加入+++++++++++" << endl;
 cout << "+++++++++++4.刪除壞蛋+++++++++++" << endl;
 cout << "+++++++++++5.360顯示+++++++++++" << endl;
 cout << "+++++++++++6.超級保存+++++++++++" << endl;
 cout << "+++++++++++7.無影清屏+++++++++++" << endl;
 cout << "+++++++++++8.退出曾BB系統+++++++++++" << endl;
 cout << "++++++++++++++++++++++++++++++++++++++++" << endl;
 
}
void xianshi()
{
 system("cls");
 cout << "****************************************" << endl;
 cout << "   1----------按學號查詢----------" << endl;
 cout << "   2----------按姓名查詢----------" << endl;
 cout << "   3----------按成績查詢----------" << endl;
 cout << "   4----------返回" << endl;
 cout << "****************************************" << endl;
}
//查找學號
void findxuehao()
{
 int flag = 0;
 cout << "曾BB提示輸入學號" << std::endl;
 string str1;
 cin >> str1;
 auto it = mylist.begin();
 for (; it != mylist.end(); ++it)
 {
  if (str1 == it->ID)
  {
   cout << "找到該童鞋" << std::endl;
   it->print();
   flag = 1;
   break;
  }
  
 }
 if (flag == 0)
 {
  std::cout << "沒找到,請百度哦" << std::endl;
 }
}
//查找名字
void findmingzi()
{
 int flag = 0;
 cout << "曾BB提示輸入名字" << std::endl;
 string str1;
 cin >> str1;
 auto it = mylist.begin();
 for (; it != mylist.end(); ++it)
 {
  if (str1 == it->name)
  {
   cout << "找到該童鞋" << std::endl;
   it->print();
   flag = 1;
   break;
  }
 }
 if (flag == 0)
 {
  std::cout << "沒找到,請百度哦" << std::endl;
 }
}
//分數查找
void findfenshu()
{
 int flag = 0;
 cout << "曾BB提示輸入分數" << std::endl;
 int num;
 cin >> num;
 auto it = mylist.begin();
 for (; it != mylist.end(); ++it)
 {
  if (num == it->grade)
  {
   cout << "找到該童鞋" << std::endl;
   it->print();
   flag = 1;
   break;
  }
 }
 if (flag == 0)
 {
  std::cout << "沒找到,請百度哦" << std::endl;
 }

}
//排序
void sortpaixu()  //這裏還有些不明白
{
 system("cls");
 cout << "分數排序完畢" << std::endl;
 mylist.sort(greater<Student>());
 auto it = mylist.begin();
 for (; it != mylist.end(); ++it)
 {
  it->print();
 }
}
//插入一個學生
void insert()
{
 system("cls");
 cout << "學號多少" << endl;
 string str1;
 cin >> str1;
 cout << "姓名多少" << endl;
 string str2;
 cin >> str2;
 cout << "分數呵呵,你懂得" << endl;
 int num;
 cin >> num;
 Student one(str2, str1, num);
 mylist.push_back(one);
 //std::cout << mylist.size() << endl;
 std::cout << "要不要再顯示呢 y or n" << endl;
 char ch;
 cin >> ch;
 if (ch == 'y')
 {
  
  for (auto it= mylist.begin(); it != mylist.end(); ++it)
  {
   (it)->print();
  }
 }
 else
 {
  std::cout << "那就算了吧,下次哇" << endl;
  Sleep(2000);
  system("cls");
 }
}
//刪除學生
void deletest()
{
 system("cls");
 cout << "請輸入要刪除的學生" << endl;
 string str1;
 cin >> str1;
 int flag = 0;
 for (auto it = mylist.begin(); it != mylist.end(); it++)
 {
  if (str1 == it->ID)
  {
   std::cout << "找到該學生" << std::endl;
   it->print();
   mylist.erase(it);
   cout << "刪除OK" << endl;
   flag = 1;
  }
  break;
 }
 if (flag == 0)
 {
  std::cout << "沒有找到" << std::endl;
 }
}
//存儲文件
void savefile()
{
 system("cls");
 ofstream ofile("student.dat", ios::out);
 if (!ofile)
 {
  std::cout << "文件打開失敗" << std::endl;
  return ;
 }
 for (auto it = mylist.begin(); it != mylist.end(); ++it)
 {
  ofile << it->name << ' ' << it->ID << ' ' << it->grade << endl;
 }
 std::cout << "ok" << endl;
}
//讀出文件
void duchu()
{
 ifstream ifle("student.dat");
 if (!ifle)
 {
  std::cout << "文件打開失敗" << endl;
 }
 for (auto it = mylist.begin(); it != mylist.end(); ++it)
 {     
  string str1, str2; 
  int num;
  ifle >> str1 >> str2 >> num;
  std::cout << "名字 " << str1 << endl;
  std::cout << "學號" << str2 << endl;
  std::cout << "成績" << num << endl;
 
 }
 

}
int main()
{
 cout << "----------------歡迎進入曾BB學生管理系統------------------" << endl;
 MessageBoxA(NULL, "歡迎歡迎,熱烈歡迎", "曾哥歡迎", 1);
 char ch;
 show();
 while (cin >> ch)
 {
  switch (ch)
  {
  case'1'://搜索
   xianshi();
   
   while (cin >> ch)
   {
    int flag = 0;
    switch (ch)
    {
    case'1':
     findxuehao(); break;
    case'2':
     findmingzi(); break;
    case '3':
     findfenshu(); break;
    case'4':
     flag = 1;
     break;
    default:
     flag = 1;
     break;
    }
    if (flag == 1)
    {
     break;
    }
   }
   break;
  case'2':
   sortpaixu(); break;//排序
  case'3':
   insert(); break;//增加
  case'4':
   deletest(); break;//刪除
  case'5':
   print(mylist); break;//輸出
  case'6':
   savefile(); break;//存儲
  case'7':
   system("cls"); break;//清除
  case'8':
   return 0;
  default:
   return 0;
  }
  show();
 }
    
 cout << "=======曾BB系統要和你說byebye嘍" << endl;
 system("pause");
 return 0;
} 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章