Hoofdstuk 10.2

15
Hoofdstuk 10.2 Rekenmachine- casus

description

Hoofdstuk 10.2. Rekenmachine-casus. Voorbeeld: Rekenmachine. Voorbeeld: Rekenmachine. Twee aspecten: User-interface Werking. class Calculator : Form. class Processor. Rekenmachine: User-interface. Label. TableLayoutPanel. Button. Calculator ( ). {. - PowerPoint PPT Presentation

Transcript of Hoofdstuk 10.2

Page 1: Hoofdstuk 10.2

Hoofdstuk 10.2

Rekenmachine-casus

Page 2: Hoofdstuk 10.2

Voorbeeld: Rekenmachine

Page 3: Hoofdstuk 10.2

Voorbeeld: Rekenmachine

Twee aspecten: User-interface

Werking

class Calculator: Form

class Processor

Page 4: Hoofdstuk 10.2

Rekenmachine: User-interface

Label

TableLayoutPanel

Calculator ( ){

}

paneel = new TableLayoutPanel( );

this.Controls.Add( paneel );

Button

for (int t=0; t<16; t++){

}

Button knop = new Button();paneel.Controls.Add( knop );

Buttons zijnondergeschikt aan

het paneel

Page 5: Hoofdstuk 10.2

User-interfaceCalculator ( ){

}

paneel = new TableLayoutPanel( );

this.Controls.Add( paneel );

for (int t=0; t<16; t++){ Button knop = new Button();

paneel.Controls.Add( knop );

Buttons zijnondergeschikt aan

het paneel}

knop.Text = "789/456*123+0C=-"[t].ToString();knop.Dock = DockStyle.Fill;knop.Click += this.klik;knop.KeyPress += this.toets;knop.Resize += this.groei;

paneel.Columncount = 4;for (int t=0; t<4; t++)

paneel.ColumnStyles.Add( ...25%... );

Page 6: Hoofdstuk 10.2

Opbouw klasse Calculator

class Calculator : Form {

Calculator ( ) {

result = new Label( );result.TextAlign = ContentAlignment.MiddleRight;// en de rest van de Userinterface-opbouwproc = new Processor ( );

}

Label result;TableLayoutPanel paneel;Proc proc;

membervariabelenook nodig in

EventHandlers

Page 7: Hoofdstuk 10.2

Calculator Event-handlersclass Calculator : Form {

void klik (object obj, EA ea){}

Label result; Proc proc;

void toets(object obj, KPEA kpea ){}void groei(object obj, EA ea ){

}

verwerk( kpea.KeyChar );

verwerk( ((Button) object) . Text[0] );

Control c = (Control) obj;

c.Font = new Font("Tahoma", h);

int h = c.Height / 2;if (c==result) h = c.Height/3;

Page 8: Hoofdstuk 10.2

Calculator Event-handlersclass Calculator : Form {

void verwerk (char c){

Label result; Proc proc;

}

if (c==‘C’) proc.Schoon();else if (c==‘=’) proc.Reken();else if (c>=‘0’&&c<=‘9’)proc.Cijfer(c- ‘0’);else proc.Operatie(c);

result . Text ( );proc . Scherm

Page 9: Hoofdstuk 10.2

Opbouw klasse Proc

class Proc{

void Schoon ( ) { ... }void Reken ( ) { ... }void Cijfer (int n) { ... }void Operatie (char c) { ... }

Proc ( ) { this . Schoon(); }

wat kaneen Proc?

wat iseen Proc?

long Scherm;

Page 10: Hoofdstuk 10.2

Wat is een Proc?

waarde op het scherm

schermwaarde wordt tienmaalzo groot plus cijfer

er lijkt niets te gebeuren...

huidige waarde vorige waarde laatst gebruikte operator

3737293348

Page 11: Hoofdstuk 10.2

Opbouw klasse Proc

class Proc{

void Schoon ( ) { ... }void Reken ( ) { ... }void Cijfer (int n) { ... }void Operatie (char c) { ... }

Proc ( ) { this . Schoon(); }

wat kaneen Proc?

wat iseen Proc?

long Scherm;long waarde, vorige;char op;

Page 12: Hoofdstuk 10.2

Proc’s methode cijfer

class Proc{

void Cijfer (int n){

}

long scherm, waarde, vorige;char op;

waarde = 10*waarde + n;

scherm = waarde;

Page 13: Hoofdstuk 10.2

Proc’s methode reken

class Proc{

void Reken ( ){

}

long scherm, waarde, vorige;char op;

if (op==‘+’)if (op==‘-’)if (op==‘*’)if (op==‘/’)scherm = vorige;waarde = 0;

vorige += waarde;vorige -= waarde;vorige *= waarde;vorige /= waarde;

Page 14: Hoofdstuk 10.2

Proc’s methode operatie

class Proc{

void Operatie (char c){

}

long scherm, waarde, vorige;char op;

op = c;this . reken ( );

void Schoon ( ){ waarde = 0;

vorige = 0;

scherm = 0;

op =}

‘+’;

Page 15: Hoofdstuk 10.2

Testen van een reeks mogelijke waarden

if (x==1)else if (x==2)else if (x==3)else if (x==4)else if (x==5)else if (x==6)else if (x==7)else

this.een();this.twee();this.drie();this.vier();this.vijf();this.zes();this.zeven();this.meer();

case 1:case 2:case 3:case 4:case 5:case 6:case 7:default:

switch (x){

}

break;break; break; break; break; break; break;