C言語実装通信録—動的順序表

7815 ワード

以前、静的順序テーブル実装通信録を書いたブログがありましたが、静的順序テーブルには、いっぱいになったら、保存を続けることができないという大きな欠陥がありました.
従来のバージョンを改善するため、今回はダイナミックシーケンステーブルの通信録を実現し、スペースがいっぱいになる心配がなく、ダイナミックにスペースを開くことができます.環境は以前と同じで、VS 2008です;
今回規範化のために作られたのは.cファイル
#include 
#include 
#include 
#include 
#pragma warning(disable:4996)
typedef struct People{
	char _name[20];  
	char _sex[2];  
	int _age;  
	char _number[20];  
	char _address[100]; 
}People; 


typedef struct PeopleBook 
{ 
	People* _a; 
	size_t _size;	//        
	size_t _capacity;	//    
}PeopleBook; 


void Init(PeopleBook* PB);//   
void CheckCapacity(PeopleBook* PB);//        ,     
void AddPeople(PeopleBook* PB);//     
void DeletePeople(PeopleBook* PB);//     
void FindPeople(PeopleBook* PB);//             
void ChangePeople(PeopleBook* PB);//       
void PrintPeopleBook(PeopleBook* PB);//         
void DestroyPeople(PeopleBook* PB);//         
void Swap(People* a,People* b);//         ,
void SortPeople(PeopleBook* PB);//       
void GUI();//    




void Init(PeopleBook* PB){
	PB->_a = NULL;
	PB->_capacity = 0;
	PB->_size = 0;
}


