Está en la página 1de 8

SEMANA03 – SENTENCIA SWITCH

EN ESTE PROBLEMA NO CONOZCO LAS OPCIONES A ELEGIR


//Operaciones matematicas
#include<iostream>
#include<stdlib.h> //Para congelar el resultado
#include<math.h>
using namespace std;
class menu{
protected:
float a,b;
public:
void leeab(float m,float n);
void operacion(float a,float b);
};
//Implementacion
void menu::leeab(float m,float n)
{a=m;b=n;}

void menu::operacion(float a,float b)


{char opc;
cout<<"Ingrese su opcion a,b,c,d,e";
cin>>opc;
cout<<"<a> Suma (a+b)"<<endl
<<"<b> Diferencia (a-b)"<<endl
<<"<c> Cociente (a/b)"<<endl
<<"d <d> Raiz(raiz(a)+raiz(b))"<<endl
<<"e <e> Salir"<<endl;
switch(opc){
case 'a':cout<<(a+b);break;
case 'b':cout<<(a-b);break;
case 'c':if(b>0)
cout<<(a/b);
else
cout<<"Sin Solucion ";
system("pause");
exit(0);break;
case 'd':if(a>0 && b>0)
cout<<(sqrt(a)+sqrt(b));
else
if(a>0)
cout<<(sqrt(a));
else
if(b>0)
cout<<(sqrt(b));
else
cout<<"Sin solucion ";break;
case 'e':exit(1);break;
default:cout<<"No es ninguna opcion"<<endl;break;
}
void main();{
float p,q;menu men1;
cout<<"Ingrese el valor de a= ";cin>>p;
cout<<"Ingrese el valor de b= ";cin>>q;
men1.leeab(p,q);
men1.operacion(p,q);
cout<<endl;
}
AHORA SE VERÁ PRIMERO LAS OPCIONES AL COMPILAR

//Operaciones matematicas
#include<iostream>
#include<stdlib.h> //Para congelar el resultado
#include<math.h>
using namespace std;
class menu{
protected:
float a,b;
public:
void leeab(float m,float n);
void operacion(float a,float b);
};
//Implementacion
void menu::leeab(float m,float n)
{a=m;b=n;}

void menu::operacion(float a,float b)


{char opc;
cout<<"Ingrese su opcion a,b,c,d,e";
cin>>opc;

switch(opc){
case 'a':cout<<(a+b);break;
case 'b':cout<<(a-b);break;
case 'c':if(b!=0) //Tambien puede poner solo ‘b’ y seria mas elegante.
cout<<(a/b);
else
cout<<"Sin Solucion ";
system("pause");
exit(0);break;
case 'd':if(a>0 && b>0)
cout<<(sqrt(a)+sqrt(b));
else
if(a>0)
cout<<(sqrt(a));
else
if(b>0)
cout<<(sqrt(b));
else
cout<<"Sin solucion ";break;
case 'e':exit(1);break;
default:cout<<"No es ninguna opcion"<<endl;break;
}
}
void main(){
float p,q;menu men1;
cout<<"Ingrese el valor de a= ";cin>>p;
cout<<"Ingrese el valor de b= ";cin>>q;
cout<<"<a> Suma (a+b)"<<endl
<<"<b> Diferencia (a-b)"<<endl
<<"<c> Cociente (a/b)"<<endl
<<"d <d> Raiz(raiz(a)+raiz(b))"<<endl
<<"e <e> Salir"<<endl;
men1.leeab(p,q);
men1.operacion(p,q);
cout<<endl;
system("pause");
}

PROBLEMA 1
/*
//CREANDO UN OBJETO MENU DE OPERACIONES ARITMETICAS
#include<iostream>
#include<stdlib.h>
using namespace std;
class menu1{
protected:
float a,b;
public:
void leeeab(float m,float n);
void operaciones(float a, float b);
} ;
//implementacion
void menu1::leeeab(float m, float n)
{a=m;b=n;}

void menu1::operaciones(float a, float b){


int opc;
cout<<"Ingrese su opcion ";cin>>opc;
switch(opc){
case 1:cout<<"La suma es= "<<(a+b);break;
case 2:cout<<"La Diferencia es= "<<(a-b);break;
case 3:cout<<"El cociente es= ";
if(b!=0)
cout<<(a*1.0/b);
else{
cout<<"Sin Solucion ";
system("pause");
exit(0);
}break;
case 4:cout<<"El producto= "<<a*b;break;
case 5:exit(1);
default:cout<<"No existe tal opcion ";break;
}
}
void main(){
float p,q;menu1 men;
cout<<"Ingrese el 1er operando a= ";cin>>p;
cout<<"Ingrese el 2do operando b= ";cin>>q;
cout<<"[1] Suma (a+b) "<<endl
<<"[2] Diferencia (a-b)"<<endl
<<"[3] Cociente (a/b)"<<endl
<<"[4] Producto (a*b)"<<endl
<<"[5] Salir "<<endl;
men.leeeab(p,q);
men.operaciones(p,q);
cout<<endl;
system("pause");
}
*/
PROBLEMA 2
//Halla el valor del nuero 'e' por taylor
//utilizar las intrucciones de contro while y for
#include<iostream>
#include<stdlib.h>
using namespace std;
class e{
protected:
int N;
public:
void leeN(int M);
double calcula_e(int N);
} ;
using namespace std;
//Implementacion
void e::leeN(int M)
{N=M;}
double e::calcula_e(int M){
double S=0;
long i=1,F=1;
while(i<=N){
F=F*i;
S=S+1.0/F;
i=i+1;
}
return(S+1);
}
void main(){
long P;e e1;
cout<<"Ingrese el numero de terminos N= ";
cin>>P;
e1.leeN(P);
cout<<"El Valor de 'e'aproximado = "
<<e1.calcula_e(P)<<endl;
system("pause");
}
SENTENCIA WHILE

La instrucción de control while( ), evalúa la condición si es verdadera o falsa.


Mientras la condición sea verdadera realizara un proceso o un grupo de procesos en cuanto
la condición deje de ser verdadera terminará el while( ).

PROBLEMA 3
//SUMA DE IMPARES INVERSOS
#include<iostream>
#include<stdlib.h>
using namespace std;
class sumaInversa{
protected:
int N;
public:
void leerN(int M);
double suma(int N);
};
//Implementacion
void sumaInversa::leerN(int M)
{N=M;}
double sumaInversa::suma(int N){
double S=0;int i=1;
while(i<=N){
S=S+1.0/(2*i-1);
i=i+1;
}
return(S);
}
void main(){
int P;sumaInversa SI;
cout<<"Ingrese el numero de terminos N= ";cin>>P;
SI.leerN(P);
cout<<"La suma de los N primeros terminos es igual a: "<<SI.suma(P)<<endl;
system("pause");
}
LO ANTERIOR CON FOR
//USANDO EL FOR
#include<iostream>
#include<stdlib.h>
using namespace std;
class sumaInversa{
protected:
int N;
public:
void leerN(int M);
double suma(int N);
};
//Implementacion
void sumaInversa::leerN(int M)
{N=M;}
double sumaInversa::suma(int N){
double S=0;int i=1;
for(i=1;i<=N;i++){
S=S+1.0/(2*i-1);
}
return(S);
}
void main(){
int P;sumaInversa SI;
cout<<"Ingrese el numero de terminos N= ";cin>>P;
SI.leerN(P);
cout<<"La suma de los N primeros terminos es igual a: "<<SI.suma(P)<<endl;
system("pause");
PROBLEMA 4
//APLICACION 2
#include<iostream>
#include<stdlib.h>
using namespace std;
class sumaInversa{
protected:
int N;
public:
void leerN(int M);
double suma(int N);
};
//Implementacion
void sumaInversa::leerN(int M)
{N=M;}
double sumaInversa::suma(int N){
double S=0;long F=1,i=1;
for(i=1;i<=N;i++){
F=F*i;
S=S+(sqrtf(F))*1.0/(2*i-1);
}
return(S);
}
void main(){
int P;sumaInversa S2;
cout<<"Ingrese el numero de terminos N= ";cin>>P;
S2.leerN(P);
cout<<"La suma de los N primeros terminos es igual a: "<<S2.suma(P)<<endl;
system("pause");
}
TAREA:

𝟏 √𝟐! √𝟑! √𝟒! √𝟓! √𝟔!


𝑺𝟏 = 𝟐 + 𝟑 + 𝟒 + 𝟓 + 𝟔 + 𝟕 +⋯
√𝟐 √𝟑 √𝟒 √𝟓 √𝟔 √𝟕

También podría gustarte