




























































































Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!





























































































UNIVERSIDAD DE M ´ALAGA Dpto. Lenguajes y CC. Computaci´on E.T.S.I. Telecomunicaci´on
using namespace std ; const tasa : 25.0; const PRECIO_HORA = 60.0; int main () { double horas , dias , total , neto ; cout < < " Introduzca las horas trabajadas : " ; cin < < horas ; cout < < " Introduzca los dias trabajados : " ; cin > > dias ; horas * dias * PRECIO_HORA = total ; neto = total - TASA ; cout > > " El valor total a pagar es : " > > total > > endl ; cout < < " El valor neto a pagar es : " < < NETO < < endl ; }
using namespace std ; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− const double TASA = 25.0; const double PRECIO_HORA = 60.0; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− int main () { double horas , dias , total , neto ; cout < < " Introduzca las horas trabajadas : " ; cin > > horas ; cout < < " Introduzca los dias trabajados : " ; cin > > dias ; total = horas * dias * PRECIO_HORA ; neto = total - TASA ; cout < < " El valor total a pagar es : " < < total < < endl ; cout < < " El valor neto a pagar es : " < < neto < < endl ; }
using namespace std ; int main () { int dato1 , dato2 ; cout < < " Introduzca un n´umero entero : " ; cin > > dato1 ; cout < < " Introduzca otro n´umero entero : " ; cin > > dato2 ; cout < < " El valor del primer n´umero introducido es : " < < dato1 < < endl ; cout < < " El valor del segundo n´umero introducido es : " < < dato2 < < endl ; } //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− // Cuando s e i n t r o d u c e por t e c l a d o un v a l o r adecuado a l t i p o de l a // v a r i a b l e que s e e s t ´a l e y e n d o , l a l e c t u r a s e hace c o r r e c t a m e n t e. // Sin embargo , s i s e i n t r o d u c e por t e c l a d o un v a l o r NO adecuado a l // t i p o de l a v a r i a b l e que s e e s t ´a l e y e n d o , e n t o n c e s l a o p e r a c i ´o n de // l e c t u r a f a l l a , l a v a r i a b l e mantiene e l v a l o r que t u v i e s e // a n t e r i o r m e n t e ( en e s t e c a s o como l a v a r i a b l e no ha s i d o // i n i c i a l i z a d a , t e n d r ´a un v a l o r INESPECIFICADO) , y e l f l u j o de // e n t r a d a (CIN) s e pondr´a en modo err´oneo , por l o que c u a l q u i e r o t r a // o p e r a c i ´o n de e n t r a d a que s e r e a l i c e p o s t e r i o r m e n t e tambi´en f a l l a r ´a.
a) -20 y 30. b) 20 y -30.
c) 147483647 y 2000000000.
d) 200000000 y 2000000000. e) 1 y 2147483647.
f ) 1 y 3000000000.
using namespace std ; int main () {
a: 1 , b: 1 , c: 1 ⇒ true a: 2 , b: 2 , c: 4 ⇒ true
a: 3 , b: 3 , c: 9 ⇒ true
a: 3 , b: 0.1, c: 0.3 ⇒ false a: 2 , b: 0.2, c: 0.4 ⇒ true
a: 3 , b: 0.3, c: 0.9 ⇒ false
a: 0.1, b: 0.1, c: 0.01 ⇒ false a: 0.2, b: 0.2, c: 0.04 ⇒ false
a: 0.3, b: 0.3, c: 0.09 ⇒ false
using namespace std ; int main () { double a , b , c ; cout < < " Introduce 3 n´umeros reales : " ; cin > > a > > b > > c ; bool cmp = ( a * b = = c ); cout < < a < < " * " < < b < < " == " < < c < < " " < < boolalpha < < cmp < < endl ; } //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− // El t i p o DOUBLE u t i l i z a una r e p r e s e n t a c i ´o n INEXACTA y FINITA , por l o // que s e produce p´e r d i d a de p r e c i s i ´o n en l a s o p e r a c i o n e s de coma // f l o t a n t e , de t a l forma que p o d r´ı a s u c e d e r que dos v a l o r e s que // matem´aticamente son i g u a l e s sean computacionalmente l i g e r a m e n t e // d i f e r e n t e s ( un v a l o r d e c i m a l muy peque ˜no ) y por l o t a n t o l a // comparaci´on de i g u a l d a d (==) e n t r e n´umeros r e a l e s no p r o d u z c a // l o s r e s u l t a d o s e s p e r a d o s. Por l o t a n t o , l o s n´umeros r e a l e s nunca s e // d e b e r´ı a n d e b e r´ı a n comparar por i g u a l d a d , s i n o mediante una // o p e r a c i ´o n s i m i l a r a : a b s ( x−y ) < 1e− 9
using namespace std ; int main () { cout < < " Introduzca dos n´umeros naturales : " ; int dato1 , dato2 ; cin > > dato1 > > dato2 ; int aux = dato1 ; dato1 = dato2 ; dato2 = aux ; cout < < " Valor almacenado en Dato1 : " < < dato1 < < endl ; cout < < " Valor almacenado en Dato2 : " < < dato2 < < endl ; }
using namespace std ; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− const double EUR_PTS = 166.386; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− int main () { cout < < " Introduzca la cantidad de euros : " ; double euros ; cin > > euros ; double pts = euros * EUR_PTS ; cout < < euros < < " euros son " < < pts < < " pts " < < endl ; }
using namespace std ; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− const int N_VALORES = 3; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− int main () { cout < < " Introduzca 3 n´umeros enteros : " ; int n1 , n2 , n3 ; cin > > n1 > > n2 > > n3 ; double media = double ( n1 + n2 + n3 ) / double ( N_VALORES ); cout < < " Media " < < media < < endl ; }
using namespace std ; int main () { cout < < " Introduzca un n´umero natural : " ; int dato ; cin > > dato ; bool es_par = ( dato % 2 = = 0 ); cout < < " El n´umero " < < dato < < " es par? " < < boolalpha < < es_par < < endl ; }
using namespace std ; int main () { cout < < " Introduzca una letra min´uscula : " ; char letra_minuscula ; cin > > letra_minuscula ; char letra_mayuscula = char ( 'A ' + ( letra_minuscula - 'a ' ) ); cout < < letra_minuscula < < " -> " < < letra_mayuscula < < endl ; }
using namespace std ; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− const int SEG_MIN = 60; const int MIN_HORA = 60; const int HORAS_DIA = 24; const int DIAS_SEMANA = 7; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− const int SEG_HORA = SEG_MIN * MIN_HORA ;
int provincia = codigo / BASE_PROV ; int operacion = ( codigo % BASE_PROV ) / BASE_OP ; int control = ( codigo % BASE_PROV ) % BASE_OP ; bool correcto = (( codigo > = BASE_PROV ) & & ( codigo < LIMITE ) & & ( control = = ( operacion * provincia % 10 ))); //−−−−−−−−−−−−−−−−−−−−−−−−−−−−− cout < < " Provincia : " < < provincia < < endl ; cout < < " N´umero de operaci´on : " < < operacion < < endl ; cout < < " D´ıgito de control : " < < control < < endl ; cout < < " Correcto : " < < boolalpha < < correcto < < endl ; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−− }
Estructuras de Control
using namespace std ; int main () { int x , y ; char c ; cout < < " Introduzca dos numeros naturales : " ; cin > > x > > y ; cout < < " Introduzca un caracter : " ; cin > > c ; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− bool prop_a = ( x > = 3 & & x < = 7); bool prop_b = ( x > = 1 & & x < = 3) | | ( x > = 7 & & x < = 9); bool prop_c = ( x > = 1 & & x < = 9) & & ( x % 2 = = 1); bool prop_d = ( x = = 2) | | ( x > = 5 & & x < = 9); bool prop_e = (( x > = 3 & & x < = 9 & & x! = 5 & & x! = 7) & & (( y > = 6 & & y < = 8) | | y = = 3)); bool prop_f = ( x < = 10 & & y < = 10); bool prop_g =! ( y! = 0 & & x % y = = 0); bool prop_h = ( c > = 'A ' & & c < = 'Z ' ); bool prop_i = ( c > = 'A ' & & c < = 'Z ') | | ( c > = 'a ' & & c < = 'z ' ); bool prop_j = (( c > = 'A ' & & c < = 'Z ') | | ( c > = 'a ' & & c < = 'z ') | | ( c > = ' 0 ' & & c < = ' 9 ' )); //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− cout < < boolalpha ; cout < < " ( a ) " < < x < < " pertenece a { 3 , 4 , 5 , 6 , 7 }: " < < prop_a < < endl ; cout < < " ( b ) " < < x < < " pertenece a { 1 , 2 , 3 , 7 , 8 , 9 }: " < < prop_b < < endl ; cout < < " ( c ) " < < x < < " pertenece a { 1 , 3 , 5 , 7 , 9 }: " < < prop_c < < endl ; cout < < " ( d ) " < < x < < " pertenece a { 2 , 5 , 6 , 7 , 8 , 9 }: " < < prop_d < < endl ; cout < < " ( e ) " < < x < < " pertenece a { 3 , 4 , 6 , 8 , 9 } , " < < y < < " pertenece a { 6 , 7 , 8 , 3 }: " < < prop_e < < endl ; cout < < " ( f ) Ni " < < x < < " ni " < < y < < " sean mayores que 10: " < < prop_f < < endl ; cout < < " ( g ) " < < x < < " no sea multiplo de " < < y < < " : " < < prop_g < < endl ;
cout < < " ( h ) '" < < c < < " ' es una letra mayuscula : " < < prop_h < < endl ; cout < < " ( i ) '" < < c < < " ' es una letra : " < < prop_i < < endl ; cout < < " ( j ) '" < < c < < " ' es un alfanumerico : " < < prop_j < < endl ; }
using namespace std ; int main () { double nota ; cout < < " Introduzca la Nota : " ; cin > > nota ; if (! (( nota > = 0.0) & & ( nota < = 10.0))) { cout < < " Error : 0 <= n <= 10 " < < endl ; } else if ( nota > = 10.0 - 1 e - 9) { cout < < " Matr´ıcula de Honor " < < endl ; } else if ( nota > = 9.0) { cout < < " Sobresaliente " < < endl ; } else if ( nota > = 7.0) { cout < < " Notable " < < endl ; } else if ( nota > = 5.0) { cout < < " Aprobado " < < endl ; } else { cout < < " Suspenso " < < endl ; } }
using namespace std ; int main () { int a , b , c , d ; cout < < " Introduzca cuatro n´umeros : " ; cin > > a > > b > > c > > d ; int mayor = a ; if ( b > mayor ) { mayor = b ; } if ( c > mayor ) { mayor = c ; } if ( d > mayor ) { mayor = d ; } cout < < " Mayor : " < < mayor < < endl ; }
cout < < " Operaci´on : " ; char op ; cin > > op ; if (! ( op = = '+ ' | | op = = ' - ' | | op = = '* ' | | op = = '/ ' )) { cout < < " ERROR : Operaci´on no v´alida " < < endl ; } else { cout < < " Operando 1: " ; int op1 ; cin > > op1 ; cout < < " Operando 2: " ; int op2 ; cin > > op2 ; int res = 0; switch ( op ) { case '+ ': res = op1 + op2 ; break ; case ' - ': res = op1 - op2 ; break ; case '* ': res = op1 * op2 ; break ; case '/ ': if ( op2! = 0) { res = op1 / op2 ; } else { res = 0; cout < < " Error , divisi´on por cero " < < endl ; } break ; } cout < < " Resultado : " < < res < < endl ; } }
Operacion : * Operando 1 : 13 Operando 2 : 10 Resultado : 130 Operacion : u ERROR!!!! Operacion : + Operando 1 : 12 Operando 2 : 3 Resultado : 15 Operacion : & FIN DEL PROGRAMA
using namespace std ; int main () { char op ; cout < < " Operaci´on : " ; cin > > op ; while ( op! = '& ') { if (! ( op = = '+ ' | | op = = ' - ' | | op = = '* ' | | op = = '/ ' )) { cout < < " ERROR : Operaci´on no v´alida " < < endl ; } else { cout < < " Operando 1: " ; int op1 ; cin > > op1 ; cout < < " Operando 2: " ; int op2 ; cin > > op2 ; int res = 0; switch ( op ) {
case '+ ': res = op1 + op2 ; break ; case ' - ': res = op1 - op2 ; break ; case '* ': res = op1 * op2 ; break ; case '/ ': if ( op2! = 0) { res = op1 / op2 ; } else { res = 0; cout < < " Error , divisi´on por cero " < < endl ; } break ; } cout < < " Resultado : " < < res < < endl ; } cout < < " Operaci´on : " ; cin > > op ; } }
using namespace std ; const char SIMBOLO = '* '; int main () { int n ; cout < < " Introduzca un n´umero : " ; cin > > n ; for ( int i = 0; i < n ; + + i ) { cout < < SIMBOLO ; } cout < < endl ; }
using namespace std ; const char SIMBOLO = '* '; int main () { int n ; cout < < " Introduzca un n´umero : " ; cin > > n ; for ( int j = 0; j < n ; + + j ) { for ( int i = 0; i < n ; + + i ) { cout < < SIMBOLO ; } cout < < endl ;
using namespace std ; const char SIMBOLO = '* '; int main () { int n ; cout < < " Introduzca un n´umero : " ; cin > > n ; for ( int j = 0; j < n ; + + j ) { for ( int i = 0; i < = j ; + + i ) { if (( j = = 0) | | ( j = = n - 1) | | ( i = = 0) | | ( i = = j )) { cout < < SIMBOLO ; } else { cout < < " " ; } } cout < < endl ; } }
using namespace std ; const char SIMBOLO = '* '; int main () { int n ; cout < < " Introduzca un n´umero : " ; cin > > n ; for ( int j = 0; j < n ; + + j ) { for ( int i = 0; i < n - j - 1; + + i ) { cout < < " " ; } for ( int i = 0; i < 2 * j + 1; + + i ) { cout < < SIMBOLO ; } cout < < endl ; } }
using namespace std ; const char SIMBOLO = '* '; int main () { int n ; cout < < " Introduzca un n´umero : " ; cin > > n ; for ( int j = 0; j < n ; + + j ) { for ( int i = 0; i < n - j - 1; + + i ) { cout < < " " ; } for ( int i = 0; i < 2 * j + 1; + + i ) { if (( j = = 0) | | ( j = = n - 1) | | ( i = = 0) | | ( i = = 2 * j )) { cout < < SIMBOLO ; } else { cout < < " " ; } } cout < < endl ; } }
using namespace std ; const char SIMBOLO = '* '; int main () { int n ; cout < < " Introduzca un n´umero : " ; cin > > n ; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−− for ( int j = 0; j < n ; + + j ) { for ( int i = 0; i < n - j - 1; + + i ) { cout < < " " ; } for ( int i = 0; i < 2 * j + 1; + + i ) { cout < < SIMBOLO ; } cout < < endl ; } //−−−−−−−−−−−−−−−−−−−−−−−−−−−−− for ( int j = n - 2; j > = 0; - - j ) { for ( int i = 0; i < n - j - 1; + + i ) { cout < < " " ; } for ( int i = 0; i < 2 * j + 1; + + i ) { cout < < SIMBOLO ; } cout < < endl ; } //−−−−−−−−−−−−−−−−−−−−−−−−−−−−− }
using namespace std ; int main () { int b , e ; cout < < " Introduce la base y el exponente : " ; cin > > b > > e ; int total = 1; for ( int i = 0; i < e ; + + i ) { total = total * b ; } cout < < " Resultado : " < < total < < endl ; }
using namespace std ; int main () { int x ; cout < < " Introduce un n´umero natural : " ; cin > > x ; int total = 1; for ( int i = 2; i < = x ; + + i ) { total = total * i ; } cout < < " Resultado : " < < total < < endl ; }
using namespace std ; int main () { int dividendo , divisor ; cout < < " Introduce dividendo y divisor : " ; cin > > dividendo > > divisor ; if ( divisor = = 0) { cout < < " El divisor no puede ser cero " < < endl ; } else { int resto = dividendo ; int cociente = 0; while ( resto > = divisor ) { resto - = divisor ;
using namespace std ; int main () { bool es_primo = false ; int num , divisor ; cout < < " Introduzca un n´umero natural : " ; cin > > num ; if ( num > 1) { divisor = 2; while (( num % divisor )! = 0) {
using namespace std ; int main () { int cnt_ct = 0; // c u e n t a de c a r a c t e r e s int cnt_cm = 0; // c u e n t a de comas cout < < " Introduzca una frase hasta '. ': " ; char c ; cin. get ( c ); // l e e un c a r ´a c t e r while ( c! = '. ') {
using namespace std ; int main () { bool ok = false ; char ant2 = ' '; char ant1 = ' '; char act ; cout < < " Introduzca una frase hasta '. ': " ; cin. get ( act ); while ( act! = '. ') { if ( ant2 = = 'a ' & & ant1 = = 'b ' & & act = = 'c ') { ok = true ; } ant2 = ant1 ; ant1 = act ; cin. get ( act ); } cout < < " La secuencia ' abc ' " ; if ( ok ) {
cin > > n ; } cout < < " Primera ocurrencia : " < < pri_oc < < endl ; cout < < " Ultima ocurrencia : " < < ult_oc < < endl ; }
using namespace std ; int main () { int lim_inf , lim_sup ; cout < < " Introduce el l´ımite inferior : " ; cin > > lim_inf ; cout < < " Introduce el l´ımite superior : " ; cin > > lim_sup ; if ( lim_inf > lim_sup ) { cout < < " Error : l´ımites err´uneos " < < endl ; } else { cout < < " Piense un n´umero dentro del rango [ " < < lim_inf < < " -" < < lim_sup < < " ] y pulse ENTER " ; char cod ; cin. get ( cod ); // e l i m i n a ENTER d e l BUFFER ENTRADA´ de l i m s u p cin. get ( cod ); // e l i m i n a ENTER d e l BUFFER ENTRADA´ de pausa int num ; do { num = ( lim_inf + lim_sup ) / 2; cout < < " >> ¿Es el n´umero " < < num < < "? " ; cin > > cod ; switch ( cod ) { case ' < ': lim_sup = num - 1; break ; case ' > ': lim_inf = num + 1; break ; case '= ': break ; default : cout < < " C´odigo Erroneo " < < endl ; } } while (( cod! = '= ') & & ( lim_inf < = lim_sup )); if ( cod = = '= ') { cout < < " El n´umero pensado es el " < < num < < endl ; } else { cout < < " Has introducido datos err´oneos " < < endl ; } } }
using namespace std ; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− const int FIB_0 = 0; const int FIB_1 = 1;
int main () { int n ; cout < < " Introduzca N : " ; cin > > n ; int fib_n ; if ( n = = 0) { fib_n = FIB_0 ; } else { int fib_n1 = FIB_0 ; fib_n = FIB_1 ; for ( int i = 2; i < = n ; + + i ) { int fib_n2 = fib_n1 ; fib_n1 = fib_n ; fib_n = fib_n1 + fib_n2 ; } } cout < < " Fibonacci ( " < < n < < " ): " < < fib_n < < endl ; }
El B´uffer de Entrada y el B´uffer de Salida de Datos
using namespace std ; int main () { cout < < " Introduzca un numero : " ; int dato_1 ; cin > > dato_1 ; cout < < " Introduzca otro numero : " ; int dato_2 ; cin > > dato_2 ; cout < < " Los numeros leidos son : " < < dato_1 < < ' ' < < dato_2 < < endl ; }
// El programa muestra e l mensaje [ I n t r o d u z c a un numero : ] e n t o n c e s l a // s e n t e n c i a [ c i n >> d a t o 1 ] a c c e d e a l BUFFER−DE−ENTRADA para l e e r un // d a t o. Como e l b u f f e r e s t ´a v a c i o , d i c h a s e n t e n c i a e s p e r a r ´a a que // haya a l g ´u n d a t o en e l b u f f e r. El u s u a r i o t e c l e a 1 2 [ENTER] que s e // almacenar´a en e l b u f f e r , por l o que e l programa s e d e s p i e r t a y // a s i g n a r ´a e l v a l o r numerico 12 a l a v a r i a b l e [ d a t o 1 ] , e l i m i n ´a n d o s e // l o s c a r a c t e r e s 1 y 2 d e l b u f f e r ( e l c a r ´a c t e r [ENTER] permanece en // e l b u f f e r ) // // P o s t e r i o r m e n t e e l programa muestra e l mensaje [ I n t r o d u z c a o t r o // numero : ] e n t o n c e s l a s e n t e n c i a [ c i n >> d a t o 2 ] a c c e d e a l // BUFFER−DE−ENTRADA para l e e r o t r o d a t o. Como e l b u f f e r NO e s t ´a // v a c i o , l a l e c t u r a s a l t a l o s espacios/ENTER i n i c i a l e s , por l o que // e l i m i n a r ´a e l c a r a c t e r [ENTER] d e l b u f f e r ( quedaba a l l ´ı de l a // l e c t u r a a n t e r i o r ) , por l o que e l b u f f e r s e queda v a c i o y por l o // t a n t o l a s e n t e n c i a de l e c t u r a e s p e r a r ´a a que haya a l g ´u n d a t o en e l // b u f f e r. El u s u a r i o t e c l e a 3 4 [ENTER] que s e almacenar´a nuevamente en // e l b u f f e r , por l o que e l programa s e d e s p i e r t a y a s i g n a r ´a e l v a l o r // numerico 34 a l a v a r i a b l e [ d a t o 2 ] , e l i m i n a n d o s e l o s c a r a c t e r e s 3 y