鱼C论坛

 找回密码
 立即注册
查看: 803|回复: 3

[已解决]格式化输出的问题

[复制链接]
发表于 2021-9-8 13:13:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #define initial_balance 100.0
  3. #define N 5
  4. #define initial_profit 6

  5. int main(void)
  6. {
  7.         float a[N];

  8.         for (int i=0; i<N; i++){
  9.                 a[i] = initial_balance;
  10.         }

  11.         printf("Years");
  12.         for (int i=0; i<N; i++){
  13.                 printf("\t%3d%%      ", initial_profit + i);
  14.         }

  15.         printf("\n");
  16.         for (int i=0; i<N; i++){
  17.                 for (int n=0, profit = initial_profit; n<N; n++){
  18.                         printf("%d", i+1);
  19.                         printf("\t%7.2f", a[n] += (a[n] * profit / 100));
  20.                         profit++;
  21.                 }       
  22.                 printf("\n");
  23.         }
  24.         return 0;
  25. }
复制代码


运行结果如图所示,我明明用了%7.2f设置了只保留两位小数, 为什么还会出现三位??
最佳答案
2021-9-8 14:41:28
本帖最后由 人造人 于 2021-9-8 14:42 编辑

但是 %d 配 %\t7.2f 没问题
  1. $ cat main.c
  2. #include <stdio.h>

  3. int main(void)
  4. {
  5.     printf("%d", 1234);
  6.     printf("\t%7.2f", (float)123.456);
  7.     return 0;
  8. }
  9. $ gcc -g -Wall -o main main.c
  10. $ ./main
  11. 1234     123.46$
复制代码

"\t%3d%%      配 %d 配 %\t7.2f 也没问题
  1. $ cat main.c
  2. #include <stdio.h>

  3. int main(void)
  4. {
  5.     printf("\t%3d%%      ", 1234);
  6.     printf("%d", 1234);
  7.     printf("\t%7.2f", (float)123.456);
  8.     return 0;
  9. }
  10. $ gcc -g -Wall -o main main.c
  11. $ ./main
  12.         1234%      1234  123.46$
复制代码




复制一行 printf("\t%7.2f", a[n] += (a[n] * profit / 100));
第二行显示错误
  1. $ cat main.c
  2. #include <stdio.h>
  3. #define initial_balance 100.0
  4. #define N 5
  5. #define initial_profit 6

  6. int main(void)
  7. {
  8.         float a[N];

  9.         for (int i=0; i<N; i++){
  10.                 a[i] = initial_balance;
  11.         }

  12.         printf("Years");
  13.         for (int i=0; i<N; i++){
  14.                 printf("\t%3d%%      ", initial_profit + i);
  15.         }

  16.         printf("\n");
  17.         for (int i=0; i<N; i++){
  18.                 for (int n=0, profit = initial_profit; n<N; n++){
  19.                         printf("%d", i+1);
  20.                         printf("\t%7.2f", a[n] += (a[n] * profit / 100));
  21.                         printf("\t%7.2f", a[n] += (a[n] * profit / 100));
  22.                         profit++;
  23.                 }
  24.                 printf("\n");
  25.         }
  26.         return 0;
  27. }
  28. $ gcc -g -Wall -o main main.c
  29. $ ./main
  30. Years     6%              7%              8%              9%             10%
  31. 1        106.00  112.361         107.00  114.491         108.00  116.641         109.00  118.811         110.00  121.00
  32. 2        119.10  126.252         122.50  131.082         125.97  136.052         129.50  141.162         133.10  146.41
  33. 3        133.82  141.853         140.26  150.073         146.93  158.693         153.86  167.713         161.05  177.16
  34. 4        150.36  159.384         160.58  171.824         171.38  185.094         182.80  199.264         194.87  214.36
  35. 5        168.95  179.085         183.85  196.725         199.90  215.895         217.19  236.745         235.79  259.37
  36. $
复制代码


