1 Fig 1 6 fig 0106 cpp 2

  • Slides: 50
Download presentation

1 // Fig. 1. 6: fig 01_06. cpp 2 // Addition program. 3 #include

1 // Fig. 1. 6: fig 01_06. cpp 2 // Addition program. 3 #include <iostream> 4 using namespace std; 5 // function main begins program execution 6 int main() Δήλωση ακέραιων μεταβλητών. 7 { 8 integer 1; // first number to be input by user 9 integer 2; // second number to be input by user Χρήση του τελεστή εξαγωγής 10 int sum; // variable in which sum will be stored Outline fig 01_06. cpp (1 of 1) ροής με την στάνταρ είσοδο για την είσοδο δεδομένων integern"; // prompt χρήστη. // read an integer 11 12 13 cout << "Enter first cin >> integer 1; 14 15 16 cout << "Enter second integern" ; // prompt cin >> integer 2; // read an integer Οι υπολογισμοί μπορούν να γίνουν και στις εντολές εξόδου. 17 18 sum = integer 1 + integer 2; 19 20 cout << "Sum is " << sum << endl; // print sum 21 22 return 0; 23 24 Ο τελεστής ροής endl αλλάζει Εναλλακτικά, αντί για τις γραμμές 18 και 20: γραμμή και στη συνέχεια, αδειάζει // assign result to sum τον buffer εξόδου cout << "Sum is " << integer 1 + integer 2 << endl; // indicate that program ended successfully } // end function main Συνένωση των πράξεων εισαγωγής στη ροή εξόδου. . Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 4

Enter first integer 45 Enter second integer 72 Sum is 117 Reproduced from the

Enter first integer 45 Enter second integer 72 Sum is 117 Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. Outline fig 01_06. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 5

n 2. 4 Δομές Ελέγχου C++ keywords n Δεν μπορούν να χρησιμοποιηθούν ως ονόματα

n 2. 4 Δομές Ελέγχου C++ keywords n Δεν μπορούν να χρησιμοποιηθούν ως ονόματα μεταβλητών ή προσδιοριστικά (identifiers) Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 9

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // Fig. 2. 9: fig 02_09. cpp // Class average program with sentinel-controlled repetition. #include <iostream> using Outline fig 02_09. cpp (1 of 3) std: : cout; std: : cin; std: : endl; std: : fixed; #include <iomanip> // parameterized stream manipulators using std: : setprecision; // sets numeric output precision // function main begins program execution Ο τύπος δεδομένων double int main() χρησιμοποιείτε για του δεκαδικούς { αριθμούς. int total; // sum of grades int grade. Counter; // number of grades entered int grade; // grade value 20 21 double average; 22 23 24 25 // initialization phase total = 0; // initialize total grade. Counter = 0; // initialize loop counter // number with decimal point for average Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 15

26 27 28 29 30 31 32 33 34 35 36 37 38 39

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 // processing phase // get first grade from user cout << "Enter grade, -1 to end: " ; cin >> grade; Outline // prompt for input // read grade from user fig 02_09. cpp (2 of 3) // loop until sentinel value read from user while ( grade != -1 )static_cast<double>() χειρίζεται το total ως { double πρόσκαιρα (casting). total = total + grade; // add grade to total grade. Counter = grade. Counter + 1; // increment counter Απαιτείται διότι η διαίρεση δύο ακεραίων αποκόβει το cout << "Enter grade, -1 to end: " ; υπόλοιπο. cin >> grade; } // end while // prompt for input // read next grade. Counter είναι int, αλλά προάγεται σε double. // termination phase // if user entered at least one grade. . . if ( grade. Counter != 0 ) { // calculate average of all grades entered average = static_cast< double >( total ) / grade. Counter; Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 16

49 50 51 // display average with two digits of precision cout << "Class

