This repository has been archived by the owner on Dec 23, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase.c
86 lines (80 loc) · 2.56 KB
/
case.c
1
2
3
4
5
6
7
8
9
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* FICHIER CASE.C
* HEADER CASE.C
* Fonction en rapport aux case du sudoku
*/
#include <stdio.h>
#include <assert.h>
#include "case.h"
void afficherCandidatsCase(T_case ca) {
if( ca.val == 0 ) {
int i = 0;
while( i < ca.n_candidats ) {
printf("%d ", ca.candidats[i]);
++i;
}
}
}
void initCase(T_case* ca)
{
ca->val = 0;
ca->n_candidats = 9;
int i = 0;
while( i < 9 ) {
ca->candidats[i] = i + 1;
++i;
}
}
int rechIndice(int t[], int n, int val)
{
int i = 0;
/* (0 ≤ n ≤ 9) ∧ (0 < val ≤ 9) ∧ (0 ≤ i < 9) */
assert( (n >= 0) && (n <= 9) && (val > 0) && (val <= 9) && (i >= 0) && (i < 9) );
while( i < n ) {
/* (0 ≤ n ≤ 9) ∧ (0 < val ≤ 9) ∧ (i < n) */
assert( (n >= 0) && (n <= 9) && (val > 0) && (val <= 9) && (i < n) );
if( t[i] == val) {
/* (O ≤ i < n) ∧ (t[i] = val) */
assert( (i >= 0) && (i < n) && (t[i] == val) );
return i;
}
++i;
/* (0 ≤ n ≤ 9) ∧ (0 < val ≤ 9) ∧ (i < n)*/
assert( (n >= 0) && (n <= 9) && (val > 0) && (val <= 9) && (i < n+1) );
}
/* (0 ≤ n ≤ 9) ∧ (0 < val ≤ 9) ∧ (0 ≤ i < 9) ∧ (i ≥ n) */
assert( (n >= 0) && (n <= 9) && (val > 0) && (val <= 9) && (i >= n) );
return n;
}
void suppVal(int t[], int n, int indiceVal)
{
/* (0 ≤ indiceVal < 9) ∧ (0 ≤ n ≤ 9) */
assert( (indiceVal >= 0) && (indiceVal < 9) && (n >= 0) && (n <= 9) );
if( indiceVal < n ) {
int i = indiceVal;
/* (0 ≤ indiceVal < 9) ∧ (0 ≤ n ≤ 9) ∧ (indiceVal < n) */
assert( (indiceVal >= 0) && (indiceVal < 9) && (n >= 0) && (n <= 9) && (indiceVal < n) );
while( i < n ) {
/* (0 ≤ indiceVal < 9) ∧ (0 ≤ n ≤ 9) ∧ (indiceVal < n) ∧ (i < n) */
assert( (indiceVal >= 0) && (indiceVal < 9) && (n >= 0) && (n <= 9) && (indiceVal < n) && (i < n) );
t[i] = t[i+1];
++i;
/* (0 ≤ indiceVal < 9) ∧ (0 ≤ n ≤ 9) ∧ (indiceVal < n) ∧ (i < n) */
assert( (indiceVal >= 0) && (indiceVal < 9) && (n >= 0) && (n <= 9) && (indiceVal < n) && (i < n+1) );
}
/* (0 ≤ indiceVal < 9) ∧ (0 ≤ n ≤ 9) ∧ (indiceVal < n) ∧ (i ≥ n) */
assert( (indiceVal >= 0) && (indiceVal < 9) && (n >= 0) && (n <= 9) && (indiceVal < n) && (i >= n) );
}
/* (0 ≤ indiceVal < 9) ∧ (0 ≤ n ≤ 9) ∧ (indiceVal ≥ n) */
assert( (indiceVal >= 0) && (indiceVal < 9) && (n >= 0) && (n <= 9));
}
int suppCandidats(T_case* ca, int val)
{
int indiceVal;
indiceVal = rechIndice(ca->candidats, ca->n_candidats, val);
if( indiceVal != ca->n_candidats ) { // la valeur existe
suppVal(ca->candidats, ca->n_candidats, indiceVal);
ca->n_candidats--;
return 1;
}
return 0;
}