3.6.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Exercise 3-6. Write a version of itoa that accepts three arguments instead of two. The third argument is a minimum field width; the converted number must be padded with blanks on the left if necessary to make it wide enough. */
  2. //itob(n,s,b) that converts the integer n into a base b character representation in the string s
  3. //In particular, itob(n,s,16) formats s as a hexadecimal integer in s.
  4. #include <stdio.h>
  5. void itoa(int n, char s[], int width);
  6. void itob(int n, char s[], int b);
  7. int clear(char* c, int count);
  8. void reverse(char s[]);
  9. #define MAGIC_1 2048
  10. #define MIN_INT -2147483648
  11. int main()
  12. {
  13. char s[MAGIC_1];
  14. int k= 1;
  15. // int l = -k;
  16. //printf("l: %i\n",l);
  17. // itoa( -2147483648,s);
  18. // printf("%s\n",s);
  19. for(;;)
  20. {
  21. clear(s,MAGIC_1);
  22. itoa(k,s,100);
  23. printf("b: %i, %i :\t %s\n",2,k,s);
  24. k*=2;
  25. }
  26. return 0;
  27. }
  28. void itoa(int n, char s[], int width)
  29. {
  30. itob(n,s,10);
  31. int i=0;
  32. for (; s[i]!=0 ; i++); //figure out how many spaces we have
  33. int spaces=i;
  34. //make temporary space
  35. char temp[width];
  36. clear(temp,width);
  37. for (int j=0;j<width-spaces;j++)
  38. {
  39. temp[j]=' ';
  40. }
  41. for (int j=width-spaces,k=0;j<width;j++,k++)
  42. {
  43. temp[j]=s[k];
  44. }
  45. for (int j=0;j<width;j++)
  46. s[j]=temp[j];
  47. //fill temporary space
  48. //fill original with 0s
  49. //fill original with content
  50. }
  51. void itob(int n, char s[], int b)
  52. {
  53. int i, sign, specialcase=0;
  54. if ((sign = n) < 0) /* record sign */
  55. {
  56. if (n != MIN_INT)
  57. n = -n; /* make n positive */
  58. else //special case
  59. {
  60. n++;
  61. n = -n;
  62. specialcase=1;
  63. }
  64. }
  65. i = 0;
  66. do { /* generate digits in reverse order */
  67. if ((i == 0) && specialcase )
  68. {
  69. s[i] = n % b + '1';
  70. if (s[i] > '9') s[i]+=7;
  71. i++;
  72. }/* get next digit */
  73. else
  74. {
  75. s[i] = n % b + '0'; /* get next digit */
  76. if (s[i] > '9') s[i]+=7;
  77. i++;
  78. }
  79. }
  80. while ((n /= b) > 0); /* delete it */
  81. // if (specialcase && b==2) //binary is kind of special.
  82. // {
  83. // s[i]='1';
  84. // i++;
  85. // }
  86. if (specialcase)
  87. {
  88. for(int k =0 ;k<=i+1;k++)
  89. if (((b <= 9) && (s[k] >= b+'0')) || ((b > 9) && (s[k] >= b+'0'+7)))
  90. {
  91. s[k]='0';
  92. if (((s[k+1]==0) && (b==2)) || (s[k+1]=='0')) s[k+1]='1';
  93. else s[k+1]++;
  94. }
  95. }
  96. if (specialcase && b==2) i++; //this is the only one that necessitates one more position i think?
  97. if (sign < 0)
  98. s[i++] = '-';
  99. s[i] = '\0';
  100. reverse(s);
  101. }
  102. void reverse(char s[])
  103. {
  104. if (s!=0)
  105. {
  106. int i=0;
  107. while (s[i]!=0)
  108. i++;
  109. char temp[i];
  110. for (int j=0; j<i; j++)
  111. {
  112. temp[j]=s[i-(j+1)];
  113. }
  114. for (int j=0; j<i; j++)
  115. {
  116. s[j]=temp[j];
  117. s[j+1]=0;
  118. }
  119. }
  120. }
  121. int clear(char* c, int count)
  122. {
  123. for (int i=0; i<count;i++)
  124. {c[i]=0;}
  125. return 0;
  126. }