49 50 51 // display average with two digits of precision cout << "Class average is " << setprecision( 2 ) << fixed << average << endl; 52 53 } // end if part of if/else 54 55 56 else // if no grades were entered, output appropriate message cout << "No grades were entered" << endl; 57 58 return 0; 59 60 Outline fig 02_09. cpp (3 of 3) fig 02_09. cpp output (1 of 1) // indicate program ended successfully } // end function main Enter Enter Enter Class fixed αναγκάζει την έξοδο να setprecision(2) εκτυπώνει δύο ψηφία μετά 75 εκτυπωθεί σε μορφή σταθερής την υποδιαστολή (στρογγυλοποίηση για να 94 υποδιαστολής (όχι σε επιστημονική ταιριάζει στην ακρίβεια). 97 μορφή). Επίσης, αναγκάζει να 88 τυπώνονται η υποδιαστολή Τα προγράμματα που τη χρησιμοποιούν πρέπει να 70 ακολουθούμενη από απαραίτητα περιλαμβάνουν: 64 μηδενικά. #include <iomanip> 83 grade, -1 to end: grade, -1 to end: 89 #include <iostream> grade, -1 to end: -1 average is 82. 50 Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 17

1 2 3 // Fig. 2. 14: fig 02_14. cpp // Preincrementing and postincrementing.

1 2 3 // Fig. 2. 14: fig 02_14. cpp // Preincrementing and postincrementing. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 9 10 11 // function main begins program execution int main() { int c; // declare variable Outline fig 02_14. cpp (1 of 2) 12 13 14 15 16 17 // demonstrate postincrement c = 5; // cout << c << endl; // cout << c++ << endl; // cout << c << endl; // assign 5 to c print 5 then postincrement print 6 18 19 20 21 22 23 // demonstrate preincrement c = 5; // cout << c << endl; // cout << ++c << endl; // cout << c << endl; // assign 5 to c print 5 preincrement then print 6 Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 22

24 25 26 27 return 0; // indicate successful termination } // end function

24 25 26 27 return 0; // indicate successful termination } // end function main 5 5 6 Outline fig 02_14. cpp (2 of 2) fig 02_14. cpp output (1 of 1) 5 6 6 Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 23

2. 16 n switch Δομή πολλαπλής επιλογής switch n n Ελέγχει μια μεταβλητή για