void CheckCapacity(PeopleBook* PB){
	People* pTemp;
	size_t newSize;
	if(PB->_capacity == PB->_size){
		newSize = (PB->_capacity)*2+3;
		pTemp = (People*)realloc(PB->_a,sizeof(People)*newSize);
		if (pTemp)
		{
			PB->_a = pTemp;
			PB->_capacity = newSize;
		}
		else{
			perror("realloc is failed!
"); } } } void AddPeople(PeopleBook* PB){ assert(PB); CheckCapacity(PB); printf("Please input people's name:
"); scanf("%s",PB->_a[PB->_size]._name); printf("Please input people's sex:
"); scanf("%s",PB->_a[PB->_size]._sex); printf("Please input people's age:
"); scanf("%d",&(PB->_a[PB->_size]._age)); printf("Please input people's number:
"); scanf("%s",PB->_a[PB->_size]._number); printf("Please input people's address:
"); scanf("%s",PB->_a[PB->_size]._address); printf("Add people success!
"); ++PB->_size; } void DeletePeople(PeopleBook* PB){ char tempname[20]; size_t temp = 0; size_t i =0; assert(PB); printf("Please input the people you want to delete:
"); scanf("%s",tempname); for (;i_size;i++) { if (strcmp(tempname,PB->_a[i]._name) == 0) { printf("The %d People is:
",i+1); printf("name:%s
",PB->_a[i]._name); printf("sex:%s
",PB->_a[i]._sex); printf("age:%d
",PB->_a[i]._age); printf("number:%s
",PB->_a[i]._number); printf("address:%s
",PB->_a[i]._address); } } printf("Please input whitch people you want to delete:
"); scanf("%d",&temp); i = temp-1; for (;i_size-1;i++) { strcpy(PB->_a[i]._name,PB->_a[i+1]._name); strcpy(PB->_a[i]._sex,PB->_a[i+1]._sex); PB->_a[i]._age,PB->_a[i+1]._age; strcpy(PB->_a[i]._number,PB->_a[i+1]._number); strcpy(PB->_a[i]._address,PB->_a[i+1]._address); } --PB->_size; printf("Delete success!
"); } void FindPeople(PeopleBook* PB){ char tempname[20]; size_t i = 0; int flag = 0; assert(PB); printf("Please input people's name who you are finding:
"); scanf("%s",tempname); printf("The result is:
"); for (;i_size;i++) { if(strcmp(tempname,PB->_a[i]._name)==0){ //printf("The %d People is:
",i+1); printf("name:%s
",PB->_a[i]._name); printf("sex:%s
",PB->_a[i]._sex); printf("age:%d
",PB->_a[i]._age); printf("number:%s
",PB->_a[i]._number); printf("address:%s
",PB->_a[i]._address); flag = 1; } } if(flag == 0){ printf("Empty!
"); } } void ChangePeople(PeopleBook* PB){ size_t i = 0 ; char tempname[20]; int temp = 0; assert(PB); printf("Please input whitch people you want to change?
"); scanf("%s",tempname); for (;i_size;i++) { if(strcmp(tempname,PB->_a[i]._name)==0){ printf("The %d People is:
",i+1); printf("name:%s
",PB->_a[i]._name); printf("sex:%s
",PB->_a[i]._sex); printf("age:%d
",PB->_a[i]._age); printf("number:%s
",PB->_a[i]._number); printf("address:%s
",PB->_a[i]._address); } } printf("Please input the number:
"); scanf("%d",&temp); printf("Please input people's new name:
"); scanf("%s",PB->_a[temp-1]._name); printf("Please input people's new sex:
"); scanf("%s",PB->_a[temp-1]._sex); printf("Please input people's new age:
"); scanf("%d",&(PB->_a[temp-1]._age)); printf("Please input people's new number:
"); scanf("%s",PB->_a[temp-1]._number); printf("Please input people's new address:
"); scanf("%s",PB->_a[temp-1]._address); printf("Change successful!
"); } void PrintPeopleBook(PeopleBook* PB){ size_t i = 0; assert(PB); if(PB->_size == 0){ printf("People is empty!
"); return; } else{ for (;i_size;i++) { printf("The %d People is:
",i+1); printf("name:%s
",PB->_a[i]._name); printf("sex:%s
",PB->_a[i]._sex); printf("age:%d
",PB->_a[i]._age); printf("number:%s
",PB->_a[i]._number); printf("address:%s
",PB->_a[i]._address); } } } void DestroyPeople(PeopleBook* PB){ assert(PB); free(PB->_a); PB->_size = 0; printf("Destroy people is successed!
"); }; void Swap(People* a,People* b){ People temp; temp = *a; *a = *b; *b = temp; } void SortPeople(PeopleBook* PB){ size_t begin; size_t finish = PB->_size; int flag = 0; assert(PB); while (finish>1) { begin = 1; while (begin_a[begin]._name,PB->_a[begin-1]._name)<0){ Swap(&(PB->_a[begin]),&(PB->_a[begin-1])); flag = 1; } begin++; } if(flag == 0){ return; } finish--; } } void GUI(){ printf("***************************************************
"); printf("* 1、 2、 *
"); printf("* 3、 4、 *
"); printf("* 5、 6、 *
"); printf("* 7、 0、 *
"); printf("***************************************************
"); } void test(){ int select = 0; PeopleBook ConnactBook; GUI(); Init(&ConnactBook); while (select!= EOF) { printf("please input your select:
"); scanf("%d",&select); switch(select){ case 1: AddPeople(&ConnactBook); GUI(); break; case 2: DeletePeople(&ConnactBook); GUI(); break; case 3: FindPeople(&ConnactBook); GUI(); break; case 4: ChangePeople(&ConnactBook); GUI(); break; case 5: PrintPeopleBook(&ConnactBook); GUI(); break; case 6: DestroyPeople(&ConnactBook); GUI(); break; case 7: SortPeople(&ConnactBook); GUI(); break; case 0: printf("Thanks For Use~! Success To Exit!
"); return ; break; default: printf("Error input,please input again!
"); } } } int main(){ test(); return 0; }
は編集者レベルに限られており、このコードには多くの不足点があります.ご指摘を歓迎します.