انجمن ویکی قلم‌های فارسی
ورود / عضویت

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

محلی برای نرم افزارهای سایت ساز و مباحث مربوط به برنامه‌نویسی چون Objective-C ،Cocoa و غیره

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

پستتوسط pouyan_objc » 30 آوریل 2010, 16:37

این برنامه یه عملیات ریاضی خیلی ساده است ولی کاملاً 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 on 30 آوریل 2010, 16:53, ویرایش شده در 2.
kalakbarghi@gmail.com
MacBook Pro MB/LA991
iPod touch 8 GIG
نماد کاربر
pouyan_objc
 
پست‌ها : 26
تاریخ عضویت: 25 آوریل 2010, 20:16
محل سکونت: Tehran

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

پستتوسط pouyan_objc » 30 آوریل 2010, 16:45

برای تعیین دفعات تکرار حلقه
کد: انتخاب همهٔ
   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
نماد کاربر
pouyan_objc
 
پست‌ها : 26
تاریخ عضویت: 25 آوریل 2010, 20:16
محل سکونت: Tehran

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

پستتوسط pouyan_objc » 30 آوریل 2010, 16:51

نکته پایانی
در xCode
============================
<-application ->Command Line tool
type -> foundation
را انتخواب کنید.
============================
موفق باشید....
kalakbarghi@gmail.com
MacBook Pro MB/LA991
iPod touch 8 GIG
نماد کاربر
pouyan_objc
 
پست‌ها : 26
تاریخ عضویت: 25 آوریل 2010, 20:16
محل سکونت: Tehran



بازگشت به برنامه‌نویسی


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