SHARE
TWEET

Kochan Solution Exercise 6-6

applehelpwriter Sep 8th, 2013 126 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* SOLUTION: ---------------
  2.  "Programming in Objective C"
  3.  Kochan, p130, Exercise 6-6.
  4.  
  5.  "Write a program that takes an integer keyed in 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:
  6.  nine
  7.  three
  8.  two"
  9.  
  10.  */
  11.  
  12.  
  13. /*
  14.  
  15.  I've seen a number of solutions for this online but I didn't like any of them as they
  16.  either:
  17.  i. included C library functions
  18.  or
  19.  ii. used concepts that Kochan hasn't introduced yet
  20.  
  21.  The solution below, while far from elegant, relies only on things you've seen in the book
  22.  up to this point and makes proper use of Objective C structures.
  23.  
  24.  Hope it helps!
  25.  
  26.  Phil
  27.  applehelpwriter.com 2013
  28.  
  29.  */
  30.  
  31.  
  32. #import <Foundation/Foundation.h>
  33.  
  34. /*---------------------------------*/
  35. // ~/objcAgain1.xcodeproj
  36.  
  37. @interface NumToWord : NSObject {
  38.    
  39.     long inputnumber;
  40. }
  41.  
  42. -(void) setInputNumber: (long) value;
  43. -(void) printNum;
  44. @end
  45.  
  46. /*---------------------------------*/
  47.  
  48.  
  49.  
  50. @implementation NumToWord
  51. -(void) setInputNumber:(long)value {
  52.     inputnumber = value;
  53. }
  54.  
  55. -(void) printNum {
  56.    
  57.    
  58.     int numword[10];
  59.     int rem = 0;
  60.     int iterator = 0;
  61.     int i = 9; //to reverse: set i to the last element of the array and count down
  62.    
  63.    
  64.    
  65.     while (inputnumber > 0){
  66.        
  67.         //dealing with any zero's:
  68.         while ((inputnumber % 10) == 0){
  69.             numword[i] = 0;
  70.             iterator++;
  71.             --i; //counting backwards so the values are stored in reverse
  72.             inputnumber /= 10;
  73.            
  74.         }//end while
  75.        
  76.         //dealing with all non-zero numbers
  77.         rem = (inputnumber % 10);
  78.        
  79.         //setting the numbers to the end of the array and counting down
  80.         for (i = i; ((numword[i] = rem) ); --i){
  81.             ++iterator;
  82.             inputnumber /= 10;
  83.            
  84.             rem = (inputnumber % 10);
  85.         }//end for
  86.        
  87.     }//end while
  88.    
  89.    
  90.     // start printing from the first used value in the array to the last
  91.     //that's why we needed the iterator, to find the first used valiue in the array
  92.     for (i = (10-iterator); i < 10; ++i)
  93.         switch (numword[i]) {
  94.             case 0:
  95.                 NSLog(@"zero ");
  96.                 break;
  97.             case 1:
  98.                 NSLog(@"one ");
  99.                 break;
  100.             case 2:
  101.                 NSLog(@"two ");
  102.                 break;
  103.             case 3:
  104.                 NSLog(@"three ");
  105.                 break;
  106.             case 4:
  107.                 NSLog(@"four ");
  108.                 break;
  109.             case 5:
  110.                 NSLog(@"five ");
  111.                 break;
  112.             case 6:
  113.                 NSLog(@"six ");
  114.                 break;
  115.             case 7:
  116.                 NSLog(@"seven ");
  117.                 break;
  118.             case 8:
  119.                 NSLog(@"eight ");
  120.                 break;
  121.             case 9:
  122.                 NSLog(@"nine ");
  123.                 break;
  124.             default:
  125.                 NSLog(@"my error");
  126.                 break;
  127.         }//end switch
  128.    
  129.    
  130.    
  131. }
  132.  
  133. @end
  134.  
  135. /*---------------------------------*/
  136.  
  137.  
  138.  
  139. int main(int argc, const char * argv[])
  140. {
  141.     long number;
  142.     int counter = 0;
  143.     int i = 0;
  144.    
  145.     @autoreleasepool {
  146.         NumToWord *myNumToWord = [[NumToWord alloc] init];
  147.        
  148.         NSLog(@"Type a number: \n");
  149.         scanf("%ld", &number);
  150.        
  151.         //if the user only enters '0' print 'zero'
  152.         if (number == 0)
  153.             NSLog(@"zero");
  154.        
  155.         //otherwise, determine the number of digits, up to 10, i.e. max num 9999999999;
  156.        
  157.         else if (number < 10)
  158.             counter = 1;
  159.         else if (number < 100)
  160.             counter = 2;
  161.         else if (number < 1000)
  162.             counter = 3;
  163.         else if (number < 10000)
  164.             counter = 4;
  165.         else if (number < 100000)
  166.             counter = 5;
  167.         else if (number < 1000000)
  168.             counter = 6;
  169.         else if (number < 10000000)
  170.             counter = 7;
  171.         else if (number < 100000000)
  172.             counter = 8;
  173.         else if (number < 1000000000)
  174.             counter = 9;
  175.         else if (number < 10000000000)
  176.             counter = 10;
  177.         else
  178.             NSLog(@"Number out of range");
  179.        
  180.        
  181.         [myNumToWord setInputNumber: number];
  182.         for (i=0; i < counter; ++i)
  183.             [myNumToWord printNum];
  184.        
  185.     }//end autoreleasepool
  186.    
  187.     return 0;
  188. }//end main
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top