Curso de Programación en C/Prog105
								
								Ir a la navegación
				Ir a la búsqueda
				
					
								
							
		Prog105
 1 //Archivo Prog105a.c
 2 #include <stdio.h>
 3 
 4 void InformeDelConteo( void );
 5 extern void acumularse( int );
 6 
 7 int Contar = 0;
 8 
 9 int main(void)
10 {
11   int Valor;
12   register int i;
13 
14   printf( "Ingresa un número entero positivo (0 a terminar): " );
15 
16   while( scanf( "%d", &Valor ) == 1 && Valor > 0 )  // mientras que scanf() lea un dato y este sea positivo
17   {
18     ++Contar;
19 
20     for( i = Valor; i >= 0; i-- )
21       acumularse(i);
22     printf( "Ingresa un número entero positivo (0 a terminar): ");
23   }
24   InformeDelConteo();   // scanf() recibe un dato negativo o cero
25 
26   return 0;
27 }
28 
29 void InformeDelConteo( void )
30 {
31   printf( "El while loop fue ejecutado %d veces\n", Contar );
32 }
33 
34 // Archivo Prog105b.c
35 #include <stdio.h>
36 
37 extern int Contar; 
38 static int total = 0;
39 
40 void acumularse( int k )
41 {
42   static int subtotal = 0;
43 
44   if( k <= 0 )
45   {
46     printf( "ciclo de loop: %d\n", Contar );
47     printf( "subtotal: %d; total: %d\n", subtotal, total );
48     subtotal = 0;
49   }
50   else
51   {
52     subtotal += k;
53     total += k;
54   }
55 }
Resultado
[rrc@llawyr CClase]$ gcc -Wall -o Prog105 Prog105a.c Prog105b.c [rrc@llawyr CClase]$ ./Prog105 Ingresa un númenro entero positivo (0 a terminar): 1 ciclo de loop: 1 subtotal: 1; total: 1 Ingresa un númenro entero positivo (0 a terminar): 1 ciclo de loop: 2 subtotal: 1; total: 2 Ingresa un númenro entero positivo (0 a terminar): 2 ciclo de loop: 3 subtotal: 3; total: 5 Ingresa un númenro entero positivo (0 a terminar): 2 ciclo de loop: 4 subtotal: 3; total: 8 Ingresa un númenro entero positivo (0 a terminar): 3 ciclo de loop: 5 subtotal: 6; total: 14 Ingresa un númenro entero positivo (0 a terminar): 3 ciclo de loop: 6 subtotal: 6; total: 20 Ingresa un númenro entero positivo (0 a terminar): 4 ciclo de loop: 7 subtotal: 10; total: 30 Ingresa un númenro entero positivo (0 a terminar): 0 El while loop fue ejecutado 7 veces [rrc@llawyr CClase]$