VC++ 完整的例子(电话本管理)

文件一:Person.h


//Person.h

#pragma once
#include <string>

using std::string;

class Person{
public:
	Person(const string& first="",const string& second="")
		:firstname(first),secondname(second){
	}
	Person(string&& first, string&& second):
		firstname(std::move(first)),secondname(std::move(second)){
	}

	//less-than operator
	bool operator<(const Person& p)const{
		return (secondname<p.secondname ||
			((secondname == p.secondname) && (firstname <p.firstname)));
	}

	string getName() const{
		return firstname + "" + secondname;
	}

private :
	string firstname;
	string secondname;
};

主程序文件.cpp 


// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <tuple>
#include <iomanip>
#include <array>
#include <map>

#include "Person.h"

using std::cin;
using std::cout;
using std::endl;
using std::tuple;
using std::string;
using std::setw;
using std::get;
using std::pair;
using std::map;

typedef std::map<Person,string> PhoneBook;
typedef std::pair<Person,string> Entry;

Person getPerson(){
	string first,second;
	cout<<"Enter a first name:";
	getline(cin,first);
	cout<<"Enter a second name:";
	getline(cin,second);
	return Person(first,second);
}

Entry inputEntry(){
	Person person = getPerson();
	string number;

	cout<<"输入"<<person.getName()<<"的电话号码:";
	getline(cin,number);

	return std::make_pair(std::move(person),std::move(number));
}

void addEntry(PhoneBook& book){
	Entry entry = inputEntry();
	auto pr = book.insert(std::move(entry));
	if(pr.second)
	{
		cout<<"Entry succesful." << endl;
	}
	else
	{
		cout<<"Entry exists for" << pr.first->first.getName()
			<<". The number is "<< pr.first->second <<endl;
	}

}

void listEntries(const PhoneBook& book){
	if(book.empty()){
		cout<<"The phone book is empty."<<endl;
		return;
	}
	cout<< std::setiosflags(std::ios::left);  
	for(auto& entry:book)
	{
		cout<< setw(30) <<entry.first.getName()
			<<setw(12) <<entry.second<<endl;
	}
	cout<<std::resetiosflags(std::ios::right);
}

void getEntry(const PhoneBook& book){
	Person person = getPerson();
	auto iter = book.find(person);

	if(iter == book.end())
		cout<<"No entry found for " <<person.getName()<<endl;
	else
		cout<<"The number for " <<person.getName() << "is " <<iter->second<<endl;
}

void deleteEntry(PhoneBook& book){
	Person person = getPerson();
	auto iter = book.find(person);
	if(iter == book.end())
	{
		cout<< " no entry found for " <<person.getName()<<endl;
	}
	else
	{
		book.erase(iter);
		cout<<person.getName() << " erased." <<endl;
	}
}

int main(int argc,_TCHAR* argv[])
{
	PhoneBook phoneBook;
	char answer=0;
	while (true)
	{
		cout << "Do youwant to enter a phone book entry(Y or N):";
		cin>>answer;
		cin.ignore(); //ignore new line in buffer;
		if(toupper(answer)  == 'N')
			break;
		if(toupper(answer) != 'Y')
		{
			cout<< "Invalid response ,Try again." <<endl;
			continue;
		}
		addEntry(phoneBook);
	}

	//Query the phonebook
	while (true)
	{
		cout<<endl<<"make a choice:"<<endl
			<<"A  Add an entry "<<endl
			<<"D  Delete an entry"<<endl
			<<"G  Get an entry"<<endl
			<<"L  List entries"<<endl
			<<"Q  Quit"<<endl;

		cin >>answer;
		cin.ignore();

		switch (toupper(answer))
		{
		case 'A':
			addEntry(phoneBook);
			break;
		case 'G':
			getEntry(phoneBook);
			break;
		case 'D':
			deleteEntry(phoneBook);
			break;
		case 'L':
			listEntries(phoneBook);
			break;
		case 'Q':
			return 0;
		default:
			cout<<"别乱选,重来"<<endl;
			break;
		}
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值