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

📄 notebook.cpp

📁 简单的通讯录管理系统
💻 CPP
字号:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#define MAX 100
int nm, mm;
struct NoteBook                                                  /*定义*/
{
	char name[25];
	char group[20];
	char birthday[10];
	char e_mail[20];
	char tel_mobile[15];
	char tel_house[15];
	char tel_company[15];
	char remark[25];
};
struct NoteBook str[MAX];

void main()
{
	FILE *fp;
	int a;
	cout << "****************************************" << endl;
	cout << setw(30) << "欢迎使用通讯录管理系统" << endl; 
	cout << "****************************************" << endl;
	cout << "该系统完成的功能如下:" << endl;
con:;	                                                     /*con表示继续操作*/

	cout << "0.退出 1.创建 2.精确查找 3.模糊查找 4.删除 5.添加 6.显示 7.修改" << endl;
	cout << "请选择你要实现的功能" << endl;
    cin >> a;
	
	switch(a)
	{
	case 1:
		{                                        /*创建*/
			int i;

			if((fp=fopen("file.txt", "wb")) == NULL)
			/*"w" means opens an empty file for writing. 
			  If the given file exists, its contents are destroyed.*/
			{
				cout << "cannot open this file, because the file is not exist" << endl;
				exit(0);
			}
			
			cout << "输入创建数据当姓名为#创建结束" << endl;
			cout << "输入格式为:姓名 分组 生日 E_mail 手机号 家庭电话 公司电话 备注信息" << endl;
			for(i=0; i<MAX; i++)
			{
				cout << "第" << i+1 << "个:" << endl;
				cin >> str[i].name;
				if(strcmp(str[i].name, "#") == 0)
				{
					break;
				}
				else
				{
					cin	>> str[i].group >> str[i].birthday >> str[i].e_mail
						>> str[i].tel_mobile >> str[i].tel_house >> str[i].tel_company 
						>> str[i].remark;				
					fwrite(&str[i], sizeof(struct NoteBook), 1, fp);
				}
			}
            cout << "创建完毕" << endl;
			fclose(fp);
			goto con;
			break;
			
		}
/////////////////////////////////////////////////////////////////////

	case 2:                                                   /*精确查找*/
        {
			char name1[25];
			int i;			
			if((fp=fopen("file.txt", "rb"))==NULL)
			/*"r" means opens for reading. If the file does not exist or cannot be found, 
			  the fopen call fails.*/	
			{
				cout << "cannot open this file, because the file is not exist" << endl;
				exit(0);
			}
			cout << "输入要查找的姓名" << endl;                       /*用strcmp匹配查找人*/
			cin >> name1;
			for(i=0; fread(&str[i],sizeof(struct NoteBook),1,fp); i++)
			{			
				if(strcmp(str[i].name, name1) == 0)
				{
					cout << "********************************************************************************" << endl;
					cout << "此人在第" << i+1 << endl;
					cout << "数据为:" << str[i].name << setw(10) << str[i].group << setw(10) 
					<< str[i].birthday << setw(10) << str[i].e_mail << setw(10) 
					<< str[i].tel_mobile << setw(10) << str[i].tel_house << setw(10)  
					<< str[i].tel_company << setw(10) << str[i].remark << endl;	
					cout << "********************************************************************************" << endl;
					goto end;
				}
				else
				{	
					cout << "查无此人" << endl;
				}
			}
end:
			cout << endl;
			fclose(fp);
			goto con;
			break;
		}
/////////////////////////////////////////////////////////////////////

	case 3:													/*模糊查找*/
		{		
			char name1[25];
			int i;			
			if((fp=fopen("file.txt", "rb"))==NULL)
			/*"r" means opens for reading. If the file does not exist or cannot be found, 
			  the fopen call fails.*/	
			{
				cout << "cannot open this file, because the file is not exist" << endl;
				exit(0);
			}
			cout << "输入要查找的姓名" << endl;                       /*用strcmp匹配查找人*/
			cin >> name1;
			for(i=0; fread(&str[i],sizeof(struct NoteBook),1,fp); i++)
			{	
				char *p = str[i].name;
				char *p1;
				p1 = name1;
				if(*p == *p1)
				{
					cout << "********************************************************************************" << endl;
					cout << "此人在第" << i+1 << endl;
					cout << "数据为:" << str[i].name << setw(10) << str[i].group << setw(10) 
						<< str[i].birthday << setw(10) << str[i].e_mail << setw(10) 
						<< str[i].tel_mobile << setw(10) << str[i].tel_house << setw(10)  
						<< str[i].tel_company << setw(10) << str[i].remark << endl;	
					cout << "********************************************************************************" << endl;
				}
			}
			cout << endl;
			fclose(fp);
			goto con;
			break;
		}
/////////////////////////////////////////////////////////////////////
		
	case 4:                                                    /*删除*/
		{
			int i,n,j;
			char name2[25];
			if((fp=fopen("file.txt", "rb"))==NULL)
			{
				cout << "Cannot open this file, because the file is not exist" << endl;
				exit(0);
			}
			for(n=0; fread(&str[n],sizeof(struct NoteBook),1,fp); n++) 
			{
				cout << "********************************************************************************" << endl;
				cout << "第" << n+1 << "个:" << str[i].name 
					<< setw(10) << str[i].group << setw(10) << str[n].birthday << setw(10) 
					<< str[n].e_mail << setw(10) << str[n].tel_mobile << setw(10) 
					<< str[n].tel_house << setw(10) << str[n].tel_company << setw(10) 
					<< str[n].remark << endl;
				cout << "********************************************************************************" << endl;
			}                    
			                              /*循环后n为文件中记录数*/
			cout << "输入要删除的姓名" << endl;       
			cin >> name2;
			for(i=0; (strcmp(str[i].name,name2)!=0 && i<n); i++)
			{
			};     /*注意;号*/
				
				if(i >= n)         /*查找需要删除的人(固定i的值)*/
				{
					cout << "查无此人" << endl;
					cout << endl;
					fclose(fp);
					goto con;
				}
				
			fclose(fp);
			fp=fopen("file.txt", "wb+");        /*以只写方式打开(文件原内容丢失)*/
				                                          
			if(n == 1)
			{
				fclose(fp);
				cout << "删除完毕" << endl;            /*只有一个数据的情况*/
				//goto con;
			}

			else
			{				
				for(j=0; j<i; j++)
				{
					fwrite(&str[j], sizeof(struct NoteBook), 1, fp); /*把除删除以外的数据写入*/
					for(j=i+1; j<n; j++)
					{
						fwrite(&str[j], sizeof(struct NoteBook), 1, fp);
					}
					
				}
				cout << "删除完毕" << endl;	
			}   

			cout << endl;
			fclose(fp);
			goto con;
			break;
				
		}
/////////////////////////////////////////////////////////////////////

	case 5:
		{                                                     /*添加*/
			struct NoteBook str1;
			if((fp=fopen("file.txt","ab+")) == NULL)              /*创建一个单独的结构体*/
			/*"a" means opens for writing at the end of the file (appending) without removing 
			  the EOF marker before writing new data to the file;
			  creates the file first if it doesn’t exist.*/
			{
				cout << "Cannot open this file, because the file is not exist" << endl;            /*追加方式打开*/
				exit(0);
			}
			
			cout << "输入要添加的数据" << endl;
			cout << "输入格式为:姓名 分组 生日 E_mail 手机号 家庭电话 公司电话 备注信息" << endl;
			
			cin >> str1.name >> str1.group >> str1.birthday >> str1.e_mail
					>> str1.tel_mobile >> str1.tel_house >> str1.tel_company 
					>> str1.remark; 
			fwrite(&str1, sizeof(struct NoteBook), 1, fp);
			cout << "添加成功" << endl;
			cout << endl;
			fclose(fp);
			goto con;
			break;
		}
/////////////////////////////////////////////////////////////////////

	case 6:                                                   /*显示*/
		{
			int i;
			if((fp=fopen("file.txt", "r")) == NULL)
			{
				cout << "Cannot open this file, because the file is not exist" << endl;
				exit(0);
			}
			for(i=0; fread(&str[i],sizeof(struct NoteBook),1,fp); i++)
			{
				cout << "********************************************************************************" << endl;
				cout << "第" << i+1 << "个:" 
					<< str[i].name << setw(10) << str[i].group << setw(10) 
					<< str[i].birthday << setw(10) << str[i].e_mail << setw(10)  
					<< str[i].tel_mobile << setw(10) << str[i].tel_house << setw(10) 
					<< str[i].tel_company << setw(10) << str[i].remark << endl;
				cout << "********************************************************************************" << endl;
			}
			cout << endl;
			fclose(fp);			
			goto con;
			break;
		}
/////////////////////////////////////////////////////////////////////

	case 7:													/*修改*/
		{
			FILE *fp;
			int i;
			char name2[25];
			if((fp=fopen("file.txt","rb+")) == NULL)
			{
				cout << "Cannot open this file, because the file is not exist";
				exit(0);
			}
			cout << "请输入要修改的人的名字";
			cin >> name2;
			for(i=0; fread(&str[i],sizeof(struct NoteBook),1,fp); i++)
			{			
				if(strcmp(str[i].name, name2) == 0)
				{
					cout << "********************************************************************************" << endl;
					cout << "要修改的人在第" << i+1 << endl;
					cout << "数据为:" << str[i].name << setw(10) << str[i].group << setw(10) 
					<< str[i].birthday << setw(10) << str[i].e_mail << setw(10) 
					<< str[i].tel_mobile << setw(10) << str[i].tel_house << setw(10)  
					<< str[i].tel_company << setw(10) << str[i].remark << endl;	
					cout << "********************************************************************************" << endl;
					fopen("file.txt", "rb+");
					fseek(fp, i*sizeof(NoteBook), SEEK_SET);
					cout<<"请输入姓名:";
					cin>>str[i].name;
					cout<<"请输入分组:";
					cin>>str[i].group;
					cout<<"请输入生日:";
					cin>>str[i].birthday;
					cout<<"请输入E_mail:";
					cin>>str[i].e_mail;
					cout<<" 请输入手机号:";
					cin>>str[i].tel_mobile;
					cout<<" 请输入家庭电话:";
					cin	>>str[i].tel_house;
					cout<<" 请输入公司电话:";
					cin	>>str[i].tel_company;
					cout<<"请输入备注信息:";
					cin>>str[i].remark;
					fwrite(&str[i], sizeof(NoteBook), 1, fp);
					cout << "修改成功" << endl;
					goto end;
				}
				else
				{	
					cout << "查无此人" << endl;
				}
			}
			cout << endl;
			fclose(fp);
			goto con;
			break;
		}
/////////////////////////////////////////////////////////////////////
		
	case 0:                                                   /*退出*/
		{
			cout << "****************************************" << endl;
			cout << setw(30) << "谢谢使用" << endl;
			cout << "****************************************" << endl;
			exit(0);
		}
/////////////////////////////////////////////////////////////////////

	default:												/*出错处理*/
		cout << "输入的操作有误" << endl;
		cout << endl;
		fclose(fp);
		goto con;
		break;
	}
}

⌨️ 快捷键说明

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