dowhile int calculatorint a int b do int
Επαναληπτικές Εντολές – do/while int calculator(int a, int b) { do { int res = 0; char ch; printf(“Add, Substract, Multiply, Dividen”); do εντολή } while (έκφραση); { printf(“Enter first letter: n”); ch = getchar(); } while ( ch!=‘A’ && ch!=‘S’ && ch != ‘M’ && ch!=‘D’); if (ch==‘A’) res = a+b; else if (ch==‘S’) res = a-b; else if (ch==‘M’) res = a*b; else if (ch==‘D’) res = a/b; printf(“n %d %c %d = %dn”, a, ch, b, res); return res; } ΗΥ 150 – Προγραμματισμός 10 Κώστας Παναγιωτάκης
Έξοδος από βρόγχο #include <stdio. h> int main() /* Tameakh mhxanh*/ { float sum = 0, SUM = 0, product = 0; int plh 8 os, id = 1; while (1) { printf("Enter product price (give -1 for false, -2 for next customer and -3 for exit)): "); scanf("%f", &product); if (product == -1) { printf("GIVE again all the list of customer %dn", id); SUM = SUM - sum; sum = 0; continue; } else if (product == -2) { printf("sum(%d) = %. 2 fn", id, sum); id++; sum = 0; continue; } else if (product == -3) { printf("INCOMES of %d customers = %. 2 fn", id-1, SUM); break; } printf("Enter products' number: "); scanf("%d", &plh 8 os); } } sum += plh 8 os* product; SUM += plh 8 os* product; return 0; ΗΥ 150 – Προγραμματισμός 12 Κώστας Παναγιωτάκης
Παραδείγματα Bρόχων /*Εκτυπώνει τουs 10 πρώτους αριθμούς που το άθροισμα των ψηφίων τους είναι 7 και την μέση τους τιμή*/ int x = 1; //metablhth gia enan tyxaio ari 8 mo int sum = 0; //metablhth gia to # tvn ari 8 mwn float mean = 0; //metablhth gia th mesh timh while (sum < 10) { if (sum. Of. Digits(x) == 7) { mean += x; sum++; printf(“O %d ari 8 mos einai o %dn”, sum, x); } ++x; } mean = mean/10; printf(“H mesh tous timh einai %. 2 fn”, mean); ΗΥ 150 – Προγραμματισμός 20 Κώστας Παναγιωτάκης
Φωλιασμένοι βρόχοι #include <stdio. h> /*Υπολογίζει την πιθανότητα όλοι οι αριθμοί του Joker να είναι μονοί ή ζυγοί. */ /*Υπολογίζει την πιθανότητα όλοι οι αριθμοί του Joker να είναι πολλαπλάσια του ίδιου αριθμού. */ int main() { int x 1, x 2, x 3, x 4, x 5; //metablhtes ari 8 mwn joker int sum. P = 0, sum. MZ = 0, total = 0, w; //metablhtes gia to # tvn ari 8 mwn for for for { (x 1 (x 2 (x 3 (x 4 (x 5 = = = 1; x 1 <= x 1+1; x 2+1; x 3+1; x 4+1; x 5 41; <= <= ++x 1) 42; ++x 2) 43; ++x 3) 44; ++x 4) 45; ++x 5) total++; if (x 1 % 2 == 0 && x 2 % 2 == 0 && x 3 % 2 == 0 && x 4 % 2 == 0 && x 5 % 2 == 0 ) sum. MZ++; else if (x 1 % 2 == 1 && x 2 % 2 == 1 && x 3 % 2 == 1 && x 4 % 2 == 1 && x 5 % 2 == 1) sum. MZ++; for (w = 2; w <= x 1; ++w) { if (x 1 % w == 0 && x 2 % w == 0 && x 3 % w == 0 && x 4 % w == 0 && x 5 % w ==0) { /*x 1 = a 1*w, x 2 = a 2*w, x 3 = a 3*w, x 4 = a 4*w, x 5 = a 5*w*/ sum. P++; break; } } } printf("Monos h zugos: To plh 8 os tous einai %d kai exoun pi 8 anothta P = %. 2 f%cn", sum. MZ, (100. 0*sum. MZ)/(float)total, '%'); printf("Pollaplasia: To plh 8 os tous einai %d kai exoun pi 8 anothta P = %. 2 f%cn", sum. P, (100. 0*sum. P)/(float)total, '%'); } return 0; ΗΥ 150 – Προγραμματισμός 21 Κώστας Παναγιωτάκης
- Slides: 23