練習一:通訊錄

設計一個班級同學的通訊錄,要求如下:
 通訊錄中每個同學的信息包含以下內容:學號(id)、姓名(name)、電話號碼(tel)。如果需要更多其他信息,請自行添加。
 程序主菜單包含以下幾個功能:
(1) 添加記錄:通過鍵盤輸入信息,添加一條通訊錄記錄。
(2) 刪除記錄:通過鍵盤輸入學號,刪除該學號的記錄。
(3) 輸出記錄:輸出通訊錄全部記錄。
(4) 按姓名查找:通過鍵盤輸入姓名,輸出該同學的所有信息。
(5) 保存記錄:把通訊錄中所有的記錄保存到文件中。
(6) 清空記錄:刪除通訊錄中的全部記錄,並刪除文件。
(7) 退出
提示:
 程序啓動時應判斷是否存在記錄文件,如果存在,則讀取每條記錄到鏈表中。
 用戶選擇並完成主菜單某功能後,除了退出程序,應該返回主菜單。
 添加一條記錄時,插入到鏈表的尾部。
 查找、刪除記錄時,如果該記錄不存在,則應該輸出不存在的提示。
 添加記錄、刪除記錄時不需要寫文件。
 保存記錄時,用覆蓋寫文件的方法。(或者先刪除原文件,再保存全部記錄信息)
 各個功能模塊寫成函數,由主函數調用。

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct Node; //A storage to store students' Id,name and telephone numbers;
typedef struct Node *PtrToNode; //A pointer with type of Node;
typedef PtrToNode Position;
typedef PtrToNode List;

//Defination of Node struct;
typedef struct Node
{
	char Id[12]; //An array to store student ID;
	char Name[8]; // An array to store student name;
	char Tel[12]; //An array to store student telephone numbers;
	Position Next; //A pointer point to the next Node storage;
};

//Add student's informations into the list;
void Add(List L,const char Id[],const char Name[],const char Tel[])
{
	Position TmpCell, P; //Define a temporary pointer named TmpCell;
	TmpCell = (PtrToNode)malloc(sizeof(Node)); //Creat a storage for TmpCell;
	strcpy(TmpCell->Id, Id); //Copy datas
	strcpy(TmpCell->Name, Name);
	strcpy(TmpCell->Tel, Tel);
	TmpCell->Next = NULL;

	P = L->Next;
	if (P) //Judge whether the first Node of the list is exits;
	{
		while (P->Next)
			P = P->Next;
		P->Next = TmpCell;
	}
	else
		L->Next = TmpCell;
}

//Open the file and read the datas into the list;
//Each line of file is like “16401010101 張三 13600000000”
int Open(List &L)
{
	string s1, s2, s3;
	ifstream file("D:\\a.txt");
	while (!file.eof())
	{
		file >> s1 >> s2 >> s3;
		const char* Id = s1.c_str();
		const char* Name = s2.c_str();
		const char* Tel = s3.c_str();
		if (file.eof())
			break;
		Add(L, Id, Name, Tel);
	}
	file.close();
	return 0;
}

//Delete a student's information by his or her student ID;
int Delete(List L, char Id[])
{
	Position P = L,TmpCell;
	while (P->Next) //Judge whether the list is empty;
	{
		if (strcmp(P->Next->Id, Id) == 0)
		{
			TmpCell = P->Next;
			P->Next = P->Next->Next;
			free(TmpCell);
			return 0; //If successfully deleted ,return 0;
		}
		P = P->Next;
	}
	return -1; //If doesn't delete ,return -1;
}

//Search a student's information by name;
int Search(List L, char Name[])
{
	Position P = L->Next;
	while (P)
	{
		if (strcmp(P->Name, Name) == 0)
		{
			cout << P->Id << " " << P->Name << " " << P->Tel << endl;
			return 0; //If successfully found,return 0;
		}
		P = P->Next;
	}
	return -1; //If doesn't find,return -1;
}

//Print all the datas on the list;
void PrintAll(List L)
{
	Position P;
	for (P = L->Next; P; P = P->Next)
		cout << P->Id << " " << P->Name << " " << P->Tel << endl;
}

//Delete the whole list;
void DeleteAll(List L)
{
	Position P, TmpCell;
	for (P = L; P->Next; ) //Delete each node;
	{
		TmpCell = P->Next;
		P->Next = P->Next->Next;
		free(TmpCell);
	}
	free(L); //Delete the headof the list;
}

//Keep the datas into the file;
int Close(List L)
{
	string s1, s2, s3;
	ofstream file("D:\\a.txt");
	Position P;
	for (P = L->Next; P; P = P->Next) //Read each student's information into file;
	{
		file << P->Id << " " << P->Name << " " << P->Tel << endl;
	}
	file.close();
	rename("D:\\a.txt", "D:\\b.txt");
	return 0;
}

//The interface of the program;
void Menu()
{
	system("cls"); //Clean the interface;
	cout << "1.添加記錄" << endl;
	cout << "2.刪除記錄" << endl;
	cout << "3.輸出記錄" << endl;
	cout << "4.按姓名查找" << endl;
	cout << "5.保存記錄" << endl;
	cout << "6.清空記錄" << endl;
	cout << "7.退出" << endl;
}

//The main founction;
int main()
{
	List L = (PtrToNode)malloc(sizeof(Node)); //Creat a storage for head of the list;
	L->Next = NULL; //Initialization of L;
	Open(L); //Read datas into the list;

	char Id[12], Name[8], Tel[12];
    Menu(); //Show the interface;
	while (1)
	{
		int a; //A variable to store the choose of users;
		int result; //A variable to store results of some founctions
		cin >> a; 
		switch (a)
		{
		case 1:cout << "請依次輸入學號、姓名、電話號碼:"; //Add a information into the list;
			cin >> Id >> Name >> Tel;
			Add(L, Id, Name, Tel);
			break;
		case 2:cout << "請輸入代刪除學生學號:"; //Delete a student's information;
			cin >> Id;
			result = Delete(L, Id);
			if (result == 0)
				cout << "刪除成功!" << endl;
			else
				cout << "刪除失敗!" << endl;
			break;
		case 3:PrintAll(L); //Print the whole information;
			break;
		case 4:cout << "請輸入代查詢學生姓名:"; //Search a student's information by name ,
			                                     //print all his or her information
			cin >> Name;
			result = Search(L, Name);
			if (result == -1)
				cout << "沒有找到該學生!" << endl;
			break;
		case 5:Close(L); //Read all informations into the file;
			break;
		case 6:DeleteAll(L); //Delete the whole list;
			break;
		case 7:Menu(); //return back,show the interface;
			   break;
		}
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章