Développement des systèmes informatiques


Documentation de programmes avec "displays"


Voir aussi :


Exemple: recherche dichotomique

Définition du problème :

Une séquence de n entiers, a1, ..., an, n ≥ 1, et un entier x sont donnés.

Discussion :

DISPLAY 1

Spécification

find(x, A, r)

R0(,) = ((1 ≤ n) ∀i [ (1 ≤ i < n) ⇒ ('A[i] ≤ 'A[i+1]) ]) ⇒
∃i (1 ≤ i ≤ n) ('A[i] = 'x) ] =
true false
r.index' | 'A[r.index'] = 'x true
r.found' = true false
  NC(x, A)

 

Programme

  static class Result {
  int index = 0;
  boolean found = false;
  }
  public static void find(int e, int V[], Result res) {
    int low, high;
    initialization(); body()
  }

Spécifications de sous-programmes

initialization variables externes: e, V, res, low, high
R1(,) = (low' = 0) (high' = n-1) (res.found' = false) (res.index' = 0) NC(e, V)
body variables externes: e, V, res, low, high
R2(,) = (('low ≤ 'high) ( res.'found = false) ∀i [ ('low ≤ i < 'high) ⇒ ('V[i] ≤ 'V[i+1]) ]) ⇒
∃i [ ('low ≤ i ≤ 'high) ('V[i] = 'e) ] =
true false
res.index' | 'V[res.index' - 1] = 'e true
res.found' = true false
low' | true true
high' | true true
  NC(e, V)

 

DISPLAY 2

Spécification

body variables externes: e, V, res, low, high
R2(,) = (('low ≤ 'high) ( res.'found = false) ∀i [ ('low ≤ i < 'high) ⇒ ('V[i] ≤ 'V[i+1]) ]) ⇒
∃i [ ('low ≤ i ≤ 'high) ('V[i] = 'e) ] =
true false
res.index' | 'V[res.index' - 1] = 'e true
res.found' = true false
low' | true true
high' | true true
  NC(e, V)

Programme

Nouvelle variable (à déclarer dans le bloc englobant) : int med;

while (! res.found && (low <= high) ) {
  med = (low + high) / 2;
  test();
}

Spécifications de sous-programmes

test variables externes: e, V, res, low, high, med
R3(,) = ('low ≤ 'med ≤ 'high) ⇒
'V['med]
< 'e = 'e > 'e
res.index' | true res.index' = 'med true
res.found' = res.'found true res.'found
low' = 'med + 1 'low 'low
high' = 'high 'high 'med - 1
  NC(e, V, med)

 

DISPLAY 3

Spécification

test variables externes: e, V, res, low, high, med
R3(,) = ('low ≤ 'med ≤ 'high) ⇒
'V['med]
< 'e = 'e > 'e
res.index' | true res.index' = 'med true
res.found' = res.'found true res.'found
low' = 'med + 1 'low 'low
high' = 'high 'high 'med - 1
  NC(e, V, med)

Programme

if (V[med] < e) low = med + 1;
else if (V[med] > e) high = med - 1;
else {
  res.index = med;
  res.found = true;
}

Spécifications de sous-programmes

Aucune

 

DISPLAY 4

Spécification

initialization variables externes: e, V, res, low, high
R1(,) = (low' = 0) (high' = n-1) (res.found' = false) (res.index' = 0) NC(e, V)

Programme

  low = 0;
  high = n-1;
  res.found = false;
  res.index = 0

Spécifications de sous-programmes

Aucune

Code de la fonction find après l'assemblage

  public static void find(int e, int V[], Result res) {
    int low, high;

    // initialization
    low = 0;
    high = n-1;
    res.found = false;
    res.index = 0;

    // body
    while (! res.found && (low <= high) ) {
      int med;
      med = (low + high) / 2;
      // test
      if (V[med] < e) low = med + 1;
      else if (V[med] > e) high = med - 1;
      else {
        res.index = med;
        res.found = true;
      }
    }
  }