Amazon.com Widgets Exercise 5, 6, 7
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 25, 2014, 02:11:57 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 12
| | | |-+ Exercise 5, 6, 7
Pages: [1] Go Down
Print
Author Topic: Exercise 5, 6, 7 (Read 967 times)
alexsom
Jr. Member
**
Posts: 63



Email




on: January 28, 2012, 10:10:31 AM

12.5

Code: (Objective-C)

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
    @autoreleasepool
    {
#define IS_UPPER_CASE(x)  (((x) >= 'A') && ((x) <= 'Z'))
#define IS_LOWER_CASE(x)  (((x) >= 'a') && ((x) <= 'z'))
#define IS_ALPHABETIC(x)  ((((x) >= 'A') && ((x) <= 'Z')) || (((x) >= 'a') && ((x) <= 'z')))
       
       
        char c;
       
       
        NSLog(@"Type in alphabetical character:");
        scanf("%c", &c);   
        if IS_ALPHABETIC(c)
            NSLog(@"The character %c is alphabetic.", c);
        else
            NSLog(@"The character %c is not alphabetic.", c);
           
        if IS_UPPER_CASE(c)
            NSLog(@"The character %c IS UPPER CASE.", c);
        else
            NSLog(@"The character %c IS NOT UPPER CASE.", c);
       
        if IS_LOWER_CASE(c)
            NSLog(@"The character %c IS LOWER CASE.", c);
        else
            NSLog(@"The character %c IS NOT LOWER CASE.", c);
       

    }
    return 0;
}

12.6

Code: (Objective-C)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
    @autoreleasepool
    {
#define IS_DIGIT(x)  (((x) >= '0') && ((x) <= '9'))
#define IS_SPECIAL(x)     (!IS_DIGIT(x) && (!IS_ALPHABETIC(x)))   
#define IS_UPPER_CASE(x)  (((x) >= 'A') && ((x) <= 'Z'))
#define IS_LOWER_CASE(x)  (((x) >= 'a') && ((x) <= 'z'))
#define IS_ALPHABETIC(x)  ((((x) >= 'A') && ((x) <= 'Z')) || (((x) >= 'a') && ((x) <= 'z')))
       
        char c;
       
       
        NSLog(@"Type in alphabetic character:");
        scanf("%c", &c);   
       
        if IS_DIGIT(c)
        NSLog(@"The character %c is a digit:", c);
        else if IS_SPECIAL(c)
            NSLog(@"The character %c is a special:",c);
        else if IS_LOWER_CASE(c)
            NSLog(@"The character %c is alphabetic Lower Case:",c);
        else if IS_UPPER_CASE(c)
            NSLog(@"The character %c is alphabetic Upper Case:",c);
       
        else
            NSLog(@"The character %c is alphabetic:",c);
       
    }
    return 0;
}

12.7

Code: (Objective-C)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
    @autoreleasepool
    {
#define ABSOLUTE_VALUE(x) (((x)> (0)) ? (x) : -(x))
#define delta y   
       
       
        int x;
        int y;
       
        NSLog(@"Type in your first integer:");
        scanf(" %i",&x);
       
        NSLog(@"Type in your second integer:");
        scanf(" %i",&y);
       
        NSLog(@" The absolute value is %i", ABSOLUTE_VALUE(x + delta));
   
   
    }
    return 0;
}



Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: January 31, 2012, 04:22:44 PM

Good job!

FYI, those #define's can go anywhere and are typically places outside of any function or method, like so:

Code: (Objective-C)
#import <Foundation/Foundation.h>  

#define ABSOLUTE_VALUE(x) (((x)> (0)) ? (x) : -(x)) 
#define delta y   

int main (int argc, const char * argv[]) 

    @autoreleasepool   
    {           
        int x; 
        int y; 
         
        NSLog(@"Type in your first integer:"); 
        scanf(" %i",&x); 
         
        NSLog(@"Type in your second integer:"); 
        scanf(" %i",&y); 
         
        NSLog(@" The absolute value is %i", ABSOLUTE_VALUE(x + delta)); 
     
     
    } 
    return 0; 
}

Cheers,

Steve
Logged
that_bman_again
Newbie
*
Posts: 3






Reply #2 on: March 02, 2012, 04:54:07 PM

12.5
For your IS_ALPHABETIC you can (re)use the IS_LOWER_CASE and IS_UPPER_CASE definitions like you did for IS_SPECIAL:

#define IS_ALPHABETIC(x) ( (IS_LOWER_CASE(x)) || (IS_UPPER_CASE(x)) )
Logged
eagle74
Newbie
*
Posts: 24


www.cucinareblog.it


WWW Email




Reply #3 on: June 03, 2012, 10:47:30 AM

12.5
For your IS_ALPHABETIC you can (re)use the IS_LOWER_CASE and IS_UPPER_CASE definitions like you did for IS_SPECIAL:

#define IS_ALPHABETIC(x) ( (IS_LOWER_CASE(x)) || (IS_UPPER_CASE(x)) )

I did the same Wink
Logged

"The more i know people the more i love dogs."Socrate
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.