Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* SOLUTION: ---------------
- "Programming in Objective C"
- Kochan, p130, Exercise 6-6.
- "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:
- nine
- three
- two"
- */
- /*
- I've seen a number of solutions for this online but I didn't like any of them as they
- either:
- i. included C library functions
- or
- ii. used concepts that Kochan hasn't introduced yet
- The solution below, while far from elegant, relies only on things you've seen in the book
- up to this point and makes proper use of Objective C structures.
- Hope it helps!
- Phil
- applehelpwriter.com 2013
- */
- #import <Foundation/Foundation.h>
- /*---------------------------------*/
- // ~/objcAgain1.xcodeproj
- @interface NumToWord : NSObject {
- long inputnumber;
- }
- -(void) setInputNumber: (long) value;
- -(void) printNum;
- @end
- /*---------------------------------*/
- @implementation NumToWord
- -(void) setInputNumber:(long)value {
- inputnumber = value;
- }
- -(void) printNum {
- int numword[10];
- int rem = 0;
- int iterator = 0;
- int i = 9; //to reverse: set i to the last element of the array and count down
- while (inputnumber > 0){
- //dealing with any zero's:
- while ((inputnumber % 10) == 0){
- numword[i] = 0;
- iterator++;
- --i; //counting backwards so the values are stored in reverse
- inputnumber /= 10;
- }//end while
- //dealing with all non-zero numbers
- rem = (inputnumber % 10);
- //setting the numbers to the end of the array and counting down
- for (i = i; ((numword[i] = rem) ); --i){
- ++iterator;
- inputnumber /= 10;
- rem = (inputnumber % 10);
- }//end for
- }//end while
- // start printing from the first used value in the array to the last
- //that's why we needed the iterator, to find the first used valiue in the array
- for (i = (10-iterator); i < 10; ++i)
- switch (numword[i]) {
- case 0:
- NSLog(@"zero ");
- break;
- case 1:
- NSLog(@"one ");
- break;
- case 2:
- NSLog(@"two ");
- break;
- case 3:
- NSLog(@"three ");
- break;
- case 4:
- NSLog(@"four ");
- break;
- case 5:
- NSLog(@"five ");
- break;
- case 6:
- NSLog(@"six ");
- break;
- case 7:
- NSLog(@"seven ");
- break;
- case 8:
- NSLog(@"eight ");
- break;
- case 9:
- NSLog(@"nine ");
- break;
- default:
- NSLog(@"my error");
- break;
- }//end switch
- }
- @end
- /*---------------------------------*/
- int main(int argc, const char * argv[])
- {
- long number;
- int counter = 0;
- int i = 0;
- @autoreleasepool {
- NumToWord *myNumToWord = [[NumToWord alloc] init];
- NSLog(@"Type a number: \n");
- scanf("%ld", &number);
- //if the user only enters '0' print 'zero'
- if (number == 0)
- NSLog(@"zero");
- //otherwise, determine the number of digits, up to 10, i.e. max num 9999999999;
- else if (number < 10)
- counter = 1;
- else if (number < 100)
- counter = 2;
- else if (number < 1000)
- counter = 3;
- else if (number < 10000)
- counter = 4;
- else if (number < 100000)
- counter = 5;
- else if (number < 1000000)
- counter = 6;
- else if (number < 10000000)
- counter = 7;
- else if (number < 100000000)
- counter = 8;
- else if (number < 1000000000)
- counter = 9;
- else if (number < 10000000000)
- counter = 10;
- else
- NSLog(@"Number out of range");
- [myNumToWord setInputNumber: number];
- for (i=0; i < counter; ++i)
- [myNumToWord printNum];
- }//end autoreleasepool
- return 0;
- }//end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement