C語言製作一個完整通訊錄

用C語言製作一個通訊錄,這個通訊錄需要完成一系列基本的通訊錄應有的工作:增加、刪除、查找、修改、展示、退出。這個通訊錄需要做到動態的內存開闢,以及將寫好的通訊錄的內容寫入文件,每次在開始時可以調出原來寫好的內容。

至於如何實現這些功能,我接下來一個接一個說明,說明完成後,就直接將源代碼全部放出來,源代碼分爲三個文件,一個頭文件,兩個源文件。

這裏強調一下,就我個人目前寫代碼的經驗,我覺得在寫一個程序開始前,一定要將整個程序的所有或大部分應該實現的功能在自己心裏想清楚,或者寫下來,在每個程序寫之前一定要有一個清晰的框架,避免寫好後一遍又一遍的修改,去增加一些突然想好的新增加的功能。

首先我們來屢一下思路:

  1. 我們首先需要確定這個通訊錄的每個成員的數據類型,毫無疑問是這個成員是一個結構體,每個成員有多重特性:序號、姓名,地址,性別,電話號碼。這幾項中的地址和性別還有自己單獨的數據類型,地址也是一個結構體,地址有四個成員:國家、省份、城市、具體地址,這四項。性別則是一個枚舉類型:男,女,保密,這三種類型。在一開始就應該在頭問價中寫好。
  2. 我們在main函數中需要做到有一個打印函數,一個通訊錄的大小的開闢(如果已經有存儲好的數據,就需要進行一個數據的讀取,並將其寫入這個通訊錄)一個選擇的輸入,還有一個對選擇做出反應的函數,這樣,便把main函數的基本結構定了下來 。並且這個時候我們還要定義兩個全局變量,分別記錄通訊的大小和通訊錄中成員的個數。
  3. 我們接下來進行其他函數的定義:
  • 打印函數:這個函數只需要進行一個基本打印就可以了
  • 增加函數:這個函數需要做到的是新增,而且在所開闢的空間滿了後,還要realloc增加空間。新增成員時,一次進行成員的寫入,每寫入一個成員,記錄成員個數的變量自增一次。
  • 刪除函數:刪除函數需要做到兩個功能,一個是刪除指定的成員,一個是要刪除全部成員。刪除其實就是將指定成員後面的成員向前覆蓋,然後記錄成員個數的變量自減一次(也可以最後一個成員直接覆蓋到指定成員,然後成員個數自減一次)。至於刪除所有成員,我的做法是直接將成員個數變量置零,這樣就做到了所有成員的刪除。在刪除完後,要進行一個內存大小的管理,如果需要將通訊錄縮小,就realloc縮小內存。
  • 查找函數:就是查找,通過循環進行一個查找。查找時使用的時name作爲關鍵詞進行查找。用strcmp進行查找。
  • 修改函數:就是修改嘛,根據所獲得選擇進行一個修改。
  • 展示函數:這個函數其實是需要做到兩個功能,一個是成員的排序,另一個是將通訊錄成員展示出來。
  • 排序函數:排序函數首先要做到一個按照名字進行的冒泡排序(或者是其他排序方法也可以),然後還要進行一個成員序號的寫入。
  • 保存函數:將目前的、未完成的成員數據暫時先寫入文件,免得數據丟失。
  • 退出函數:這個函數需要做到一個將目前的通訊錄成員的數據寫入文件,然後結束通訊。

在這幾個基本框架搭建完成後,還需要做一些數據有有效行的檢查,確保數據輸入沒有問題。覺得我寫的還行的的話,點個贊再走唄。

好了,話不多說,直接上代碼:(代碼分爲三部分:第一部分是頭文件,第二部分是各個函數所在的源文件,第三部分是main函數所在的源文件)

#ifndef _CONTECT_H_
#define _CONTECT_H_

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>

#define NUM 2

extern int NumPeople;//all people
extern int SizeContect;//realloc function 

typedef struct ConAddress
{
	char Country[15];
	char Province[15];
	char City[15];
	char Detail[15];
}ConAddress;

typedef enum ConSex
{
	men,
	women,
	secret
}ConSex;

typedef struct Contect
{
	int Number;
	char Name[20];
	ConAddress Address;
	ConSex Sex;
	char CallNumber[15];
}Contect;


void PrintContect();
Contect* DoChange(int change,Contect *people);



#endif _CONTECT_H_
#define _CRT_SECURE_NO_WARNINGS 1
#include "Contects.h"

int NumPeople = 0;//all people
int SizeContect = NUM;//realloc function 
int i = 0;//Show function


