如何正确格式化输出?

时间:2014-02-03 20:05:27

标签: c formatting hex printf output

我似乎无法让我的输出正常工作。我想这样做,使括号中的最后一个余数在第一行间距后的一列中完美排列。除了最后一部分之外,我的大部分输出都是正确的,它必须与我教授想要的方式完全一致。

我能想到的最好的是 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;
}

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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

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

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

rhs的宽度是根据数字的初始长度计算的,vwidth加上两倍的数字加上两倍于具有周围空格的操作数的三倍。宽度必须为负数,因为您希望RHS左对齐并向右传递空格。

如果您让printf执行填充,则无需存储sprintf来电w中的字符串长度。