1. 程式人生 > >用c++寫的簡易學生通訊錄

用c++寫的簡易學生通訊錄

#include<iostream>
#include<cstdlib> //主要是用到exit()退出程序函式 
#include<string.h>//字串標頭檔案

#define NoFind -1
#define NoOperation -2
#define Fill  -3
#define Exist -4

using 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置0
int 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下要新增一個頭檔案,具體什麼標頭檔案需要的可以百度下);