【问题标题】:How to format output correctly?如何正确格式化输出?
【发布时间】:2014-02-27 11:42:33
【问题描述】:

我似乎无法让我的输出正常工作。我想这样做,以便括号中的最后一个余数在第一行间距之后的一列中完美排列。除了最后一部分之外,我拥有大部分输出权利,它必须完全符合我的教授想要的方式。

我能想到的最好的是EDITED:

Marcus Lorenzana
314156 = 19634  * 16 + 12 (C)
 19634 = 1227   * 16 +  2 (2)
  1227 = 76     * 16 + 11 (B)
    76 = 4      * 16 + 12 (C)
     4 = 0      * 16 +  4 (4)
0x4CB2C

这是我想要的输出:

Marcus Lorenzana
314156 = 19634 * 16 + 12 (C)
 19634 = 1227 * 16 + 2   (2)
  1227 = 76 * 16 + 11    (B)
    76 = 4 * 16 + 12     (C)
     4 = 0 * 16 + 4      (4)
0x4CB2C

但正如您所见,输出并不完全正确。

这是我的程序已编辑:

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

#define BUFFER 50
static int base = 16; 


int main(int argc, char * argv[]) {
   printf("Marcus Lorenzana\n");
   if (argc == 2) {
      char hexstr[] = "0123456789ABCDEF";
      int i = 0;
      long long oldresult;
      int remainder;
      char remainders[BUFFER];
      char w_num[BUFFER];
      long long value = atoll(argv[1]);
      //Get width of original number for formatting purposes
      int vwidth = strlen(argv[1]);  
      char oldwidth[BUFFER];

        //Convert the decimal to hexadecimal
      while(value != 0) {
         oldresult=value;
         remainder = value%base;
         value = value/base;
         //Store the remainder in an array for later use
         remainders[i]=hexstr[remainder];
            char line[BUFFER];
         //Get length of line for formatting purposes
         int w = sprintf(line,"%*lld = %-*lld * %2d + %2d", \
            vwidth,oldresult,vwidth,value,base,remainder);


         printf("%s (%c)\n", line,hexstr[remainder]);
         i++;

      }
      //Print the the hexadecimal number
      int x = i;
      printf("0x");
      while(x > 0) {
         printf("%c",remainders[--x]);
      }
      printf("\n");
      } else {
         printf("Error: Wrong arguments\n");
         return 1; 
      }
   return 0;
}

【问题讨论】:

  • 而不是50,使用first w计算(并加1)。
  • 如何为接下来的几行计算第一个 w?导致 w 根据新值发生变化。
  • 那么你可以在w_max 中使用w 的最大值,因为这发生在第一行......瞧
  • 我明白了,谢谢 :)

标签: c formatting hex printf output


【解决方案1】:

根据你的代码修改:

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

#define BUFFER 50
static int base = 16; 


int main(int argc, char * argv[]) {
   printf("Marcus Lorenzana\n");
   if (argc == 2) {
      char hexstr[] = "0123456789ABCDEF";
      int i = 0;
      long long oldresult;
      int remainder;
      char remainders[BUFFER];
      char w_num[BUFFER];
      long long value = atoll(argv[1]);
      //Get width of original number for formatting purposes
      int vwidth = strlen(argv[1]);  
      char oldwidth[BUFFER];
      int wMax = 0;
        //Convert the decimal to hexadecimal
      while(value != 0) {
         oldresult=value;
         remainder = value%base;
         value = value/base;
         //Store the remainder in an array for later use
         remainders[i]=hexstr[remainder];
            char line[BUFFER];
         //Get length of line for formatting purposes

       int w = sprintf(line,"%*lld = %-lld * %2d + %-2d", \
            vwidth,oldresult,value,base,remainder);

        wMax = w > wMax ? w:wMax;

         printf("%s %*s(%c)\n", line,wMax-w,"",hexstr[remainder]);
         i++;

      }
      //Print the the hexadecimal number
      int x = i;
      printf("0x");
      while(x > 0) {
         printf("%c",remainders[--x]);
      }
      printf("\n");
      } else {
         printf("Error: Wrong arguments\n");
         return 1; 
      }
   return 0;
}

【讨论】:

  • 您能否简要解释一下这一点: wMax = w > wMax ? w:wMax;
  • 我想我明白了。谢谢
  • 如果 w > wMax 则设置 wMax = w,否则设置 wMax = wMax 本身。总之,它总是将 wMax 设置为从第一个“sprintf”开始的字符串的“Max”宽度。但实际上第一行总是最长的引用你的代码,所以 wMax 总是第一行的宽度。
【解决方案2】:

您将右侧打印到临时字符串的想法很好,但是对于您想要的输出,您应该将内容打印到该字符串的等号右侧。此外,由于您不希望操作数排成一行,因此请从字符串中删除所有格式信息,即宽度。

     snprintf(line, BUFFER, "%lld * %d + %d", value, base, remainder);

     printf("%*lld = %*s (%c)\n", vwidth, oldresult, 
        -(vwidth + 10), line, hexstr[remainder]);

rhs 的宽度是根据数字的初始长度计算的,vwidth 加上数字的 2 乘以 2 加上周围有空格的操作数的 2 乘以 3。宽度必须为负数,因为您希望 RHS 左对齐并在右侧传递空格。

如果您让printf 进行填充,则无需存储来自sprintf 调用w 的字符串长度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-23
    • 2020-02-22
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多