1 / 19

شرط و تصميم

شرط و تصميم. اصول كامپيوتر 1. الگوريتم اقليدس. 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 ▐. آشنايي.

osanna
Download Presentation

شرط و تصميم

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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. آشنايي • اجراي برخي از دستورات الگوريتم، وابسته به برقراري شرايط خاصي است • E2[it is zero?] if r =0 , the algorithm terminates, n is the answer • If <conditions> , then <Statements> • دستورات شرطي، ممكن است مسير اجراي برنامه را تغيير دهند • شرط مورد استفاده ممكن است، ساده يا مركب باشد

  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. A decision can be made on any expression. zero - false nonzero - true Example: 3 - 4 istrue true false print “Passed” grade >= 10 دستور شرطي if

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

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

  8. برنامه نمونه #include <iostream> // 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 << number1 << " > " << number2 << endl; if ( number1 <= number2 ) cout << number1 << " <= " << number2 << endl; if ( number1 >= number2 ) cout << number1 << " >= " << number2 << endl; return 0; // indicate that program ended successfully } // end function main

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

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

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

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

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

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

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

  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. پياده سازي الگوريتم اقليدس #include <cstdlib> #include <iostream> 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. Edsger W. Dijkstra Edsger Wybe Dijkstra (May 11, 1930 – August 6, 2002;) was a Dutchcomputer 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. His most famous quote is: “go to is determined harmful” More at wikipedia

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

More Related