void PrintContect(change)// print function
{
	printf("                     **********************************\n");
	printf("                     ******* 1.Add      2.Del    ******\n");
	printf("                     ******* 3.Show     4.Search ******\n");
	printf("                     ******* 5.Modify   6.Modify ******\n");
	printf("                     ******* 7.Save     0.Exit   ******\n");
	printf("                     **********************************\n");
	switch (change)
	{
	case 1:
		printf("Input success\n");
		break;
	case 2:
		if (NumPeople == 0)
		{
			printf("Contect is void\n");
			break;
		}
		else
		{
			printf("Delete success\n");
			break;
		}
 	//case 3:
	//	if (NumPeople == 0)
	//	{
	//		printf("Contect is void ,don't show\n");
	//		break;
	//	}
	case 4:
		if (NumPeople == 0)
		{
			printf("Contect is void ,don't search\n");
			break;
		}
	default:
		break;
	}
	printf("Please do change:>");

}

void SortContact(Contect *people)
{
	Contect tmp;
	int i = 0;
	int j = 0;
	for (i = NumPeople; i > 0;--i)
	{
		if (NumPeople == 1)
		{
			break;
		}
		for (j = 0; j < i;++j)
		{
			if (strcmp(people[j].Name,people[j+1].Name)>0)
			{
				tmp = people[j];
				people[j] = people[j + 1];
				people[j + 1] = tmp;
			}
		}
	}
	for (i = 0; i < NumPeople; ++i)
	{
		people[i].Number = i + 1;
	}

}


Contect* AddContect(Contect* people)
{
	assert(people != NULL);
	int tmp = 0;
	int key = 1;

	if (NumPeople == SizeContect)
	{
		int tmp = SizeContect;
 		SizeContect = SizeContect + tmp;
		people = (Contect*)realloc(people, SizeContect*sizeof(Contect));
	}

	printf("-----------------------------------------------------------------------------------\n");
	printf("Number   Name          Country    Province    City   Detail      Sex     CallNumber    \n");
	printf("-----------------------------------------------------------------------------------\n");

	printf(" Input name:>");
	scanf("%s", people[NumPeople].Name);
	if (strcmp(people[NumPeople].Name,"exit0")==0)
	{
		return people;
	}


	printf(" Input address(Country):>");
	scanf("%s", people[NumPeople].Address.Country);
	
	printf(" Input address(Province):>");
	scanf("%s", people[NumPeople].Address.Province);
	
	printf(" Input address(City):>");
	scanf("%s", people[NumPeople].Address.City);
	
	printf(" Input address(Detail):>");
	scanf("%s", people[NumPeople].Address.Detail);


	
	printf(" Input sex number( men(0) or women(1) or secret(2) ):>");
	scanf("%d",&tmp);
	while (key)
	{
		if (tmp == 0 || tmp == 1 || tmp == 2)
		{
			people[NumPeople].Sex = tmp;
			key = 0;
		}
		else
		{
			printf("Input error\n");
			scanf("%d", &tmp);
		}
	}
	printf(" Input PhoneNumber:>");
	scanf("%s", people[NumPeople].CallNumber);
	
	++NumPeople;
	return people;
}


Contect* DelContect(Contect* people)
{
	assert(people != NULL);
	char DelName[15] = { 0 };
	int i = 0;

	printf("Please input need delete name(Input rma could delete all name):>");
	scanf("%s", DelName);
	if (strcmp(DelName,"rma")==0)
	{
		NumPeople = 0;
		SizeContect = NUM;
		people = (Contect*)realloc(people, NUM*sizeof(Contect));
	}
	for (i = 0; i < NumPeople;++i)
	{
		if (strcmp(DelName,people[i].Name)==0)
		{
			while (i < NumPeople)
			{
				people[i] = people[i + 1];
				++i;
			}
			--NumPeople;
		}

	}
	if (NumPeople != 0 && NumPeople < SizeContect/2)
	{
		SizeContect /= 2;
		people = (Contect*)realloc(people, SizeContect*sizeof(Contect));
	}

	return people;
}


