Search This Blog

Sunday, October 18, 2009

Stanford CS193P - Assigenment1B - WhatATool


这里就不具体介绍就说了。。直接上代码吧

WhatATool.m 代码

#import
#import "PolygonShape.h"

//sample function for one section, us a similar function per section
//返回用户路径
void printPathInfo()
{
NSString *path = @"~";
path = [path stringByExpandingTildeInPath];
NSArray *pathComponents = [path pathComponents];
NSLog(@"My home folder is at '%@'",path);
NSLog(@"%@",pathComponents);
}

//返回本程序进程名与ID
void PrintProcessInfo()
{
NSString *processName = [[NSProcessInfo processInfo] processName];
int processIdentifier = [[NSProcessInfo processInfo] processIdentifier];
NSLog(@"Process Name: '%@' Process ID: '%d'",processName,processIdentifier);
}

//书签&&字典
void PrintBookmarkInfo()
{
NSArray *keys = [NSArray arrayWithObjects:@"Stanford University", @"Apple", @"CS193P",@"Stanford on iTunes U", @"Stanford Mail", nil];
NSArray *objects = [NSArray arrayWithObjects:@"http://www.stanford.edu", @"http://www.apple.com", @"http://cs193p.stanford.edu", @"http://itunes.stanford.edu", @"http://stanfordshop.com", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
int length = [dictionary count];
//NSLog(@"%d",length);
/*
for (id key in dictionary)
{
NSLog(@"Key: %@, URL: %@", key, [dictionary objectForKey:key]);
}*/
for(int i=0;i
{
NSString *aString =@"Stanford";
id buffer = [[dictionary allKeys]objectAtIndex:i];
if([buffer hasPrefix:aString])
NSLog(@"Key: %@, URL %@", buffer,[dictionary objectForKey:buffer]);
}
}

//Class 识别(数组应用)
void PrintIntrospectionInfo()
{
NSMutableArray *array = [[NSMutableArray alloc]init];
NSString *obj1 = @"Iphone Beginner";
NSURL *obj2 = [[NSURL alloc]initWithString:@"www.apple.com" ];
NSProcessInfo *obj3 = [NSProcessInfo processInfo];

[array addObject:obj1];
[array addObject:obj2];
[array addObject:obj3];
int count = [array count];
for(int i=0;i
{
id obj = [array objectAtIndex:i];
NSLog(@"Class Name: %@",[obj className]);
if([obj isMemberOfClass:[NSString class]])
NSLog(@"Is Member of NSString: YES");
else
NSLog(@"Is Member of NSString: NO");
if([obj isKindOfClass:[NSString class]])
NSLog(@"IS Kind of NSString: YES");
else
NSLog(@"Is Kind of NSString: NO");
if([obj respondsToSelector:@selector(lowercaseString)])
{
NSLog(@"Responds to lowercaseString: YES");
NSLog(@"lowercaseString is: %@",[obj performSelector:@selector(lowercaseString)]);
}
else
NSLog(@"Responds to lowercaseString: NO");

NSLog(@"================================================");
}
}

//多边形
void PrintPolygonInfo()
{
PolygonShape *square = [[PolygonShape alloc]init];
[square initWithNumberOfSides:4 minimumNumberOfSides:3 maximumNumberOfSides:7];
PolygonShape *hexagon = [[PolygonShape alloc]initWithNumberOfSides:6 minimumNumberOfSides:5 maximumNumberOfSides:9];
PolygonShape *dodecagon = [[PolygonShape alloc]initWithNumberOfSides:12 minimumNumberOfSides:9 maximumNumberOfSides:12];

NSLog(@"%@",[square description]);
NSLog(@"%@",[hexagon description]);
NSLog(@"%@",[dodecagon description]);
}

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

printPathInfo(); //Section1
PrintProcessInfo(); //Section2
PrintBookmarkInfo(); //Section3
PrintIntrospectionInfo(); //Section4

PrintPolygonInfo(); //Section6
//NSLog(@"Hello world");
[pool drain];
return 0;
}




PolygonShape.h 代码

//
// PolygonShape.h
// WhatATool
//
// Created by Nono on 29/12/2008.
// Copyright 2008 user. All rights reserved.
//

#import


@interface PolygonShape : NSObject
{
int numberOfSides;
int minimumNumberOfSides;
int maximumNumberOfSides;
}

@property int numberOfSides;
@property int minimumNumberOfSides;
@property int maximumNumberOfSides;
@property (readonly) float angleInDegrees;
@property (readonly) float angleInRadians;
@property (readonly) NSString *name;

-(id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max;
@end


PolygonShape.m 代码

//
// PolygonShape.m
// WhatATool
//
// Created by Nono on 29/12/2008.
// Copyright 2008 user. All rights reserved.
//

#import "PolygonShape.h"


@implementation PolygonShape

@synthesize numberOfSides;
@synthesize minimumNumberOfSides;
@synthesize maximumNumberOfSides;

-(id)init
{
[self initWithNumberOfSides:5 minimumNumberOfSides:3 maximumNumberOfSides:10];
return self;
}

-(id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max
{
self = [super init];
[self setMinimumNumberOfSides:min];
[self setMaximumNumberOfSides:max];
[self setNumberOfSides:sides];

NSLog(@"The Minimum of sides is: %d, The Maximum of sides is: %d, The sides of the Polygon is: %d",min,max,sides);
return self;
}

-(void) setMinimumNumberOfSides: (int)min
{
if(min<2)
{
NSLog(@"Invalid number of minimumNumberOfSides: %d is less than 2",min);
}
else
{
minimumNumberOfSides = min;
}
}

-(void) setMaximumNumberOfSides: (int)max
{
if(max>12)
{
NSLog(@"Invalid number of maximunberOfSides: %d is greater than 12",max);
}
else
{
maximumNumberOfSides = max;
}
}

-(void) setNumberOfSides: (int)sides
{
if(sides {
NSLog(@"Invalid number of sides: %d is less than the minimum of %d allowed",sides,minimumNumberOfSides);
}
else if(sides>maximumNumberOfSides)
{
NSLog(@"Invalid number of sides: %d is greater than the maxinmum of %d allowed",sides,maximumNumberOfSides);
}
else
{
numberOfSides = sides;
NSLog(@"Number of sides: %d",sides);
}
}

-(float) angleInDegrees
{
float angleInDegrees = (180*(numberOfSides-2)/numberOfSides);
return angleInDegrees;
}

-(float) angleInRadians
{
float Pi = 3.141592;
float angleInRadians = [self angleInDegrees]/180*Pi;
return angleInRadians;
}

-(NSString *)name
{
NSString *nameArray[] = {nil,@"Henagon", @"Digon", @"Triangle",@"Square", @"Pentagon", @"Hexagon",
@"Heptagon",@"Octagon",@"Enneagon",@"Decagon",@"Hendecagon",@"Dodecagon",nil};
return nameArray[numberOfSides];
}

-(NSString *)description
{
return [NSString stringWithFormat:@"Hello I am a %d-sides polygon ('%@') with angles of %f degrees (%f radians).",[self numberOfSides],[self name],[self angleInDegrees],[self angleInRadians]];
}

@end

No comments:

Post a Comment