How do I align a number like this in C?

后端 未结 11 717
清歌不尽
清歌不尽 2020-12-04 12:13

I need to align a series of numbers in C with printf() like this example:

-------1
-------5
------50
-----100
----1000

Of

相关标签:
11条回答
  • 2020-12-04 13:09

    As far as I can tell from the question, the amount of padding you want will vary according to the data you have. Accordingly, the only solution to this is to scan the data before printing, to figure out the widest datum, and so find a width value you can pass to printf using the asterix operator, e.g.

    loop over data - get correct padding, put into width
    
    printf( "%*d\n", width, datum );
    
    0 讨论(0)
  • 2020-12-04 13:10

    So, you want an 8-character wide field with spaces as the padding? Try "%8d". Here's a reference.

    EDIT: What you're trying to do is not something that can be handled by printf alone, because it will not know what the longest number you are writing is. You will need to calculate the largest number before doing any printfs, and then figure out how many digits to use as the width of your field. Then you can use snprintf or similar to make a printf format on the spot.

    char format[20];
    snprintf(format, 19, "%%%dd\\n", max_length);
    while (got_output) {
        printf(format, number);
        got_output = still_got_output();
    }
    
    0 讨论(0)
  • 2020-12-04 13:11

    If you can't know the width in advance, then your only possible answer would depend on staging your output in a temporary buffer of some kind. For small reports, just collecting the data and deferring output until the input is bounded would be simplest.

    For large reports, an intermediate file may be required if the collected data exceeds reasonable memory bounds.

    Once you have the data, then it is simple to post-process it into a report using the idiom printf("%*d", width, value) for each value.

    Alternatively if the output channel permits random access, you could just go ahead and write a draft of the report that assumes a (short) default width, and seek back and edit it any time your width assumption is violated. This also assumes that you can pad the report lines outside that field in some innocuous way, or that you are willing to replace the output so far by a read-modify-write process and abandon the draft file.

    But unless you can predict the correct width in advance, it will not be possible to do what you want without some form of two-pass algorithm.

    0 讨论(0)
  • 2020-12-04 13:15

    Looking at the edited question, you need to find the number of digits in the largest number to be presented, and then generate the printf() format using sprintf(), or using %*d with the number of digits being passed as an int for the * and then the value. Once you've got the biggest number (and you have to determine that in advance), you can determine the number of digits with an 'integer logarithm' algorithm (how many times can you divide by 10 before you get to zero), or by using snprintf() with the buffer length of zero, the format %d and null for the string; the return value tells you how many characters would have been formatted.

    If you don't know and cannot determine the maximum number ahead of its appearance, you are snookered - there is nothing you can do.

    0 讨论(0)
  • 2020-12-04 13:15
    fp = fopen("RKdata.dat","w");
    fprintf(fp,"%5s %12s %20s %14s %15s %15s %15s\n","j","x","integrated","bessj2","bessj3","bessj4","bessj5");
    for (j=1;j<=NSTEP;j+=1)
        fprintf(fp,"%5i\t %12.4f\t %14.6f\t %14.6f\t %14.6f\t %14.6f\t %14.6f\n",
        j,xx[j],y[6][j],bessj(2,xx[j]),bessj(3,xx[j]),bessj(4,xx[j]),bessj(5,xx[j]));
    fclose(fp);
    
    0 讨论(0)
提交回复
热议问题