【问题标题】:How can I "reset" the \n alignment in order to print aligned at the first line?如何“重置” \n 对齐以便在第一行对齐打印?
【发布时间】:2020-09-24 21:29:40
【问题描述】:

这个函数必须打印纸牌游戏的牌堆。

void print_stacks(Stack *stacks){
    char moretab[20] = {0};
    Stack *currentPtr = &(stacks[0]);
    for (size_t i=1;i<10;++i){
        while (currentPtr != NULL){
            printf("%s%s\n%s",currentPtr->cardd.face,currentPtr->cardd.suit,moretab);
            currentPtr = currentPtr->nextPtr;
        }
        strcat(moretab,"\t");
        currentPtr = &(stacks[i]);
    }
}

如果你愿意,这里是完整的代码:https://hastebin.com/iguteleqiq.cpp

我有这个输出例如:

> cmd /c .\"solitaire.exe" 

8♣

        2♣
        Q♣

                9♠
                2♦
                2♥

                        Q♥
                        6♠
                        7♠
                        J♥

                                Q♦
                                10♦
                                7♦
                                J♦
                                4♣

                                        K♦
                                        4♠
                                        2♠
                                        A♥
                                        3♣
                                        J♣

                                                9♥
                                                3♦
                                                8♠
                                                10♥
                                                K♠
                                                3♠
                                                8♥

                                                        A♣
                                                        A♦
                                                        8♦
                                                        J♠
                                                        9♣
                                                        4♥
                                                        9♦
                                                        5♥

                                                                K♣
                                                                6♦
                                                                A♠
                                                                3♥
                                                                4♦
                                                                K♥
                                                                5♠
                                                                10♠
                                                                5♣

预期输出:

> cmd /c .\"somerset.exe" 
8♣      2♣      9♠      Q♥      Q♦        etc etc etc
        Q♣      2♦      6♠      10♦
                2♥      7♠      7♦
                        J♥      J♦
                                4♣

我怎样才能让所有这些卡片堆在同一条第一行对齐?我试图刷新标准输出,但不起作用。为了实现这一目标,我能做些什么?我不是很有经验。

【问题讨论】:

  • “在同一行对齐”到底是什么意思?您能否显示预期的输出,以便毫无疑问。
  • 您一直在字符串的开头添加制表符:strcat(moretab,"\t");“重置字符串”,您应该没问题 :) 问:您首先想要制表符做什么? Q:如果需要更多帮助,能否提供更完整的example
  • @kaylum 更新了话题。
  • 如果您不想移动到下一行,请不要打印换行符。为了实现您想要的输出,您最好的选择是逐行而不是逐列打印。
  • @HugoCunha 他想要一个垂直回车,将光标带回屏幕顶部。约翰的想法是对的。这是一个工作示例:onlinegdb.com/ryi9K95SP

标签: c windows printf stdout


【解决方案1】:

正如@JohnBollinger 在 cmets 中建议的那样,您需要逐行打印数据。堆栈是列,因此您需要跟踪您在 9 个不同堆栈中的位置。如果你有一个由 9 个堆栈指针组成的数组初始化到 9 个堆栈的顶部,你可以循环遍历这 9 个指针并打印每个堆栈中的当前项以形成一行。然后循环,直到没有任何东西可以打印。

类似这样的:

// you need to #include <stdbool.h> or change finished to an int
void print_stacks(Stack *stacks){
    // make 9 stack pointers
    Stack *currentPtr[9];
    // point them to the first 9 stacks
    for (size_t i=0;i<9;++i){
        currentPtr[i] = &(stacks[i]);
    }
    // loop through printing one row at a time
    bool finished;
    do{
        finished=true;
        // print the current item from each of the 9 stack pointers
        for (size_t i=0;i<9;++i){
            if (currentPtr[i] != NULL){
                printf("%s%s",currentPtr[i]->cardd.face,currentPtr[i]->cardd.suit);
                currentPtr[i] = currentPtr[i]->nextPtr;
                // we printed something so we aren't finished
                finished=false;
            }
            printf("\t");
        }
        printf("\n");
    // loop until we are finished
    } while (!finished);
}

在线试用:https://onlinegdb.com/SyPZoqcBw

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 2020-03-04
    • 1970-01-01
    • 2012-11-25
    • 2021-04-08
    • 2016-09-02
    • 2012-05-23
    • 2021-11-12
    • 1970-01-01
    相关资源
    最近更新 更多