2. 16 n switch Δομή πολλαπλής επιλογής switch n n Ελέγχει μια μεταβλητή για πολλαπλές τιμές Μια σειρά από ετικέτες case και προαιρετικά default case switch ( variable ) { case value 1: statements break; case value 2: case value 3: statements break; // taken if variable == value 1 // necessary to exit switch // taken if variable == value 2 or == value 3 default: // taken if variable matches no other cases statements break; Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. } 24

2. 16 switch Δομή πολλαπλής επιλογής Reproduced from the Power. Points for C++ How

2. 16 switch Δομή πολλαπλής επιλογής Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 25

1 2 3 // Fig. 2. 22: fig 02_22. cpp // Counting letter grades.

1 2 3 // Fig. 2. 22: fig 02_22. cpp // Counting letter grades. #include <iostream> 4 5 6 7 using std: : cout; using std: : cin; using std: : endl; 8 9 10 11 12 13 14 15 16 17 // function main begins program execution int main() { int grade; // one grade int a. Count = 0; // number of As int b. Count = 0; // number of Bs int c. Count = 0; // number of Cs int d. Count = 0; // number of Ds int f. Count = 0; // number of Fs 18 19 20 Outline fig 02_22. cpp (1 of 4) cout << "Enter the letter grades. " << endl << "Enter the EOF character to end input. " << endl; 21 Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 27

22 23 // loop until user types end-of-file key sequence while ( ( grade

22 23 // loop until user types end-of-file key sequence while ( ( grade = cin. get() ) != EOF ) { break ολοκληρώνει το Outline switch και το πρόγραμμα συνεχίζει με την grade was input εντολή μετά από τη δομή switch. fig 02_22. cpp // switch structure nested in while (2 of 4) 24 25 26 // determine which switch ( grade ) { 27 28 29 30 31 case 'A': case 'a': ++a. Count; break; // // 32 33 34 35 36 case 'B': case 'b': ++b. Count; break; // // Συγκρίνει το grade (έναν 37 int) με την αριθμητική 38 case 'C': αναπαράσταση του A και a. 39 case 'c': 40 ++c. Count; 41 break; 42 // // grade was uppercase A or lowercase a increment a. Count necessary to exit switch Οι δηλώσεις ανάθεσης έχουν τιμή που είναι η ίδια με τη grade was uppercase B μεταβλητή αριστερά του =. Η or lowercase b increment b. Count τιμή της δήλωσης είναι η ίδια exit switch με την τιμή που επιστρέφει η cin. get(). grade was uppercase C or lowercase c Αυτό μπορεί να increment c. Count αρχικοποιήσει: exit switch a = b = c = 0; cin. get() χρησιμοποιεί την τελεία. Αυτή η συνάρτηση επιστρέφει ένα χαρακτήρα από το πληκτρολόγιο (πριν το Enter), και την αναθέτει στην grade. cin. get() επιστρέφει EOF (end -of-file) αφού ο χαρακτήρας EOF εισαχθεί, για να δείξει το τέλος εισαγώμενων δεδομένων. EOF δίνεται με ctrl-d ή ctrl-z, ανάλογα το λειτουργικό σύστημα. Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 28

43 44 45 46 case 'D': case 'd': ++d. Count; break; // // grade

43 44 45 46 case 'D': case 'd': ++d. Count; break; // // grade was uppercase D or lowercase d increment d. Count exit switch Ο έλεγχος είναι αναγκαίος μια grade was και δίνεται Enter μετά από uppercase F or lowercase f κάθε γράμμα-βαθμό. Αυτό increment f. Count προσθέτει ένα χαρακτήρα exit switch newline που πρέπει να αφαιρεθεί. Ομοίως πρέπει να ignore newlines, αγνοηθεί το κενό. tabs, 47 48 49 50 51 case 'F': case 'f': ++f. Count; break; // // 52 53 54 55 56 case 'n': case 't': case ' ': break; // // // and spaces in input Παρατηρούμε τη δήλωση default, // exit switch 57 58 59 60 61 default: // catch cout << "Incorrect letter grade entered. " << " Enter a new grade. " << endl; break; // optional; will exit switch anyway 62 63 64 65 Outline fig 02_22. cpp (3 of 4) που πιάνει (catches) όλες τις υπόλοιπες allπεριπτώσεις εισόδου. other characters } // end switch } // end while 66 Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 29

67 68 69 70 71 72 73 74 // output summary of results cout

67 68 69 70 71 72 73 74 // output summary of results cout << "nn. Totals for each letter grade are: " << "n. A: " << a. Count // display number of << "n. B: " << b. Count // display number of << "n. C: " << c. Count // display number of << "n. D: " << d. Count // display number of << "n. F: " << f. Count // display number of << endl; 75 76 return 0; 77 78 Outline A B C D F grades grades fig 02_22. cpp (4 of 4) // indicate successful termination } // end function main Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 30

Enter the letter grades. Enter the EOF character to end input. a B c

Enter the letter grades. Enter the EOF character to end input. a B c C A d f C E Incorrect letter grade entered. Enter a new grade. D A b ^Z Outline fig 02_22. cpp output (1 of 1) Totals for each letter grade are: A: 3 B: 2 C: 3 D: 2 F: 1 Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 31

2. 17 do/while Δομή επανάληψης Reproduced from the Power. Points for C++ How to

2. 17 do/while Δομή επανάληψης Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 33

1 2 3 // Fig. 2. 26: fig 02_26. cpp // Using the break

1 2 3 // Fig. 2. 26: fig 02_26. cpp // Using the break statement in a for structure. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 9 10 // function main begins program execution int main() { Outline fig 02_26. cpp (1 of 2) 11 12 int x; 13 14 15 // loop 10 times for ( x = 1; x <= 10; x++ ) { // x declared here so it can be used after the loop Έξοδος από τη δομή for μόλις εκτελεστεί το break. 16 17 18 19 // if x is 5, terminate loop if ( x == 5 ) break; // break loop only if x is 5 20 21 cout << x << " "; // display value of x 22 23 } // end for 24 25 cout << "n. Broke out of loop when x became " << x << endl; Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 35

26 27 28 29 return 0; // indicate successful termination } // end function

26 27 28 29 return 0; // indicate successful termination } // end function main 1 2 3 4 Broke out of loop when x became 5 Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. Outline fig 02_26. cpp (2 of 2) fig 02_26. cpp output (1 of 1) 2003 Prentice Hall, Inc. All rights reserved. 36

1 2 3 // Fig. 2. 27: fig 02_27. cpp // Using the continue

1 2 3 // Fig. 2. 27: fig 02_27. cpp // Using the continue statement in a for structure. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 9 10 11 12 // function main begins program execution int main() { // loop 10 times for ( int x = 1; x <= 10; x++ ) { Outline fig 02_27. cpp (1 of 2) Περνά στην επόμενη επανάληψη. next iteration of loop 13 14 15 16 // if x is 5, continue with if ( x == 5 ) continue; // skip remaining code in loop body 17 18 cout << x << " "; // display value of x 19 20 } // end for structure 21 22 23 cout << "n. Used continue to skip printing the value 5" << endl; 24 25 return 0; // indicate successful termination Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 38

26 27 } // end function main 1 2 3 4 6 7 8

