【问题标题】:How to format the output in C?如何在C中格式化输出?
【发布时间】:2015-02-01 11:55:54
【问题描述】:

我有一个长度等于 52 的一维数组,它由随机整数值组成。我想使用大写和小写字母打印出带有索引的数组。 例如,

index:    A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T
array:   -1 -1 -1 -1 32 -1 -1 -1 -1 12 -1 -1 -1 -1 20 -1 -1 -1 -1 -1
index:    U  V  W  X  Y  Z  a  b  c  d  e  f  g  h  i   ...  x  y  z
array:   -1 -1 -1 -1 15 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1   ... -1 -1 -1 

我尝试使用for 循环到printf("%c\t\r%d\t\b", 'A', -1),但\b 没有向上移动光标。我没有生成我想要的格式(如上所示),而是得到了如下所示的内容,

 A
-1   B
-1   C
-1   .
 .   .
 .   .
 .

有人知道如何生成上面显示的格式吗? 提前致谢。

【问题讨论】:

  • 打印"index: A B ... T" 然后"array: -1 -1 ... -1"
  • 当数组的长度超过一个屏幕长度时,我认为不会打印出我想要的格式。它只会先打印出索引数组,然后再打印出内容数组。

标签: c data-structures formatting printf


【解决方案1】:

这里,我只给你用turbo C编译的C语言程序你想要的格式示例。

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
    int j=96;
    clrscr();
    for(int i=1;i<=52;i+=2)
    {
        gotoxy(i,1);
        printf("%c",j+=1);

        gotoxy(i,2);
        printf("%d ",random(2));
    }
    j=96;
    for(i=1;i<=52;i+=2)
    {
        gotoxy(i,3);
        printf("%c",j+=1);

        gotoxy(i,4);
        printf("%d ",random(2));
    }

    getch();
}

【讨论】:

    【解决方案2】:

    以下不依赖于 ASCII 并且非常便携。

    只需在另一个数组中查找索引显示字符。

    static const char index[52] = 
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    int A[52] = { -1, -1, -1, -1, 32, -1, -1, -1, -1, 12, -1, -1, -1, -1, 20, -1 ...};
    
    int i;
    fputs("index:  ", stdout);
    for (i=0; i<52; i) {
      printf("  %c", index[i]);
    }
    fputs("\n", stdout);
    
    fputs("array:  ", stdout);
    for (i=0; i<52; i) {
      printf(" %2d", A[i]);
    }
    fputs("\n", stdout);
    

    【讨论】:

    • 感谢您的回复,但代码并没有打印出我想要的确切形式。当输出超过一个屏幕长度时,代码会先打印出索引数组,然后打印出数组A。我需要一个可以同时打印出索引数组和A数组的东西,这样两个数组都可以在同一时间。
    • @Steve 建议修改您的帖子以明确说明屏幕长度要求,包括屏幕最小、最大和输出系统类型。答案是系统特定的。
    【解决方案3】:

    C 和 C++ 没有任何屏幕或控制台的概念,它们只看到字节流,没有固有的显示特征。有像 ncuses 这样的第三方 API 可以帮助您做到这一点。

    一种方法,对cursor movement 使用 ANSI 转义序列,其中 ESC[y;xH 将光标移动到行 y,col x,其中左上角是 {1,1}。

    printf("\033[%d;%dH", row, col);
    

    windows下可以试试,

    使用 SetConsoleCursorPosition。 SetConsoleCursorPosition

    MSDN 库的同一部分中还有许多其他功能。其中一些可能也很有用。

    否则,您可以将打印限制为一行的特定数量的索引,您可以尝试下面的代码。

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    
    int main() {
    
        const char charIndex[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        const unsigned int counter = 133;
        const unsigned int hBuffer = 14; // max number of indexes in a line
    
        unsigned int row,j;
        unsigned int arrIndex = 0,yetToPrintcount = counter,htempBuffer;
    
        // Nof of Rows needed to print the counter
        unsigned int noOfRows = ( counter / hBuffer );
        // if no reminder means no trailing values less than hBuffer to print
        if ( counter % hBuffer == 0 ) noOfRows--;
    
        srand(time(NULL)); // For random numbers
    
        for ( row = 0 ; row <= noOfRows; ++row)
        {
    
            // For printing the trailing numbers
            // which are less than the h_buffer size
            if ( yetToPrintcount < hBuffer ) 
                htempBuffer = yetToPrintcount;
            else 
                htempBuffer = hBuffer;
    
            // printing the index first
            printf("index:  ");
            for (j = 0; j < htempBuffer; ++j){
                // circular printing of index
                if( arrIndex == (sizeof(charIndex) / sizeof(char))-1 ) arrIndex = 0;
                printf("  %c", charIndex[arrIndex++]);
            }
            printf("\n");
    
            // printing the array random values
            printf("array:  ");
            for (j = 0; j < htempBuffer; ++j) 
                printf("  %d", rand()%10); // Prints Random numbers between 0 to 9
            printf("\n");
    
            yetToPrintcount -= hBuffer;
        }
    
        return 0;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      相关资源
      最近更新 更多