#include "stdafx.h"
#include "stdio.h"
#include "math.h"
int A[]={127,-2,2,5,1,-8,-127,4,45,-45,23,67,-23};
int i;
int n;
int x1;
int a;
int b;
int x2;
int c;
int x3;
int d;
int x4;
int e;
int x5;
int f;
int x6;
char num;
int B= sizeof(A)/4;
char Bi[6];
printf(" %d \n",B);
for (i=0;i0){
n=A[i];
a=n%2;
x1=n/2;
b=x1%2;
x2=x1/2;
c=x2%2;
x3=x2/2;
d=x3%2;
x4=x3/2;
e=x4%2;
x5=x4/2;
f=x5%2;
x6=x5/2;
printf("El numero %d es: %d%d%d%d%d%d%d\n",A[i],x6,f,e,d,c,b,a);
}
else{
n=abs(A[i]);
a=n%2;
x1=n/2;
b=x1%2;
x2=x1/2;
c=x2%2;
x3=x2/2;
d=x3%2;
x4=x3/2;
e=x4%2;
x5=x4/2;
f=x5%2;
x6=x5/2;
if(x6==1){x6=0;}
else{x6=1;}
if(d==1){d=0;}
else{d=1;}
if(c==1){c=0;}
else{c=1;}
if(b==1){b=0;}
else{b=1;}
if(a==1){a=0;}
else{a=1;}
if(e==1){e=0;}
else{e=1;}
if(f==1){f=0;}
else{f=1;}
printf("El numero %d es: %d%d%d%d%d%d%d\n",A[i],x6,f,e,d,c,b,a);
}
}
scanf( "%i", &num );
return 0;
}
lunes, 31 de octubre de 2011
domingo, 30 de octubre de 2011
sábado, 29 de octubre de 2011
Decodificacion en C++
Decodificacion en C++ de numeros positivos y negativos
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
char* numerounico(char dato[]); //Para transformar cuando es un solo bit
char* numeronegativo(char a[]); //detecta si el numero es negativo
double transformacion(char binario[]);//transforma a decimal el string de binarios
int main(int argc, char* argv[]){
double y; //numero a mostrar
char secuencia_bits[]={"0101"}; //secuencia entrante de bits
if (secuencia_bits[0]=='0'){//detecta si el string de bits es negativo
if (strlen(secuencia_bits)==1){ //detecta si el string es de un solo bit
y=(-2)*transformacion(numerounico(secuencia_bits));
}
else{ //si es mas grande que uno llama a la subfuncion numero negativo
y=(-1)*transformacion(numeronegativo(secuencia_bits));
}
}
else {// El string de bits es positivo
if (strlen(secuencia_bits)==1){ //detecta si es un solo bit
y=2*transformacion(secuencia_bits);
}
else{
y=transformacion(secuencia_bits);//transforma directamente el a decimales
}
}
cout<<y<<endl; //imprime el numero en decimal positivo o negativo
system("PAUSE");
return EXIT_SUCCESS;
}
char* numerounico(char dato[]){ //reemplaza unos por ceros para hacer el complemento cuando es numero unico
if (dato[0]=='0'){
dato[0]='1';
}
else{
dato[0]='0';
}
return dato;
}
char* numeronegativo(char a[]){ //reemplaza uno por ceros cuando es mayor a uno la cadena de caracteres
int i;
double p;
for(i=0;i<strlen(a);i++){
if(a[i]=='0'){
a[i]='1';
}
else if(a[i]=='1'){
a[i]='0';
}
}
return a;
}
double transformacion(char binario[]){ //transforma la cadena de binarios a decimal
int i,j;
double h=0;
for(i=strlen(binario);i>-1;i--){
if(binario[i]=='1'){
j=strlen(binario)-i;
h+=pow(2,j);
}
}
h=h/2;
return h;
}
jueves, 27 de octubre de 2011
Suscribirse a:
Entradas (Atom)