Cool C/C++

Programacion en C/C++

Home Foros
Bienvenido, Invitado
Por favor Identificarse o Registrarse.    Contraseña olvidada?

Menú con funciones, no se si lo esta haciendo bien
(1 viendo) (1) Invitado
AbajoPágina: 1
TEMA: Menú con funciones, no se si lo esta haciendo bien
#233
Menú con funciones, no se si lo esta haciendo bien Creado el día: 10/03/2013 13:16 Karma: 0
Hola muchachos es la segunda vez que pido ayuda emm tengo un problema? , bueno no he entendido del todo el tema de los nodos y estructuras en c (mi profe no domina el tema, y quiere que le entendamos a la primera), puesto que me han dejado un código incompleto el cual he tratado de terminar bueno principalmente me enfoco a que tengo que desarrollar una rutina de generar la función seno y senh-1 pero al ejecutar mi programa no se si estén correctos los valores que este muestra solicito su colaboración para ver si lo que he estado haciendo esta bien, no entiendo el tema espero me ayuden muchas gracias de antemano .
Anexo el código para que lo chequen :

Código:
/* PROGRAMA: SUMA, RESTA, MULTIPLICACION Y DIVISION DE POLINOMIOS */
# include <stdio.h>
# include <stdlib.h>
# include <alloc.h>
# include <conio.h>
# include <dos.h>
# include <string.h>

# include <math.h>

# define DELAY 64000

struct apuntador
{
float coef;
int exp1;
struct apuntador *siguiente;
} nodo;
 
struct apuntador
*polydat1, *polydat2, *polysum1, *polysum2, *polysuma, *polymin,
*polysus, *polyres, *polyfac1, *polyfac2, *polyprod, *polynum,
*polyden, *polycos, *polyresta, *polyresul, *aux_poly, *nuevo,
*polyder;
 
void pausa(void), crea(void), iniciapoly(void),
presentacion(void),
insert_lista(struct apuntador **poly, float c, int e),
imprime(struct apuntador *poly),
suma_poly(struct apuntador *poly1, struct apuntador *poly2,
struct apuntador **poly3),
multy_poly(struct apuntador *poly1, struct apuntador *poly2,
struct apuntador **poly3),
resta_poly(struct apuntador *poly1, struct apuntador *poly2,
struct apuntador **poly3),
div_poly(struct apuntador *poly1, struct apuntador *poly2,
struct apuntador **poly3, struct apuntador **poly4),
der_poly(struct apuntador *poly1, struct apuntador **poly3),
borrar_poly(struct apuntador **poly3, int e);
float eval_poly(struct apuntador *poly1,float x);
float factorial(int);
 
 
int seleccion_menu(void);
int i,caso,e;
float c,epsilon;
float coef;
int exp1,fx,x0; // SE CAMBIA EXP SOLO AQUI O EN TODOS?
char n;
int opcion;
 
