您的位置:首页 > 编程语言

小练习3与4,为代码2.21添加查询功能

2012-10-10 23:11 429 查看
在support.h中声明 bool query()

在function.cpp中的函数void Usage()添加

printf("shearch info :-sh\n");


在support.cpp的函数bool DispatchCommand(const char *strCommandHead, const char *strCommandParameter) 添加

if(strcmp(strCommandHead,"-sh")==0)
	{
		printf("search phonebook....");
		printf("please enter name or phone number,both, or one of them, replace the one don't know with ?\n");
		query();
	}


在funtion.cpp当中实现

bool query( )
{
	cout<<"进入函数体"<<endl;
	char buffer[100]="";
	while(1)
	{
		if(scanf("%[^\n]", buffer) == 0)
		{
			fflush(stdin);
			continue;
		}

		break;
	}
	fflush(stdin);

	int position=0;
	while(position<strlen(buffer))
	{
		if(buffer[position]=='-')break;
		position++;
	}
	char name[20]="";
	char phone[20]="";

	char fname[20]="";
	char fphone[20]="";
	for(int i=0;i<position;i++)name[i]=buffer[i];
	for(int i=0;buffer[i+position+1]!='\0';i++)phone[i]=buffer[i+position+1];
	cout<<"name"<<name<<'\t'<<"phone"<<phone<<endl;
	fstream f(DefaultFileFullPath,ios::in);
	if(strcmp(name,"?"))
	{
		while(!f.eof())
		{
			f>>fname>>fphone;
			if(strcmp(phone,fphone)){cout<<fname<<'\t'<<fphone<<endl;break;}
		}
		cout<<"no one"<<endl;
	}
	else
	{
		while(!f.eof())
		{
			
			f>>name>>phone;
			if(strcmp(name,fname)){cout<<fname<<'\t'<<fphone<<endl;break;}
		}
		cout<<"no one"<<endl;
	}
	return true;
}


小练习4,在3的基础之上,在Function.cpp当中添加:

int bufferFileIntoMemery(char *DefaultFileFullPath,char ***array, int howMany=30)
{	//向缓存空间中读入记录,并返回记录的条数

	//初始化缓存
	for(int i=0;i<30;i++)
		for(int j=0;j<2;j++)
			for(int k=0;k<20;k++)array[i][j][k]=' ';

	fstream f(DefaultFileFullPath,ios::in);
	int n=0;
	if(f==NULL)return 0;
	else
	{
		while(  (n<howMany)  &&! (f.eof())  )
		{
			//这里假设文件中的记录是先名字后电话号码,中间用空格或者TAB键隔开
			f>>array
[0]>>array
[1];
			n++;
		}
		//返回在内存当中的记录
		return 0;
	}
	return n;
}
bool query( )
{
	char buffer[100]="";//得到用户输入的名字和电话号码,或者两者之一
	while(1)
	{
		if(scanf("%[^\n]", buffer) == 0)
		{
			fflush(stdin);
			continue;
		}

		break;
	}
	fflush(stdin);

	int position=0;
        //找到名字与电话分隔符在buffer中的位置
        while(position<strlen(buffer))
	{
		if(buffer[position]=='-')break;
		position++;
	}
    //用户输入的名字和电话 
	char name[20]="";
	char phone[20]="";
	for(int i=0;i<position;i++)name[i]=buffer[i];
	for(int i=0;buffer[i+position+1]!='\0';i++)phone[i]=buffer[i+position+1];

	//创建缓存30条记录,每条记录包括20个字符的name和20个字符的phone
	char ***array;
	array=new char**[30];
	for(int i=0;i<30;i++)
	{
         array[i]=new char*[2];
		 for(int j=0;j<2;j++)
		 {
			 array[i][j]=new char[20];
		 }
	}

	int howManyLoop=bufferFileIntoMemery(DefaultFileFullPath,array,30);
	int n=0;
	bool flag=false;
	while((howManyLoop!=0)&&(flag!=true))
	{
		if(strcmp(name,"?"))
		{
		
			while(n<howManyLoop)
			{
				if(strcmp(phone,array
[0]))
				{
					cout<<"name:"<<array
[0]<<'\t'<<"phone:"<<array
[1]<<endl;
					flag=true;
					break;
				}
				n++;
			}
		}
		else
		{
			while(n<howManyLoop)
			{
				if(strcmp(phone,array
[1]))
				{
					cout<<"name:"<<array
[0]<<'\t'<<"phone:"<<array
[1]<<endl;
					flag=true;
					break;
				}
				n++;
			}
		}
	howManyLoop=bufferFileIntoMemery(DefaultFileFullPath,array,30);
	}
	return true;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: