Presentation is loading. Please wait.

Presentation is loading. Please wait.

شرط و تصميم اصول كامپيوتر 1. 2 الگوريتم اقليدس E1: [find remainder] Divide m by n and let r be the remainder. Clearly, 0 <=r <=n ) E2[it is zero?] if.

Similar presentations


Presentation on theme: "شرط و تصميم اصول كامپيوتر 1. 2 الگوريتم اقليدس E1: [find remainder] Divide m by n and let r be the remainder. Clearly, 0 <=r <=n ) E2[it is zero?] if."— Presentation transcript:

1 شرط و تصميم اصول كامپيوتر 1

2 2 الگوريتم اقليدس E1: [find remainder] Divide m by n and let r be the remainder. Clearly, 0 <=r <=n ) E2[it is zero?] if r =0, the algorithm terminates, n is the answer E3[Interchange] Set m  n, n  r and go back to step E1 ▐

3 3 آشنايي اجراي برخي از دستورات الگوريتم، وابسته به برقراري شرايط خاصي است – E2[it is zero?] if r =0, the algorithm terminates, n is the answer If, then دستورات شرطي، ممكن است مسير اجراي برنامه را تغيير دهند شرط مورد استفاده ممكن است، ساده يا مركب باشد

4 4 شرط ساده //Based on the acquired mark, is the student in the tops group? S1[tops]: if Mark > 17 then the student is in tops شرط ساده، براي بيان گزاره هاي شرطي داراي يك متغير استفاده مي شود – ارزش آن درست يا نادرست است

5 5 دستور شرطي if true false grade >= 10 print “Passed” A decision can be made on any expression. zero - false nonzero - true Example: 3 - 4 is true

6 6 دستور شرط در C++ if ( ) ; if ( N > 0 ) cout << N << “positive” ; عملگرهاي مقايسه براي بيان شرطهاي ساده استفاده مي شوند

7 7 عملگرهاي مقايسه

8 8 برنامه نمونه #include // allows program to perform input and output using namespace std; // program uses cout int main() { int number1; // first integer to compare int number2; // second integer to compare cout << "Enter two integers to compare: "; // prompt user for data cin >> number1 >> number2 ; // read two integers from user if ( number1 == number2 ) cout << number1 << " == " << number2 << endl; if ( number1 != number2 ) cout << number1 << " != " << number2 << endl; if ( number1 < number2 ) cout << number1 << " < " << number2 << endl; if ( number1 > number2 ) cout " << number2 << endl; if ( number1 <= number2 ) cout << number1 << " <= " << number2 << endl; if ( number1 >= number2 ) cout = " << number2 << endl; return 0; // indicate that program ended successfully } // end function main

9 9 If …Else… دستور مقيد به شرط در if… زماني اجرا مي شود كه ارزش گزاره شرطي درست باشد با دستور if..else… مي توان در صورت برقرار نبودن شرط if دستورات خاص ديگري را اجرا كرد If student's grade >= 10 Print "Passed" Else Print "Failed"

10 10 نمودار دستور if-else truefalse print “Failed” print “Passed” grade >= 10

11 11 دستور شرط در C++ if ( grade >= 10 ) cout << "Passed"; else cout << "Failed";

12 12 بلوك دستورات اگر بخواهيم بيش از يك دستور را هنگام درستي يا نادرستي شرطي اجرا كنيم، از عملگر { } استفاده مي كنيم If student's grade >= 10 Print “Congrats!" Print "Passed" Else Print " Sorry!" Print "Failed“ Print “Try More Next Semester!"

13 13 بلوك دستورات در C++ if (grade >= 10 ){ cout << “Congrats!“; cout << "Passed“; } else { cout << " Sorry! "; cout << "Failed " ; cout << " Try More Next Semester!" ; }

14 14 تكرار شرطي – حلقه while دستور while به شكل زير است: while ( ) while( x > 0 ) x = x -1 ; // decrement x مشابه دستور if است، با اين تفاوت كه: – پس از هر بار اجراي دستورات while شرط حلقه بررسي مي شود. – در صورت برقراري شرط مورد نظر، اجراي حلقه تكرار مي شود از بلوك دستورات مي توان براي اجراي تكراري چندين دستور استفاده كرد

15 15 نمودار حلقه تکرار while product <= 1000 product = 2 * product true false

16 16 الگوريتم اقليدس E1: [find remainder] Divide m by n and let r be the remainder. Clearly, 0 <=r <=n ) E2[it is zero?] if r =0, the algorithm terminates, n is the answer E3[Interchange] Set m  n, n  r and go back to step E1 ▐

17 17 پياده سازي الگوريتم اقليدس #include using namespace std; int main(int argc, char *argv[]) { int m, n,r ; cout <<"Enter two positive integers: " ; cin >> m >> n ; cout << "G.C.D. " << m << ", " << n << " is " ; r = m % n ; // r is the remainder while ( r != 0 ){ m = n ; n = r ; r = m % n ; } cout << n << endl ; system("PAUSE"); return EXIT_SUCCESS; } r = m % n ; while ( r != 0 ){ m = n ; n = r ; r = m % n ; }

18 18 Edsger W. Dijkstra Edsger Wybe Dijkstra (May 11, 1930 – August 6, 2002;) was a Dutch computer scientist. He received the 1972 A. C. M. Turing Award for fundamental contributions in the area of programming languages, and was the Schlumberger Centennial Chair of Computer Sciences at The University of Texas at Austin from 1984 until 2000.May 11 1930August 62002 Dutchcomputer scientistA. C. M. Turing AwardThe University of Texas at Austin His most famous quote is: “go to is determined harmful” More at wikipedia

19 19 تمرين با استفاده از الگوريتم اقليدس، الگوريتم و برنامه اي بنويسيد كه كوچكترين مضرب مشترك دو عدد طبيعي را محاسبه و چاپ كند بفرستيد: – ايميل من :mansoorm@modares.ac.ir – موضوع ايميل شما: [csb1-cs-assign03]


Download ppt "شرط و تصميم اصول كامپيوتر 1. 2 الگوريتم اقليدس E1: [find remainder] Divide m by n and let r be the remainder. Clearly, 0 <=r <=n ) E2[it is zero?] if."

Similar presentations


Ads by Google