main()
{
presentacion();
clrscr();
for(;;)
{
switch(seleccion_menu())
{
 
 
case 1:
int limite, iter=0;
float fxd,epsilon,xa,xn;
clrscr();
printf("\n Metodo de Newton Raphson (RAICES) de poly1");
printf("\n Dame el valor inicial de x0 para poly1 ==>");
scanf("%f",&x0);
printf("\n Dame el Epsilon ==>");
scanf("%f",&epsilon);
printf("\n Dame el nuemro maximo de iteraciones ==>");
scanf("%d",&limite);
der_poly(polydat1, &polyder);
printf("\n #iter----x0----------fx----------fxd----------xn");
do
{
xa=x0;
fx=eval_poly(polydat1,xa);
fxd=eval_poly(polyder,xa);
xn=xa - fx/fxd;
iter=iter+1;
printf("\n %d %f %f %f %f",iter,xa,fx,fxd,xn);
x0=xn;
} while((fabs(xa-xn)>epsilon)&&(iter<=limite));
printf("\n el valor final de x es = 5f",xn);
printf("\n\n");
pausa();
pausa();
pausa();
break;
 
 
case 2:
clrscr();
iniciapoly();
do
{ do
{
printf("\n Dame el valor del coeficiente del poly 1 ==> ");
scanf("%f",&c);
} while (c == 0);
 
printf("\n Dame el valor del exponente del poly 1 ==> ");
scanf("%d",&e);
insert_lista(&polydat1,c,e);
printf("\n Desea otro termino (s/n) ? ==> ");
opcion=getche();
} while((opcion==115) || (opcion==83));
imprime(polydat1);
printf("\n\n");
do
{
do
{
printf("\n Dame el valor del coeficiente del poly 2 ==> ");
scanf("%f",&c);
} while (c == 0);
printf("\n Dame el valor del exponente de poly 2 ==> ");
scanf("%d",&e);
insert_lista(&polydat2,c,e);
printf("\n Desea otro termino (s/n) ? ==> ");
opcion=getche();
} while((opcion==115) || (opcion==83));
imprime(polydat2);
pausa();
break;
case 3:
polysum1=polydat1;
polysum2=polydat2;
suma_poly(polysum1,polysum2,&polysuma);
imprime(polysuma);
pausa();
break;
case 4:
polyfac1=polydat1;
polyfac2=polydat2;
multy_poly(polyfac1,polyfac2,&polyprod);
imprime(polyprod);
pausa();
break;
case 5:
polymin=polydat1;
polysus=polydat2;
resta_poly(polymin,polysus,&polyresta);
imprime(polyresta);
pausa();
break;
case 6:
polyder = polydat1;
der_poly(polydat1,&polyder);
printf("Derivada:\n");
imprime(polyder);
pausa();
break;
case 7:
printf("\ Fin del Programa");
exit(0);
default:
printf("\n Operacion no valida");
//return (0);
 
case 8:
fx=eval_poly(polydat1,x0);
break;
case 9:
 
break;
case 10:
clrscr();
printf("\n Dame el valor del exponente del termino a borrar de poly1 ==> ");
scanf("%d",&e);
borrar_poly(&polydat1,e);
imprime(polydat1);
printf("\n\n");
pausa();
break;
 
 
 
case 11:
struct apuntador *fx;
fx=polydat1;
float a,b,c,it_limit,fa,fb,fc;
int it=0;
while(1)
{//0
clrscr();
printf("\n Metodo de la biseccion (PARA RAICES) de poly1");
printf("\n dar la cota inferior a = ?");
scanf("%f",&a);
printf("\n dar la cota superior b = ?");
scanf("%f",&b);
printf("\n Dar el epsilon ==> ?");
scanf("%f",&epsilon);
printf("\n Dar el limite de iteraciones =? ");
scanf("%f",&it_limit);
printf("\n It.--a-----b-----c-----f(a)-----f(c)-----abs(f(c)-f(a))/2");
fa= eval_poly(fx,a);
fb= eval_poly(fx,b);
if((fa*fb)>0)
{//1
printf("\n La solucion no esta entre a..b ya que f(a) f(b)>0 \n");
} //-1
else
while(1)
{//2
it=it+1;
c=(a+b)/2;
fc=eval_poly(fx,c);
printf("\n %3d %10.6f %10.6f %10.6f %10.6f %10.6f",it,a,c,b,fa,fc);
printf("\n %14.8f \n",fabs((fb-fa)/2));
pausa();
if(it>it_limit)
break;
if(fabs(c-a)<epsilon)
break;
if(fa*fc<=0)
{ b=c; fb=fc; }
else
{ a=c; fa=fc; }
}//-2
if(it<=it_limit)
printf("\n Tolerancia satisfecha");
if(it>it_limit)
printf("\n limite exedido de iteraciones");
printf("\n el resultado final de x es =%g \n",c);
printf("\n\n");
 
pausa();
pausa();
}//0
break;
 
case 12:
struct apuntador *fsen=NULL;
int t,expo,signo;
double coefi,x,ter;
double suma=0;
double vfsen;
clrscr();
printf("\n Dar el valor de terminos: ");
scanf("%d",&t);
printf("\n Dar el valor de X: ");
scanf("%f",&x);
x=x*3.1416/180;
printf("\n #ter sig exp coefi ter suma");
for(int ni=0;ni<t;ni++)
{
signo=pow(-1,ni);
expo=2*ni+1;
coefi=signo/factorial(expo);
insert_lista(&fsen,coefi,expo);
ter=coefi*pow(x,expo);
suma=suma+ter;
printf("\n\n %2d %2d %2d %10.8f %10.8f %10.8f",ni+1,signo,expo,coefi,ter,suma);
}
printf("\n\n\n El valor sel seno con sumatoria es : %14.12f\n\n",suma);
getche();
imprime(fsen);
vfsen=eval_poly(fsen,x);
printf("\n\n El valor del seno con polinomio es : %14.12f",vfsen);
getche();
polydat1=fsen;
printf("\n\n");
pausa();
break;
 
case 13:
struct apuntador *fsinhmu=NULL;

int te,expon,sign;
double coefic,xi,termin,dn;
double sumas=0;
double vfsinhmu;

clrscr();
printf("\n Dar el valor de terminos: ");
scanf("%d",&te);
printf("\n Dar el valor de X: ");
scanf("%f",&xi);
xi=xi*3.1416/180;
printf("\n #Term Signo Exp Coeficiente Termino Suma");
for(int n=0;n<te;n++)
{
sign=pow(-1,n);
expon=2*n+1;
dn=2*n;
coefic=(sign*factorial(dn))/(pow(4,n)*pow(factorial(n),2)*expon);
insert_lista(&fsinhmu,coefic,expon);
termin=coefic*pow(xi,expon);
sumas=sumas+termin;
printf("\n\n %2d %2d %2d %10.8f %10.8f %10.8f",n+1,sign,expon,coefic,termin,sumas);
}
printf("\n\n\n El valor sel seno con sumatoria es : %14.12f\n\n",sumas);
getche();
imprime(fsinhmu);
vfsinhmu=eval_poly(fsinhmu,xi);
printf("\n\n El valor del seno con polinomio es : %14.12f",vfsinhmu);
getche();
polydat1=fsinhmu;
printf("\n\n");
pausa();
break;
 
}
}
}
 
