C++ tab键实现自动补全输入功能

时间:2021-05-11 22:49:01

一、简介

由于项目中写了个测试的控制台程序,是每次读取一行,即通过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键实现自动补全输入功能的更多相关文章

  1. CentOS中按tab键不能自动补全问题解决办法

    CentOS中按tab键不能自动补全问题解决办法 一:检查一下系统有没有安装bash-completion包. 二:yum查找一下 三:yum安装bash-completion包 前言 在CentOS ...

  2. CentOS 8 按tab键不能自动补全问题解决方案

    CentOS中按tab键不能自动补全问题解决办法 检查一下系统有没有安装bash-completion包 [root@Sonarqube ~]# rpm -lq bash-completion yum ...

  3. Unbuntu 终端中使用Tab键不能自动补全

    解决方案 1.利用vi编辑器打开 /etc/bash.bashrc文件(需要root权限) sudo vi /etc/bash.bashrc 2.找到文件中的下列代码 #enable bash com ...

  4. SecureCRT ,可是进入模拟器后TAB键还是无法补全

    SecureCRT是做网络,路由,交换机等设备的人都知道的工具 ,可是进入模拟器后TAB键还是无法补全,就很懊恼了. 设置步骤: 1)打开SecureCRT软件,选项—全局选项—常规—默认的会话设置— ...

  5. 利用redis完成自动补全搜索功能(一)

    最近要做一个搜索自动补全的功能(目前只要求做最前匹配),自动补全就是自动提示,类似于搜索引擎,再上面输入一个字符,下面会提示多个关键词供参考,比如你输入 nb 2字符, 会自动提示nba,nba录像, ...

  6. UITextFiled自动补全输入,选中补全内容。NSRange和UITextRange的相互转换。-b

    有个需求就是 需要用户输入几位以后账号,可以根据本地存储的登录成功的账号,进行自动补全,并且补全内容为选中状态,不影响用户的新输入. 研究了一下,下面是完整的实现的方法. 补充个下载地址http:// ...

  7. TextBox 设置数据源的自动补全输入字符串功能

    这个东西首先说明是不是自己原创,但是比较简单.所以讲起分享如下.主要是用到TextBox的自动补全属性,这个东西虽然自己以前经常用TextBox,但是补全从没接触过. 关键代码是在窗体载入时加载如下代 ...

  8. linux下让irb实现代码自动补全的功能

    我不知道其他系统上irb是否有此功能,但是在ubuntu上ruby2.1.2自带的irb默认是没有代码自动补全功能的,这多少让人觉得有所不便.其实加上也很简单,就是在irb里加载一个模块:requir ...

  9. 利用redis完成自动补全搜索功能(三)

    前面已经完成了分词和自动提示功能,最后把搜索结合在一起,来个完成的案例.当然最好还是用搜索分词解决,这个只是一个临时解决方案. 其实加上搜索很简单,要做的就是3件事 1. 分词的时候,把有用词的id存 ...

随机推荐

  1. 基本shell编程【3】- 常用的工具awk&bsol;sed&bsol;sort&bsol;uniq&bsol;od

    awk awk是个很好用的东西,大量使用在linux系统分析的结果展示处理上.并且可以使用管道, input | awk ''  | output 1.首先要知道形式 awk 'command' fi ...

  2. python中使用xlrd、xlwt操作excel表格详解

    python读excel——xlrd 这个过程有几个比较麻烦的问题,比如读取日期.读合并单元格内容.下面先看看基本的操作: 首先读一个excel文件,有两个sheet,测试用第二个sheet,shee ...

  3. 7 -- Spring的基本用法 -- 8&period;&period;&period;

    7.8 深入理解容器中的Bean 7.8.1 抽象Bean与子Bean 把多个<bean.../>配置中相同的信息提取出来,集中成配置模版------这个配置模版并不是真正的Bean,因此 ...

  4. HDU 5875 Function 【倍增】 &lpar;2016 ACM&sol;ICPC Asia Regional Dalian Online&rpar;

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  5. 各种语言中的urlencode方法

    转载自:http://blog.sina.com.cn/s/blog_3f195d2501000a9b.html URLENCODE和URLDECODE是比较常用的URL参数转换方法,为以后使用方便, ...

  6. Jsp中使用EL表达式对字符串进行操作

    用fn函数:<%@ taglib prefix="fn" uri="http://Java.sun.com/jsp/jstl/functions" %&g ...

  7. vue学习记录③(路由)

    上篇文章我们用vue-cli脚手架工具做了个简单的hello world页面,但是我们破坏了原来的流程,而正常的访问页面应该是通过路由来实现的. 那么什么是路由呢? 路由就是通过不同的url来访问不同 ...

  8. vue与mapbox

    1.引入mapbox 在index.html里面引入maobox gl 或者使用 npm install mapbox-gl 2.功能 我主要这次要实现 地图展示 地图打点(添加Marker) 地图的 ...

  9. Minimalist GNU for Windows

    MinGW | Minimalist GNU for Windows http://www.mingw.org/ MinGW, a contraction of "Minimalist GN ...

  10. 18华南理工校赛 K 小马哥的超级盐水

    https://www.nowcoder.com/acm/contest/94/K sum(ai)/sum(bi) = x/y <=> sum(ai*yi-bi*x) = 0 跟这题有点类 ...