gpt4 book ai didi

c - 需要有关在 printf 转换规范中设置字段宽度的指示

转载 作者:行者123 更新时间:2023-11-30 18:51:57 25 4
gpt4 key购买 nike

这是一个非常基本的事情,但我需要有关如何设置字段宽度的指导,以便表格对于所有允许的值都可以很好且整齐地打印出来。我已经玩了一段时间并且遇到了麻烦。是否有一些技巧可以快速弄清楚?只要朝着正确的方向插入,我们将不胜感激。

 //Print the results in a table
printf("\n#============================================#\n");
printf("| Description | Input Data |\n");
printf("|============================================|\n");
printf("| Loan amount | $ %8.2f |\n", loan);
printf("| Yearly interest rate | %6.2f% |\n", rate);
printf("| Number of years | %d |\n", years);
printf("|============================================|\n");
printf("| Payment Details | Results |\n");
printf("|============================================|\n");
printf("| The monthly payment will be | $ %5.2f |\n", monthlyPayment);
printf("| Interest paid | $ %6.2f |\n", interestEarned);
printf("#============================================#\n\n");

最佳答案

格式化数据更接近艺术而不是科学。

  1. 考虑到字段宽度会在项目的生命周期中发生变化(更有可能增长)。

  2. 发布代码以便于维护。第一次永远不会正确。性能并不那么重要,因为输出无论如何都是 CPU 周期的一个坑。

  3. 重复使用格式。

  4. 避免使用字符串文字的 fprintf(),而是使用 fputs()

  5. fprintf() 上的 RTFM,让最小宽度说明符成为您的 friend 。

  6. 注意格式中的单独“%”。应为"%%"

第一次尝试:

  double loan, rate,  monthlyPayment, interestEarned;
loan = interestEarned = monthlyPayment = 12345.78;
rate = 999.99;
int years = 30;

// Let us assume interesting part of the table is 10 wide

// 10 =
// .max_width
const char *fdash = "%s%.10s%s";

// space, 8 char, space
// - left justify
// min_width.max_width
const char *ftext = "%s %-8.8s %s";

// $, 8 width with 2 dec. places, space
// width.precision
const char *fcash = "%s$%8.2f %s";

// space, 7 width, %, space
// width.precision
const char *fperc = "%s %7.2f%% %s";

// space, 8 wide, space
// width
const char *fyear = "%s %8d %s";
const char *bar = "=================================="; // extra long

fputs("\n", stdout);
printf(fdash, "#==============================", bar , "#\n");
printf(ftext, "| Description |", "Input Data" , "|\n");
printf(fdash, "|==============================", bar , "|\n");
printf(fcash, "| Loan amount |", loan , "|\n");
printf(fperc, "| Yearly interest rate |", rate , "|\n");
printf(fyear, "| Number of years |", years , "|\n");
printf(fdash, "|==============================", bar , "|\n");
printf(ftext, "| Payment Details |", "Results" , "|\n");
printf(fdash, "|==============================", bar , "|\n");
printf(fcash, "| The monthly payment will be |", monthlyPayment, "|\n");
printf(fcash, "| Interest paid |", interestEarned, "|\n");
printf(fdash, "#==============================", bar , "#\n");
fputs("\n", stdout);

输出:对于“输入数据”来说字段太小。为用户修复左侧。

#========================================#
| Description | Input Da |
|========================================|
| Loan amount |$12345.78 |
| Yearly interest rate | 999.99% |
| Number of years | 30 |
|========================================|
| Payment Details | Results |
|========================================|
| The monthly payment will be |$12345.78 |
| Interest paid |$12345.78 |
#========================================#

关于c - 需要有关在 printf 转换规范中设置字段宽度的指示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35515684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com