课程设计

题目2.李刚是一爱折腾的人,当然爱折腾的人均有梦想,他想当中国的盖次呢。可不,现在个人好友信息多了,复杂了,他想制作一个个人通讯录的制作管理软件。 刚好这个学期学了数据结构课,所以他准备使用数据结构知识来实现了。并考虑使用双向链表作数据结构。并制定了初步要求:

1每个好友信息包含姓名、性别、住址、邮编、几岁、电话、QQ、微信帐号、生日等。

2作为一个完整的系统,应具有友好的界面和较强的容错能力。


代码:

(Card.h):

<pre name="code" class="cpp">//名片类:姓名name,性别sex,住址loca,邮编mail,年龄age,电话tel,QQ,微信wechat,生日birth。
#include<iostream>
#include<string>
#include <fstream>
using namespace std;

class Card
{
private:
	string name;
	string sex;
	string loca;
	string mail;
	string age;
	string tel;
	string QQ;
	string wechat;
	long birth;
public:
	Card(){}
	Card(Card &p);//复制构造函数
	void setCard();

	bool checkName(string n){return name.compare(n)==0;}

	void printCard();

	void readFile(ifstream &read);
	void writeFile(ofstream &write);
};
Card::Card(Card &p)
{
	name=p.name;
	sex=p.sex;
	loca=p.loca;
	mail=p.mail;
	age=p.age;
	tel=p.tel;
	QQ=p.QQ;
	wechat=p.wechat;
	birth=p.birth;
}
void Card::setCard()
{
	cout<<"请输入姓名:";cin>>name;
	cout<<"请输入性别:";cin>>sex;
	cout<<"请输入住址:";cin>>loca;
	cout<<"请输入邮编:";cin>>mail;
	cout<<"请输入年龄:";cin>>age;
	cout<<"请输入电话:";cin>>tel;
	cout<<"请输入QQ号码:";cin>>QQ;
	cout<<"请输入微信号:";cin>>wechat;
	cout<<"请输入生日(如20140102):";cin>>birth;
}
void Card::printCard()
{
	cout<<name<<'\t'<<sex<<'\t'<<loca<<'\t'<<mail<<'\t'<<age<<'\t'<<tel<<'\t'<<QQ<<'\t'<<wechat<<'\t'<<birth<<'\n';
}
void Card::readFile(ifstream &read)
{
	read>>name>>sex>>loca>>mail>>age>>tel>>QQ>>wechat>>birth;
}
void Card::writeFile(ofstream &write)
{
	write<<name<<'\n'<<sex<<'\n'<<loca<<'\n'<<mail<<'\n'<<age<<'\n'<<tel<<'\n'<<QQ<<'\n'<<wechat<<'\n'<<birth<<'\n';
}


 (DoublelyLinkedList.h): 

<pre name="code" class="cpp">#include<iostream>
using namespace std;

template <class DataType>
struct DulNode
{
	DataType data;
	DulNode<DataType> *prior,*next;
};
template <class DataType>
class DoublelyLinkedList
{
public:
	DoublelyLinkedList();
//	DoublelyLinkedList(DataType a[],int n);
	~DoublelyLinkedList();
	int Lenth();
	DataType Get(int i);
//	int Location(DataType x);
	void Insert(DataType x,int i);
	void Delete(int i);
//	void Print();
private:
	DulNode<DataType> *first;
};
template <class DataType>
DoublelyLinkedList<DataType>::DoublelyLinkedList()
{
	//建立头结点
	first=new DulNode<DataType>;
	first->next=NULL;

}
template <class DataType>
DoublelyLinkedList<DataType>::~DoublelyLinkedList()
{
	DulNode<DataType> *p;
	while(first!=NULL)
	{
		p=first;
		first=first->next;
		delete p;
	}
}
template <class DataType>
int DoublelyLinkedList<DataType>::Lenth()
{
	int count=0;
	DulNode<DataType> *p=first->next;

	while(p!=NULL)
	{	
		p=p->next;
		count++;
	}
	return count;
}
template <class DataType>
DataType DoublelyLinkedList<DataType>::Get(int i)
{
	DulNode<DataType> *p=first->next;
	int count=1;
	while(p!=NULL&&count<i)
	{
		p=p->next;
		count++;
	}
	if(p==NULL) throw "位置";
	else return p->data;
}
template <class DataType>
void DoublelyLinkedList<DataType>::Insert(DataType x,int i)
{
	DulNode<DataType> *p=first;

	int count=0;
	while(p!=NULL&&count<i-1)
	{
		p=p->next;
		count++;
	}

	if(p==NULL)	throw "位置";

	else
	{
		DulNode<DataType> *s=new DulNode<DataType>;
		s->data=x;

		s->prior=p;
		s->next=p->next;

		if(p->next!=NULL)
			p->next->prior=s;

		p->next=s;
	}
}
template <class DataType>
void DoublelyLinkedList<DataType>::Delete(int i)
{
	DulNode<DataType> *p=first;int count=0;
	while(p!=NULL&&count<i-1)
	{
		p=p->next;
		count++;
	}
	if(p==NULL||p->next==NULL) throw "位置";
	else
	{
		DulNode<DataType> *q=p->next;

		p->next=q->next;
		if(q->next!=NULL)
			q->next->prior=p;

		delete q;
	}
}


 (Contacts.cpp): 

