一、简介

由于项目中写了个测试的控制台程序,是每次读取一行,即通过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. AutoLayout具体解释+手把手实战

    首先说一下这篇博客尽管是标记为原创,可是事实并不是本人亲自写出来的.知识点和样例本人花了一天各处查找和整理终于决定写一个汇总的具体解释,解去各位朋友到处盲目查找的必要,由于不是转载某一个人的内容.故此 ...

  2. Codeforces 429D Tricky Function 近期点对

    题目链接:点击打开链接 暴力出奇迹. 正解应该是近期点对.以i点为x轴,sum[i](前缀和)为y轴,求随意两点间的距离. 先来个科学的暴力代码: #include<stdio.h> #i ...

  3. vue ---- 实现手机端(左滑 删除。右划 正常)

    touchstart: // 手指放到屏幕上的时候触发 touchmove: // 手指在屏幕上移动的时候触发 touchend: // 手指从屏幕上拿起的时候触发 touchcancel: // 系 ...

  4. Android studio文件名颜色分别表示含义

    这其实是主要和版本控制工具有关,含义如下: 绿色,已经加入控制暂未提交红色,未加入版本控制蓝色,加入,已提交,有改动白色,加入,已提交,无改动 灰色:版本控制已忽略文件

  5. GridView1.DataKeys[e.RowIndex].Value 是什么含义?

    https://zhidao.baidu.com/question/88518619.html举个例子来说吧 你将一个student表绑定到grid上 这个表里有一些字段 包括id 姓名 学号 等等等 ...

  6. JavaScript--数据结构与算法之链表实现约瑟夫环

    传说在公元1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的40 个同胞被罗马士兵包围.犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案.他们围成一个圈,从一个人开始,数到第三个人时将 ...

  7. 常用的130个vim命令

    最近VIM用的也越来越多了...因为确实在慢慢的把win下的编辑习惯转成unix下的编辑习惯..._vimrc也在不断的完善中先贴一下平时在VIM中使用中的命令...有很多也是我没有掌握的 (估计也是 ...

  8. JS面向对象系列教程 — 对象的基本操作

    面向对象概述  面向对象(Object Oriented)简称OO,它是一种编程思维,用于指导我们如何应对各种复杂的开发场景. 这里说的对象(Object),意思就是事物,在面向对象的思维中,它将一 ...

  9. POJ——T 2796 Feel Good

    http://poj.org/problem?id=2796 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15375   ...

  10. 虚拟机上的Ubuntu开机显示“无法应用原保存的显示器配置”

    如图: 解决方法: 删除monitors.xml 文件 rm ~/.config/monitors.xml