1 / 16

Inleiding programmeren in C++ Life Science & Technology 26 januari 2004

Inleiding programmeren in C++ Life Science & Technology 26 januari 2004. http://www.liacs.nl/home/kosters/lst/ Universiteit Leiden. Week 2. Inhoud Variabelen, operatoren en controlestructuren Doel Leren omgaan met basiselementen van C++ Materiaal

ronia
Download Presentation

Inleiding programmeren in C++ Life Science & Technology 26 januari 2004

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. Inleiding programmeren in C++Life Science & Technology26 januari 2004 http://www.liacs.nl/home/kosters/lst/ Universiteit Leiden

  2. Week 2 • Inhoud • Variabelen, operatoren en controlestructuren • Doel • Leren omgaan met basiselementen van C++ • Materiaal • Ammeraal Hoofdstuk 2 (niet p. 23, niet 2.6) • Dictaat Hoofdstuk 3.2 en opgaven 6/14 (vooralsnog zonder functies) • De eerste programmeeropgave

  3. int – geheel getal int getal; // een geheel getal int a = 3, b = -5; // en nog twee // nu wel geinitialiseerd getal = a + b; // wordt dus -2 a = a + 7; // a wordt 10 b++; // equivalent aan b = b + 1 // b wordt dus -4 --a; // en nu wordt a 9: a = a - 1 getal += a; // betekent getal = getal + a // dus getal wordt -2 + 9 = 7 a = 19 / 7; // 2: geheeltallige deling van 19 door 7 b = 19 % 7; // 5: rest bij deling van 19 door 7

  4. double en float – reëel getal double x; // een reeel getal double temp = 36.5, euro = 2.20371; int i; i = 9 / 5; // resultaat 1 x = 9 / 5; // en weer 1 (1.0000) x = 9.0 / 5; // maar nu 1.8000 x = (double)9 / 5; // casting // nu 1.8000 x = static_cast<double>(9) / 5; // zelfde (chic) i = x + 0.5; // slim afronden const double pie = 3.28; pie = 3.14; // FOUT!!

  5. Typeconversie (cast) • Expliciete typeconversie • Is iets anders dan impliciet, zoals in x = 9.0 / 5 • Conventionele notatie: gewenst type tussen haakjes voor de expressie: (double)9 • Maar in (int) a + b / c wordt alleen a naar int geconverteerd! • Daarom is logischer (en dit mag ook allemaal) double(9) of double(a + b / c) • Nieuwe C++-notatie onderscheidt verschillende typen casts: static_cast<double>(9) of static_cast<int>(a + b / c)

  6. Afdrukken van double en float #include <iostream> #include <iomanip> using namespace std; ... double z = 92.36718; cout << "En z is: " << setw(8) // 8 posities breed << setprecision(2) // 2 cijfer na de komma afdrukken << fixed // vaste komma in plaats van scientific << showpoint // altijd een ., dus ook 88.00 << z // in plaats van 88 << endl; // en we zien 92.37

  7. char – karakter • ieder karakter correspondeert met een uniek geheel getal tussen 0 en 255: de ASCII-waarde ( ’\n’ blijkt 10) char letter; // een karakter char let1 = 'q', let2 = '$'; // en nog twee int i; // een geheel getal i = 'h' -'c'; // 5 (c-d-e-f-g-h) i = 'a'; // ASCII-waarde van a blijkt 97 cout << "Geef een karakter: "; cin >> letter; if ( 'a' <= letter && letter <= 'z') { // kleine letter? letter = letter + 'A' - 'a'; // letter naar hoofdletter converteren } // if // letter = letter - 32 cout << "Resultaat: " << letter << endl;

  8. bool – waar / niet waar • true = 1 • in C++ is alles wat niet 0 is, true; dus 7 is ook true! • false = 0 bool p, q; // twee booleans (niet geinitialiseerd!)

  9. Toekenningen int getal; bool p; getal = 17; // toekenning p = false; • l-value = r-value • aan een “l-value” mag worden toegekend; het is een variabele, een geheugenlocatie • de “r-value” is een expressie p = ( getal >= 15 ); // het feit of getal minstens 15 is if ( p ) cout << "Groot getal" << endl; if ( p == true ) ...; // beter: if ( p ) … if ( p = true ) ...; // ?????????????

  10. Variabelen – Scope #include <iostream> #include <iomanip> using namespace std; double koers = 2.20371; // scope hele programma int main ( ) { double nlg; // scope alleen functie main // zet bij voorkeur alle variabelen HIER cout << "Guldens: "; cin >> nlg; if ( nlg > 0 ) { double eur = nlg / koers; // scope alleen de if // dit liever niet doen cout << "Euro: " << fixed << setprecision(2) << eur << endl; } // if eur = 0; // FOUT!! niet in scope // dus niet doen return 0; } // main

  11. Operatoren en prioriteit • Prioriteit (zie boek p.50 …); gebruik liever punt 3! • Associativiteit • meeste operatoren (+, -, * en dergelijke) • links-associatief: a - b - c is (a - b) – c • toekenningen (=, += en dergelijke) en unaire operatoren (++, --) • rechts-associatief: a = b = 5 is a = (b = 5) • Zet overvloedig haakjes • Met behulp van operatoren worden expressies gebouwd

  12. While statement • While: iteratief statement van de vorm • while ( expressie ) statement • expressie is een relationele expressie (waar / niet waar) • het statement is vaak een compound-statement • Bij een while is het aantal “doorgangen” door de lus vaak onbekend of lastig te berekenen while ( n != 0 ) n = n - 1; while ( n ) n--; int i = 1, result = 0; while ( i <= n ) { result += i * i; i++; } // while int x = 1; while ( x < 1000 ) x = 2 * x; // x is nu 1024

  13. For statement • For: iteratief statement van de vorm • for (statement1 expressie1; expressie2) statement2 • statement1: initialisatie, heel soms null-statement • expressie1: test • expressie2: aan het einde van elke ronde, vaak de- of increment, heel soms null-statement • statement2: vaak een compound-statement met { } • Bij een for is het aantal “doorgangen” door de lus vaak wel bekend int i, result = 0; for ( i = 1; i <= n; i++ ) result += i * i;

  14. Dubbele for-loop De dubbele for-loop for ( i = 1; i <= 4; i++ ) { cout << i << ”: ”; for ( j = 1; j <= i; j++ ) cout << i*j << ” ”; cout << endl; } // for-i 1: 1 2: 2 4 3: 3 6 9 4: 4 8 12 16 levert op:

  15. For statement (2) for ( A; B; C) D komt overeen met A; while (B) { D; C; } for ( i = 1; i <= n; i++ ) result += i * i; i = 1; while ( i <= n ) { result += i * i; i++; } // while

  16. Stopt deze while-loop? int getal; cout << ”Geef een positief geheel getal ..”: cin >> getal; while ( getal != 1 ) if ( getal % 2 == 0 ) // getal even getal = getal / 2; else getal = 3 * getal + 1; Het “3x+1 vermoeden” zegt: dit stopt altijd! Voorbeeld: 11 – 34 – 17 – 52 – 26 – 13 – 40 – 20 – 10 – 5 – 16 – 8 – 4 – 2 – 1

More Related