seleccion_menu(void)
{
char s[80];
int x;
clrscr();
printf("\n\n OPERACIONES DE POLINOMIOS:");
printf("\n\n\n 1 ) Metodo de Newton Rhapson \n");
printf(" 2 ) Crear polinomio. \n");
printf(" 3 ) Suma. \n");
printf(" 4 ) Multiplicacion. \n");
printf(" 5 ) Resta. \n");
printf(" 6 ) Derivada. \n");
printf(" 7 ) Salir. \n");
printf(" 8 ) Evaluar polyn(x). \n");
printf(" 9 ) Modificar termino de polinomio.\n");
printf(" 10 ) Borrar algun termino. \n");
printf(" 11 ) Metodo de Biseccion \n");
printf(" 12 ) Genera funcion Seno de Taylor \n");
printf(" 13 ) Genera funcion SenoHyperbolico Inverso \n");
do
{
printf(" OPCION ==> ");
gets(s);
x=atoi(s);
} while (x<0 || x>14);
return (x);
}
 
void pausa(void)
{
printf("\n Pulsar cualquier tecla para continuar...");
getch();
}
 
void iniciapoly(void)
{
polydat1=NULL;
polydat2=NULL;
polysum1=NULL;
polysum2=NULL;
polysuma=NULL;
polymin=NULL;
polysus=NULL;
polyres=NULL;
polyfac1=NULL;
polyfac2=NULL;
polyprod=NULL;
polynum=NULL;
polyden=NULL;
polycos=NULL;
polyres=NULL;
polyresta=NULL;
polyder=NULL;
}
 
void insert_lista(struct apuntador **poly, float coefi, int expo)
{ //0
struct apuntador *nuevo, *aux,*ant;
int band=0;
nuevo=(struct apuntador *) malloc (sizeof(nodo));
if(nuevo == NULL)
{//1
printf("MEMORIA AGOTADA \n");
return;
}//-1
nuevo->coef=coefi;
nuevo->exp1=expo;
nuevo->siguiente=NULL;
if(*poly==NULL)
*poly=nuevo;
else
{//-2
aux=*poly;
if(aux->exp1 < expo)
{ //3
nuevo->siguiente=*poly;
*poly=nuevo;
}//-3
else //EN OTRSOS CASOS
{//4
while((aux)&&(band))
{//5
if(aux->exp1==expo)
{//6 PARA TERMINO CON IGUAL EXPONENTE
aux->coef=aux->coef+coefi;
delete(nuevo); // O ES "if(aux->coef==0)"
band=1;
}//-6
else //PARA EL CASO: aux->exp1>expo
{//7
if((aux->siguiente!= NULL)&&(expo>aux->siguiente->exp1))
{//8
nuevo->siguiente=aux->siguiente;
aux->siguiente=nuevo;
band=1;
}//-8
else
{//9
if(aux->siguiente==NULL)
{//10
aux->siguiente=nuevo;
band=1;
}//-10
else
ant=aux;
aux=aux->siguiente;
}//-9
}//-7
}//-5
}//-4
}
}//-0
 
void imprime(struct apuntador *poly)
{
while(poly)
{
printf("\n\n Coeficiente : %f \n",poly->coef);
printf(" Exponente : %i",poly->exp1);
poly=poly->siguiente;
}
}
 
void suma_poly(struct apuntador *poly1, struct apuntador *poly2,
struct apuntador **poly3)
{
float suma;
struct apuntador *primero;
primero = NULL;
suma=0;
while((poly1 != NULL) && (poly2 != NULL))
{
if (poly1->exp1 == poly2-> exp1)
{
suma=poly1->coef + poly2->coef;
if(suma !=0)
{
insert_lista(&primero,suma,poly1->exp1);
}
poly1=poly1->siguiente;
poly2=poly2->siguiente;
}
else
if(poly1->exp1 > poly2->exp1)
{
insert_lista(&primero,poly1->coef,poly1->exp1);
poly1=poly1->siguiente;
}
else
{
insert_lista(&primero,poly2->coef,poly2->exp1);
poly2=poly2->siguiente;
}
suma=0;
}
while(poly1 != NULL)
{
insert_lista(&primero,poly1->coef,poly1->exp1);
poly1=poly1->siguiente;
}
while(poly2 != NULL)
{
insert_lista(&primero,poly2->coef,poly2->exp1);
poly2=poly2->siguiente;
}
*poly3 = primero;
}
 
void multy_poly(struct apuntador *poly1, struct apuntador *poly2,
struct apuntador **poly3)
{
struct apuntador *primero;
float multy;
int sum;
struct apuntador *p, *q, *aux1, *aux2;
 
primero = NULL;
*poly3=NULL;
multy=1;
sum=0;
while(poly2 != NULL)
{
p=poly1;
while(p!=NULL)
{
multy=p->coef *poly2->coef;
sum=p->exp1 + poly2->exp1;
if(multy != 0)
{
insert_lista(&primero,multy,sum);
p=p->siguiente;
}
else
{
insert_lista(&primero,p->coef,p->exp1);
p=p->siguiente;
poly2=poly2->siguiente;
}
multy=1;
}
poly2=poly2->siguiente;
}
q = primero;
while(q)
{
if(q->exp1==q->siguiente->exp1)
{
q->coef=q->coef + q->siguiente->coef;
q->siguiente=q->siguiente->siguiente;
}
else
q=q->siguiente;
}
*poly3=primero;
}
 
void resta_poly(struct apuntador *poly1, struct apuntador *poly2,
struct apuntador **poly3)
{
float resta;
struct apuntador *primero;
primero = NULL;
resta= 0;
while ((poly1 != NULL) && (poly2 != NULL))
{
if (poly1->exp1 == poly2->exp1)
{
resta= poly1->coef - poly2->coef;
if (resta != 0) {
insert_lista (&primero, resta, poly1->exp1); }
poly1= poly1->siguiente;
poly2= poly2->siguiente;
}
else
if (poly1->exp1 > poly2->exp1)
{
insert_lista (&primero, poly1->coef, poly1->exp1);
poly1= poly1->siguiente;
}
else
{
insert_lista (&primero, poly2->coef, poly2->exp1);
poly2= poly2->siguiente;
}
resta= 0;
}
 
while (poly1 != NULL)
{
insert_lista (&primero, poly1->coef, poly1->exp1);
poly1= poly1->siguiente;
}
 
while (poly2 != NULL)
{
insert_lista (&primero, -poly2->coef, poly2->exp1);
poly2= poly2->siguiente;
}
*poly3 = primero;
}
 
void borrar_poly(struct apuntador **poly, int expo)
{
// aqui va el codigo correspondiente
}
 
void div_poly(struct apuntador *polynume, struct apuntador *polydeno,
struct apuntador **polycoci, struct apuntador **polyresi)
{
 
}
 
void der_poly(struct apuntador *poly1,struct apuntador **poly3)
{
struct apuntador *primero=NULL;
float co=0;
int ex=0;
while((poly1!=NULL)&&(poly1->exp1!=0))
{
co=poly1->coef*poly1->exp1;
ex=poly1->exp1-1;
insert_lista(&primero,co,ex);
poly1=poly1->siguiente;
}
}
 
float eval_poly(struct apuntador *poly1, float ev)
{
float res2;
res2=0;
while(poly1)
{
res2=res2+poly1->coef*pow(ev,poly1->exp1);
poly1=poly1->siguiente;
}
return (res2);
}
 
void presentacion(void)
{
char c[1];
clrscr();
printf("\n\n PROGRAMA POLINOMIO \n\n");
printf(" REALIZA LA SUMA, RESTA, MULTIPLICACION Y DIVISION \n\n");
 
}
 
float factorial(int n)
{
int k;
float f=1;
for(k=1;k<=n;k++)
f=f*k;
return(f);
}
 
ivanel93
Navegador Iniciado
Mensajes: 1
graphgraph
Usuario Offline Presiona aquí para ver el perfil de este usuario
El administrador ha deshabilitado la escritura pública.
 
#235
Re: Menú con funciones, no se si lo esta haciendo bien Creado el día: 11/03/2013 11:13 Karma: 2
Qué compilador estás usando?

Puedes compilar y ejecutar el programa para probar si este funciona bien, a la par hacer usar la calculadora para verificar si los resultados son correctos o no.
adrianvaca
Administrador
Mensajes: 110
graph
Usuario Offline Presiona aquí para ver el perfil de este usuario
El administrador ha deshabilitado la escritura pública.
El conocimiento es una forma de empezar a ser libre
 
ArribaPágina: 1
obtén los últimos mensajes directamente en tu escritorio