Code fill-in-the-blank coupling with program modules

程式碼填空與程式模組耦合性 

敘述統計學上中位數和平均數均為數據資料的集中趨勢,中位數是將一

組數值資料由小到大排列,最中間的數值為中位數。若有奇數個資料,

則取最中間的數值為中位數,例如 1, 2, 3, 3, 4, 6, 7, 7, 21 的中位數是 4;

若有偶數個資料,則取最中間兩個數值的平均為中位數,例如 1, 2, 3, 3,

4, 6, 7, 7, 8, 21 的中位數是(4+6)/2=5。算術平均數是將一組數值加總,

除以這組數值的個數,例如 1, 2, 3, 3, 4, 6, 7, 7, 21 的算術平均數=54/9=6。

以下 C++程式輸出:4, 6,

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

#include <iostream>

#include <algorithm>

using namespace std;

class Compute{

public:

void setData(int *, int);

(I) =0;

protected:

int *data, d_size;

};

class ComputeMedian: public Compute{

public:

double getNum();

};

class ComputeMean: public Compute{

public:

double getNum();

};

void Compute::setData(int *d, int s){

data = d;

d_size = s;

sort(data, data+d_size);

}

double ComputeMedian::getNum(){

if ( (II) ==1) return data[d_size/2];

else return (data[ (III) ]+data[1+d_size/2])/2.0;

}

double ComputeMean::getNum(){

double sum=0, avg=0;

for (int i=0; i<d_size; i++)

sum = sum + data[i];

return ( (IV) );

}

class Report {

public:

Report(int *d, int s){

data = d;

d_size = s;

}

void setCompute(Compute *c) { cp = c; }

void report(){

cp-> (V) ;

cout<<cp->getNum()<<", ";

}

private:

int *data, d_size;

Compute *cp;

};

int main(){

int data[10] ={6, 7, 1, 21, 2, 3, 4, 3, 7};

Report r(data, 9);

Compute *cp1 = new ComputeMedian();

Compute *cp2 = new ComputeMean();

r.setCompute(cp1);

r.report();

r.setCompute(cp2);

r.report();

return 0;

}

請完成程式碼(I~V)使程式正常運作。(15 分)

請根據程式碼完成下面 UML 類別圖的關係連線,並說明此設計對模

組耦合性(Coupling)的影響。(10 分



Comments

Popular posts from this blog

How to write data into a excel file using vbscript

Format date as yyyy-mm-dd using vbscript