26 27 } // end function main 1 2 3 4 6 7 8 9 10 Used continue to skip printing the value 5 Outline fig 02_27. cpp (2 of 2) fig 02_27. cpp output (1 of 1) Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 2003 Prentice Hall, Inc. All rights reserved. 39

2. 19 n n Λογικοί Τελεστές Χρησιμοποιούνται ως συνθήκες σε βρόγχους και δηλώσεις if

2. 19 n n Λογικοί Τελεστές Χρησιμοποιούνται ως συνθήκες σε βρόγχους και δηλώσεις if && (λογικό ΚΑΙ) n true αν και οι δύο συνθήκες είναι true if ( gender == 1 && age >= 65 ) ++senior. Females; n || (λογικό Ή) n true αν μία από τις δύο συνθήκες είναι true if ( semester. Average >= 90 || final. Exam >= 90 ) cout << "Student grade is A" << endl; Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 40

2. 19 n Λογικοί Τελεστές ! (λογικό NOT) n Επιστρέφει true όταν η συνθήκη

2. 19 n Λογικοί Τελεστές ! (λογικό NOT) n Επιστρέφει true όταν η συνθήκη είναι false, & αντιστρόφως if ( !( grade == sentinel. Value ) ) cout << "The next grade is " << grade << endl; Εναλλακτικά: if ( grade != sentinel. Value ) cout << "The next grade is " << grade << endl; Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 41

2. 21 Περίληψη δομών Reproduced from the Power. Points for C++ How to Program,

2. 21 Περίληψη δομών Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 45

2. 21 Περίληψη δομημένου προγραμματισμού Reproduced from the Power. Points for C++ How to

2. 21 Περίληψη δομημένου προγραμματισμού Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 47

2. 21 Περίληψη δομημένου προγραμματισμού Reproduced from the Power. Points for C++ How to

2. 21 Περίληψη δομημένου προγραμματισμού Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 48

2. 21 Περίληψη δομημένου προγραμματισμού Reproduced from the Power. Points for C++ How to

2. 21 Περίληψη δομημένου προγραμματισμού Reproduced from the Power. Points for C++ How to Program, 4/e by Deitel and Deitel © 2003. Reproduced by permission of Pearson Education, Inc. 49