|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #define initial_balance 100.0
- #define N 5
- #define initial_profit 6
- int main(void)
- {
- float a[N];
- for (int i=0; i<N; i++){
- a[i] = initial_balance;
- }
- printf("Years");
- for (int i=0; i<N; i++){
- printf("\t%3d%% ", initial_profit + i);
- }
- printf("\n");
- for (int i=0; i<N; i++){
- for (int n=0, profit = initial_profit; n<N; n++){
- printf("%d", i+1);
- printf("\t%7.2f", a[n] += (a[n] * profit / 100));
- profit++;
- }
- printf("\n");
- }
- return 0;
- }
复制代码
运行结果如图所示,我明明用了%7.2f设置了只保留两位小数, 为什么还会出现三位??
本帖最后由 人造人 于 2021-9-8 14:42 编辑
但是 %d 配 %\t7.2f 没问题
- $ cat main.c
- #include <stdio.h>
- int main(void)
- {
- printf("%d", 1234);
- printf("\t%7.2f", (float)123.456);
- return 0;
- }
- $ gcc -g -Wall -o main main.c
- $ ./main
- 1234 123.46$
复制代码
"\t%3d%% 配 %d 配 %\t7.2f 也没问题
- $ cat main.c
- #include <stdio.h>
- int main(void)
- {
- printf("\t%3d%% ", 1234);
- printf("%d", 1234);
- printf("\t%7.2f", (float)123.456);
- return 0;
- }
- $ gcc -g -Wall -o main main.c
- $ ./main
- 1234% 1234 123.46$
复制代码
复制一行 printf("\t%7.2f", a[n] += (a[n] * profit / 100));
第二行显示错误
- $ cat main.c
- #include <stdio.h>
- #define initial_balance 100.0
- #define N 5
- #define initial_profit 6
- int main(void)
- {
- float a[N];
- for (int i=0; i<N; i++){
- a[i] = initial_balance;
- }
- printf("Years");
- for (int i=0; i<N; i++){
- printf("\t%3d%% ", initial_profit + i);
- }
- printf("\n");
- for (int i=0; i<N; i++){
- for (int n=0, profit = initial_profit; n<N; n++){
- printf("%d", i+1);
- printf("\t%7.2f", a[n] += (a[n] * profit / 100));
- printf("\t%7.2f", a[n] += (a[n] * profit / 100));
- profit++;
- }
- printf("\n");
- }
- return 0;
- }
- $ gcc -g -Wall -o main main.c
- $ ./main
- Years 6% 7% 8% 9% 10%
- 1 106.00 112.361 107.00 114.491 108.00 116.641 109.00 118.811 110.00 121.00
- 2 119.10 126.252 122.50 131.082 125.97 136.052 129.50 141.162 133.10 146.41
- 3 133.82 141.853 140.26 150.073 146.93 158.693 153.86 167.713 161.05 177.16
- 4 150.36 159.384 160.58 171.824 171.38 185.094 182.80 199.264 194.87 214.36
- 5 168.95 179.085 183.85 196.725 199.90 215.895 217.19 236.745 235.79 259.37
- $
复制代码
再复制一行
第三行不对
- $ cat main.c
- #include <stdio.h>
- #define initial_balance 100.0
- #define N 5
- #define initial_profit 6
- int main(void)
- {
- float a[N];
- for (int i=0; i<N; i++){
- a[i] = initial_balance;
- }
- printf("Years");
- for (int i=0; i<N; i++){
- printf("\t%3d%% ", initial_profit + i);
- }
- printf("\n");
- for (int i=0; i<N; i++){
- for (int n=0, profit = initial_profit; n<N; n++){
- printf("%d", i+1);
- printf("\t%7.2f", a[n] += (a[n] * profit / 100));
- printf("\t%7.2f", a[n] += (a[n] * profit / 100));
- printf("\t%7.2f", a[n] += (a[n] * profit / 100));
- profit++;
- }
- printf("\n");
- }
- return 0;
- }
- $ gcc -g -Wall -o main main.c
- $ ./main
- Years 6% 7% 8% 9% 10%
- 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
- 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
- 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
- 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
- 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
- $
复制代码
我认为这是 bug,我无法解释这一现象
|
-
|