Le forum des 7
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
-40%
Le deal à ne pas rater :
-40% sur le Pack Gaming Mario PDP Manette filaire + Casque filaire ...
29.99 € 49.99 €
Voir le deal

Correction du tp du 10mars...

Aller en bas

Correction du tp du 10mars... Empty Correction du tp du 10mars...

Message  Ewasx Mer 10 Mar - 16:39

Bits.c
Code:

#include "bits.h"

unsigned char teste_bit(unsigned char octet,int b){
  return (octet & (1 << b))?1:0;
}


void allume_bit(unsigned char *octet,int b){
  *octet = (*octet) | (1 << b);
}


void eteint_bit(unsigned char *octet,int b){
  *octet = (*octet) & ~(1 << b);
}



Bits.h
Code:
#ifndef __BITS__H
#define __BITS__H

/*constante qui donne le nombre de bits dans un octet*/

#define CHAR_BIT 8

/* prototypes de fonctions manipulant les bits*/

unsigned char teste_bit(unsigned char octet,int b);
void allume_bit(unsigned char * octet,int b);
void eteint_bit(unsigned char * octet,int b);


#endif
Ewasx
Ewasx
Humain
Humain

Messages : 85
Date d'inscription : 26/09/2009
Age : 35
Localisation : Vitry-Sur-Seine

Revenir en haut Aller en bas

Correction du tp du 10mars... Empty Re: Correction du tp du 10mars...

Message  Ewasx Mer 10 Mar - 16:42

ensemblre.c
Code:
#include "ensemble.h"
#include "bits.h"
#include <stdio.h>
#include <stdlib.h>

void init_ensemble(unsigned char *t){
  int i;
  for(i = 0 ; i < MAX ; i++){
    t[i] = 0;
  }
}


void ajoute(int val, unsigned char e[]){
  int indice = val / CHAR_BIT;
  int b = val % CHAR_BIT;

  if(indice >= MAX){
    fprintf(stderr,"ATTENTION : la taille max du tableau ne permet par l'ajout de %d\n",val);
    return;
  }

  allume_bit(&(e[indice]),b);
}


void ajoute_ensemble(unsigned char e1[], unsigned char e2[]){
  int i,j,val;

  for(i = 0 ; i < MAX ; i++){
    for(j = 0 ; j < CHAR_BIT ; j++){
      val = i * CHAR_BIT +j;
      if(appartient(val,e2)){
        ajoute(val,e1);
      }
    }
  }

}

void affiche_ensemble(unsigned char e[]){
  int i,j;
  int prems = 1;
  if(e == NULL){
    printf("{}\n");
    return;
  }
  printf("{");
  for(i = 0 ; i < MAX ; i ++){
    for(j = 0 ; j < CHAR_BIT ; j++){
      if(teste_bit(e[i],j)){
        if(!prems){
          printf(",");
        }
        else{
          prems = 0;
        }
        printf("%d",i * CHAR_BIT + j);
      }
    }
  }
  printf("}\n");
}

char appartient(int val, unsigned char e[]){
  int indice = val / CHAR_BIT;
  int bit = val % CHAR_BIT;
  if(indice >= MAX){
    return 0;
  }
  return teste_bit(e[indice],bit);
}


void unio(unsigned char e[],unsigned  char e1[],unsigned char e2[]){
int i;
  init_ensemble(e);
 /* ajoute_ensemble(e,e1);
  ajoute_ensemble(e,e2);*/
  for(i = 0 ; i < MAX ; i++){
    e[i] = e1[i] | e2[i];
  }
}

void intersection(unsigned char e[],unsigned char e1[],unsigned char e2[]){
  init_ensemble(e);
  int i;

  for(i = 0 ; i < MAX ; i++){
  /*   
  *    for(j = 0 ; j < CHAR_BIT ; j++){
      val = i * CHAR_BIT + j;
      if(appartient(val,e1) && appartient(val,e2)){
        ajoute(val,e);
      }
    }
    */
  e[i] = e1[i] & e2[i];
  }
}


int cardinal(unsigned char e[]){
  int i,j,cnt = 0;

  for(i = 0 ; i < MAX ; i++){
    for(j = 0 ; j < CHAR_BIT ; j++){
      if(teste_bit(e[i],j)){
        cnt++;
      }
    }
  }
  return cnt;
}


void complementation(unsigned char eb[],unsigned char e[]){
  int i;

  for(i = 0 ; i< MAX ; i++){
      eb[i] = ~(e[i]);
  }
}


void difference(unsigned char e[], unsigned char a[], unsigned char b[]){
  init_ensemble(e);
  int val;

  for(val = 0 ; val < CHAR_BIT * MAX ; val++){
    if(appartient(val,a) && ! appartient(val,b)){
      ajoute(val,e);
    }
  }
}

void diff_sym(unsigned char e[],unsigned char a[], unsigned char b[]){
  unsigned char c[MAX],d[MAX];
  unio(c,a,b);
  intersection(d,a,b);
  difference(e,c,d);
}

ensemble.h
Code:
#ifndef __ENSEMBLE__H
#define __ENSEMBLE__H

#define MAX 100


/* prototypes des fonctions */

void init_ensemble(unsigned char e[]);
void ajoute(int val, unsigned char e[]);
void affiche_ensemble(unsigned char e[]);
char appartient(int val,unsigned char e[]);
void unio(unsigned char e[],unsigned char e1[],unsigned char e2[]);
void intersection(unsigned char e[],unsigned char e1[],unsigned char e2[]);
int cardinal(unsigned char e[]);
void complementation(unsigned char eb[],unsigned char e[]);
void difference(unsigned char e[], unsigned char a[], unsigned char b[]);
void diff_sym(unsigned char e[],unsigned char a[], unsigned char b[]);

#endif
Ewasx
Ewasx
Humain
Humain

Messages : 85
Date d'inscription : 26/09/2009
Age : 35
Localisation : Vitry-Sur-Seine

Revenir en haut Aller en bas

Revenir en haut


 
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum