如何用C语言制作条形图?

发布于 2024-08-27 17:13:55 字数 665 浏览 9 评论 0原文

我编写了一个程序,以与输入相同的顺序打印出整数,每行 5 个数字。即第一行打印前5个整数;下一行接下来的 5 个整数;等等。我还尝试以条形图格式打印数字,例如

81-105 ( 1) x
56-80 ( 5) xxxxx
6-11(5) xxxxx
-1-5 (3) xxx

我的程序:

cntr=0;
 while (fscanf(in, "%d", &a[i]) != EOF)
   {i++;

 fprintf(out, "%d-%d (%d) %s\n", A, B, k, x, cntr);
 fprintf(out, "%d\n", k, cntr);

    fprintf(out, "x", a[i]);
    i++;
   }

   fprintf(out, "1864-2336 (%d)%s\n", k, x);
   fprintf(out, "1391-1863 (%d)%s\n", k, x);
   fprintf(out, "918-1390 (%d)%s\n", k, x);
   fprintf(out, "445-917 (%d)%s\n", k, x);
   fprintf(out,"-28-444 (%d)%s\n", k, x);
    fclose(in);
    fclose(out);
return 0;
}

I have written a program that prints out the integers in the same order as the input with 5 numbers per line. That is, the first 5 integers will be printed in the first line; the next 5 integers in the next line; and so on. I was also was trying to print out the numbers in a bar chart format, like,

81-105 ( 1) x
56-80 ( 5) xxxxx
6-11(5) xxxxx
-1-5 (3) xxx

My program:

cntr=0;
 while (fscanf(in, "%d", &a[i]) != EOF)
   {i++;

 fprintf(out, "%d-%d (%d) %s\n", A, B, k, x, cntr);
 fprintf(out, "%d\n", k, cntr);

    fprintf(out, "x", a[i]);
    i++;
   }

   fprintf(out, "1864-2336 (%d)%s\n", k, x);
   fprintf(out, "1391-1863 (%d)%s\n", k, x);
   fprintf(out, "918-1390 (%d)%s\n", k, x);
   fprintf(out, "445-917 (%d)%s\n", k, x);
   fprintf(out,"-28-444 (%d)%s\n", k, x);
    fclose(in);
    fclose(out);
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末染殇 2024-09-03 17:13:55

您不必使用循环来打印 x。您可以声明一个与您需要的最大值一样长的 x 字符串,然后在 printf 调用中使用大小规范来控制打印的数量。下面是一个简单的例子来说明我的意思:

#include <stdio.h>
#include <string.h>

int main(void)
{
  char maxBar[] = "xxxxxxxxxx";
  size_t i;

  for (i = 0; i < strlen(maxBar); i++)
  {
    printf("%lu: %-*.*s\n", (unsigned long) i, strlen(maxBar), i, maxBar); 
  }
  return 0;
}

输出应如下所示:

0: __________          
1: x_________
2: xx________
3: xxx_______
4: xxxx______
5: xxxxx_____

等等,其中 _ 表示空格字符。

printf 转换说明符中的 * 表示字段宽度或精度规范作为参数传递给 printf。书写

printf("%-*.*s\n", 10, 2, "test");

与书写相同

printf("%-10.2s\n", "test");

,从左到右阅读,表示

  1. -:左对齐输出
  2. 10:最小字段宽度为 10 个字符
  3. .2:精度(打印的最大字符数)为 2

因此输出看起来像

te________

其中 _ 表示空格字符。

因此,假设您提前知道您的栏需要有多大,您可以编写类似的内容

for (i = 0; i < k; i++)
{
  printf("%d-%d (%d) %*.*s\n", lo[k], hi[k], ct[k], strlen(maxBar), ct[k], maxBar);
}

You don't have to use a loop to print out your x's. You can declare a single string of x's that's as long as the maximum you will need, and then use a size specification in the printf call to control how many are printed. Here's a simple example to illustrate what I mean:

#include <stdio.h>
#include <string.h>

int main(void)
{
  char maxBar[] = "xxxxxxxxxx";
  size_t i;

  for (i = 0; i < strlen(maxBar); i++)
  {
    printf("%lu: %-*.*s\n", (unsigned long) i, strlen(maxBar), i, maxBar); 
  }
  return 0;
}

The output should look as follows:

0: __________          
1: x_________
2: xx________
3: xxx_______
4: xxxx______
5: xxxxx_____

etc., where _ represents a space character.

A * in a printf conversion specifier indicates that a field width or precision specification is being passed as an argument to printf. Writing

printf("%-*.*s\n", 10, 2, "test");

is the same as writing

printf("%-10.2s\n", "test");

which, reading left to right, means

  1. -: Left-justify the output
  2. 10: Minimum field width is 10 characters
  3. .2: Precision (max number of chars printed) is 2

So the output looks like

te________

where _ represents a space character.

So, assuming you know how big your bar needs to be in advance, you can write something like

for (i = 0; i < k; i++)
{
  printf("%d-%d (%d) %*.*s\n", lo[k], hi[k], ct[k], strlen(maxBar), ct[k], maxBar);
}
赴月观长安 2024-09-03 17:13:55

只需执行一个循环即可打印您需要的 x 数量

   fprintf(out, "1864-2336 (%d)%s\n", k, x);
   for(x=k;x>0;x--)
      fprint(out,"%s","x");
   fprintf(out,"\n");

干杯!

/B2S

just do a loop to print the amount of x's you need

   fprintf(out, "1864-2336 (%d)%s\n", k, x);
   for(x=k;x>0;x--)
      fprint(out,"%s","x");
   fprintf(out,"\n");

Cheers!

/B2S

晌融 2024-09-03 17:13:55

有很多方法可以做到这一点

似乎您已经在某处声明了用户输入的值的数组。 a[]

为了计算条形图,您可以在用户输入所有值后,对该数组进行排序(例如使用 qsort()),然后遍历数组检查数组中出现的相同值的数量 - 这将是数字'*' 打印出来

我认为你应该首先让用户输入值,然后进行输出。

您显示的代码看起来错误,例如您在 while 循环中增加“i”两次。您在增量之后使用 a[i],因此它不会具有读取值。

另外,在读取数字时最好使用 fgets/atoi 来代替,这样无效的数字不会导致程序潜在崩溃。

There are many ways to do this

It seems you already have somewhere declared an array of the values that user enters. a[]

In order to calculate the bar chart you could after user has entered all values, sort that array (e.g. using qsort()) then traverse the array checking the number of same values that occur in the array - this would be the number of '*' to print out

I think you should first let user enter the values, then do the output.

The code you show looks wrong, e.g. you increment 'i' twice in the while loop. You use the a[i] after the increment so it will not have the read value.

Also its good to use fgets/atoi instead when reading numbers that way invalid numbers will not cause your program to potentially crash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文