每一列后的C对齐)W /正好2空间列瓦特/ printf的(

问题描述 投票:3回答:2

我相信我可能是过于复杂这一点,并在这里寻找多种解决方案。

这是C代码编写在双数的命令行的输入流,如“10 1 666 10000 666 77 88 3 5 9”使用默认13小数位(PREC)和默认3列(COLS),这样,输出实际上是

    10.0000000000000 1.0000000000000 666.0000000000000
 10000.0000000000000 666.0000000000000 77.0000000000000
    88.0000000000000 3.0000000000000 5.0000000000000
     9.0000000000000

在3列。 (所有数字输入必须是1万以下)。

基本上,我需要我的输出数据,看起来像这样(2位最多每列后):

enter image description here

5163.1575670644243  6806.8180791650138  8977.2739646595655
2598.0792870876185  7162.5237586596268  6691.2041993469038
1043.6422009949033  6922.8216193121125     3.0480056154058
9926.6081118198181   100.3082369457076  5135.1567125461588
7808.2382885219886  1439.6542863246559   249.6179692983795
 214.0065309610279  9280.5883968626968  2687.3871883297220
7612.8426770836513  6644.2027649769589  8364.5604419080173
4740.7550279244360   254.6181218909269  2500.3814813684498
2293.6803491317482   835.3306680501725  5962.7923215430155
9622.5988341929387    57.3069246498001  1557.9630726035341
8398.5614795373385  5958.4870143742182  2568.3835566270945
9935.9135715811644  3410.1040681173131   982.0299691763055
8393.5613269447913  9066.2766808069100  4896.4546037171549
7597.8422193060087  8551.5661488692895  1076.6432081057162
1911.3635059663686  7586.8418836024048  9282.8936429944770
4696.1433149204995  1388.0423596911528  1936.3642689291055
3408.4091921750542  3556.4137089144565  9241.8923917355878
5003.4578691976685  3366.7130954924160  4270.1303140354621
 620.6292916653950  4700.7538071840572  1766.0538956877347
 441.6238288521989  8153.8591875972779

这里是我的代码。你可以简单地通过GCC编译.c文件并运行.exe和输入#的与之间的空白将它输出(格式化):

#include <stdio.h>   // scanf(), fscanf(), fprintf(), fopen()
#include <stdlib.h>  // atoi(), getenv()

int main(int argc, char *argv[])
{
   int cols = 3; // default value for columns is 3.
   int prec = 13; // default value for precision is 13 decimal places.

   int count =  0; // keep track of length of input stream, i.e. "10 55 13 666 77" has length 5.
   double x;

   // Get a command line argument (if it exists) to set precision. If it's not there, do nothing and just use default precision (13)
   if (argc > 1)
   {  // get an operand from the command line
      prec = atoi(argv[1]);
      cols = atoi(argv[2]);
   }

   // User gets prompted to enter input
   printf("Enter input stream numbers: \n");

   // While loop spits output and formats it
   while (scanf("%lf", &x) != EOF)
   {
   // Asterisk(*) keeps precision for dynamic. Default value for precision is 13.


      printf("%19.*f", prec, x);
      count++;

   // When count == cols, \n is output to format columns
   if (count == cols) 
   {
      printf("\n");
      count = 0;
   }

   }
      return 0;
}

我现在有列的宽度定为19岁,但直觉告诉我,这需要有一个星号*,因此它可以是动态的。只要我有不同的大小#的输入,他们不每一列后留2位一致。

我想我应该重新分配我的双X;这是我的输入,也许一个char X []数组,但我想如果我问有可能是一个更容易实现。我在它是与每一列#的前导空格预感。

由于我使用的scanf(),有什么办法来算我的#的的每个字符?像,如果我的第一个数字是10.0000000000000,有一个方法来计算每一个位置,所以可总量可达(在这种情况下)一个int值= 15,这样,我可以垫动态每个号码?

请让我知道,如果我需要更加具体。

c printf scanf
2个回答
3
投票

做到这一点的方法之一是,他们在一个阵列到达保存价值,并跟踪最大的价值在每列中,然后格式化该值,找出它有多长,而从这个推断值来指定。

下面是一些代码,做的是:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int cols = 3;
    int prec = 13;

    if (argc > 3)
    {
        fprintf(stderr, "Usage: %s [precision [columns]]\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    if (argc > 1)
        prec = atoi(argv[1]);
    if (argc > 2)
        cols = atoi(argv[2]);
    double max[cols];
    for (int i = 0; i < cols; i++)
        max[i] = 0.0;

    enum { MAX_DATA = 100 };
    double data[MAX_DATA];
    int count =  0;
    double x;
    while (scanf("%lf", &x) == 1 && count < MAX_DATA)
    {
        int colnum = count % cols;
        if (x > max[colnum])
            max[colnum] = x;
        data[count++] = x;
    }

    int width[cols];
    const char *pad[cols];
    for (int i = 0; i < cols; i++)
    {
        pad[i] = (i == 0) ? "" : "  ";
        width[i] = snprintf(0, 0, "%.*f", prec, max[i]);
        if (width[i] < 4 + prec)
            width[i] = 4 + prec;
    }

    for (int i = 0; i < count; i++)
    {
        int colnum = i % cols;
        printf("%s%*.*f", pad[colnum], width[colnum], prec, data[i]);
        if (colnum == cols - 1)
            putchar('\n');
    }
    if (count % cols != 0)
        putchar('\n');

    return 0;
}

请注意,代码谨慎地拒绝了太多的参数,而不是访问不存在争论。我还除去提示;有没有办法,我用手输入的数字,当输入来自文件,提示仅仅是一个滋扰。

另外,具有零长度(和任选的空指针)使用snprintf()的是由标准作为一种方式来获得格式化串所需的空间量记载。

给定这样一些样本的数据(随机生成的,但随后操纵,使得有一个10K值中的每一列,并且也是“小数点前一位数字”中每个列的值 - 在默认3列布局):

2730.8075416749843 9153.7050562644145 8264.2778874481955
5393.9722906483921 9659.6077493184748 59.6077493184748
4973.9718947965630 3.7623787002290 5975.6392547304667 682.2153319663826
6236.5964619743863 7786.2954141327737 3.7623787002290 6735.6044984396849
1069.6226524395413 8709.7209141371932 3854.7386329491574 3.7623787002290
4960.9318291197014 40.3314639772034 9017.1314461534275
1717.9459363110184 8682.9285936347133 10000 6671.2353105858210
4119.1373095038844 70.3291668437700 4528.3226201367906
1926.8741591097082 2101.4643722293158 760.9213269470772 10000
7366.6932284462664 1287.1299466478447 3418.7415326626078
3144.9791945834349 2385.3575762876035 3779.9164071168789
9743.9571880258318 10000 7432.8398636749780 3011.9532204395937
5883.0779787486517

默认的输出是这样的:

 2730.8075416749843   9153.7050562644145   8264.2778874481955
 5393.9722906483921   9659.6077493184748     59.6077493184748
 4973.9718947965630      3.7623787002290   5975.6392547304667
  682.2153319663826   6236.5964619743863   7786.2954141327737
    3.7623787002290   6735.6044984396849   1069.6226524395413
 8709.7209141371932   3854.7386329491574      3.7623787002290
 4960.9318291197014     40.3314639772034   9017.1314461534275
 1717.9459363110184   8682.9285936347133  10000.0000000000000
 6671.2353105858210   4119.1373095038844     70.3291668437700
 4528.3226201367906   1926.8741591097082   2101.4643722293158
  760.9213269470772  10000.0000000000000   7366.6932284462664
 1287.1299466478447   3418.7415326626078   3144.9791945834349
 2385.3575762876035   3779.9164071168789   9743.9571880258318
10000.0000000000000   7432.8398636749780   3011.9532204395937
 5883.0779787486517

或用6位小数和6列,它看起来像这样:

$ ./fmt53 6 6 < data
2730.807542   9153.705056  8264.277887   5393.972291  9659.607749     59.607749
4973.971895      3.762379  5975.639255    682.215332  6236.596462   7786.295414
   3.762379   6735.604498  1069.622652   8709.720914  3854.738633      3.762379
4960.931829     40.331464  9017.131446   1717.945936  8682.928594  10000.000000
6671.235311   4119.137310    70.329167   4528.322620  1926.874159   2101.464372
 760.921327  10000.000000  7366.693228   1287.129947  3418.741533   3144.979195
2385.357576   3779.916407  9743.957188  10000.000000  7432.839864   3011.953220
5883.077979
$

或5个小数位和第7列:

$ ./fmt53 5 7 < data
2730.80754  9153.70506   8264.27789   5393.97229   9659.60775    59.60775  4973.97189
   3.76238  5975.63925    682.21533   6236.59646   7786.29541     3.76238  6735.60450
1069.62265  8709.72091   3854.73863      3.76238   4960.93183    40.33146  9017.13145
1717.94594  8682.92859  10000.00000   6671.23531   4119.13731    70.32917  4528.32262
1926.87416  2101.46437    760.92133  10000.00000   7366.69323  1287.12995  3418.74153
3144.97919  2385.35758   3779.91641   9743.95719  10000.00000  7432.83986  3011.95322
5883.07798
$

0
投票

第一把一切都在阵列中找到的整数部分的最大长度,计算field_width关闭的这一点,精度。

#include <stdio.h>
#include <stdlib.h>
#include <float.h>

#define MAX_NUM 128

int main(int argc, char *argv[])
{
    int cols = 3; // default value for columns is 3.
    int prec = 13; // default value for precision is 13 decimal places.

    if (argc > 1) {
        prec = atoi(argv[1]);
        cols = atoi(argv[2]);
    }
    printf("Enter input stream numbers: \n");

    int count = 0;
    double val[MAX_NUM];
    double max = DBL_MIN;
    for(double x = 0; count < MAX_NUM && scanf("%lf", &x) != EOF;) {
        if(x > max)
            max = x;

        val[count++] = x;
    }

    int integer_width = snprintf(0, 0, "%d", (int)max);
    int field_width = integer_width + prec + 1;
    for(int i = 0; i < count; i++) {
        printf("%*.*f%s", field_width, prec, val[i], i % cols == 2 || i == count - 1 ? "\n" : "  ");
    }

    return 0;
}

不足之处:

  • 仅支持MAX_NUM
  • 可怕的方式来处理输入
  • integer_width与调用snprintf这是不是真的计算最优
© www.soinside.com 2019 - 2024. All rights reserved.