0

I cannot seem to get my output to work correctly. I want to make it so that the last remainder in parenthesis is lined up perfectly in a column following the first line's spacing. I have most of the output right except for this last part and it has to be exactly the way my professor wants it.

The best I've been able to come up with is 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

This is the output I want:

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

But as you can see it the output is not exactly correct.

Here is my program EDITED:

#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;
}
3
  • Rather than 50, use the first w calculated (and add 1). Commented Feb 3, 2014 at 20:16
  • How can I keep the first w calculated for the next couple of lines? Cause w changes based on the new values. Commented Feb 3, 2014 at 20:28
  • Well you could use the maximum value of w in w_max and since that occurs on the first line ... voila Commented Feb 3, 2014 at 20:33

2 Answers 2

1

Modify based on your code:

#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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Could you briefly explain this bit: wMax = w > wMax ? w:wMax;
Scratch that I think I got it. Thank you
if w > wMax then set wMax = w, otherwise set wMax = wMax itself. In a word, it alway set wMax to the "Max" width of your string from the first "sprintf". But actually the first line would alway be the longest refer to your code, so the wMax would alway be the first line's width.
0

Your idea to print the right-hand side to a temporary string is good, but for the output you want, you should just print the stuff to thr right of the equals sign to that string. Also, because you don't want the operands lined up, strip all formatting information, i.e. the widths, from the string.

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

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

The width of the rhs is calculated based on the initial length of the number, vwidth plus two times two for the numbers plus two times three for the operands with surrounding spaces. The width has to be negative, because you want the RHS to be left-aligned and passed with spaces to the right.

If you let printf do the padding, there's no need to store the string length from the sprintf call, w.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.