فاکتوریل با objective c

محلی برای نرم افزارهای سایت ساز و مباحث مربوط به برنامه‌نویسی چون Objective-C ،Cocoa و غیره
ارسال پست
p
pouyan_objc
پست: 26
تاریخ عضویت: 25 آپریل 2010, 20:16
محل اقامت: Tehran
تماس:

فاکتوریل با objective c

پست توسط pouyan_objc »

این برنامه یه عملیات ریاضی خیلی ساده است ولی کاملاً oop است

کد: انتخاب همه

#import <Foundation/Foundation.h>

@interface Factorial : NSObject
{
	long numToFact;
	long factResult;
}

-( long ) rFactorial: x;
-( void ) displayFactorial;

@end

@implementation Factorial

-( long ) rFactorial: x
{
	factResult = 1;
	numToFact = x;
	for ( int i = x; i > 1; i-- )
		factResult *= i;
	
}

-( void ) displayFactorial
{
	NSLog(@" the factorial of %i is : %i ", numToFact, factResult );
}
@end



int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	Factorial *fact = [ [ Factorial  alloc] init];
	
	for ( int i = 1; i <= 10; i ++ )
	{
		[ fact rFactorial: i ];
		[ fact displayFactorial ]; 
	}
	
    
	[pool drain];
    return 0;
}
اگر warning اخطار دیدید مشکل نداره
  • خروجی
2010-05-01 16:40:17.150 oopFactorial[2006:903] the factorial of 1 is : 1
2010-05-01 16:40:17.153 oopFactorial[2006:903] the factorial of 2 is : 2
2010-05-01 16:40:17.153 oopFactorial[2006:903] the factorial of 3 is : 6
2010-05-01 16:40:17.154 oopFactorial[2006:903] the factorial of 4 is : 24
2010-05-01 16:40:17.154 oopFactorial[2006:903] the factorial of 5 is : 120
2010-05-01 16:40:17.155 oopFactorial[2006:903] the factorial of 6 is : 720
2010-05-01 16:40:17.155 oopFactorial[2006:903] the factorial of 7 is : 5040
2010-05-01 16:40:17.156 oopFactorial[2006:903] the factorial of 8 is : 40320
2010-05-01 16:40:17.156 oopFactorial[2006:903] the factorial of 9 is : 362880
2010-05-01 16:40:17.157 oopFactorial[2006:903] the factorial of 10 is : 3628800
any problem mail me at : kalakbarghi@gmail.com
آخرین ویرایش توسط pouyan_objc در 30 آپریل 2010, 16:53، در مجموع 2 بار ویرایش شده است.
kalakbarghi@gmail.com
MacBook Pro MB/LA991
iPod touch 8 GIG
p
pouyan_objc
پست: 26
تاریخ عضویت: 25 آپریل 2010, 20:16
محل اقامت: Tehran
تماس:

Re: فاکتوریل با objective c

پست توسط pouyan_objc »

برای تعیین دفعات تکرار حلقه

کد: انتخاب همه

	for ( int i = 1; i <= 10; i ++ )
	{
		[ fact rFactorial: i ];
		[ fact displayFactorial ]; 
	}
مقدار i >= 10 را به دلخواه تغییر دهیید
مثلاً i >= 100 فاکتوریل ۱۰۰ عدد را حساب می کند
kalakbarghi@gmail.com
MacBook Pro MB/LA991
iPod touch 8 GIG
p
pouyan_objc
پست: 26
تاریخ عضویت: 25 آپریل 2010, 20:16
محل اقامت: Tehran
تماس:

Re: فاکتوریل با objective c

پست توسط pouyan_objc »

نکته پایانی
در xCode
============================
<-application ->Command Line tool
type -> foundation
را انتخواب کنید.
============================
موفق باشید....
kalakbarghi@gmail.com
MacBook Pro MB/LA991
iPod touch 8 GIG
ارسال پست

چه کسی حاضر است؟

کاربران حاضر در این انجمن: کاربر جدیدی وجود ندارد. و 1 مهمان