<pre name="code" class="cpp">#include<iostream>
#include <fstream>
#include"Card.h"
#include"DoublelyLinkedList.h"
using namespace std;

void title()
{
	cout<<"姓名\t"<<"性别\t"<<"住址\t"<<"邮编\t"<<"年龄\t"<<"电话\t"<<"QQ\t"<<"微信\t"<<"生日\n";
}

void main()
{
	//建立一个名片链表。
	DoublelyLinkedList<Card> myContact;
	Card aCard;
       //建立文件
<span style="white-space:pre">	</span>ofstream file("联系人.txt",iostream::out);
<span style="white-space:pre">	</span>file.close();
	//读取文件
	ifstream read("联系人.txt",ios::out);

	if(!read.good()){cout<<"打开数据文件失败!\n";exit(0);}
	//string first;read>>first;//去掉第一行
	while(!read.eof())
	{
		aCard.readFile(read);
		myContact.Insert(aCard,myContact.Lenth()+1);
	}
	read.close();
	myContact.Delete(myContact.Lenth());//去掉最后一行

	//开始操作
	while(true)
	{
		cout<<"\n\n\n\t\t\t\t我的通讯录\n\n";
		cout<<"\t\t1。新建联系人\t\t2。删除联系人\n";
		cout<<"\t\t3。搜索联系人\t\t4。所有联系人\n\n\n";
		cout<<"输入序号选择操作(按0退出):\n";
		int k=0;cin>>k;
		if(k==1)
		{
			aCard.setCard();
			myContact.Insert(aCard,myContact.Lenth()+1);

			ofstream write("联系人.txt",ios::trunc);

			for(int k=1;k<=myContact.Lenth();k++)
				myContact.Get(k).writeFile(write);

			write.close();
		}
		if(k==2)
		{
			if(myContact.Lenth()==0)	{cout<<"通讯录中无数据!\n";system("pause");system("cls");continue;}

			cout<<"输入要删除的联系人姓名:";string name;cin>>name;
			for(int i=0;i<myContact.Lenth();i++)
			{
				if(myContact.Get(i+1).checkName(name))
				{
					title();
					myContact.Get(i+1).printCard();
					cout<<"删除?(Y or N)\n";char d;cin>>d;

					if(d=='y'||d=='Y')
					{
						myContact.Delete(i+1);

						ofstream write("联系人.txt",ios::trunc);
						for(int k=1;k<=myContact.Lenth();k++)
							myContact.Get(k).writeFile(write);
						write.close();

						cout<<"已删除!\n";
					}
				
					system("pause");
					continue;
				}
			}
			cout<<"没有查到联系人\""<<name<<"\"!\n";
		}
		if(k==3)
		{
			if(myContact.Lenth()==0)	{cout<<"通讯录中无数据!\n";system("pause");system("cls");continue;}

			cout<<"输入要搜索的联系人姓名:";string name;cin>>name;
			for(int i=0;i<myContact.Lenth();i++)
			{
				if(myContact.Get(i+1).checkName(name))
				{
					title();
					aCard.printCard();

					system("pause");
					continue;
				}
			}
			cout<<"没有查到联系人\""<<name<<"\"!\n";
		}
		if(k==4)
		{
			if(myContact.Lenth()==0)	{cout<<"通讯录中无数据!\n";system("pause");system("cls");continue;}
			
			title();

			for(int i=0;i<myContact.Lenth();i++)
				myContact.Get(i+1).printCard();

			system("pause");
			system("cls");
			continue;
		}
		if(k==0)
		{
			break;
		}
		system("pause");
		system("cls");
	}
	
}


 


运行结果:


初界面:



新建联系人:



搜索联系人:



所有联系人:



删除联系人:





学期个人总结------------懒。




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值