http://wiki.cabal.mx/index.php?title=Curso_de_Programaci%C3%B3n_en_C/Prog117&feed=atom&action=history
Curso de Programación en C/Prog117 - Historial de revisiones
2024-03-29T15:43:40Z
Historial de revisiones para esta página en el wiki
MediaWiki 1.32.1
http://wiki.cabal.mx/index.php?title=Curso_de_Programaci%C3%B3n_en_C/Prog117&diff=5518&oldid=prev
Perseuz: /* Prog117 */
2014-06-04T15:13:29Z
<p><span dir="auto"><span class="autocomment">Prog117</span></span></p>
<p><b>Página nueva</b></p><div>__NOTOC__<br />
* [[:#Prog117 | struct]]<br />
** [[:#Resultado | Resultado]]<br />
** [[:#Explicación | Explicación]]<br />
<br />
== Prog117 ==<br />
<br />
<syntaxhighlight lang="c" line="GESHI_FANCY_LINE_NUMBERS"><br />
#include <stdio.h><br />
#include <stdlib.h><br />
#include <string.h><br />
<br />
#define MAXTITL 41<br />
#define MAXAUTL 31<br />
#define MAXVALL 10<br />
<br />
<br />
struct Libro<br />
{<br />
char Titulo[MAXTITL];<br />
char Autor[MAXAUTL];<br />
float Valor;<br />
};<br />
<br />
int main( void )<br />
{<br />
int i;<br />
char ValSTR[MAXVALL + 1];<br />
struct Libro Biblio;<br />
<br />
printf( "Ingresa el nombre del libro: " );<br />
<br />
<br />
fgets( Biblio.Titulo, MAXTITL - 2, stdin );<br />
<br />
if( *(Biblio.Titulo + strlen( Biblio.Titulo ) -1 ) != '\n' )<br />
{<br />
printf( "El nombre que entraste es demasiado largo.\n"<br />
"Solo %d chars MAX\n", MAXTITL - 2 );<br />
exit(1);<br />
}<br />
else<br />
*(Biblio.Titulo + strlen( Biblio.Titulo ) -1 ) = '\0'; <br />
<br />
printf( "Ingresa el nombre del autor: " );<br />
<br />
fgets( Biblio.Autor, MAXAUTL - 2, stdin );<br />
<br />
if( *(Biblio.Autor + strlen( Biblio.Autor ) -1 ) != '\n' )<br />
{<br />
printf( "El nombre que entraste es demasiado largo.\n"<br />
"Solo %d chars MAX\n", MAXAUTL - 2 );<br />
exit(1);<br />
}<br />
else<br />
*(Biblio.Autor + strlen( Biblio.Autor ) -1 ) = '\0'; <br />
<br />
printf( "Ingresa el valor: " );<br />
scanf( "%10s", ValSTR );<br />
<br />
if( strlen( ValSTR ) > MAXVALL -1 )<br />
{<br />
printf( "El valor que entraste es demasiado largo.\n"<br />
"Solo %d chars MAX\n", MAXVALL - 1 );<br />
exit(1);<br />
}<br />
<br />
if( ValSTR[0] == '-' )<br />
{<br />
puts( "No se permiten valores negativos." );<br />
exit(1);<br />
}<br />
<br />
for( i = 0; i < strlen( ValSTR ) - 1; i++ )<br />
{<br />
if( ValSTR[i] < '0' || ValSTR[i] > '9' )<br />
{<br />
if( ValSTR[i] == '.' )<br />
{<br />
if( strlen( ValSTR ) - i > 3 )<br />
{<br />
puts( "Se permiten solo 2 decimales después del punto." );<br />
exit(1);<br />
}<br />
}<br />
else<br />
{<br />
puts( "Caracteres ilegales en su valor." );<br />
exit(1);<br />
}<br />
}<br />
}<br />
<br />
Biblio.Valor = atof( ValSTR );<br />
<br />
printf( "%s por %s: $%.2f\n", Biblio.Titulo,<br />
Biblio.Autor, Biblio.Valor );<br />
printf( "%s: \"%s\" ($%.2f)\n", Biblio.Autor,<br />
Biblio.Titulo, Biblio.Valor );<br />
printf( "Listo.\n" );<br />
<br />
return 0;<br />
}<br />
</syntaxhighlight><br />
<br />
=== Resultado ===<br />
<pre><br />
[rrc@llawyr CClase]$ gcc -Wall -o Prog117 Prog117.c<br />
[rrc@llawyr CClase]$ ./Prog117<br />
Ingresa el nombre del libro: Mi Libro<br />
Ingresa el nombre del autor: El Autor<br />
Ingresa el valor: 345<br />
Mi Libro por El Autor: $345.00<br />
El Autor: "Mi Libro" ($345.00)<br />
Listo.<br />
[rrc@llawyr CClase]$ ./Prog117<br />
Ingresa el nombre del libro: Mi Otro Libro<br />
Ingresa el nombre del autor: El otro Autor<br />
Ingresa el valor: -34.56<br />
Se permiten valores negativos.<br />
[rrc@llawyr CClase]$ ./Prog117<br />
Ingresa el nombre del libro: Un Otro Libro<br />
Ingresa el nombre del autor: Un Otro Auto<br />
Ingresa el valor: 4567543.34<br />
El valor que entraste es demasiado largo.<br />
Solo 9 chars MAX<br />
[rrc@llawyr CClase]$ ./Prog117<br />
Ingresa el nombre del libro: Otro Libro<br />
Ingresa el nombre del autor: Otro Autor<br />
Ingresa el valor: 34566.456<br />
Se permiten solo 2 numerales después del punto.<br />
[rrc@llawyr CClase]$ ./Prog117<br />
Ingresa el nombre del libro: Otro Libro<br />
Ingresa el nombre del autor: Otra Autora<br />
Ingresa el valor: 345r.56<br />
Caracteres ilegales en su valor.<br />
[rrc@llawyr CClase]$ ./Prog117<br />
Ingresa el nombre del libro: Ultimo Libro<br />
Ingresa el nombre del autor: Ultimo Autor<br />
Ingresa el valor: 435456.43<br />
Ultimo Libro por Ultimo Autor: $435456.44<br />
Ultimo Autor: "Ultimo Libro" ($435456.44)<br />
Listo.<br />
[rrc@llawyr CClase]$ <br />
</pre><br />
<br />
=== Explicación ===<br />
<br />
[[Category:Programación en C]]<br />
[[Category:CCabal]]<br />
[[Category:CursoC]]</div>
Perseuz