[转]C++ list 实例

 转自: http://hi.baidu.com/jizj06/blog/item/1abfa5fd61dc6841d7887d32.html

初学C++,理论看了不少,没做什么东西,感觉很虚^^^^^

今天在网上看到一个作业题,就很想尝试做一下,结果很受打击,看来还的多多练习;

回来请教高手,一句话就轻松解决了,终于,在高手帮助下,我完成了自己的第一个作业

下面就是这道题的答案,只是初步实现了功能,还有很多不完善,欢迎大家批评指教

/*具体要求是:定义一个描述班级学生通讯录的类,
数据成员记录一个班级每名学生的学号、姓名、电话号码;成员函数包括:
(1)添加一个学生的各个数据项
(2)删除某个学号学生
(3)修改某个学号学生所有数据项
(4)输出某学号学生各个数据项
(5) 输出所有学生个个数据项。编写一个测试程序实现对该类的测试。(要求将学生信息用数组存储)
已有框架,再此基础上添加些程序能运行出来就可以了!*/


#include "stdafx.h"
#include<iostream>
#include<string>
#include <list>
using namespace std;

struct student

{
student (int no, string &name, string &tel)

{
this ->tno = no ;
this ->tname = name ;
this ->ttel = tel ;

}

int tno;

string tname; //不能用char[],当数组超过最大时,很容易造成崩溃!!

string ttel;

};



class Tstudent

{

public:

      
       list <student> m_liststudent; //定义一个list容器

       bool insert( int theNo, string &name, string &tel)
       {
      
        for ( list < student > :: iterator it = m_liststudent.begin () ; it != m_liststudent.end (); it ++ )
        {
    if ((*it).tno == theNo )
    {
     printf( "学号不能重复" );
     return false ;
    }
        }
      
   m_liststudent.push_back (student (theNo, name, tel));
  
   return true;
  
       }

       /*插入一个学生,插入成功返回true,否则返回false;

       要求判断学生学号不能重复,插入按照学生学号有序插入*/

       void update(const int theNo, const string &name, const string &tel)
       {
     for (list <student>::iterator it = m_liststudent.begin (); it != m_liststudent.end (); it ++)
     {
      if ((*it).tno == theNo)
      {
     
       it ->tname = name;
       it ->ttel = tel;
      
       return;
      }
     }
       }

       /*修改theNO学号的姓名和电话*/

       bool deleteOne( int theNo)
       {
     for (list <student>::iterator it = m_liststudent.begin (); it != m_liststudent.end (); it ++)
     {
      if ((*it).tno == theNo)
      {
       m_liststudent.erase ( it) ;
      
       return true;
      }
     }
    printf( "学号不存在" );
    return false;
       }

       /*删除theNo学号的学生记录,删除成功返回true,否则返回false;

       要求判断学生学号是否存在,删除成功后被删除记录后面的记录要前移*/

       bool getOne(int theNo, string &name, string &tel)
       {
     for (list <student>::iterator it = m_liststudent.begin (); it != m_liststudent.end (); it ++)
     {
      if ((*it).tno == theNo)
      {
       name = it ->tname;
       tel = it ->ttel;
       return true ;
      }
     }
    printf( "学号不存在" );
    return false;
       }

       /*获取theNo学号学生姓名、电话存储到name,tel中

       若该学号学生存在返回true,否则返回false;*/

       void print ( )
       {
     for (list <student>::iterator it = m_liststudent.begin (); it != m_liststudent.end (); it ++)
     {
      cout << it ->tno << "," << it -> tname << ","
      << it -> ttel << endl;
     }
       }

      /* 输出所有学生记录项*/

private:


};

int main()

{    

       //菜单控制如下:

       int select;

    Tstudent ts;

     int no1 ;
     string name1,tel1;

       while(1)

       {

              cout<<"      ********************"<<endl;

              cout<<'/t'<<"1、添加学生"<<endl;


              cout<<'/t'<<"2、修改学生"<<endl;

              cout<<'/t'<<"3、删除学生"<<endl;

              cout<<'/t'<<"4、获取学生"<<endl;

              cout<<'/t'<<"5、输出所有信息"<<endl;

              cout<<'/t'<<"6、退出"<<endl;

              cout<<"      ********************"<<endl;

              cout<<"请选择: ";

              cin>>select;

              switch(select)

              {

              case 1:

                     //添加学生
    
      printf ( " 请输入学号,姓名和电话 :");
      cin >> no1 >> name1 >> tel1   ;
      ts.insert(no1,name1,tel1);
      ts.print();
                  break;

              case 2:

                     //修改学生             

                     break;

              case 3:

                     //删除学生

                     break;

              case 4:

                     //获取学生信息

                     break;

              case 5:

                     //输出所有信息

                     break;

              case 6:

      return 0;
                     //退出

                     break;

              }

       }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值