Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7358
  • 博文数量: 2
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 30
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-21 03:49
文章分类
文章存档

2015年(2)

我的朋友

分类: C/C++

2015-09-11 22:46:12

前几天用户要求在使用汉语拼音的首字母来检索,因为之前没做过,而且本身对汉字编码就有点发怵,所以有点头皮发麻。但是怎么办呢,用户提需求了。
先上代码吧,随后解释

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
 class MainWindow;
}
//自定义结构体,用来做测试用的

typedef struct _text
{
 QString _text;
 QString _alphbat;
}TEXT;

class MainWindow : public QMainWindow
{
 Q_OBJECT

public:
 explicit MainWindow(QWidget *parent = 0);
 ~MainWindow();

private:
 Ui::MainWindow *ui;
 QList<TEXT> _texts;

signals:
 void sgFind( const QString &text );

 
private slots:
 void onTextChanged( const QString &text );
 void onReturnPressed();
 

private:
 QString firstPinyin( QString &text );
 bool In( wchar_t start, wchar_t end, wchar_t code);
 char convert(wchar_t n);    //谢谢网上的一哥哥,记不得名字了,功能就是用GBK码查找到汉字的首拼音字母

};

#endif // MAINWINDOW_H
 
 
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 

MainWindow::MainWindow(QWidget *parent) :
 QMainWindow(parent),
 ui(new Ui::MainWindow)
{
     ui->setupUi(this);

    //测试数据
     TEXT t1;
 t1._text = QString::fromLocal8Bit("南京123");
 t1._alphbat = firstPinyin( t1._text );
 _texts.append( t1 );
 TEXT t2;
 t2._text = QString::fromLocal8Bit("武汉");
 t2._alphbat = firstPinyin( t2._text );
 _texts.append( t2 );
 TEXT t3;
 t3._text = QString::fromLocal8Bit("南昌");
 t3._alphbat = firstPinyin( t3._text );
 _texts.append( t3 );

 TEXT t4;
 t4._text = QString::fromLocal8Bit("123济南");
 t4._alphbat = firstPinyin( t4._text );
 _texts.append( t4 );
 TEXT t5;
 t5._text = QString::fromLocal8Bit("南京345");
 t5._alphbat = firstPinyin( t5._text );
 _texts.append( t5 );
 TEXT t6;
 t6._text = QString::fromLocal8Bit("南京789");
 t6._alphbat = firstPinyin( t6._text );
 _texts.append( t6 );
 
    //文本输入框输入完毕,开始检索
 connect( ui->lineEdit, SIGNAL(returnPressed()), this, SLOT(onReturnPressed()) );
 connect( this, SIGNAL(sgFind(QString)), this, SLOT(onTextChanged(QString)) );
}

MainWindow::~MainWindow()
{
 delete ui;
}

void MainWindow::onReturnPressed()
{
 QString text = ui->lineEdit->text();
 emit sgFind( text );
}

//思路其实很简单,windows本地一般用的gbk编码,那就把要检索的所有条目的拼音首字母提出来,保存着,带检索比对。
QString MainWindow::firstPinyin(QString &string)
{
 QTextCodec *codec4gbk = QTextCodec::codecForName("GBK");    //获取qt提供的gbk的解码器
 QByteArray buf = codec4gbk->fromUnicode( string );        //qt用的unicode,专程gbk
 int size = buf.size();
 quint16 *array = new quint16[size+1];
 QString alphbats;
 for( int i = 0, j = 0; i < buf.size(); i++, j++ )
 {
 if( (quint8)buf[i] < 0x80 )                        //gbk的第一个字节都大于0x81,所以小于0x80的都是符号,字母,数字etc
 continue;
 array[j] = (((quint8)buf[i]) << 8) + (quint8)buf[i+1];    //计算gbk编码
 i++;
 alphbats.append( convert( array[j] ) );            //相当于查表,用gbk编码得到首拼音字母
 }
    delete [] array;
 return alphbats;
}

void MainWindow::onTextChanged(const QString &text)
{
 ui->comboBox->clear();
 int size = _texts.size();
 int j = 0;
 for( int i = 0; i < size; i ++ )
 {
 QString string = _texts.at(i)._alphbat;
 if( string.contains( text ) )
 {
 ui->comboBox->insertItem( j, _texts.at(i)._text );    //把符合条件的测试数据插入下拉框,共选择
 }
 }
}

bool MainWindow::In( wchar_t start, wchar_t end, wchar_t code)
{
 return (code>=start && code<=end) ? true : false;
}
char MainWindow::convert(wchar_t n)
{
 if (In(0xB0A1,0xB0C4,n)) return 'a';
 if (In(0XB0C5,0XB2C0,n)) return 'b';
 if (In(0xB2C1,0xB4ED,n)) return 'c';
 if (In(0xB4EE,0xB6E9,n)) return 'd';
 if (In(0xB6EA,0xB7A1,n)) return 'e';
 if (In(0xB7A2,0xB8c0,n)) return 'f';
 if (In(0xB8C1,0xB9FD,n)) return 'g';
 if (In(0xB9FE,0xBBF6,n)) return 'h';
 if (In(0xBBF7,0xBFA5,n)) return 'j';
 if (In(0xBFA6,0xC0AB,n)) return 'k';
 if (In(0xC0AC,0xC2E7,n)) return 'l';
 if (In(0xC2E8,0xC4C2,n)) return 'm';
 if (In(0xC4C3,0xC5B5,n)) return 'n';
 if (In(0xC5B6,0xC5BD,n)) return 'o';
 if (In(0xC5BE,0xC6D9,n)) return 'p';
 if (In(0xC6DA,0xC8BA,n)) return 'q';
 if (In(0xC8BB,0xC8F5,n)) return 'r';
 if (In(0xC8F6,0xCBF0,n)) return 's';
 if (In(0xCBFA,0xCDD9,n)) return 't';
 if (In(0xCDDA,0xCEF3,n)) return 'w';
 if (In(0xCEF4,0xD188,n)) return 'x';
 if (In(0xD1B9,0xD4D0,n)) return 'y';
 if (In(0xD4D1,0xD7F9,n)) return 'z';
 return '\0';
}

有图有真相
 


                                   
阅读(3538) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~