Posted By

bobtiki on 01/06/09


Tagged

broken pioc2


Versions (?)

Programming in Objective-C 2.0, Chapter 6, Exercise 6, Horribly broken


 / Published in: Objective C
 

Thanks for the hints, DC NSCoders. I've got a working version now. It might not be the most elegant, but it should work for all signed 32-bit integers.

Write a program that takes an integer keyed from the terminal and extracts and displays each digit of the integer in English. So, if the user types in 932, the program should display the following:

nine
three
two

(Remember to display 'zero' if the user types in just 0.) Note This exercise is a hard one!

The trick is, in the book, you haven't yet learned about arrays, so the solution must not include arrays. Nor do you "know" about file operations, string operations, or most other things that might prove useful here.

  1. #import <Foundation/Foundation.h>
  2.  
  3. // numbers to word-numbers
  4.  
  5. int main (int argc, const char * argv[]) {
  6. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  7. int number, remainder, leftDigit, counter;
  8. BOOL tooBig;
  9.  
  10. // This should now work for every 32-bit signed integer (fingers crossed)
  11.  
  12. NSLog(@"Enter a valid 32-bit signed integer (from -2147483648 to 2147483647:");
  13. scanf("%i", &number);
  14.  
  15. // flip negative numbers to positive, and check for special case -2147483648
  16.  
  17. if (number < 0) {
  18. if (number == -2147483648){
  19. NSLog(@"negative\ntwo\none\nfour\nseven\nfour\neight\nthree\nsix\nfour\neight");
  20. return 1; // just to prove to myself that the program exited here with this value
  21. }
  22. else {
  23. number = -number;
  24. NSLog(@"negative");
  25. }
  26. }
  27.  
  28. if (!number) {
  29. NSLog(@"zero");
  30. }
  31.  
  32. // to prevent leading zeros, check for first counter that will divide into the number
  33.  
  34. if (1000000000 > number)
  35. tooBig = TRUE;
  36.  
  37. for (counter = 1000000000; counter >= 1; counter /= 10) {
  38. if (tooBig == TRUE && counter > number)
  39. continue;
  40. else
  41. tooBig = FALSE; // trips the switch so all subsequent zeros will be counted
  42.  
  43. remainder = number % counter;
  44.  
  45. if (remainder == number)
  46. leftDigit = 0;
  47. else
  48. leftDigit = (number - remainder) / counter;
  49.  
  50. // Debugging printout for checking variable numbers
  51. // NSLog(@"R%i-N%i-C%i-L%i", remainder, number, counter, leftDigit);
  52.  
  53. switch (leftDigit) {
  54. case 0:
  55. NSLog(@"zero");
  56. break;
  57. case 1:
  58. NSLog(@"one");
  59. break;
  60. case 2:
  61. NSLog(@"two");
  62. break;
  63. case 3:
  64. NSLog(@"three");
  65. break;
  66. case 4:
  67. NSLog(@"four");
  68. break;
  69. case 5:
  70. NSLog(@"five");
  71. break;
  72. case 6:
  73. NSLog(@"six");
  74. break;
  75. case 7:
  76. NSLog(@"seven");
  77. break;
  78. case 8:
  79. NSLog(@"eight");
  80. break;
  81. case 9:
  82. NSLog(@"nine");
  83. break;
  84. default:
  85. break;
  86. }
  87. number = remainder;
  88. }
  89.  
  90.  
  91.  
  92. [pool drain];
  93. return 0;
  94. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: phi-nom on September 30, 2010

i have a better answer.

http://www.caliordie.com/2010/09/29/programming-in-objective-c-chapter-6-exercise-6/

You need to login to post a comment.