elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: AIO elhacker.NET 2021 Compilación herramientas análisis y desinfección malware


  Mostrar Temas
Páginas: [1]
1  Programación / Programación C/C++ / Programa para convertir Decimal - Binario - Hexadecimal - Octal en: 27 Agosto 2013, 16:50 pm
Hola ,como estan el otro día no tenia nada que hacer y aprovechando las vacaciones de la uni me puse a hacer un programa sencillo pero bastante util para resolver otro tipo de problemas, el programa realiza conversiones de decimal a binario,hexadecimal y octal y viceversa con los 4 sistemas.
Sientanse libres de dejar sus sugerencias para mejorar el codigo

Código:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
char* Decimal_a_Binario (int *decimal)
{
int i=0 ,*paso,*dec_aux;
char *bin,*aux;
paso = (int *) malloc (sizeof(int));
dec_aux = (int *) malloc (sizeof(int));
bin = (char *) malloc (sizeof(char)*32);
aux = (char *) malloc (sizeof(char)*3);
*paso = 0;
*dec_aux = *decimal;
strcpy (aux,"\0");
strcpy (bin,"\0");
while (true)
{
*paso=*decimal/2;
itoa (*decimal%2,aux,10);
*(aux+1) = '\0';
*decimal=*paso;
*(bin+i) = *(aux+0);
      *(aux+0) = '\0';
i++;
if (*paso == 0)
break;
}
*decimal = *dec_aux;
*(bin+i)='\0';
strrev (bin);
return bin;
free (paso);
free (dec_aux);
free (bin);
free (aux);
}
char* Decimal_a_Octal (int *decimal)
{
int i=0 ,*paso,*dec_aux;
char *oct,*aux;
paso = (int *) malloc (sizeof(int));
dec_aux = (int *) malloc (sizeof(int));
oct = (char *) malloc (sizeof(char)*12);
aux = (char *) malloc (sizeof(char)*3);
*paso = 0;
*dec_aux = *decimal;
strcpy (aux,"\0");
strcpy (oct,"\0");
while (true)
{
*paso=*decimal/8;
itoa (*decimal%8,aux,10);
*(aux+1) = '\0';
*decimal=*paso;
*(oct+i) = *(aux+0);
      *(aux+0) = '\0';
i++;
if (*paso == 0)
break;
}
*decimal = *dec_aux;
*(oct+i)='\0';
strrev (oct);
return oct;
free (paso);
free (dec_aux);
free (oct);
free (aux);
}
char* Decimal_a_Hexadecimal (int *decimal)
{
int i=0 ,*paso,*dec_aux;
char *hex,*aux;
paso = (int *) malloc (sizeof(int));
dec_aux = (int *) malloc (sizeof(int));
hex = (char *) malloc (sizeof(char)*12);
aux = (char *) malloc (sizeof(char)*3);
*paso = 0;
*dec_aux = *decimal;
strcpy (aux,"\0");
strcpy (hex,"\0");
while (true)
{
*paso=*decimal/16;
if (*decimal%16 < 10)
itoa (*decimal%16,aux,10);
else
{
switch (*decimal%16)
{
case 10:
*(aux+0) = 'A';
break;
case 11:
*(aux+0) = 'B';
break;
case 12:
*(aux+0) = 'C';
break;
case 13:
*(aux+0) = 'D';
break;
case 14:
*(aux+0) = 'E';
break;
case 15:
*(aux+0) = 'F';
break;
}
}
*(aux+1) = '\0';
*decimal=*paso;
*(hex+i) = *(aux+0);
*(aux+0) = '\0';
i++;
if (*paso == 0)
break;
}
*decimal = *dec_aux;
*(hex+i)='\0';
strrev (hex);
return hex;
free (paso);
free (dec_aux);
free (hex);
free (aux);
}
char* Binario_a_Hexadecimal (char *bin)
{
int i=0;
char *hex,*aux;
hex = (char *) malloc (sizeof(char)*12);
aux = (char *) malloc (sizeof(char)*5);
strcpy (hex, "\0");
strcpy (aux, "\0");
i = strlen (bin) - 1;
while (i >= 0)
{
*(aux+0) = *(bin+(i-3));
*(aux+1) = *(bin+(i-2));
*(aux+2) = *(bin+(i-1));
*(aux+3) = *(bin+i);
*(aux+4) = '\0';
for (int j = 0 ; j < 4 ; j++)
if (*(aux+j) != '0' && *(aux+j) != '1')
*(aux+j) = '0';
if (strcmp (aux,"0000") == 0)
strcat (hex ,"0");
else if (strcmp (aux,"0001") == 0)
strcat (hex ,"1");
else if (strcmp (aux,"0010") == 0)
strcat (hex ,"2");
else if (strcmp (aux,"0011") == 0)
strcat (hex ,"3");
else if (strcmp (aux,"0100") == 0)
strcat (hex ,"4");
else if (strcmp (aux,"0101") == 0)
strcat (hex ,"5");
else if (strcmp (aux,"0110") == 0)
strcat (hex ,"6");
else if (strcmp (aux,"0111") == 0)
strcat (hex ,"7");
else if (strcmp (aux,"1000") == 0)
strcat (hex ,"8");
else if (strcmp (aux,"1001") == 0)
strcat (hex ,"9");
else if (strcmp (aux,"1010") == 0)
strcat (hex ,"A");
else if (strcmp (aux,"1011") == 0)
strcat (hex ,"B");
else if (strcmp (aux,"1100") == 0)
strcat (hex ,"C");
else if (strcmp (aux,"1101") == 0)
strcat (hex ,"D");
else if (strcmp (aux,"1110") == 0)
strcat (hex ,"E");
else if (strcmp (aux,"1111") == 0)
strcat (hex ,"F");
i -= 4;
*(aux+0) = '\0';
}
strrev (hex);
return hex;
free (hex);
free (aux);
}
char* Binario_a_Octal (char *bin)
{
int i=0;
char *oct,*aux;
oct = (char *) malloc (sizeof(char)*12);
aux = (char *) malloc (sizeof(char)*4);
strcpy (oct, "\0");
strcpy (aux, "\0");
i = strlen (bin) - 1;
while (i >= 0)
{
*(aux+0) = *(bin+(i-2));
*(aux+1) = *(bin+(i-1));
*(aux+2) = *(bin+i);
*(aux+3) = '\0';
for (int j = 0 ; j < 3 ; j++)
if (*(aux+j) != '0' && *(aux+j) != '1')
*(aux+j) = '0';
if (strcmp (aux,"000") == 0)
strcat (oct ,"0");
else if (strcmp (aux,"001") == 0)
strcat (oct ,"1");
else if (strcmp (aux,"010") == 0)
strcat (oct ,"2");
else if (strcmp (aux,"011") == 0)
strcat (oct ,"3");
else if (strcmp (aux,"100") == 0)
strcat (oct ,"4");
else if (strcmp (aux,"101") == 0)
strcat (oct ,"5");
else if (strcmp (aux,"110") == 0)
strcat (oct ,"6");
else if (strcmp (aux,"111") == 0)
strcat (oct ,"7");
i -= 3;
*(aux+0) = '\0';
}
strrev (oct);
return oct;
free (oct);
free (aux);
}
int Binario_a_Decimal (char *bin)
{
int *decimal;
char *bin_aux,*aux;
decimal = (int *) malloc (sizeof(int));
bin_aux = (char *) malloc (sizeof(char)*32);
aux = (char *) malloc (sizeof(char)*2);
*decimal = 0;
strcpy (bin_aux,"\0");
strcpy (aux,"\0");
strcpy (bin_aux,bin);
strrev (bin_aux);
for (int i = 0 ; i < strlen (bin_aux) ; i++)
{
*(aux+0) = *(bin_aux+i);
*(aux+1) = '\0';
*decimal += (pow (2.0,i) * atoi(aux));
}
return *decimal;
free (decimal);
free (bin_aux);
free (aux);
}
int Hexadecimal_a_Decimal (char *hex)
{
int *decimal;
char *hex_aux,*aux;
decimal = (int *) malloc (sizeof(int));
hex_aux = (char *) malloc (sizeof(char)*12);
aux = (char *) malloc (sizeof(char)*2);
*decimal = 0;
strcpy (hex_aux,"\0");
strcpy (aux,"\0");
strupr (hex);
strcpy (hex_aux,hex);
strrev (hex_aux);
for (int i = 0 ; i < strlen (hex_aux) ; i++)
{
if (*(hex_aux+i) >= '0' && *(hex_aux+i) <= '9')
{
*(aux+0) = *(hex_aux+i);
*(aux+1) = '\0';
*decimal += (pow (16.0,i) * atoi(aux));
}
else
{
switch (*(hex_aux+i))
{
case 'A':
*decimal += (pow (16.0,i) * 10);
break;
case 'B':
*decimal += (pow (16.0,i) * 11);
break;
case 'C':
*decimal += (pow (16.0,i) * 12);
break;
case 'D':
*decimal += (pow (16.0,i) * 13);
break;
case 'E':
*decimal += (pow (16.0,i) * 14);
break;
case 'F':
*decimal += (pow (16.0,i) * 15);
break;
}
}
}
return *decimal;
free (decimal);
free (hex_aux);
free (aux);
}
char* Hexadecimal_a_Binario (char *hex)
{
char *bin;
bin = (char *) malloc (sizeof(char)*32);
strcpy (bin, "\0");
strupr (hex);
for (int i = 0 ; i < strlen (hex) ; i++)
{
switch (*(hex+i))
{
case '0':
strcat (bin ,"0000");
break;
case '1':
strcat (bin ,"0001");
break;
case '2':
strcat (bin ,"0010");
break;
case '3':
strcat (bin ,"0011");
break;
case '4':
strcat (bin ,"0100");
break;
case '5':
strcat (bin ,"0101");
break;
case '6':
strcat (bin ,"0110");
break;
case '7':
strcat (bin ,"0111");
break;
case '8':
strcat (bin ,"1000");
break;
case '9':
strcat (bin ,"1001");
break;
case 'A':
strcat (bin ,"1010");
break;
case 'B':
strcat (bin ,"1011");
break;
case 'C':
strcat (bin ,"1100");
break;
case 'D':
strcat (bin ,"1101");
break;
case 'E':
strcat (bin ,"1110");
break;
case 'F':
strcat (bin ,"1111");
break;
}
}
return bin;
free (bin);
}
char* Hexadecimal_a_Octal (char *hex)
{
char *oct ,*bin;
oct = (char *) malloc (sizeof(char)*12);
bin = (char *) malloc (sizeof(char)*32);
strcpy (oct, "\0");
strcpy (bin, "\0");
strupr (hex);
bin = Hexadecimal_a_Binario (hex);
oct = Binario_a_Octal (bin);
return oct;
free (oct);
free (bin);
}
int Octal_a_Decimal (char *oct)
{
int *decimal;
char *oct_aux,*aux;
decimal = (int *) malloc (sizeof(int));
oct_aux = (char *) malloc (sizeof(char)*12);
aux = (char *) malloc (sizeof(char)*2);
*decimal = 0;
strcpy (oct_aux,"\0");
strcpy (aux,"\0");
strcpy (oct_aux,oct);
strrev (oct_aux);
for (int i = 0 ; i < strlen (oct_aux) ; i++)
{
*(aux+0) = *(oct_aux+i);
*(aux+1) = '\0';
*decimal += (pow (8.0,i) * atoi(aux));
}
return *decimal;
free (decimal);
free (oct_aux);
free (aux);
}
char* Octal_a_Binario (char *oct)
{
char *bin;
bin = (char *) malloc (sizeof(char)*32);
strcpy (bin, "\0");
for (int i = 0 ; i < strlen (oct) ; i++)
{
switch (*(oct+i))
{
case '0':
strcat (bin ,"000");
break;
case '1':
strcat (bin ,"001");
break;
case '2':
strcat (bin ,"010");
break;
case '3':
strcat (bin ,"011");
break;
case '4':
strcat (bin ,"100");
break;
case '5':
strcat (bin ,"101");
break;
case '6':
strcat (bin ,"110");
break;
case '7':
strcat (bin ,"111");
break;
}
}
return bin;
free (bin);
}
char* Octal_a_Hexadecimal (char *oct)
{
char *hex ,*bin;
hex = (char *) malloc (sizeof(char)*12);
bin = (char *) malloc (sizeof(char)*32);
strcpy (hex, "\0");
strcpy (bin, "\0");
strupr (hex);
bin = Octal_a_Binario (oct);
hex = Binario_a_Hexadecimal (bin);
return hex;
free (hex);
free (bin);
}
int main ()
{
int *decimal,*opc;
char *bin,*oct,*hex;
decimal = (int *) malloc (sizeof(int));
opc = (int *) malloc (sizeof(int));
bin = (char *) malloc (sizeof(char)*32);
oct = (char *) malloc (sizeof(char)*12);
hex = (char *) malloc (sizeof(char)*12);
*decimal = 0;
*opc = 0;
strcpy (bin, "\0");
strcpy (oct,"\0");
strcpy (hex,"\0");
printf("Convertir de decimal a otros sistemas press 1\n");
printf("Convertir de hexadecimal a otros sistemas press 2\n");
printf("Convertir de binario a otros sistemas press 3\n");
printf("Convertir de octal a otros sistemas press 4\n");
printf("opcion: ");
scanf ("%d", &*opc);
system("cls");
switch (*opc)
{
case 1:
printf ("Ingresa un numero decimal: ");
scanf ("%d" ,&*decimal);
printf ("Decimal: %d \nBinario: %s \nOctal: %s \nHexadecimal: %s\n",*decimal,Decimal_a_Binario(decimal),Decimal_a_Octal(decimal),Decimal_a_Hexadecimal(decimal));
break;
case 2:
printf ("Ingresa un numero hexadecimal: ");
fflush (stdin);
gets (hex);
printf ("Hexadecimal: %s \nDecimal: %d \nBinario: %s \nOctal: %s\n",hex,Hexadecimal_a_Decimal(hex),Hexadecimal_a_Binario(hex),Hexadecimal_a_Octal(hex));
break;
case 3:
printf ("Ingresa un numero binario: ");
fflush (stdin);
gets (bin);
printf ("Binario: %s \nDecimal: %d \nHexadecimal: %s \nOctal: %s\n",bin,Binario_a_Decimal(bin),Binario_a_Hexadecimal(bin),Binario_a_Octal(bin));
break;
case 4:
printf ("Ingresa un numero octal: ");
fflush (stdin);
gets (oct);
printf ("Octal: %s \nDecimal: %d \nHexadecimal: %s \nBinario: %s\n",oct,Octal_a_Decimal(oct),Octal_a_Hexadecimal(oct),Octal_a_Binario(oct));
break;
}
system("pause");
free (opc);
free (decimal);
free (hex);
free (oct);
free (bin);
return 0;
}
2  Programación / Programación C/C++ / Programa para resolver sistemas de ecuaciones hecho en C++ en: 21 Octubre 2012, 05:50 am
Buenas Noches, les dejo un programa que hize hace a penas un rato, el cual sirve para resolver sistemas de ecuaciones de 2x2 y de 3x3 a travez de Gauss-Jordan, el programa esta muy basico, pero funciona bien, falta validar algunas cosas pero en general funciona bien, por favor dejenme sus comentarios, sugerencias, etc., estoy abierto a toda critica constructiva soy estudiante de Ingenieria en Sistemas Computacionales a penas cursando mi primer materia de programación, por lo mismo este programa se basa mas en la logica matematica que en la logica de programación.

