Calculator Objective-C

Posts 1–8 of 8 · Page 1 of 1
Calculator Objective-C
Well, this is really choob and newbish The fact is that i started to learn Objective-C tonight. Basic stuff, Classes, etc,etc...

Just finished by now my first full program from what i have been reading, it's in fact a calculator, really simple stuff (+,-,*,/) , normally the first program we make when we are learning a language.
Plus, I'm learning this to be able to code for Apple stuff. (as i do own a macbook, an ipod, etc).
I know there are already a few coders that know this language well i guess, so hints and critics are always welcome

So, i just want to share what i have done so far.

Code:
//
//  main.m
//  Started
//
//  Created by Bruno Monteiro on 10/06/13. (Aka Brinuz)
//

#import <Cocoa/Cocoa.h>

@interface Calculator : NSObject
{
	int num1;
	int num2;
}

-(void) askNums;
-(void) doAddition;
-(void) doSubtraction;
-(void) doMultiplication;
-(void) doDivision;

@end

@implementation Calculator

-(void) askNums {
	NSLog(@"First Number:");
	do {
		scanf("%d",&num1);
	} while (num1 == 0); //Don't want it to be 0
	
	NSLog(@"Last Number:");
	do {
		scanf("%d",&num2);
	} while (num2 == 0); //Don't want it to be 0
}

-(void) doAddition {
	NSLog(@"Result: %d", num1 + num2);
	NSLog(@"\n\n");
}

-(void) doSubtraction {
	NSLog(@"Result: %d", num1 - num2);
	NSLog(@"\n\n");
}

-(void) doMultiplication {
	NSLog(@"Result: %d", num1 * num2);
	NSLog(@"\n\n");
}

-(void) doDivision {
	NSLog(@"Result: %d", num1 / num2);
	NSLog(@"\n\n");
}

@end

int menu();

int main(int argc, char *argv[])
{	
	Calculator *calc = [[Calculator alloc] init];
	int option;
	
	do
	{
		option = menu();
		if(option == 0)
		{
			NSLog(@"Will Exit...");
			return 0;
		}
		
		[calc askNums];
		
		switch (option) {
			case 1:
				[calc doAddition];
				break;
			case 2:
				[calc doSubtraction];
				break;
			case 3:
				[calc doMultiplication];
				break;
			case 4:
				[calc doDivision];
				break;
		}
	}
	while (option != 0);
	
	return 0;
}

int menu()
{
	NSLog(@"Option:");
	NSLog(@"1- Add. \n");
	NSLog(@"2- Sub. \n");
	NSLog(@"3- Multi. \n");
	NSLog(@"4- Div. \n");
	NSLog(@"0- Exit");
	
	int option = 0;
	
	do {
		NSLog(@"Choose Option:");
		scanf("%d",&option);
	} while(option < 0 || option > 4);
		
	return option;
}
Well, i would give an explanation on what is being done, but since I'm too fresh i don't want to say stupid things ^^

Edit:

Credits to this for showing me how HelloWorld works:
http://guides.macrumors.com/Compilin...ld_application
Ahh Objective - C, it's horrific!

Naws just kidding. What's NS in NSLog stand for? What's with all the hyphens. I don't really know any objective C otherwise I'd give you some tips. Regardless, great job.
Quote Originally Posted by Void View Post
Ahh Objective - C, it's horrific!

Naws just kidding. What's NS in NSLog stand for? What's with all the hyphens. I don't really know any objective C otherwise I'd give you some tips. Regardless, great job.
As i don't know much yet about Objective-C and coding for Apple in general, i did a fast search about that question and found this:

NS is used a prefix for classes (and other things like constants) in the Mac OS X frameworks. NS stands for NEXTSTEP, an operating system that Apple obtained when they acquired NeXT Software in 1996, and which formed the basis for Mac OS X.
Better answer i won't give ^^ and thanks for asking for it david, which made me to search and learn more myself about this.
Oh, and from what i read when i was "learning" tonight i realised that i could have used: (standard output command on C)
Code:
printf();
About those hyphens, it's just syntax.. from what i have seen so far o.o

Offtopic: Good luck on minion David
Quick question.. how come the calculation functions have hyphens and have non-C like syntax, but the main function has the same syntax as C? Seems a little weird.
Quote Originally Posted by Void View Post
Quick question.. how come the calculation functions have hyphens and have non-C like syntax, but the main function has the same syntax as C? Seems a little weird.
Objective-C uses standard C stuff, the objective of it was to make C full object-orientated language, so i guess thats because of it. Plus, Cocoa framework... Meh, i might as well be wrong <.<
GO AWAY! YOU'RE WALKING TO THE DARK SIDE
I hate objective C so much, it just doesn't make ANY sense at all.
Quote Originally Posted by Hell_Demon View Post
GO AWAY! YOU'RE WALKING TO THE DARK SIDE
I hate objective C so much, it just doesn't make ANY sense at all.
you evil ^^
Quote Originally Posted by 'Bruno View Post
you evil ^^
Well he is right. Objective C is nothing more than some MumboJumbo a few guys through together after drinking A LOT of beer. /yea
Posts 1–8 of 8 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us