我是一名工科生。作为一个考试测试,教授要求我们用C语言实现接龙。作为一个初学者,我在显示牌的花色时遇到了一些困难。如果我将十进制数转换为ASCII符号,输出的结果是一个问号"?";有什么方法可以显示符号而不是问号?下面是我编译的代码。在编译过程中,它没有返回任何错误,但可执行文件是空的;它没有返回任何东西。我使用的编译器是Dev-C ++.难道是我的控制台字体不支持这个字符集?如果是,是否可以修改一下使其兼容?
测试1.如果是的话,可以修改一下,让它兼容。
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#include <windows.h>
typedef enum { cuori, quadri, fiori, picche } semi;
typedef enum { coperta, scoperta, eliminata } status;
typedef struct _card
{
int valore;
semi seme;
status stato;
} card;
const wchar_t visSemi[][2] = { L"\u2665", L"\u2660", L"\u2663", L"\u2666" };
void visCard(card *c)
{
_setmode(_fileno(stdout), _O_U16TEXT);
if (c->stato == scoperta)
{
switch (c->valore)
{
case 1:
printf ("A\n");
break;
case 11:
printf ("J\n");
break;
case 12:
printf("Q\n");
break;
case 13:
printf("K\n");
break;
}
printf("\n", visSemi[c->seme]);
}
else
{
if (c->stato == coperta)
printf("??\n");
}
_setmode(_fileno(stdout), _O_TEXT);
}
int main()
{
card pila[8] = {
{ 1, cuori, scoperta },
{ 11, quadri, scoperta },
{ 12, fiori, scoperta },
{ 13, picche, eliminata },
{ 1, quadri, scoperta },
{ 11, fiori, scoperta },
{ 12, picche, scoperta },
{ 13, cuori, coperta },
};
int i;
for (i = 0; i < 8; i++)
visCard(&pila[i]);
return 0;
}
测试二(比较笼统):
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
int main (void){
_setmode(_fileno(stdout),_O_U16TEXT);
wprintf(L"\x043a\x043e\x0448\x043a\x0430\x65e5\x672c\x56fd\n");
return 0;
}
这种使用 printf()
:
printf("\n", visSemi[c->seme]);
就其用意而言,是不正确的,尽管 "A "和 "B "的论点是不正确的。visSemi[c->seme]
被评估但被忽略。
"如果格式的参数不足,则行为未定义。如果格式用尽而参数还在,则多余的参数会被评估(和往常一样),但其他情况下会被忽略。"
资料来源:C18,§7.21.6.12。C18, §7.21.6.12
要打印 wchar_t
与 printf()
是不正确的。
使用 wprintf()
打印 wchar_t
:
wprintf(L"%lc\n", visSemi[c->seme]);
而不是: