Objective-C Daily (May 21, 2012)
| Date |
2011/4/9 |
| Name |
Add Two Numbers |
| Description |
Add Two Numbers using Objective-C |
| Filename |
AddTwoNumObjC.c |
| Content |
- Using the Standard input stream to enter different numbers or text
- Find two integers each time and,
- Display output the sum to the standard output stream.
- Repeat the all the steps until an EOF (End of File) signal received
|
| Sample Input: |
123 3.5 dsfasd 4 fdsafds 6 |
| Sample Output: |
126 0 |
| Expected Test Command: |
|
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableData* data=[NSMutableData dataWithLength:sizeof(char) * 256];
NSInteger total=0;
NSInteger nCount=0;
char* temp=[data mutableBytes];
do{
temp[0]=0;
scanf("%255s",temp);
if (temp[0]!=0) {
NSInteger val;
NSString *data=[[NSString alloc] initWithCString:temp];
NSScanner *scan=[NSScanner scannerWithString:data];
if([scan scanInteger:&val]){
if (nCount++ % 2==0) {
total=val;
}else {
total+=val;
printf("%d ",(int)total);
}
}
}
}while(temp[0]!=0);
[pool drain];
return 0;
}
| Date |
2011/4/8 |
| Name |
Hello Friend |
| Description |
Ask user's name and echo greeting message to user using Objective C |
| Filename |
HelloFriendObjC.c |
| Content |
- Using the Standard output stream to prompt your:"Your name?"
- Then use the input stream to receive input from user.
- Display the greeting message "Hello, [user name]!" using standard output stream.
- Repeat all the steps until users press CTRL-Z (MS windows) or CTRL-D (Mac) or receive the EOF signal.
|
| Sample Input: |
Peter Pan |
| Sample Output: |
Hello, Peter Pan! |
| Expected Test Command: |
|
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableData* data=[NSMutableData dataWithLength:sizeof(char) * 256];
char* name=[data mutableBytes];
printf("Your name?");
do{
name[0]=0;
scanf("%[^\n]+",name);
if (name[0]!=0) {
printf("Hello, %s!\n",name);
printf("Your name?");
scanf("%c",name);
}
}while(name[0]!=0);
[pool drain];
return 0;
}
| Date |
2011/4/7 |
| Name |
Hello World |
| Description |
Display greeting message to user using Objective C |
| Filename |
HelloWorldObjC.c |
| Content |
- Display the greeting message "Hello, World!" using standard output stream by Windows Scripting File using JScript
|
| Sample Input: |
|
| Sample Output: |
Hello, World! |
| Expected Test Command: |
|
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
printf("Hello, World!\ns");
[pool drain];
return 0;
}