Código:
#include <iostream>
using namespace std;
void main()
{
int opcion,repetir=1;
float matriz2x3[2][3],matriz3x4[3][4],x,y,z;
do
{
cout<<"Este programa resuleve sistemas ecuaciones, de 2 y 3 incognitas"<<endl;
cout<<"Para resolver un sistema de 2 incognitas Presiona 1"<<endl;
cout<<"Para resolver un sistema de 3 incognitas Presiona 2"<<endl;
cout<<"Opci\xA2n: ";
cin>>opcion;
while(opcion<1||opcion>2)
{
cout<<endl<<"Seleccione una opci\xA2n valida"<<endl;
cout<<"Opci\xA2n: ";
cin>>opcion;
}
system("cls");
switch(opcion)
{
case 1:
cout<<"Primera Ecuaci\xA2n"<<endl<<endl;
cout<<"Ingrese el coeficiente de x: ";
cin>>matriz2x3[0][0];
cout<<endl;
cout<<"Ingrese el coeficiente de y: ";
cin>>matriz2x3[0][1];
cout<<endl;
cout<<"Ingrese el valor del termino independiente: ";
cin>>matriz2x3[0][2];
cout<<endl;
cout<<"Segunda Ecuaci\xA2n"<<endl<<endl;
cout<<"Ingrese el coeficiente de x: ";
cin>>matriz2x3[1][0];
cout<<endl;
cout<<"Ingrese el coeficiente de y: ";
cin>>matriz2x3[1][1];
cout<<endl;
cout<<"Ingrese el valor del termino independiente: ";
cin>>matriz2x3[1][2];
cout<<endl;
//Resolviendo sistema con base en metodo de Gauss-Jordan
matriz2x3[0][1]=matriz2x3[0][1]/matriz2x3[0][0];
matriz2x3[0][2]=matriz2x3[0][2]/matriz2x3[0][0];
matriz2x3[0][0]=matriz2x3[0][0]/matriz2x3[0][0];// Se hace 1
//----------------------------------------------
matriz2x3[1][1]=((-matriz2x3[1][0])*matriz2x3[0][1])+matriz2x3[1][1];
matriz2x3[1][2]=((-matriz2x3[1][0])*matriz2x3[0][2])+matriz2x3[1][2];
matriz2x3[1][0]=((-matriz2x3[1][0])*matriz2x3[0][0])+matriz2x3[1][0];// Se hace 0
//-------------------------------------------------------------------
matriz2x3[1][2]=matriz2x3[1][2]/matriz2x3[1][1];//Solucion variable y
matriz2x3[1][1]=matriz2x3[1][1]/matriz2x3[1][1];
//----------------------------------------------
matriz2x3[0][2]=((-matriz2x3[0][1])*matriz2x3[1][2])+matriz2x3[0][2];//Solucion variable x
matriz2x3[0][1]=((-matriz2x3[0][1])*matriz2x3[1][1])+matriz2x3[0][1];
//-------------------------------------------------------------------
x=matriz2x3[0][2];
y=matriz2x3[1][2];
cout<<"Soluci\xA2n: "<<endl;
cout<<"\tx="<<x<<endl;
cout<<"\ty="<<y<<endl;
break;
case 2:
cout<<"Primera Ecuaci\xA2n"<<endl<<endl;
cout<<"Ingrese el coeficiente de x: ";
cin>>matriz3x4[0][0];
cout<<endl;
cout<<"Ingrese el coeficiente de y: ";
cin>>matriz3x4[0][1];
cout<<endl;
cout<<"Ingrese el coeficiente de z: ";
cin>>matriz3x4[0][2];
cout<<endl;
cout<<"Ingrese el valor del termino independiente: ";
cin>>matriz3x4[0][3];
cout<<endl;
cout<<"Segunda Ecuaci\xA2n"<<endl<<endl;
cout<<"Ingrese el coeficiente de x: ";
cin>>matriz3x4[1][0];
cout<<endl;
cout<<"Ingrese el coeficiente de y: ";
cin>>matriz3x4[1][1];
cout<<endl;
cout<<"Ingrese el coeficiente de z: ";
cin>>matriz3x4[1][2];
cout<<endl;
cout<<"Ingrese el valor del termino independiente: ";
cin>>matriz3x4[1][3];
cout<<endl;
cout<<"Tercera Ecuaci\xA2n"<<endl<<endl;
cout<<"Ingrese el coeficiente de x: ";
cin>>matriz3x4[2][0];
cout<<endl;
cout<<"Ingrese el coeficiente de y: ";
cin>>matriz3x4[2][1];
cout<<endl;
cout<<"Ingrese el coeficiente de z: ";
cin>>matriz3x4[2][2];
cout<<endl;
cout<<"Ingrese el valor del termino independiente: ";
cin>>matriz3x4[2][3];
cout<<endl;
//Resolviendo sistema con base en metodo de Gauss-Jordan
matriz3x4[0][1]=matriz3x4[0][1]/matriz3x4[0][0];
matriz3x4[0][2]=matriz3x4[0][2]/matriz3x4[0][0];
matriz3x4[0][3]=matriz3x4[0][3]/matriz3x4[0][0];
matriz3x4[0][0]=matriz3x4[0][0]/matriz3x4[0][0];// Se hace 1
//----------------------------------------------
matriz3x4[1][1]=((-matriz3x4[1][0])*matriz3x4[0][1])+matriz3x4[1][1];
matriz3x4[1][2]=((-matriz3x4[1][0])*matriz3x4[0][2])+matriz3x4[1][2];
matriz3x4[1][3]=((-matriz3x4[1][0])*matriz3x4[0][3])+matriz3x4[1][3];
matriz3x4[1][0]=((-matriz3x4[1][0])*matriz3x4[0][0])+matriz3x4[1][0];// Se hace 0
matriz3x4[2][1]=((-matriz3x4[2][0])*matriz3x4[0][1])+matriz3x4[2][1];
matriz3x4[2][2]=((-matriz3x4[2][0])*matriz3x4[0][2])+matriz3x4[2][2];
matriz3x4[2][3]=((-matriz3x4[2][0])*matriz3x4[0][3])+matriz3x4[2][3];
matriz3x4[2][0]=((-matriz3x4[2][0])*matriz3x4[0][0])+matriz3x4[2][0];// Se hace 0
//-------------------------------------------------------------------
matriz3x4[1][2]=matriz3x4[1][2]/matriz3x4[1][1];
matriz3x4[1][3]=matriz3x4[1][3]/matriz3x4[1][1];
matriz3x4[1][1]=matriz3x4[1][1]/matriz3x4[1][1];// Se hace 1
//----------------------------------------------
matriz3x4[0][2]=((-matriz3x4[0][1])*matriz3x4[1][2])+matriz3x4[0][2];
matriz3x4[0][3]=((-matriz3x4[0][1])*matriz3x4[1][3])+matriz3x4[0][3];
matriz3x4[0][1]=((-matriz3x4[0][1])*matriz3x4[1][1])+matriz3x4[0][1];// Se hace 0
matriz3x4[2][2]=((-matriz3x4[2][1])*matriz3x4[1][2])+matriz3x4[2][2];
matriz3x4[2][3]=((-matriz3x4[2][1])*matriz3x4[1][3])+matriz3x4[2][3];
matriz3x4[2][1]=((-matriz3x4[2][1])*matriz3x4[1][1])+matriz3x4[2][1];// Se hace 0
//-------------------------------------------------------------------
matriz3x4[2][3]=matriz3x4[2][3]/matriz3x4[2][2];//Solucion variable z
matriz3x4[2][2]=matriz3x4[2][2]/matriz3x4[2][2];
//-------------------------------------------------------------------
matriz3x4[0][3]=((-matriz3x4[0][2])*matriz3x4[2][3])+matriz3x4[0][3];//Solucion variable x
matriz3x4[0][2]=((-matriz3x4[0][2])*matriz3x4[2][2])+matriz3x4[0][2];
matriz3x4[1][3]=((-matriz3x4[1][2])*matriz3x4[2][3])+matriz3x4[1][3];//Solucion variable y
matriz3x4[1][2]=((-matriz3x4[1][2])*matriz3x4[2][2])+matriz3x4[1][2];
//-------------------------------------------------------------------
x=matriz3x4[0][3];
y=matriz3x4[1][3];
z=matriz3x4[2][3];
cout<<"Soluci\xA2n: "<<endl;
cout<<"\tx="<<x<<endl;
cout<<"\ty="<<y<<endl;
cout<<"\tz="<<z<<endl;
break;
}
cout<<"Para realizar otro calculo presione 1, para salir presione 0"<<endl;
cout<<"Opci\xA2n: ";
cin>>repetir;
while (repetir<0||repetir>1)
{
cout<<endl<<"Ingrese una opci\xA2n valida"<<endl;
cout<<"Opci\xA2n: ";
cin>>repetir;
}
system("cls");
}while(repetir==1);
}
3  Programación / Programación C/C++ / Ayuda C++ Urgente!!! en: 10 Octubre 2012, 22:18 pm
Hola, buenas tardes necesito ayuda ,alguien sabe como puedo leer una fraccion en c++?, a mi se me habia ocurrido algo como esto pero no funciono, ya una vez en el programa la puedo trabajar como numero con decimales pero al ingresar si o si debe ser fraccion
Código:
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
float fraccion,numerador,denominador;
cout<<"Ingrese una fraccion:"<<endl;
cout<<"Fraccion: ";
cin>>numerador;
cout>>"/";
cin>>denominador;
fraccion=numerador/denominador;
cout<<"El equivalente en decimales es:"<<fraccion;
getch();
}
4  Programación / Programación C/C++ / Ayuda Programa C++ en: 9 Octubre 2012, 18:36 pm
Estoy haciendo un programa en C++ para resolver ecuaciones de segundo grado el problema es que al validar si se tienen coeficientes fraccionarios el compilador (Visual C++ 2010) me marca error en que estoy usando las variables x1,x2,a,b,c y discriminante las estoy usando sin inicializarlas ,pero segun yo si le estoy asignando datos correctamente, antes de hacer la validacion de coeficientes fraccionarios, funcionaba perfectamente obviamente antes de ingresar coeficientes fraccionarios

