/* * Pierre Hyvernat, exemple de programme pour le TP2 du cours info803. C'est * presque une correction... */ #include #include #include typedef unsigned char octet ; octet masque[8] = { 1,2,4,8,16,32,64,128 }; #define INVERSE /* pour inverser l'ordre des bits dans un octet */ octet inverse_octet(octet o) { octet u=o; octet v=0; int i,b; for(i=0;i<8;i++) { b=u&1; u=u>>1; v=v<<1;v=v|b; } return v; } void process_octets(octet*T,int n) { int i; #ifdef INVERSE int o; for(i=0;i>1; } } void print_float(const float x) { octet X[4] ; memcpy(X,&x, sizeof x) ; process_octets(X,4) ; octet w ; int i ; for(i=0;i<34;i++) { if((i==0) | (i==9) | (i==18) | (i==26)) printf("."); else printf(" "); } printf("\n"); /* bit de signe */ w=X[0] ; printf("%u ",w&masque[0]); w=w>>1; /* exposant */ for(i=0;i<7;i++) { printf("%u",w&masque[0]); w=w>>1; } w=X[1]; printf("%u ",w&masque[0]); w=w>>1; /* mantisse */ for(i=0;i<7;i++) { printf("%u",w&masque[0]); w=w>>1; } w=X[2]; for(i=0;i<8;i++) { printf("%u",w&masque[0]); w=w>>1; } w=X[3]; for(i=0;i<8;i++) { printf("%u",w&masque[0]); w=w>>1; } printf("\n"); } int main (void) { octet xo[4] ; float x=0; float y=0; float res=0; float t; char c; int i,j; while(1) { c = getchar(); switch(c) { case 'q' : printf("Quitting...\n") ; return(1) ; break ; case 'r' : printf("Representation of X:\n") ; print_float(x) ; printf("\nRepresentation of Y:\n") ; print_float(y) ; printf("\nRepresentation of R:\n") ; print_float(res) ; break ; case 'v' : printf("Value of X: %f\n",x) ; printf("\nValue of Y: %f\n",y) ; printf("\nValue of R: %f\n",res) ; break ; case 'X' : printf("Set value of X: "); scanf("%f",&x) ; break ; case 'Y' : printf("Set value of Y: "); scanf("%f",&y) ; break ; case 'b' : printf("Modify bit in X:\n") ; printf(" Which byte? ") ; i=getchar()-48; printf(" Which bit? ") ; j=getchar()-48; if ((i>0) && (i<5) && (j>0) && (j<9)) { memcpy(xo,&x,4) ; process_octets(xo,4); xo[i-1] = xo[i-1] ^ masque[j-1] ; process_octets(xo,4); memcpy(&x,xo,4) ; } break ; case 'S' : printf("Swap X and Y.\n") ; t=x; x=y; y=t; break ; case 'R' : printf("Swap X and R.\n") ; t=x; x=res; res=t; break ; case 'C' : if (yx) printf("X < Y.\n") ; else if (y==x) printf("X == Y.\n") ; else printf("Comparison failed!!!\n") ; break ; case 'M' : printf("Multiply X and Y in R.\n") ; res = x*y ; break ; case 'A' : printf("Add X and Y in R.\n") ; res = x+y ; break ; case 'D' : printf("Divide X by Y in R.\n") ; res = x/y ; break ; case '\n' : break ; case 'h' : printf("Help: press \'q\'(uit), \'r\'(epresentation), \'v\'(alues), \'X\' or \'Y\' (set value of X or Y), \'b\' (modify bit in X), \'S\'(wap X and Y), \'R\' (swap X and R), \'C\'(ompare X and Y), \'A\'(dd X and Y), \'M\'(ultiply X and Y), \'D\'(ivide X by Y) or \'h\'(elp).\n"); break ; /* rajouter : 3 registres : x,y et res, multiplication, addition, division */ default : printf("???\n") ; break ; } } return(-1); }