Contect* ShowContect(Contect* people)
{
	assert(people != NULL);
	SortContact(people);
	printf("-----------------------------------------------------------------------------------\n");
	printf("Number   Name          Country    Province    City   Detail      Sex     CallNumber    \n");
	printf("-----------------------------------------------------------------------------------\n");
	for (i = 0; i < NumPeople;++i)
	{
		printf("%-8d", people[i].Number);
		printf("%-7s", people[i].Name);
		printf("%10s %10s %10s %10s", people[i].Address.Country, people[i].Address.Province, people[i].Address.City, people[i].Address.Detail);
		//printf("%8d", people[NumContect].Sex);
		if (people[i].Sex==0)
		{
			printf("       man");
		}
		else if (people[i].Sex == 1)
		{
			printf("     women");
		}
		else if (people[i].Sex == 2)
		{
			printf("    secret");
		}


		printf("%15s", people[i].CallNumber);
		printf("\n");
	}
	printf("-----------------------------------------------------------------------------------\n");
	printf("\n");
	printf("\n");
	
	return people;

}


Contect* SearchContect(Contect* people)
{
	assert(people != NULL);
	int i = 0;
	char search[15] = { 0 };
	int check = 0;

	printf("Please input you need search name :>");
	scanf("%s", search);
	printf("-------------------------------------------------------------------------------\n");
	printf("Name          Country    Province    City   Detail      Sex     CallNumber    \n");
	for (i = 0; i < NumPeople;++i)
	{
		if (strcmp(search,people[i].Name) == 0)
		{

			printf("%-7s", people[i].Name);
			printf("%10s %10s %10s %10s", people[i].Address.Country, people[i].Address.Province, people[i].Address.City, people[i].Address.Detail);
			printf("%8d", people[i].Sex);
			printf("%15s", people[i].CallNumber);
			printf("\n");

			++check;

		}
	}
	printf("-------------------------------------------------------------------------------\n");
	printf("\n");
	if (check==0)
	{
		printf("Not found\n");
	}
	return people;

}


Contect* ModifyContect(Contect* people)
{
	int j = 0;
	int i = 0;
	int num = 0;
	char tmp[15] = { 0 };
	char Itmp = 0;
	int key = 0;
	int Check = 0;
	int CheckArr[10] = { 0 };

	printf("Change need modify name:>");
	scanf("%s", tmp);
	system("cls");
	printf("-----------------------------------------------------------------------------------\n");// Print pinter
	printf("Number   Name          Country    Province    City   Detail      Sex     CallNumber    \n");
	printf("-----------------------------------------------------------------------------------\n");
	for (i = 0; i < NumPeople; ++i)
	{
		if (strcmp(tmp, people[i].Name) == 0)
		{
			CheckArr[j] = people[i].Number;
			++j;
			++Check;

			printf("%-8d", people[i].Number);
			printf("%-7s", people[i].Name);
			printf("%10s %10s %10s %10s", people[i].Address.Country, people[i].Address.Province, people[i].Address.City, people[i].Address.Detail);
			printf("%8d", people[i].Sex);
			printf("%15s", people[i].CallNumber);
			printf("\n");
			if (Check == 1)
			{
				num = people[i].Number;
			}

		}
	}
	printf("-------------------------------------------------------------------------------\n");
	printf("\n");
	printf("                     *****************************************************\n");
	printf("                     *****                   1.Name                  *****\n");
	printf("                     *****  2.Country  3.Province  4.City  5.Detail  *****\n");
	printf("                     *****                    6.Sex                  *****\n");
	printf("                     *****                7.CallNumber               ******\n");
	printf("                     ******************************************************\n");

	if (Check == 0)
	{
		printf("Contect can't found this name\n");
		return people;
	}
	if (Check == 1)
	{
		printf("Which need be changed of the options :>");
		scanf("%d", &key);
	}
	if (Check > 1)
	{
		printf("Which need be changed of the number and options :>");
		scanf("%d", &num);
		scanf("%d", &key);
		for (j = 0; j < Check;++j)//Check num are invalid
		{
			if (CheckArr[j] == num)
			{
				break;
			}
			if (CheckArr[Check - 1] != num&&j == Check - 1)//If num is invalid
			{
				while (1)
				{
					printf("Input error ,again input\n");
					printf("Which need be changed of the number :>");
					scanf("%d", &num);

					for (j = 0; j < Check; ++j)
					{
						if (CheckArr[j] == num)
						{
							break;
						}
					}
					if (CheckArr[j] == num)
					{
						break;
					}
				}
				if (CheckArr[j] == num)
				{
					break;
				}
			}

		}
		if (key < 1||key > 7)
		{
			printf("Input option is invalid,again:>");
			while (key > 0&&key < 8)
			{
				scanf("%d", &key);
			}

		}


	}



	printf("What are you modify:>");//Input modify
	if (key == 6)//Modify sex
	{
		scanf("%d", &Itmp);
		while (1)//Check sex invalid
		{
			if (Itmp == 0 || Itmp == 1 || Itmp == 2)
			{
				break;
			}
			printf("Input error\n");
			scanf("%d", &Itmp);
		}
	}
	else
	{
		scanf("%s", tmp);
	}
	switch (key) //Modify
	{
	case 1:
		strcpy(people[num - 1].Name, tmp);
		break;
	case 2:
		strcpy(people[num - 1].Address.Country, tmp);
		break;
	case 3:
		strcpy(people[num - 1].Address.Province, tmp);
		break;
	case 4:
		strcpy(people[num - 1].Address.City, tmp);
		break;
	case 5:
		strcpy(people[num - 1].Address.Detail, tmp);
		break; 
	case 6:
		people[num - 1].Sex = Itmp;
		break;
	case 7:
		strcpy(people[num - 1].CallNumber, tmp);
		break;
	default:
		break;
	}
	return people;
}


Contect* WriteContect(Contect* people)
{
	assert(people != NULL);
	int i = 0;
	FILE* FileContect = NULL;
	FileContect = fopen("contact.txt", "w");
	if (FileContect == NULL)
	{
		perror("contect.txt");
		exit(EXIT_FAILURE);
	}
	for (i = 0; i < NumPeople; i++)
	{
		fprintf(FileContect, "%d ", NumPeople);
		fprintf(FileContect, "%s %s %s %s %s %d %s", people[i].Name, people[i].Address.Country, people[i].Address.Province,
			people[i].Address.City, people[i].Address.Detail, people[i].Sex, people[i].CallNumber);
	}

	fclose(FileContect);
	if (NumPeople == 0)
	{
		remove("contact.txt");
	}
	return people;
}


Contect* SaveContect(Contect* people)
{
	assert(people != NULL);
	int i = 0;
	FILE* FileContect = NULL;
	FileContect = fopen("contact.txt", "w");
	if (FileContect == NULL)
	{
		perror("contect.txt");
		exit(EXIT_FAILURE);
	}
	for (i = 0; i < NumPeople; i++)
	{
		fprintf(FileContect, "%d ", NumPeople);
		fprintf(FileContect, "%s %s %s %s %s %d %s", people[i].Name, people[i].Address.Country, people[i].Address.Province,
			people[i].Address.City, people[i].Address.Detail, people[i].Sex, people[i].CallNumber);
	}

	fclose(FileContect);

	return people;
}


Contect* DoChange(int change,Contect *people)//do switch case
{
	switch (change)
	{
	case 1:
		AddContect(people);
		break;
	case 2:
		ShowContect(people);
		DelContect(people);
		break;
	case 3:
		system("cls");
		ShowContect(people);
		break;
	case 4:
		SearchContect(people);
		break;
	case 5:
		ShowContect(people);
		ModifyContect(people);
		break;
	case 6:
		system("cls");
		return people;
	case 7:
		SaveContect(people);
		return people;
	case 0:
		WriteContect(people);
		printf("   Exit from Contect \n");
		return people;
	default:
		printf("Input error\n");
		break;
	}
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contects.h"

int main()
{
	Contect *people = NULL;

	int change = 0;
	int i = 0;
	int check = 0;

	FILE* FileContect = fopen("contact.txt", "r");
	if (FileContect != NULL)
	{
		check = fscanf(FileContect, "%d", &NumPeople);
		people = (Contect*)malloc(NumPeople * sizeof(Contect));
		SizeContect = NumPeople;

		for (i = 0; i < NumPeople; ++i)
		{
			fscanf(FileContect, "%s", people[i].Name);
			fscanf(FileContect, "%s", people[i].Address.Country);
			fscanf(FileContect, "%s", people[i].Address.Province);
			fscanf(FileContect, "%s", people[i].Address.City);
			fscanf(FileContect, "%s", people[i].Address.Detail);
			fscanf(FileContect, "%d", &people[i].Sex);
			fscanf(FileContect, "%s", people[i].CallNumber);
		}
		fclose(FileContect);
	}
	else
	{
		people = (Contect*)malloc(SizeContect * sizeof(Contect));
	}
	do{
		PrintContect(change);
		scanf("%d", &change);
		while (change < 0|| change > 7)
		{
			printf("Input error,again:>");
			scanf("%d", &change);
			if (change >= 0&&change <= 7)
			{
				break;
			}
		}
		if (change == 1 || change == 2)
		{
			system("cls");
		}
		people = DoChange(change, people);

	} while (change);


	free(people);
	people = NULL;
	return 0;
}

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章