再复制一行
第三行不对
  1. $ cat main.c
  2. #include <stdio.h>
  3. #define initial_balance 100.0
  4. #define N 5
  5. #define initial_profit 6

  6. int main(void)
  7. {
  8.         float a[N];

  9.         for (int i=0; i<N; i++){
  10.                 a[i] = initial_balance;
  11.         }

  12.         printf("Years");
  13.         for (int i=0; i<N; i++){
  14.                 printf("\t%3d%%      ", initial_profit + i);
  15.         }

  16.         printf("\n");
  17.         for (int i=0; i<N; i++){
  18.                 for (int n=0, profit = initial_profit; n<N; n++){
  19.                         printf("%d", i+1);
  20.                         printf("\t%7.2f", a[n] += (a[n] * profit / 100));
  21.                         printf("\t%7.2f", a[n] += (a[n] * profit / 100));
  22.                         printf("\t%7.2f", a[n] += (a[n] * profit / 100));
  23.                         profit++;
  24.                 }
  25.                 printf("\n");
  26.         }
  27.         return 0;
  28. }
  29. $ gcc -g -Wall -o main main.c
  30. $ ./main
  31. Years     6%              7%              8%              9%             10%
  32. 1        106.00  112.36  119.101         107.00  114.49  122.501         108.00  116.64  125.971         109.00  118.81  129.501         110.00  121.00        133.10
  33. 2        126.25  133.82  141.852         131.08  140.26  150.072         136.05  146.93  158.692         141.16  153.86  167.712         146.41  161.05        177.16
  34. 3        150.36  159.38  168.953         160.58  171.82  183.853         171.38  185.09  199.903         182.80  199.26  217.193         194.87  214.36        235.79
  35. 4        179.08  189.83  201.224         196.72  210.49  225.224         215.89  233.16  251.824         236.74  258.04  281.274         259.37  285.31        313.84
  36. 5        213.29  226.09  239.665         240.98  257.85  275.905         271.96  293.72  317.225         306.58  334.17  364.255         345.23  379.75        417.72
  37. $
复制代码



我认为这是 bug,我无法解释这一现象


屏幕截图 2021-09-08 131209.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-8 14:09:19 | 显示全部楼层
7.2
占7个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-8 14:33:00 | 显示全部楼层
这应该是 bug 吧,我无法解释这一现象
对于这个程序,去掉前面的 printf("%d", i+1);,输出就正常了

  1. $ cat main.c
  2. #include <stdio.h>
  3. #define initial_balance 100.0
  4. #define N 5
  5. #define initial_profit 6

  6. int main(void)
  7. {
  8.     float a[N];

  9.     for (int i=0; i<N; i++){
  10.         a[i] = initial_balance;
  11.     }

  12.     printf("Years");
  13.     for (int i=0; i<N; i++){
  14.         printf("\t%3d%%      ", initial_profit + i);
  15.     }

  16.     printf("\n");
  17.     for (int i=0; i<N; i++){
  18.         for (int n=0, profit = initial_profit; n<N; n++){
  19.             printf("%d", i+1);
  20.             printf("\t%7.2f", a[n] += (a[n] * profit / 100));
  21.             profit++;
  22.         }
  23.         printf("\n");
  24.     }
  25.     return 0;
  26. }
  27. $ gcc -g -Wall -o main main.c
  28. $ ./main
  29. Years     6%              7%              8%              9%             10%
  30. 1        106.001         107.001         108.001         109.001         110.00
  31. 2        112.362         114.492         116.642         118.812         121.00
  32. 3        119.103         122.503         125.973         129.503         133.10
  33. 4        126.254         131.084         136.054         141.164         146.41
  34. 5        133.825         140.265         146.935         153.865         161.05
  35. $




  36. $ cat main.c
  37. #include <stdio.h>
  38. #define initial_balance 100.0
  39. #define N 5
  40. #define initial_profit 6

  41. int main(void)
  42. {
  43.     float a[N];

  44.     for (int i=0; i<N; i++){
  45.         a[i] = initial_balance;
  46.     }

  47.     printf("Years");
  48.     for (int i=0; i<N; i++){
  49.         printf("\t%3d%%      ", initial_profit + i);
  50.     }

  51.     printf("\n");
  52.     for (int i=0; i<N; i++){
  53.         for (int n=0, profit = initial_profit; n<N; n++){
  54.             //printf("%d", i+1);
  55.             printf("\t%7.2f", a[n] += (a[n] * profit / 100));
  56.             profit++;
  57.         }
  58.         printf("\n");
  59.     }
  60.     return 0;
  61. }
  62. $ gcc -g -Wall -o main main.c
  63. $ ./main
  64. Years     6%              7%              8%              9%             10%
  65.          106.00  107.00  108.00  109.00  110.00
  66.          112.36  114.49  116.64  118.81  121.00
  67.          119.10  122.50  125.97  129.50  133.10
  68.          126.25  131.08  136.05  141.16  146.41
  69.          133.82  140.26  146.93  153.86  161.05
  70. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-8 14:41:28 | 显示全部楼层    本楼为最佳答案   
本帖最后由 人造人 于 2021-9-8 14:42 编辑

