Jan 23, 2018 at 11:56pm UTC
#include <string>
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
/*int buscar_menores(int pivote const std::vector<std::string> &datos,
std::vector<int> &menores);*/
int pasar_decimal (int pivote, const std::vector<std::string> &datos,
std::vector<int> decimales);
int main(int argc, char *argv[]){
int pivote = 32;
std::string numero_binario;
std::vector<std::string> datos;
std::vector<int> decimales;
std::vector<int> menores;
std::ifstream fent;
if(argc < 2){
std::cerr << "Es necesario especificar el fichero que " <<
"desea abrir";
return 1;}
if(argc == 3){
pivote = std::atoi(argv[2]);}
if(argc > 3){
std::cerr << "Solo se admiten tres parametros de entrada.";
return 1;}
std::string nombre = argv[1];
std::string nombre_fichero = nombre;
std::size_t buscardat = nombre.find(".dat");
std::size_t buscardatos = nombre.find("datos");
if(buscardat == std::string::npos){
nombre_fichero = nombre_fichero + ".dat";}
if(buscardatos == std::string::npos){
nombre_fichero = "datos" + nombre_fichero;}
std::cerr << "\n\nFichero a abrir: " << nombre_fichero << ".\n";
fent.open(nombre_fichero);
if(fent.fail()){
std::cerr << "\n\nNo se pudo leer el archivo";
return 2;}
fent >> numero_binario;
while(fent.good()){
datos.push_back(numero_binario);
fent >> numero_binario;}
std::cerr << "\nSe han leido los siguientes datos: ";
for(std::size_t i = 0 ; i < datos.size() ; i++){
if(i == 0){
std::cerr << datos[i];}
else if(i > 0 && i < (datos.size() - 1)){
std::cerr << ", " << datos[i];}
else{
std::cerr << ", " << datos[i] << ".\n" << std::endl;}}
pasar_decimal( pivote, datos, decimales);
fent.close();}
int pasar_decimal (int pivote, const std::vector<std::string> &datos,
std::vector<int> decimales){
int exp = 0;
std::string bin;
//std::string c;
int d;
int a = 0;
int resultado = 0;
int entero = 0;
for(std::size_t i = 0 ; i < datos.size() ; i++){
bin = datos[i];
//std::cerr << " \n\nNumero binario\n" << bin;
for(std::size_t j = 0 ; j < bin.size() ; j++){
//std::cerr << "\n" << j << " " << bin[j];
if((bin[j] & 1) == 1){
a = 1;
//std::cerr << "\n" << j /*<< " " << bin[j]<< " a = " << a*/;}
if((bin[j] & 1) == 0){
//std::cerr << "\n" << j /*<< " " << bin[j] << " a = " << a*/;
a = 0;}
exp = ((bin.size() - 1 ) - j);
entero = a * pow(2, exp);
d = pow(2, exp);
//std::cerr << "\nOperacion : " << a << " * " << d << "\n";
resultado = resultado + entero;
//std::cerr << resultado;}
decimales.push_back(resultado);
resultado = 0;}
std::cerr << "\nSe han pasado a decimal los siguientes datos: ";
for(std::size_t i = 0 ; i < decimales.size() ; i++){
if(i == 0){
std::cerr << decimales[i];}
else if(i > 0 && i < (decimales.size() - 1)){
std::cerr << ", " << decimales[i];}
else{
std::cerr << ", " << decimales[i] << ".\n" << std::endl;}}
for(std::size_t i = 0 ; i < decimales.size() ; i++){
if(decimales[i] < pivote){
std::cerr << "El numero " << decimales[i] <<
" es menor que el pivote " << pivote << std::endl;}}
std::cerr << "\n" << std::endl;
for(std::size_t i = 0 ; i < decimales.size() ; i++){
std::cerr << "El numero " << std::dec << std::showbase
<< decimales[i];
std::cerr <<" en hexadecimal es: " << std::hex << std::showbase
<< decimales[i] << ".\n";
}return 0;}
Jan 24, 2018 at 2:16pm UTC
@Andy
He has no question. Take a look at his profile.
Jan 24, 2018 at 7:49pm UTC
@coder777,
After I wrote that I noticed that one of the posts was reported and all the posts re bout the same.
Anyway thanks for the input.
Andy