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

#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 informationvoid 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下要添加一个头文件,具体什么头文件需要的可以百度下);

,那我想明天可以是我的来世。

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

相关文章:

你感兴趣的文章:

标签云: