visual studio cyrillic text c++
visual studio cyrillic text c++
我正在使用Visual Studio 2013,运行在Windows 7 64位上。我正在尝试显示与英语不同的语言。我的代码是:
#include <iostream>
#include <string>
using namespace std;
typedef char byte;
byte text[] = "текст";
int text_len;
struct Huf {
byte id;
int wh;
Huf *left, *right;
};
struct List {
List *next;
Huf *tree;
};
List *head;
char code[256];
void createList();
void writeList();
void delList(List *);
void addList(Huf *);
Huf *findDels();
void createTree();
void rlrootTree(Huf *, unsigned);
int main()
{
text_len = strlen(text);
createList();
writeList();
createTree();
cout << "writeCodesn";
rlrootTree(head->tree, 0);
cout << endl;
system("pause");
return 0;
}
void createList()
{
int i;
int ch[256] = { 0 };
for (i = 0; i<text_len; i++) ch[text[i]]++;
List *l;
Huf *h;
head = 0;
for (i = 0; i<255; i++) if (ch[i]>0)
{
h = new Huf;
h->id = i; h->wh = ch[i];
h->left = 0; h->right = 0;
l = new List;
l->tree = h;
l->next = head; head = l;
}
}
void writeList()
{
cout << "writeListn";
List *l = head;
while (l)
{
cout << (l->tree)->id << " ";
l = l->next;
}
cout << endl;
l = head;
while (l)
{
cout << (l->tree)->wh << " ";
l = l->next;
}
cout << endl;
}
void delList(List *l)
{
List *lp, *lc;
if (l == head) { head = l->next; delete l; }
else
{
lp = head; lc = lp->next;
while (lc != l) { lp = lc; lc = lc->next; }
lp->next = lc->next; delete lc;
}
}
void addList(Huf *h)
{
List *l = new List;
l->tree = h;
l->next = head;
head = l;
}
Huf *findDels()
{
List *l = head, *sm = head;
Huf *h;
while (l)
{
if ((l->tree)->wh < (sm->tree)->wh) sm = l;
l = l->next;
}
h = sm->tree;
delList(sm);
return h;
}
void createTree()
{
Huf *h, *h1, *h2;
while (head->next)
{
h1 = findDels();
h2 = findDels();
h = new Huf;
h->id = ' '; h->wh = h1->wh + h2->wh;
h->left = h1; h->right = h2;
addList(h);
}
}
void rlrootTree(Huf *h, unsigned index)
{
if (h)
{
code[index] = '0';
rlrootTree(h->right, index + 1);
if (h->left == 0)
{
code[index] = ' ';
cout << h->id << "->" << code << " ";
}
code[index] = '1';
rlrootTree(h->left, index + 1);
}
}
我尝试添加<locale>
<windows.h>
包含AnsiToOem()
但它似乎无法编译 - 我收到字符错误。
您正在逐字节遍历文本。但是每个字母(字素)由两个代码单元组成,即两个字节。
为了使这变得简单,我将字符串存储为char32_t数组,因为这样整个代码点(一个字素)将适合数组的一个元素。
如果你想能够处理绝对每一个国际"字符",你还必须处理字素簇,那么你可以看看ICU库或类似的东西。
一个危险的简化建议:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
typedef char32_t byte;
byte text[] = U"текст";
int text_len;
struct Huf {
byte id;
int wh;
Huf *left, *right;
};
struct List {
List *next;
Huf *tree;
};
List *head;
char code[16000];
void createList();
void writeList();
void delList(List *);
void addList(Huf *);
Huf *findDels();
void createTree();
void rlrootTree(Huf *, unsigned);
unsigned strlen(char32_t* st) {
unsigned len = 0;
while (*(st+len) != 0) ++len;
return len;
}
int main()
{
text_len = strlen(text);
std::cout << "len = " << text_len << std::endl;
createList();
writeList();
createTree();
cout << "writeCodesn";
rlrootTree(head->tree, 0);
cout << endl;
system("pause");
return 0;
}
void createList()
{
int i;
int ch[16000] = { 0 }; //you really need more here
for (i = 0; i<text_len; i++) ch[text[i]]++;
List *l;
Huf *h;
head = 0;
for (i = 0; i<16000; i++) if (ch[i]>0)
{
h = new Huf;
h->id = i; h->wh = ch[i];
h->left = 0; h->right = 0;
l = new List;
l->tree = h;
l->next = head; head = l;
}
}
void writeList()
{
cout << "writeListn";
List *l = head;
while (l)
{
cout << (l->tree)->id << " ";
l = l->next;
}
cout << endl;
l = head;
while (l)
{
cout << (l->tree)->wh << " ";
l = l->next;
}
cout << endl;
}
void delList(List *l)
{
List *lp, *lc;
if (l == head) { head = l->next; delete l; }
else
{
lp = head; lc = lp->next;
while (lc != l) { lp = lc; lc = lc->next; }
lp->next = lc->next; delete lc;
}
}
void addList(Huf *h)
{
List *l = new List;
l->tree = h;
l->next = head;
head = l;
}
Huf *findDels()
{
List *l = head, *sm = head;
Huf *h;
while (l)
{
if ((l->tree)->wh < (sm->tree)->wh) sm = l;
l = l->next;
}
h = sm->tree;
delList(sm);
return h;
}
void createTree()
{
Huf *h, *h1, *h2;
while (head->next)
{
h1 = findDels();
h2 = findDels();
h = new Huf;
h->id = ' '; h->wh = h1->wh + h2->wh;
h->left = h1; h->right = h2;
addList(h);
}
}
void rlrootTree(Huf *h, unsigned index)
{
if (h)
{
code[index] = '0';
rlrootTree(h->right, index + 1);
if (h->left == 0)
{
code[index] = ' ';
cout << h->id << "->" << code << " ";
}
code[index] = '1';
rlrootTree(h->left, index + 1);
}
}
相关文章:
- 使用 sf::Text 数组时崩溃
- 为什么 sf::Text 显示点而不是数字?
- 从DLL导出函数,LoadLibrary()需要用TEXT转换的字符串才能编译而不会出错
- 为 g++ build 设置超时,为 Sublime Text 运行?
- 编辑期间的 QTableWidgetItem text() 属性
- 如何找到目标文件 *.o 的 ram rom 使用情况(.bss .text .rodata .data)?
- 在 Linux 上编译 - 在函数 '_start' 中:(.text+0x20):对 'main' 的未定义引用
- 当我尝试绘制 sf::Text 对象时,SFML 中的分段错误
- 在 .text 部分中定义只读数据的原因是什么?
- (.text+0x20):对"主要"的未定义引用是什么意思
- 如何使用Stroustrup的Fltk Text类调用变量?
- 带有定制 GLFW 二进制 /usr/bin/ld 的 Centos 7: /usr/local/lib/libglfw3.a(init.c.o):".text"部分中无法识别的重定位 (0x2a)
- 从emxArray_uint8_T检测到错误:.text+0xc1):未定义对"emxInit_uint8_T1(emxArray_uint8_T**, int)"的引用?
- Poco c++ 如何从PostgreSQL DB读取"text"数据类型?
- crt1.o:在函数"_start"中:(.text+0x20):对"main"的未定义引用
- Sublime Text 3 在我尝试编译C++代码时在状态栏上显示"No Build System"。为什么?
- 看起来如此主要的错误.cpp:(.text.startup+0xd6):未定义对"vtable for Counter"的引用?
- 为什么参数接受"text"而不是字符串变量?
- main.cpp:(.text+0x8ea):在C++中未定义的引用
- crtdbg 在使用 sf::Text::setOutlineThickness 时转储内存泄漏