小练习3与4,为代码2.21添加查询功能
2012-10-10 23:11
429 查看
在support.h中声明 bool query()
在function.cpp中的函数void Usage()添加
在support.cpp的函数bool DispatchCommand(const char *strCommandHead, const char *strCommandParameter) 添加
在funtion.cpp当中实现
小练习4,在3的基础之上,在Function.cpp当中添加:
在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;
相关文章推荐
- 用代码操作mysql数据库(添加和查询数据功能)
- AngularJS模糊查询功能实现代码(过滤内容下拉菜单排序过滤敏感字符验证判断后添加表格信息)
- JSP简单添加,查询功能代码
- BOS项目练习(定区添加及查询,Hessian入门,基于hessian实现定区关联客户功能)
- 框架 day51 BOS项目练习(定区添加及查询,Hessian入门,基于hessian实现定区关联客户功能)
- Angularjs添加排序查询功能的实例代码
- JSP简单添加,查询功能代码
- java Web项目实践之7 添加功能、精确查询功能的设计及实现
- 创建ASP.NET Core MVC应用程序(5)-添加查询功能 & 新字段
- HOOK大法实现不修改程序代码给程序添加功能
- SharpPad文本编辑器: 已完成添加代码自动完成、代码折叠等功能
- android studio添加代码(类)注释功能
- GridView 实现自定义分页、排序、查询、添加、编辑、多选删除 之代码规范
- ofbiz的form查询功能关键代码
- 纯代码实现WordPress评论回复自动添加@评论者的功能
- 使用easyUI添加查询功能在datagrid
- 手写链表(二)-使用内部类实现添加、查询、定位、插入等功能
- django框架中,添加用户操作记录和查询功能
- .net连接数据库,实现增加,修改,删除,查询的四大功能完整代码。
- JqueryMobile为Listview动态添加、删除查询功能