我相信我可能会对此过于复杂,并在此处搜索了许多解决方案。
这是 C代码,用于以双数字形式将输入流写入“ 10 1 666 10000 666 77 88 3 5 9”等默认值为 13个小数位的 (精确)和默认的 3列(列),因此输出实际上是
10.0000000000000 1.0000000000000 666.0000000000000
10000.0000000000000 666.0000000000000 77.0000000000000
88.0000000000000 3.0000000000000 5.0000000000000
9.0000000000000
3列。 (所有输入的数字必须为 10,000或更少)。
我基本上需要我的输出数据看起来像这样(每列后最多2个空格):
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,那么我可以动态填充每个数字吗?
请让我知道我是否需要更具体。
答案 0 :(得分: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的值,并且每列中还有一个“小数点前的一位数字”的值-在默认的三列布局中) :
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
$
答案 1 :(得分: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
来实现的,
真的很棒