Skip to content

Commit

Permalink
Libreria de conversiones, terminando la parte de decimales
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian-Martinez-Rincon committed Jul 23, 2022
1 parent 9fd617e commit 1016f33
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 4 deletions.
142 changes: 142 additions & 0 deletions Pruebas/pruebas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
function binaryAddition(nro){
/**
* Sumo un 1 en binario.
*/
let myFunc = num => Number(num);
var intArr = Array.from(nro, myFunc);
var tamanio = intArr.length;

while (true){
if (intArr[tamanio-1] == 1){
intArr[tamanio-1] = 0;
}
else{
if (tamanio>0){
intArr[tamanio-1] = 1;
return intArr.join('');
}
intArr.unshift(1)
return intArr.join('');
}
tamanio = tamanio -1;
}
}

function toDecimal(nro){
var aux = 0;
var binArr = nro.split('').reverse();
binArr.forEach((item, index) => item === '1' ? aux += Math.pow(2, index) : void 0);
return aux;
}

function convertToBinary (number) {
if (number > 0) {
return convertToBinary( parseInt(number / 2) ) + (number % 2)
};
return '';
}

function toBinario(nro){
if (nro[0] !== '-'){
return '0' + convertToBinary(nro);
}

return '1' + convertToBinary(nro.slice(1));
}

function toBss(nro){
var aux = toDecimal(nro);
return [nro,aux.toString()];
}

function toBcs(nro){
var aux = toDecimal(nro.slice(1));
aux = nro[0] === '1' ? aux*-1: aux;
return [nro,aux.toString()];
}

function toCa1(nro){
if (nro[0] === '0'){
return toBss(nro);
}
var ca1 = nro.
replace(/\s|[1]/g,'2').
replace(/\s|[0]/g,'1').
replace(/\s|[2]/g,'0');

var aux = toDecimal(ca1);
return [ca1,(aux*-1).toString()];
}

function toCa2(nro){
if (nro[0] === '0') {
return toBss(nro);
}
var ca1 = toCa1(nro);
var ca2 = binaryAddition(ca1[0]);
var aux = toDecimal(ca2);

return [ca2,(aux*-1).toString()];
}

function toEx2(nro){
const aux = toDecimal(nro);
const result = aux - Math.pow(2, (nro.length)-1);
return [nro,result];
}

function puedeRepresentar(nro,bits){
var aux = toBinario(nro);
if (Number(bits)<aux.length){
return false;
}
return true;
}

function toDecimalBit(nro,bits){
var aux = toBinario(nro);
for (var i = aux.length; i<Number(bits); i++){
aux = '0' + aux;
}
return aux;
}



function toBinarioBss(nro){
return toBinario(nro);
}

function toBinarioBcs(nro){
return toBinario(nro);
}


function toBinarioCa1(nro){
aux = toBinario(nro);
if (aux[0] === '0'){
return toBss(aux);
}
var ca1 = '1' + aux.substring(1).
replace(/\s|[1]/g,'2').
replace(/\s|[0]/g,'1').
replace(/\s|[2]/g,'0');
var dec = toDecimal(ca1.substring(1));
return [ca1,(dec*-1).toString()];
}
console.log(toBinarioCa1('-21'));

function toBinarioCa2(nro){
if (Number(nro) > 0) {
return toBss(aux);
}
var ca1 = toBinarioCa1(nro);
var ca2 = binaryAddition(ca1[0]);
var aux = toDecimal(ca2.substring(1));
return [ca2,(aux*-1).toString()];
}
console.log(toBinarioCa2('-21'));

function toBinarioEx2(){

}
1 change: 1 addition & 0 deletions Pruebas/tempCodeRunnerFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
toBinarioBss
11 changes: 10 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ function binarioProcesado(nro){
allMaxMin(bits);
rangos(bits);
pf.OUTPUT_BITS.value = "Bits: " + bits;

}

function decimalBit(nro,bit){

}

pf.BTN.addEventListener('click', () => {
Expand All @@ -56,8 +61,12 @@ pf.BTN.addEventListener('click', () => {
binarioProcesado(inputBinario);
}
else if (!(esBinario) && (esDecimal) && !(esBitImplicito)){
var aux = conversion.toBinario(inputDecimal)
var aux = conversion.toBinario(inputDecimal);
console.log(aux);
binarioProcesado(aux);
if (inputDecimal[0] ==='-'){
pf.OUTPUT_BSS.value = 'No tiene representación ';
}
}
else if (!(esBinario) && (esDecimal) && (esBitImplicito)){
decimalBit(inputDecimal,inputBit);
Expand Down
File renamed without changes.
23 changes: 20 additions & 3 deletions scrips/conversion.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function binaryAddition(nro){
export function binaryAddition(nro){
/**
* Sumo un 1 en binario.
*/
Expand All @@ -21,7 +21,7 @@ function binaryAddition(nro){
tamanio = tamanio -1;
}
}

export function toDecimal(nro){
var aux = 0;
var binArr = nro.split('').reverse();
Expand All @@ -38,7 +38,7 @@ export function convertToBinary (number) {

export function toBinario(nro){
if (nro[0] !== '-'){
return convertToBinary(nro);
return '0' + convertToBinary(nro);
}

return '1' + convertToBinary(nro.slice(1));
Expand Down Expand Up @@ -84,3 +84,20 @@ export function toEx2(nro){
const result = aux - Math.pow(2, (nro.length)-1);
return [nro,result];
}

export function puedeRepresentar(nro,bits){
var aux = toBinario(nro);
if (Number(bits)<aux.length){
return false;
}
return true;
}

export function toDecimalBit(nro,bits){
var aux = toBinario(nro);
for (var i = aux.length; i<Number(bits); i++){
aux = '0' + aux;
}
return aux;
}

0 comments on commit 1016f33

Please sign in to comment.