如何正确格式化该程序的输出?

时间:2014-01-31 02:54:02

标签: c formatting hex output

我创建了一个程序,手动将基数为10的数字转换为任何其他基数。我认为重要的事情是有效的,但有一些我无法解决的问题。

我遇到的一个问题是每行最右侧括号中的余数的格式不正确排列。我意识到这不是一件必要的事情,但是我的教授希望输出能够以这种方式输出,如果不是这样的话就会取消分数。的固定

我遇到的一个更大的问题是输出的最后一行(十六进制数)是正确的,但有时会有一个杂散的ASCII字符或0x和十六进制之间的几个空格。

以下是错误输出 UPDATE

的示例
Marcus Lorenzana
8526495043095935640 = 532905940193495977 * 16 + 8 (8)
 532905940193495977 = 33306621262093498 * 16 + 9  (9)
  33306621262093498 = 2081663828880843 * 16 + 10  (A)
   2081663828880843 = 130103989305052 * 16 + 11   (B)
    130103989305052 = 8131499331565 * 16 + 12     (C)
      8131499331565 = 508218708222 * 16 + 13      (D)
       508218708222 = 31763669263 * 16 + 14       (E)
        31763669263 = 1985229328 * 16 + 15        (F)
         1985229328 = 124076833 * 16 + 0          (0)
          124076833 = 7754802 * 16 + 1            (1)
            7754802 = 484675 * 16 + 2             (2)
             484675 = 30292 * 16 + 3              (3)
              30292 = 1893 * 16 + 4               (4)
               1893 = 118 * 16 + 5                (5)
                118 = 7 * 16 + 6                  (6)
                  7 = 0 * 16 + 7                  (7)
����76543210FEDCBA98

看起来应该是这样的:

Marcus Lorenzana
2147483647 = 134217727 * 16 + 15  (F)
 134217727 = 8388607 * 16 + 15    (F)
   8388607 = 524287 * 16 + 15     (F)
    524287 = 32767 * 16 + 15      (F)
     32767 = 2047 * 16 + 15       (F)
      2047 = 127 * 16 + 15        (F)
       127 = 7 * 16 + 15          (F)
         7 = 0 * 16 + 7           (7)
0x7FFFFFFF

这是我的计划更新

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

#define BUFFER 50
static const char hexstr[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main(int argc, char * argv[]) {
   if (argc == 3) {
      int base = atoi(argv[1]);
      int i = 0;
      long oldresult;
      int remainder;
      char remainders[BUFFER];
      char w_num[BUFFER];

      long value = atol(argv[2]);
      int width = sprintf(w_num,"%ld",value);  

      printf("Marcus Lorenzana\n");

      while(value != 0) {
         oldresult=value;
         remainder = value%base;
         value = value/base;
         remainders[i]=hexstr[remainder];

            char line[BUFFER];

         int w = sprintf(line,"%*ld = %ld * %d + %d", \
            width,oldresult,value,base,remainder);
         printf("%s%*s(%c)\n",line,50-w,"",hexstr[remainder]);
         i++;
      }

      int x = strlen(remainders);
      while(x > 0) {
         printf("%c",remainders[--x]);
      }
      printf("\n");
      } else {
         printf("Error: Wrong arguments\n");
      }
   return 0;
}

1 个答案:

答案 0 :(得分:0)

使用printf()的返回值来确定在余数之前打印的空格数。

int w = printf("%*lld = %lld * %d + %d",width,oldresult,value,base,remainder);
if (remainder > 9) {
  remainders[i]=hexstr[remainder-10];
  // Here and in the else clause
  printf("%*s(%c)\n", 34-w, "", hexstr[remainder-10]);  
}

第2期:更改x的测试和减少方式。看不到"0x"发生的位置。

while(x > 0) {
  printf("%c",remainders[--x]);
}

注意:似乎static const char hexstr[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";会简化事情。


[编辑]

第二个问题的简单修复因为remainders永远不会\0终止。

// int x = strlen(remainders);
int x = i;