visual studio cyrillic text c++

visual studio cyrillic text c++

本文关键字:c++ text cyrillic studio visual      更新时间:2023-10-16

我正在使用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);
  }
}