但是 %d 配 %\t7.2f 没问题
  1. $ cat main.c
  2. #include <stdio.h>

  3. int main(void)
  4. {
  5.     printf("%d", 1234);
  6.     printf("\t%7.2f", (float)123.456);
  7.     return 0;
  8. }
  9. $ gcc -g -Wall -o main main.c
  10. $ ./main
  11. 1234     123.46$
复制代码

"\t%3d%%      配 %d 配 %\t7.2f 也没问题
  1. $ cat main.c
  2. #include <stdio.h>

  3. int main(void)
  4. {
  5.     printf("\t%3d%%      ", 1234);
  6.     printf("%d", 1234);
  7.     printf("\t%7.2f", (float)123.456);
  8.     return 0;
  9. }
  10. $ gcc -g -Wall -o main main.c
  11. $ ./main
  12.         1234%      1234  123.46$
复制代码




复制一行 printf("\t%7.2f", a[n] += (a[n] * profit / 100));
第二行显示错误
  1. $ cat main.c
  2. #include <stdio.h>
  3. #define initial_balance 100.0
  4. #define N 5
  5. #define initial_profit 6

  6. int main(void)
  7. {
  8.         float a[N];

  9.         for (int i=0; i<N; i++){
  10.                 a[i] = initial_balance;
  11.         }

  12.         printf("Years");
  13.         for (int i=0; i<N; i++){
  14.                 printf("\t%3d%%      ", initial_profit + i);
  15.         }

  16.         printf("\n");
  17.         for (int i=0; i<N; i++){
  18.                 for (int n=0, profit = initial_profit; n<N; n++){
  19.                         printf("%d", i+1);
  20.                         printf("\t%7.2f", a[n] += (a[n] * profit / 100));
  21.                         printf("\t%7.2f", a[n] += (a[n] * profit / 100));
  22.                         profit++;
  23.                 }
  24.                 printf("\n");
  25.         }
  26.         return 0;
  27. }
  28. $ gcc -g -Wall -o main main.c
  29. $ ./main
  30. Years     6%              7%              8%              9%             10%
  31. 1        106.00  112.361         107.00  114.491         108.00  116.641         109.00  118.811         110.00  121.00
  32. 2        119.10  126.252         122.50  131.082         125.97  136.052         129.50  141.162         133.10  146.41
  33. 3        133.82  141.853         140.26  150.073         146.93  158.693         153.86  167.713         161.05  177.16
  34. 4        150.36  159.384         160.58  171.824         171.38  185.094         182.80  199.264         194.87  214.36
  35. 5        168.95  179.085         183.85  196.725         199.90  215.895         217.19  236.745         235.79  259.37
  36. $
复制代码


再复制一行
第三行不对
  1. $ cat main.c
  2. #include <stdio.h>
  3. #define initial_balance 100.0
  4. #define N 5
  5. #define initial_profit 6

  6. int main(void)
  7. {
  8.         float a[N];

  9.         for (int i=0; i<N; i++){
  10.                 a[i] = initial_balance;
  11.         }

  12.         printf("Years");
  13.         for (int i=0; i<N; i++){
  14.                 printf("\t%3d%%      ", initial_profit + i);
  15.         }

  16.         printf("\n");
  17.         for (int i=0; i<N; i++){
  18.                 for (int n=0, profit = initial_profit; n<N; n++){
  19.                         printf("%d", i+1);
  20.                         printf("\t%7.2f", a[n] += (a[n] * profit / 100));
  21.                         printf("\t%7.2f", a[n] += (a[n] * profit / 100));
  22.                         printf("\t%7.2f", a[n] += (a[n] * profit / 100));
  23.                         profit++;
  24.                 }
  25.                 printf("\n");
  26.         }
  27.         return 0;
  28. }
  29. $ gcc -g -Wall -o main main.c
  30. $ ./main
  31. Years     6%              7%              8%              9%             10%
  32. 1        106.00  112.36  119.101         107.00  114.49  122.501         108.00  116.64  125.971         109.00  118.81  129.501         110.00  121.00        133.10
  33. 2        126.25  133.82  141.852         131.08  140.26  150.072         136.05  146.93  158.692         141.16  153.86  167.712         146.41  161.05        177.16
  34. 3        150.36  159.38  168.953         160.58  171.82  183.853         171.38  185.09  199.903         182.80  199.26  217.193         194.87  214.36        235.79
  35. 4        179.08  189.83  201.224         196.72  210.49  225.224         215.89  233.16  251.824         236.74  258.04  281.274         259.37  285.31        313.84
  36. 5        213.29  226.09  239.665         240.98  257.85  275.905         271.96  293.72  317.225         306.58  334.17  364.255         345.23  379.75        417.72
  37. $
复制代码



我认为这是 bug,我无法解释这一现象


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 15:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表