C++ tab键实现自动补全输入功能
一、简介
由于项目中写了个测试的控制台程序,是每次读取一行,即通过getline()来实现的,所以每次必须输入全路径名称,才能实现运行。
大家都觉得麻烦,就写了个tab键自动选择补全的。
目前基本可实现功能,但是没有回退的功能和跳出循环,有需用的就自己改吧。
此程序基于windows的控制台实现的。
二、实现过程
1. 代码自我感觉还算清楚。里面有部分注释。
#include<stdio.h>
#include <string>
#include <conio.h>
#include <map>
using namespace std;
#include<windows.h>
#include<stdlib.h>
#include<wincon.h> // tab键 获取相关值,并打印到屏幕上。
void tab_find_char(std::map<int,char*> store_compare_,char* store_input,int i_store_input_count,int& tab_count);
// 从字符中找到相关字符,并返回相关字符。
char* get_attach(const char* stacks_,const char* needle_); void console_start();
void console_end(); void write(const char* message,int length);
void write(const char* message);
void read(char* buffer, size_t size);
#include <iostream>
#include "conio.h"
// 记录屏幕光标位置
static COORD curser_position; void main()
{
console_start();
// 自定义的可搜索库字符集
static std::map<int,char*> store_compare;
store_compare[0] = "thread_parameter_";
store_compare[1] = "parameter_";
store_compare[2] = "build";
store_compare[3] = "compile"; int count=0;
char ch;
while (true)
{
int tab_count = 0;
char store_input[256];
int i_store_input_count = 0;
// 记录光标位置
HANDLE hOut;
CONSOLE_SCREEN_BUFFER_INFO bInfo;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hOut, &bInfo );
curser_position.X = bInfo.dwCursorPosition.X;
curser_position.Y = bInfo.dwCursorPosition.Y; do
{
ch =(char)_getch();
if (ch !='\n')
{
if (ch == 'q')
{
break;
}
else if (ch == 9)
{
tab_find_char(store_compare,store_input,i_store_input_count,tab_count);
tab_count++;
}
// 接受数字和全部字母和部分特殊符号。
else if( (ch >= 48 && ch <= 57) || (ch >= 65 && ch <= 122) )
{
char* temp_char = new char[1];
temp_char[0] = ch;
write(temp_char,1);
store_input[i_store_input_count] = ch;
i_store_input_count++;
}
}
}
while (ch!='\r');
write("\n");
}
console_end();
} void tab_find_char(std::map<int,char*> store_compare_,char* store_input,int i_store_input_count,int& tab_count)
{
if (i_store_input_count < 0 || tab_count < 0)
{
return;
} int map_size = store_compare_.size();
tab_count = tab_count % map_size; i_store_input_count += 1;
store_input[i_store_input_count-1] = '\0';
char* pri_temp_input = new char [i_store_input_count];
strncpy(pri_temp_input,store_input,i_store_input_count);
int i_catch_times = 0; HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE); for (int i = 0;i < map_size;i++)
{
char* pri_get_supply = get_attach(store_compare_[i],pri_temp_input);
if (NULL != pri_get_supply)
{
if (tab_count > i_catch_times)
{
i_catch_times++;
continue;
} //clear
SetConsoleCursorPosition(hOut,curser_position);
string out_put_string = " ";
write(out_put_string.c_str()); SetConsoleCursorPosition(hOut,curser_position);
write(store_compare_[i]);
break;
}
}
delete pri_temp_input;
pri_temp_input = NULL;
i_store_input_count -= 1;
return;
} char* get_attach(const char* stacks_,const char* needle_)
{
char* pri_string_stack = (char*)stacks_;
char* pri_string_needle = (char*)needle_;
char* pri_string = strstr(pri_string_stack,pri_string_needle);
return pri_string;
} /*---------------------------------------------------------*/ void console_start()
{
AllocConsole(); DWORD dwConsoleMode;
GetConsoleMode(NULL, &dwConsoleMode);
dwConsoleMode ^= ENABLE_LINE_INPUT;
dwConsoleMode ^= ENABLE_ECHO_INPUT;
//wConsoleMode ^= ENABLE_AUTO_POSITION; SetConsoleMode(NULL, dwConsoleMode);
const char* message = "welcome! test started. atuhor by zhang pengju\n";
write(message);
} void console_end()
{
FreeConsole();
} void write(const char* message,int length)
{
if (length <= 0)
{ return;
}
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), message, length, NULL, NULL);
} void write(const char* message)
{
int length = strlen(message);
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), message, length, NULL, NULL);
} void read(char* buffer, size_t size)
{
DWORD nNumberOfCharsToRead;
BOOL result = ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE), buffer, size, &nNumberOfCharsToRead, NULL);
buffer[nNumberOfCharsToRead - 2] = 0;
}
2.在vs2012下 测试结果是可以的。就不给贴图了。
运行程序,测试输入,t,然后切换tab键就可以看见有自动补齐的功能。
输入_,有不同的单词供你选择,然后回车,可换其他测试。
不足之处,请多指教。
全部源码都在这里了。也就不给资源链接啥的。
C++ tab键实现自动补全输入功能的更多相关文章
- CentOS中按tab键不能自动补全问题解决办法
CentOS中按tab键不能自动补全问题解决办法 一:检查一下系统有没有安装bash-completion包. 二:yum查找一下 三:yum安装bash-completion包 前言 在CentOS ...
- CentOS 8 按tab键不能自动补全问题解决方案
CentOS中按tab键不能自动补全问题解决办法 检查一下系统有没有安装bash-completion包 [root@Sonarqube ~]# rpm -lq bash-completion yum ...
- Unbuntu 终端中使用Tab键不能自动补全
解决方案 1.利用vi编辑器打开 /etc/bash.bashrc文件(需要root权限) sudo vi /etc/bash.bashrc 2.找到文件中的下列代码 #enable bash com ...
- SecureCRT ,可是进入模拟器后TAB键还是无法补全
SecureCRT是做网络,路由,交换机等设备的人都知道的工具 ,可是进入模拟器后TAB键还是无法补全,就很懊恼了. 设置步骤: 1)打开SecureCRT软件,选项—全局选项—常规—默认的会话设置— ...
- 利用redis完成自动补全搜索功能(一)
最近要做一个搜索自动补全的功能(目前只要求做最前匹配),自动补全就是自动提示,类似于搜索引擎,再上面输入一个字符,下面会提示多个关键词供参考,比如你输入 nb 2字符, 会自动提示nba,nba录像, ...
- UITextFiled自动补全输入,选中补全内容。NSRange和UITextRange的相互转换。-b
有个需求就是 需要用户输入几位以后账号,可以根据本地存储的登录成功的账号,进行自动补全,并且补全内容为选中状态,不影响用户的新输入. 研究了一下,下面是完整的实现的方法. 补充个下载地址http:// ...
- TextBox 设置数据源的自动补全输入字符串功能
这个东西首先说明是不是自己原创,但是比较简单.所以讲起分享如下.主要是用到TextBox的自动补全属性,这个东西虽然自己以前经常用TextBox,但是补全从没接触过. 关键代码是在窗体载入时加载如下代 ...
- linux下让irb实现代码自动补全的功能
我不知道其他系统上irb是否有此功能,但是在ubuntu上ruby2.1.2自带的irb默认是没有代码自动补全功能的,这多少让人觉得有所不便.其实加上也很简单,就是在irb里加载一个模块:requir ...
- 利用redis完成自动补全搜索功能(三)
前面已经完成了分词和自动提示功能,最后把搜索结合在一起,来个完成的案例.当然最好还是用搜索分词解决,这个只是一个临时解决方案. 其实加上搜索很简单,要做的就是3件事 1. 分词的时候,把有用词的id存 ...
随机推荐
- ArcGIS engine中Display类库 (局部刷新)
转自原文 ArcGIS engine中Display类库 (局部刷新) Display类库包括了用于显示GIS数据的对象.除了负责实际输出图像的主要显示对象(display object)外,这个类库 ...
- 小白算法之路-非确定性多项式(non-deterministic polynomial,缩写NP)
前端小白的算法之路 时隔多日终于解决了埋在心头的一道难题,霎时云开雾散,今天把一路而来碰到的疑惑和心得都记录下来,也算是开启了自己探索算法的大门. 问题背景 曾经有一个年少轻狂的职场小白,在前端圈 ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第六章 2(Binary Trees)
112 - Tree Summing 题目大意:给出一个数,再给一颗树,每个头节点的子树被包含在头节点之后的括号里,寻找是否有从头节点到叶子的和与给出的数相等,如果有则输出yes,没有输出no! 解题 ...
- 如何让hudson的两个job共用一个svn工作目录
作者:朱金灿 来源:http://blog.csdn.net/clever101 现在我的需求是这样的:一个软件需要编译完全版本和基础版本,完全版本的基础功能较多,基础版本只包含了基础功能.有时只需要 ...
- 【bzoj4864】神秘物质
Description 给出一个长度为n的序列,第i个数为ai,进行以下四种操作: merge x e:将当前第x个数和第x+1个数合并,得到一个新的数e: insert x e:在当前第x个数和第x ...
- 【习题 8-12 UVA - 1153】Keep the Customer Satisfied
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 结束时间比较早的,就早点开始做. 所以,将n件事情,按照结束时间升序排. 然后对于第i件事情. 尽量把它往左排. 即t+1..t+a ...
- 分析深圳电信的新型HTTP劫持方式
昨天深圳下了一天的暴雨,2014年的雨水真是够多的. 用户的资源就是金钱,怎的也要好好利用嘛不是? ISP的劫持手段真是花样百出.从曾经的DNS(污染)劫持到后来的共享检測.无不通过劫持正常的请求来达 ...
- Spark通过YARN提交任务不成功(包含YARN cluster和YARN client)
无论用YARN cluster和YARN client来跑,均会出现如下问题. [spark@master spark-1.6.1-bin-hadoop2.6]$ jps 2049 NameNode ...
- BZOJ3510首都(LCT)
Description 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B国将永远从 ...
- 解决使用SecureCRT不能连接Ubuntu的问题
一.现象 SecureCRT是远程登陆工具及串口,可以远程进行登陆Linux服务器或者串口打印数据.但我下载安装了之后想通过SecureCRT来远程登陆我的Ubuntu,出现一直连接不上. 二.问题原 ...