c/c++语言开发共享C中的重音/变音字符?

我刚刚学习了C并得到了一项任务,我们必须将纯文本转换为莫尔斯代码并返回。 (我大部分都熟悉Java,所以请遵守我使用的条款)。

为此,我有一个包含所有字母字符串的数组。

char *letters[] = { ".- ", "-... ", "-.-. ", "-.. ", ".", "..-." etc 

我写了一个函数来返回所需字母的位置。

 int letter_nr(unsigned char c) { return c-97; } 

这是有效的,但是分配规范要求处理瑞典语的变形字母åäö。 瑞典语字母与最后用这三个字母的英语相同。 我尝试检查这些,如下:

 int letter_nr(unsigned char c) { if (c == 'å') return 26; if (c == 'ä') return 27; if (c == 'ö') return 28; return c-97; } 

不幸的是,当我尝试测试这个函数时,我得到了所有这三个函数的相同值:98。这是我的主要测试函数:

 int main() { unsigned char letter; while(1) { printf("Type a letter to get its position: "); scanf("%c", &letter); printf("%dn", letter_nr(letter)); } return 0; } 

我该怎么做才能解决这个问题?

    通常编码的东西非常复杂。 另一方面,如果您只想要一个特定于您的编译器/平台的脏解决方案,而不是在代码中添加类似这样的东西:

     printf("letter 0x%x is number %dn", letter, letter_nr(letter)); 

    它将为您的变音符号提供hex值。 而不仅仅是在if语句中替换你的带有数字的字母。

    编辑你说你总是得到98所以你的scanf从控制台获得98 + 97 = 195 = 0x3C 。 根据这个表, 0x3C是常见的LATIN SMALL LETTER N的UTF8序列的开始, 带有 Latin1 block 东西 。 您使用的是Mac OS X

    编辑这是我最后的电话。 相当hackery但它适用于我:)

     #include  // scanf for for letter. Return position in Morse Table. // Recognises UTF8 for swedish letters. int letter_nr() { unsigned char letter; // scan for the first time, scanf("%c", &letter); if(0xC3 == letter) { // we scanf again since this is UTF8 and two byte encoded character will come scanf("%c", &letter); //LATIN SMALL LETTER A WITH RING ABOVE = å if(0xA5 == letter) return 26; //LATIN SMALL LETTER A WITH DIAERESIS = ä if(0xA4 == letter) return 27; // LATIN SMALL LETTER O WITH DIAERESIS = ö if(0xB6 == letter) return 28; printf("Unknown letter. 0x%x. ", letter); return -1; } // is seems to be regular ASCII return letter - 97; } // letter_nr int main() { while(1) { printf("Type a letter to get its position: "); int val = letter_nr(); if(-1 != val) printf("Morse code is %d.n", val); else printf("Unknown Morse code.n"); // strip remaining new line unsigned char new_line; scanf("%c", &new_line); } return 0; } 

    字符常量的编码实际上取决于您的语言环境设置。

    最安全的选择是使用宽字符和相应的function。 您将字母表声明为const wchar_t* alphabet = L"abcdefghijklmnopqrstuvwxyzäöå" ,并将单个字符声明为L'ö';

    这个小的示例程序适用于我(也可以在UNIX控制台上使用UTF-8) – 尝试一下。

     #include  #include  #include  #include  int main(int argc, char** argv) { wint_t letter = L''; setlocale(LC_ALL, ""); /* Initialize locale, to get the correct conversion to/from wchars */ while(1) { if(!letter) printf("Type a letter to get its position: "); letter = fgetwc(stdin); if(letter == WEOF) { putchar('n'); return 0; } else if(letter == L'n' || letter == L'r') { letter = L''; /* skip newlines - and print the instruction again*/ } else { printf("%dn", letter); /* print the character value, and don't print the instruction again */ } } return 0; } 

    示例会话:

     Type a letter to get its position: a 97 Type a letter to get its position: A 65 Type a letter to get its position: Ö 214 Type a letter to get its position: ö 246 Type a letter to get its position: Å 197 Type a letter to get its position: <^D> 

    据我所知,在Windows上,这不适用于Unicode BMP之外的字符,但这不是问题。

    嗯…起初我会说“有趣”的角色不是char 。 您不能将其中一个传递给接受char参数的函数并期望它能够工作。

    试试这个(添加剩余的位):

     char buf[100]; printf("Enter a string with funny characters: "); fflush(stdout); fgets(buf, sizeof buf, stdin); /* now print it, as if it was a sequence of `char`s */ char *p = buf; while (*p) { printf("The character '%c' has value %dn", *p, *p); p++; } 

    现在尝试使用宽字符: #include 并用wprintf替换printf ,用fgetws替换fgets等等…

      以上就是c/c++开发分享C中的重音/变音字符?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/544798.html

      (0)
      上一篇 2021年1月9日
      下一篇 2021年1月9日

      精彩推荐