用c++写的简易学生通讯录

来源:互联网 发布:lte优化工程师前景 编辑:程序博客网 时间:2024/06/08 17:04

        昨天我一大学室友找到我说他亲戚要个学生通讯录程序,一定要c++来写,而他是学Java的,对c++的基本语法都忘干净了。因为我是做c方面开发的,所以问我能不能搞定。虽然我也半年多没用c++写过东西了,但作为室友怎么能拒绝呢,再个看了下他的需求感觉挺简单的(没涉及到数据库操作),于是就应承下来了。

        他给我的需求是这样的:

        学生通讯录系统
        学生通信录信息包括:姓名、学号、年龄、性别、家庭住址、联系电话、寝室号等信息。
        系统以菜单方式工作,使之能提供以下功能::
        学生通信录信息的输入
        学生的通信录信息删除和修改
        学生的通信录信息查询和统计功能
        学生的通信录信息输出显示 
        而且数据都保存在内存中;

        我在Linux下用了半上午时间给他写了个简易的通讯录程序,下面我把代码贴出来,希望可以给那些大一大二想写这个程序的同学一点思路。其中不规范的地方还望大家指出了(因为为了尽快完成功能,所以一些规范就没太注意),谢谢!
#include<iostream>#include<cstdlib> //主要是用到exit()退出进程函数 #include<string.h>//字符串头文件#define NoFind -1#define NoOperation -2#define Fill  -3#define Exist -4using namespace std;class student{  public:    void printStudent(); // print a student information    void setName();    string getName();        void setId();    unsigned int getId();    void setAge();    unsigned int getAge();    void setSex();    char getSex();     void setAddr();    string getAddr();    void setPhone();    string getPhone();    void setRoom();    string getRoom();  private:    string name;    unsigned int  id;    unsigned int  age;    char sex;    string addr;    string phone;    string room;};void student::setId(){  cout<<"Id:";  cin>>id;}unsigned int student::getId(){  return id;}void student::setName(){  cout<<"Name:";  cin>>name;}string student::getName(){  return name;}void student::setAge(){  cout<<"Age:";  cin>>age;}unsigned int student::getAge(){  return age;}void student::setSex(){  cout<<"Sex:";  cin>>sex;}char student::getSex(){  return sex;}void student::setAddr(){  cout<<"Addr:";  cin>>addr;}string student::getAddr(){  return addr;}void student::setPhone(){  cout<<"Phone:";  cin>>phone;}string student::getPhone(){  return phone;}void student::setRoom(){  cout<<"Room:";  cin>>room;}string student::getRoom(){   return room;}void student::printStudent(){  cout<<"Id:"<<id<<endl;  cout<<"Name:"<<name<<endl;  cout<<"Age:"<<age<<endl;  cout<<"Sex:"<<sex<<endl;  cout<<"Addr:"<<addr<<endl;  cout<<"Phone:"<<phone<<endl;  cout<<"Room:"<<room<<endl;  cout<<endl;}//上面都是学生类,以及类的属性设置和获取函数////////////////////////////////////////////////////////////////#define LEN 1024//定义一个结构体,保存类对象和数据有效性标志typedef struct Node{     student s;     int flag;//如果为0 表示该结构体中的对象无效}Node;static int studentNum = 0;static Node buff[LEN] = {};//用来存放上面结构体对象的,一个对象表示一个同学的信息//在数组中得到一个空闲的元素,返回数组小标;是否空闲可以查看flag标志位int getArrayFree(){int i;for(i = 0; i < LEN; i++){if(0 == buff[i].flag)return i;}return Fill;}//在数组中查找指定学生的信息,该函数被删除和修改函数调用//用学号查询和姓名查询两种方式,返回查找到的学生在数组中下标号int getArrayIndex(){unsigned int select;unsigned int id = 0;string name = "";cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;cout<<"select operation student way, id or name?"<<endl;    cout<<"****************************"<<endl;    cout<<"       1   Use  id          "<<endl;    cout<<"       2   Use  name        "<<endl;    cout<<"       3   Break            "<<endl;    cout<<"       4   Exit             "<<endl;    cout<<"****************************"<<endl;cin>>select;switch(select){case 1:cout<<"please enter the student Id:";cin>>id;    break;case 2:cout<<"please enter the student Name:";cin>>name;break;case 3:return NoOperation;case 4:cout<<"exit process!"<<endl;exit(0);        default:            cout<<"other select will go break!"<<endl;return NoOperation;}    int i;for(i = 0; i < LEN; i++){    if(!(buff[i]).flag) continue;if(0 == id){if(name == buff[i].s.getName())return i;}if(id == buff[i].s.getId())return i;}return NoFind;}//判断该id是否存在,姓名可以相同,但学号一定不能相同int isExist(int id){int i;int count = 0;for(i = 0; i < LEN; i++){if(id == buff[i].s.getId())   count++;}return count;}//增加一个学生的信息到数组中,也即是通讯录中增加一条通讯录int addStudentInfo(){student newStd;string name;int i;int index;char yORn;    cout<<endl;cout<<"-----------------------------------"<<endl;cout<<"addStudentInfo:"<<endl;newStd.setId();newStd.setName();newStd.setAge();newStd.setSex();newStd.setAddr();newStd.setPhone();newStd.setRoom();cout<<endl;newStd.printStudent();cout<<"Are you sure this information is correct?[y or N]"<<endl;cin>>yORn;if(!(('y' == yORn) || ('Y' == yORn)))return 0;if( -1 == (index = getArrayFree()) ){cout<<"The contacts filled!"<<endl;return Fill;}    if(isExist(newStd.getId())){cout<<"The id is exist!"<<endl;return Exist;    }buff[index].s = newStd;buff[index].flag = 1;studentNum++;cout<<endl;cout<<"Success"<<endl;cout<<endl;return 0;}//删除指定学生的通讯录信息,只要flag置0int delStudentInfo(){int index;index = getArrayIndex();    if(NoFind == index){cout<<"No find the student!"<<endl;return 0;}if(NoOperation == index) return 0;    buff[index].flag = 0;studentNum--;    cout<<"--------------------------------------"<<endl;cout<<"Success"<<endl;    return 0;}//修改指定学生的信息int updateStudentInfo(){int index;int select;    int count = 2;cout<<endl;cout<<"---------------------------------"<<endl;cout<<"update the student information:"<<endl;index = getArrayIndex();if(NoFind == index){cout<<"No find the student!"<<endl;return NoFind;}if(NoOperation == index) return 0;    while(1){cout<<endl;cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl;cout<<"   1  Id            "<<endl;cout<<"   2  Name          "<<endl;cout<<"   3  Age           "<<endl;cout<<"   4  Sex           "<<endl;cout<<"   5  Addr          "<<endl;cout<<"   6  Phone         "<<endl;cout<<"   7  Room          "<<endl;cout<<"   8  Break         "<<endl;cout<<"   9  Exit          "<<endl;cout<<"~~~~~~~~~~~~~~~~~~~~"<<endl;cout<<endl;cout<<"please select update informaton:"<<endl;cin>>select;switch(select){case 1://下面循环要判断id(学号)是否重合,如果重合就再选择一个学号,直到没有重合的while((count-1)){buff[index].s.setId();count = isExist(buff[index].s.getId());if(count >= 2) cout<<"id is exist!"<<endl;}break;case 2:buff[index].s.setName();break;case 3:buff[index].s.setAge();break;case 4:buff[index].s.setSex();break;case 5:buff[index].s.setAddr();break;case 6:buff[index].s.setPhone();break;case 7:buff[index].s.setRoom();break;case 8: return NoOperation;case 9: cout<<"exit process!"<<endl;exit(0);default:return NoOperation;}}return 0;}//统计通讯录中有多少个学生int accoutStudent() // accout Student number{   cout<<endl;   cout<<"---------------------------------"<<endl;   cout<<"student number:"<<studentNum<<endl;   return 0;}//打印指定学生信息void printStudentInfo(){int index;index = getArrayIndex();    if(NoFind == index){cout<<"No find the student!"<<endl;return;}if(NoOperation == index) return;    cout<<endl;cout<<"---------------------------------"<<endl;buff[index].s.printStudent();return;}//打印所有学生的信息void showAllStudentInfo(){int i;cout<<endl;    cout<<"show all stduent information:"<<endl;    for(i = 0; i < LEN; i++){if(1 == buff[i].flag)buff[i].s.printStudent();}return;}//根据菜单选择调用对应函数void select(int number){   switch(number)   {     case 1:           addStudentInfo();break;     case 2:           delStudentInfo();break;     case 3:           updateStudentInfo();break;     case 4:           accoutStudent();break;     case 5:           printStudentInfo();break;     case 6:           showAllStudentInfo();break;     default:           cout<<"error"<<endl;return;   }  }//选择菜单函数void menu(){    unsigned int number = 7;    while(1) {cout<<endl;cout<<"**********************************"<<endl;cout<<"    1 Add student information"     <<endl;cout<<"    2 Del student information"     <<endl;cout<<"    3 Update student information"  <<endl;cout<<"    4 Accout student number"       <<endl;cout<<"    5 Printf a student information"<<endl;cout<<"    6 Show all student information"<<endl;cout<<"    7 Exit                        "<<endl;cout<<"**********************************"<<endl;cout<<endl;cout<<"please enter the number:<1~7>"<<endl;cin>>number; if(7 == number)   return;           if((1 <= number)&&(7 > number))select(number);sleep(1); }}int main(int argc, char **argv){   menu();   return 0;}
        程序基本就是这样的,在Linux系统上测试通过,没问题。在其他系统上应该没有大问题,如果有的话就是头文件的问题(听室友说在mac上sleep()函数是没有的,Windows下要添加一个头文件,具体什么头文件需要的可以百度下);
        程序还有个问题,就是在menu()函数中输入值给number时,如果你输入字符就会出现死循环(这个死循环不是因为while(1)造成的,如果正常死循环,每循环一次就会等待用户输入一个值),在我预计中不会出现这个问题的,因为输入字符也是转化成ascii码,然后也会被剔除的。可惜,不是这样,我打印了下number(当输入字符A时),结果却是0,而不是64;还有当输入学生信息时,如果在id输入时,不小心输入名字时(其实就是字符串)也会出现未知的错误。查了资料说是c++中类型不能混用,本应该要有防止这种失误操作的处理方法,但我实验了下,没成功,如果谁有好的办法希望可以告诉我一声,共同学习嘛。谢谢!
         转载请注明作者和原文出处,原文地址:http://blog.csdn.net/yuzhihui_no1/article/details/43530445

0 0
原创粉丝点击