⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 我大一时候做的C++课程设计
💻 CPP
字号:
#include<iostream.h>
#include<string.h>
#include<iomanip.h>
#include <stdlib.h>
#include<stdlib.h>

char name1[20];					//全局变量,各个函数都可以实现对它们的修改
char phone1[15];				//由于类的公有成员函数在类外部实现,没办法用函数返回字符串地址,故如此
char tel1[15];
char sex1;

int counter=0;

class list						//通讯录类的声明
{
private:
	char name[20];
	int age;
	char sex;
	char tel[15];
	char phone[15];
	bool flag;
public:
	list();
	~list();
	void add(char *n, int a, char s, char *t, char *p);
	void find_n(char *n);
	int find_a();
	void find_t(char *t);
	void find_p(char *p);
	char find_s();
	void alter(char *n, int a, char s, char *t, char *p);
	void print();
	bool judge();
	void set_flag(bool f);
};

//类的公有成员函数的实现
list::list()					//构造函数
{
	strcpy(name,"Null");
	age=0;
	strcpy(phone,"Null");
	sex='N';
	strcpy(tel,"Null");
	flag=false;
}
list::~list() {}				//析构函数

void list::add(char *n, int a, char s, char *t, char *p)		//增加信息
{
	strcpy(name,n);
	age=a;
	strcpy(phone,p);
	sex=s;
	strcpy(tel,t);	
}

void list::find_n(char *n)	{strcpy(n,name);}		//查找姓名,将私有成员赋值给全局变量
void list::find_t(char *t)	{strcpy(t,tel);}		//查找电话
void list::find_p(char *p)	{strcpy(p,phone);}		//查找手机
char list::find_s()	{return sex;}					//查找性别,直接将私有成员sex返回
int list::find_a()	{return age;}					//查找年龄,直接返回age

void list::alter(char *n, int a, char s, char *t, char *p)
{
	strcpy(name,n);
	age=a;
	strcpy(phone,p);
	sex=s;
	strcpy(tel,t);	
}

void list::print()
{
	cout << setw(5)<< name <<  setw(10)<< sex <<  setw(10)
		<< age <<  setw(10) << tel << setw(10)<< phone << endl;
}

bool list::judge()	{return flag;}
void list::set_flag(bool f)		{flag=f;}

void main()
{
	int i, j;
	char select;
	char n[20], t[15], p[15];
	char s;
	int a;
	strcpy(n,"Null");
	a=0;
	strcpy(p,"Null");
	s='N';
	strcpy(t,"Null");

	list person[30];
	person[1].add("wang",20,'m',"4711", "7408");
	person[1].set_flag(true);
	person[2].set_flag(true);
	person[2].add("li",20,'f',"8011", "5570");


	for(int temp;;temp++)
	{
		system("pause");
		system("cls");
		cout << endl;
		cout << "************************功能菜单**********************" << endl;
		cout << "Add(a)  Find(f)  Alter(r)  Delete(d)  List(l)  Quit(t)" << endl;
		cin >> select ;
		if(select=='q' || select=='Q')
		{
			cout <<"感谢使用本通讯录\nCOPYRight@Wang"<< endl;
			break;
		}
		else if(select=='a' || select=='A')
		{
			cout << "添加数据:" << endl;
			cout <<"输入姓名、性别(M or F)、电话、手机、年龄:" << endl;
			cin >> n >> s >> t >> p >> a;
			for(i=1; i<=30; i++)
			{
				bool tf=true;
				if(person[i].judge()==false)
				{
					person[i].set_flag(tf);
					person[i].add(n, a, s, t, p);
					cout << "设置完成!" << endl;
					break;
				}					
			}	
		}

		else if(select=='r' || select=='R')
		{
			cout << "修改数据:" << endl;
			int tr;
			cout << "输入编号:" << endl;
			for(i=0;;i++)
			{
				cin >>tr;
				if(tr<0 || tr>30)
					cout << "输入有误,请确认范围后重新输入!" << endl;
				else break;
			}	
			cout <<"输入新的姓名、性别(M or F)、电话、手机、年龄:" << endl;
			cin >> n >> s >> t >> p >> a;
			person[tr].add(n, a, s, t, p);
			cout << "设置完成!" << endl;					
		}

		else if(select=='d' || select=='D')
		{
			cout << "Delete" << endl;
			int tr;
			cout << "Input No" << endl;
			for(i=0;;i++)
			{
				cin >>tr;
				if(tr<0 || tr>30)
					cout << "输入范围有误,请确认您输入的数在0-29之间,请重新输入!" << endl;
				else break;
			}	
			person[tr].set_flag(false);
			cout << "设置完成!" << endl;					
		}

		else if(select=='f' || select=='F')
		{
			cout << "Find" << endl;		
			char t_sec;
			bool temp_f=false;
			
			for(int tt=1;;tt++)
			{
				temp_f=false;
				cout << "N(ame)	A(ge)	P(hone)	T(el) S(ex)	Q(uit)" << endl;
				cin >> t_sec;
				if(t_sec=='N' || t_sec=='n')
				{	
					char t2[20];
					cout << "输入姓名:" << endl;
					cin >> t2;
					cout << setw(4)<<"编号" << setw(10)<<"姓名"
						<<setw(10)<<"性别"<< setw(10)<<"年龄"<<setw(9)<<"电话"<<setw(10)<<"手机"<< endl;
					for(int t1=0; t1<30; t1++)
					{
						person[t1].find_n(name1);
						if(strcmp(name1,t2)==0)
						{
							cout << "No:" << t1 <<"\t";	
							person[t1].print();
							temp_f=true;
						}						
					}
					if(temp_f==false)
						cout << "未找到!" << endl;
				}
				if(t_sec=='A' || t_sec=='a')
				{
					temp_f=false;
					int t2;
					cout << "输入年龄:" << endl;
					cin >> t2;
					cout << setw(4)<<"编号" << setw(10)<<"姓名"
						<<setw(10)<<"性别"<< setw(10)<<"年龄"<<setw(9)<<"电话"<<setw(10)<<"手机"<< endl;
					for(int t1=0; t1<30; t1++)
					{
						if(person[t1].find_a()==t2)
						{
							cout << "No:" << t1 <<"\t";	
							person[t1].print();
							temp_f=true;
						}						
					}
					if(temp_f==false)
						cout << "未找到!" << endl;
				}
				if(t_sec=='P' || t_sec=='p')
				{
					temp_f=false;
					char t2[15];
					cout << "输入手机:" << endl;
					cin >> t2;
					cout << setw(4)<<"编号" << setw(10)<<"姓名"
						<<setw(10)<<"性别"<< setw(10)<<"年龄"<<setw(9)<<"电话"<<setw(10)<<"手机"<< endl;
					for(int t1=0; t1<30; t1++)
					{
						person[t1].find_p(phone1);
						if(strcmp(phone1,t2)==0)
						{
							cout << "No:" << t1 <<"\t";
							person[t1].print();
							temp_f=true;
						}						
					}
					if(temp_f==false)
						cout << "未找到!" << endl;
				}	
				if(t_sec=='T' || t_sec=='t')
				{
					temp_f=false;
					char t2[15];
					cout << "输入电话:" << endl;
					cin >> t2;
					cout << setw(4)<<"编号" << setw(10)<<"姓名"
						<<setw(10)<<"性别"<< setw(10)<<"年龄"<<setw(9)<<"电话"<<setw(10)<<"手机"<< endl;
					for(int t1=0; t1<30; t1++)
					{
						person[t1].find_t(tel1);
						if(strcmp(tel1,t2)==0)
						{
							cout << "No:" << t1 <<"\t";	
							person[t1].print();
							temp_f=true;
						}
						
					}
					if(temp_f==false)
						cout << "未找到!" << endl;
				}	
				if(t_sec=='S' || t_sec=='s')
				{
					temp_f=false;
					char t2;
					cout << "输入性别(M or F):" << endl;
					cin >> t2;
					cout << setw(4)<<"编号" << setw(10)<<"姓名"
						<<setw(10)<<"性别"<< setw(10)<<"年龄"<<setw(9)<<"电话"<<setw(10)<<"手机"<< endl;
					for(int t1=0; t1<30; t1++)
					{
						if(person[t1].find_s()==t2 || person[t1].find_s()==(t2-'A'+'a') ||person[t1].find_s()==(t2-'a'+'A'))
						{
							cout << "No:" << t1 <<"\t";	
							person[t1].print();
							temp_f=true;
						}
						
					}
					if(temp_f==false)
						cout << "未找到!" << endl;	
				}
				if(t_sec=='Q' || t_sec=='q')
				{
					//cout <<"感谢使用本通讯录\nCOPYRight@Wang"<< endl;
					break;
				}
			}
		}
		else if(select=='l' || select=='L')
		{
			bool f1=false;
			system("pause");
			system("cls");
			cout << setw(4)<<"编号" << setw(10)<<"姓名"
				<<setw(10)<<"性别"<< setw(10)<<"年龄"<<setw(9)<<"电话"<<setw(10)<<"手机"<< endl;
			for(int t1=0; t1<30; t1++)
				if(person[t1].judge()==true)
				{
					f1=true;
					
					cout << "No:" << t1 << "\t" ;
					person[t1].print();
				}
			if(f1==false)
				cout << "Empty" << endl;					
		}
		else
			cout << "输入错误,请重新输入:" << endl;			
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -