Está en la página 1de 2

Solución Practica Calificada’08 - Programación C++ Pag.

ÍNDICE
ÍNDICE.............................................................................................................................................................................. 1
1. EJERCICIO 1: RAÍCES POR MÉTODO DE NEWTON-RAPHSON ............................................................. 1
2. EJERCICIO 2: CONTROL DE LLAMADAS CON CLASES.......................................................................... 2

1. Ejercicio 1: Raíces por el método de Newton-Raphson


#include <iostream.h>
#include <math.h>
double NewtonRaphson(double *coef, double x, int &numIter, int MAX);

void main()
{
double cf[3];
double x;
int nIt, nMax=1000;

cout << "** Metodo Newton-Raphson ***"<<endl;


cout << "\nDar Coeficientes:?";
cin >> cf[0] >> cf[1] >> cf[2];
cout << "Dar valor inicial de X:?";
cin >> x;

double val = NewtonRaphson(cf,x,nIt,nMax);


if (nIt < nMax)
cout <<"> Raiz="<< val<< " en "<<nIt<<" Iteraciones.."<<endl;
else
cout << "> No converge..."<<endl;

cout << endl;


}

double NewtonRaphson(double *coef, double x, int &numIter, int MAX)


{
double val=0,x1;
double err=1; //para que entre
numIter=0; //iteraciones

while( err > 1e-12 && numIter < MAX)


{
x1 = x - (coef[0]*x*x +coef[1]*x+coef[2])/(coef[0]*2*x + coef[1]*x);
err = fabs(x - x1);
x = x1;
numIter++;
}
val = x;

return val;
}

Tecnun (Universidad de Navarra) ©Paul Bustamante


Solución Practica Calificada’08 - Programación C++ Pag. 2

2. Ejercicio 2: Control de llamadas con clases


#include <iostream.h>
#include <string.h>
#include <iomanip.h>

class Llamada
{
char telf[20];
double dur; //duracion en minutos
double precio; //por minuto
public:
Llamada(){}
void PedirDatos(){
cout << "Telefono:?"; cin >> telf;
cout << "Duracion:?"; cin >> dur;
cout << "Precio/Min:?"; cin >> precio;

}
void Ver()
{
cout <<setw(15) <<telf <<setw(10)<< dur <<setw(10) <<precio <<endl;
}
double GetCosto() { return precio*dur; }
};

void main()
{
int n=0,opc,i;
double tot=0;
Llamada lista[30];

while(1){
cout<<"\n**** Menu **** \n1.Agregar \n2.Ver Llamadas \n3.Salir
\n\tOpc:?";
cin >> opc;
switch (opc){
case 1:
lista[n].PedirDatos();
n++;
break;
case 2:
cout << setw(15) << "Telefono" << setw(10) << "Dur(min)";
cout << setw(10) << "Precio" << endl;
for ( i=0;i<n;i++) {
lista[i].Ver();
tot += lista[i].GetCosto();
}
cout << setw(15) << " " << setw(10) << "Total";
cout << setw(10) << tot << endl;
break;

case 3: return;

}
}
}

Tecnun (Universidad de Navarra) ©Paul Bustamante

También podría gustarte