Código:
#include <math.h>//Para usar pow y sqrt
#include <iostream>
using namespace std;
void main()
{
int repetir=1,fraccionario;
char opcion;
float numerador,denominador,a,b,c,x1,x2,discri…
do //Ciclo do while para repetir el programa si el usuario lo desea
{
cout<<"Este programa sirve para resolver ecuaciones de segundo grado"<<endl<<endl;
cout<<"\xA8 \bQue tipo de ecuaci\xA2n de segundo grado desea resolver?"<<endl<<endl;
cout<<"De la forma: ax\xFD+bx+c=0, Presionar A"<<endl;
cout<<"De la forma: ax\xFD+c=0, Presionar B"<<endl;
cout<<"De la forma: ax\xFD+bx=0, Presionar C"<<endl;
cout<<"Opci\xA2n: ";
cin>>opcion;
switch(opcion)
{
case 'A': case 'a':
cout<<"\xA8 \bLa ecucacion contiene coeficientes fraccionarios?"<<endl;
cout<<"En caso de que si presione 1"<<endl;
cout<<"En caso de que no presione 2"<<endl;
cin>>fraccionario;
system("cls");
if (fraccionario==1)//Validacion en caso de que la ecuacion tenga exponentes fraccionarios
{
cout<<endl<<"Ingrese el valor del coeficiente del termino cuadratico: ";
cin>>numerador>>denominador;
a=numerador/denominador;
numerador=0;
denominador=0;
cout<<endl<<"Ingrese el valor del coeficiente del termino lineal: ";
cin>>numerador>>denominador;
b=numerador/denominador;
numerador=0;
denominador=0;
cout<<endl<<"Ingrese el valor del coeficiente del termino independiente: ";
cin>>numerador>>denominador;
c=numerador/denominador;
numerador=0;
denominador=0;
discriminante=pow(b,2)-4*a*c;

}
if (fraccionario==0)
{
cout<<endl<<"Ingrese el valor del coeficiente del termino cuadratico: ";
cin>>a;
cout<<endl<<"Ingrese el valor del coeficiente del termino lineal: ";
cin>>b;
cout<<endl<<"Ingrese el valor del coeficiente del termino independiente: ";
cin>>c;
discriminante=pow(b,2)-4*a*c;
}
if (discriminante>0)
{
x1=((-b)+(sqrt(discriminante)))/(2*…
x2=((-b)-(sqrt(discriminante)))/(2*…
}
if (discriminante==0)
{
x1=(-b)/(2*a);
x2=x1;
}
if (discriminante<0)
{
x1=((-b)/(2*a))+(sqrt(-discriminant…
x2=((-b)/(2*a))-(sqrt(-discriminant…
cout<<endl<<"Las raices de la ecuacion son:"<<endl<<endl;
if (x1==1)
{
if (x2==-1)
cout<<"\tx1=i"<<endl;
cout<<"\tx2=-i"<<endl;
}
else
{
cout<<"\tx1="<<x1<<" i"<<endl;
cout<<"\tx2="<<x2<<" i"<<endl;
}
}
else
{
cout<<endl<<"Las raices de la ecuacion son:"<<endl<<endl;
cout<<"\tx1="<<x1<<endl;
cout<<"\tx2="<<x2<<endl;
}
break;
case 'B': case 'b': //Falta actualizar por ahora es una copia del case 'a'
cout<<endl<<"Ingrese el valor del coeficiente del termino cuadratico: ";
cin>>a;
cout<<endl<<"Ingrese el valor del coeficiente del termino lineal: ";
cin>>b;
cout<<endl<<"Ingrese el valor del coeficiente del termino independiente: ";
cin>>c;
x1=((-b)+(sqrt(discriminante)))/(2*a…
x2=((-b)-(sqrt(discriminante)))/(2*a…
cout<<"Las raices de la ecuacion son:"<<endl;
cout<<"\tx1="<<x1<<endl;
cout<<"\tx2="<<x2<<endl;
break;
default:
cout<<"Elija una opci\xA2n valida"<<endl;
cout<<"Opci\xA2n: ";
cin>>opcion;
break;
}
cout<<endl<<"Para realizar otro calculo presione 1, para salir presione 0"<<endl;
cout<<"Opci\xA2n: ";
cin>>repetir;
system("cls");
}while(repetir==1);

}
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines