人造人 发表于 2017-3-21 13:55:38

Linux下用C语言输出宽字符串问题

Windows下正常


Linux下就不行了


这样又可以


/*洗盘程序*/

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>

#define CARD_NUMBER 14

typedef struct
{
        char play_name;
        unsigned char card; // 每个人的牌数
} PlayAttribute;


void shuffle(PlayAttribute *play1, PlayAttribute *play2, PlayAttribute *play3)
{
        int i;
        char card_flag = {0}; // 牌的标志,为1表示此牌已使用
        unsigned char card1, card2, card3;

        for(i = 0; i < CARD_NUMBER; i++)
        {
                do
                {
                        card1 = rand() % 54; // 生成0~53之间的数字
                } while(card_flag == 1); // 此牌已使用,重新生成
                do
                {
                        card2 = rand() % 54; // 生成0~53之间的数字
                } while((card_flag == 1) || (card2 == card1)); // 此牌已使用,重新生成
                do
                {
                        card3 = rand() % 54; // 生成0~53之间的数字
                } while((card_flag == 1) || (card3 == card2) || (card3 == card1)); // 此牌已使用,重新生成

                                                                                          // 以下3张牌已使用,进行标记
                card_flag = 1;
                card_flag = 1;
                card_flag = 1;


                play1->card = card1;
                play2->card = card2;
                play3->card = card3;
        }
}

wchar_t *get_card(unsigned char card) // 返回数字对应的字符串
{
        static wchar_t table = {L"方", L"梅", L"红", L"黑"};

        if((card >= 0) && (card < 13))
        {
                switch((card % 13) + 1)
                {
                case 11:
                        swprintf(table + 1, 4, L"%c", L'J');
                        return table;
                case 12:
                        swprintf(table + 1, 4, L"%c", L'Q');
                        return table;
                case 13:
                        swprintf(table + 1, 4, L"%c", L'K');
                        return table;
                case 1:
                        swprintf(table + 1, 4, L"%c", L'A');
                        return table;
                }
                swprintf(table + 1, 4, L"%d", (card % 13) + 1);
                return table;
        }
        else if((card >= 13) && (card < 2 * 13))
        {
                switch((card % 13) + 1)
                {
                case 11:
                        swprintf(table + 1, 4, L"%c", L'J');
                        return table;
                case 12:
                        swprintf(table + 1, 4, L"%c", L'Q');
                        return table;
                case 13:
                        swprintf(table + 1, 4, L"%c", L'K');
                        return table;
                case 1:
                        swprintf(table + 1, 4, L"%c", L'A');
                        return table;
                }
                swprintf(table + 1, 4, L"%d", (card % 13) + 1);
                return table;
        }
        else if((card >= 2 * 13) && (card < 3 * 13))
        {
                switch((card % 13) + 1)
                {
                case 11:
                        swprintf(table + 1, 4, L"%c", L'J');
                        return table;
                case 12:
                        swprintf(table + 1, 4, L"%c", L'Q');
                        return table;
                case 13:
                        swprintf(table + 1, 4, L"%c", L'K');
                        return table;
                case 1:
                        swprintf(table + 1, 4, L"%c", L'A');
                        return table;
                }
                swprintf(table + 1, 4, L"%d", (card % 13) + 1);
                return table;
        }
        else if((card >= 3 * 13) && (card < 4 * 13))
        {
                switch((card % 13) + 1)
                {
                case 11:
                        swprintf(table + 1, 4, L"%c", L'J');
                        return table;
                case 12:
                        swprintf(table + 1, 4, L"%c", L'Q');
                        return table;
                case 13:
                        swprintf(table + 1, 4, L"%c", L'K');
                        return table;
                case 1:
                        swprintf(table + 1, 4, L"%c", L'A');
                        return table;
                }
                swprintf(table + 1, 4, L"%d", (card % 13) + 1);
                return table;
        }
        else // 大王和小王
        {
                if(card == 52)
                {
                        return L"大王";
                }
                else
                {
                        return L"小王";
                }
        }
}

int main(void)
{
        int i;
        char replay;
        PlayAttribute play1, play2, play3;

        setlocale(LC_ALL, "chs");

        srand((unsigned int)time(NULL));

        printf("请输入1号玩家的名字:");
        scanf("%s", play1.play_name);
        printf("请输入2号玩家的名字:");
        scanf("%s", play2.play_name);
        printf("请输入3号玩家的名字:");
        scanf("%s", play3.play_name);

        printf("方=方角,梅=梅花,红=红桃,黑=黑桃\n");
       
        do
        {
                shuffle(&play1, &play2, &play3); // 洗牌

                printf("%s手上的牌是:", play1.play_name);
                for(i = 0; i < CARD_NUMBER; i++)
                {
                        wprintf(L"%ls ", get_card(play1.card));
                }
                putchar('\n');

                printf("%s手上的牌是:", play2.play_name);
                for(i = 0; i < CARD_NUMBER; i++)
                {
                        wprintf(L"%ls ", get_card(play2.card));
                }
                putchar('\n');

                printf("%s手上的牌是:", play3.play_name);
                for(i = 0; i < CARD_NUMBER; i++)
                {
                        wprintf(L"%ls ", get_card(play3.card));
                }
                putchar('\n');

       
                printf("重新洗牌(Y/N)?");
                getchar();
                scanf("%c", &replay);
        } while((replay == 'Y') || (replay == 'y'));

        return 0;
}

人造人 发表于 2017-3-21 20:40:21

@小甲鱼

小甲鱼 发表于 2017-3-21 23:20:38

printf 和 wprintf 两个不能混在一起使用,会导致冲突。

人造人 发表于 2017-3-22 00:02:25

小甲鱼 发表于 2017-3-21 23:20
printf 和 wprintf 两个不能混在一起使用,会导致冲突。

哦,谢谢
页: [1]
查看完整版本: Linux下用C语言输出宽字符串问题