New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C++

costo periódicos

280 Views
Copy Code Show/Hide Line Numbers
#include <stdio.h>
#include <stdbool.h>
/*    Desarrollado en lenguaje C/ webpage: http://ezequielher.wordpress.com/
 *    Calcula lo que cuesta en el mes comprar todos los dias un periódico asumiendo 
 *    que los precios varían según el de la semana. 
 *    Puedes cambiar los precios en el fuente, en función de lo que te cobren a vos.
 *    Tambien sive como calendario y comienza desde año 1994.
 *    Se omiten los días que no se publican periódicos: año nuevo, navidad y 1 de mayo.*/
 
#define DONT 23
#define YES 13
 
int MESES[]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
char const *SEMANA[]= {NULL,"DOM","LUN","MAR","MIE","JUE","VIE","SAB"};
char const *MES_NAME[]= {NULL,"ENERO","FEBRERO","MARZO","ABRIL","MAYO","JUNIO","JULIO","AGOSTO","SETIEMBRE","OCTUBRE","NOVIEMBRE","DICIEMBRE"};
 
// PRECIO DIARIO SEGUN DIA SEM
float const PAGO_DOM= 3.5;
float const PAGO_SAB= 2.25;
float const PAGO_SEM= 1.5;
 
int const CORRE= 6;     // dia que termina año anterior: CORRE 1= lunes
int const INIT= 1994;
 
bool sera_bis(int ano);
 
int main(int argc, char *argv[])
{
    int i, weeks, temp;
 
    if (argc!=3) {
         fprintf(stderr, "Uso programa: %s [numero_mes] [año]\n", argv[0]);
          return 1;
    }
    int mes_act= atoi(argv[1]);
    int ano_act= atoi(argv[2]);
 
    bool condic= (ano_act < INIT || ano_act > INIT + 400);
    if (mes_act > 12 || condic==true) return 1;
 
    int cant_dias= 0;
    for (i= INIT; i< ano_act; i++){
         if (sera_bis(i)==true)
              cant_dias= cant_dias + 366;
         else     cant_dias= cant_dias + 365;
    }
 
    if (sera_bis(ano_act)==true) MESES[2]= 29;    
 
    for (i=0; i< mes_act; i++)
         // cant dias hasta mes actual
         cant_dias= cant_dias + MESES[i];
 
    weeks= (cant_dias/ 7);     // redondea a entero
    temp= weeks * 7;
 
    int last_day_back= cant_dias -temp + CORRE;     // ult dia mes anterior
 
    int flag= YES;
    float precio_tmp;
    float precio= 0;
 
    for (i=1; i<= MESES[mes_act]; i++) {
         // DIAS QUE NO SE IMPRIME DIARIO
         if (mes_act== 5 && i== 1) flag= DONT;     // 1 mayo
         if (mes_act== 1 && i== 1) flag= DONT;     // año nuevo
         if (mes_act==12 && i==25) flag= DONT;     // navidad
 
         if (flag!= DONT) {
              temp= i + last_day_back;
              while (temp > 7) temp= temp -7;
              int dia = temp; // dia: (digito) dia de la semana (1-7)
 
              switch ( dia ) {
                   case 7:  precio_tmp= PAGO_SAB; break;     // dia= 7 (sabado)
                   case 1:  precio_tmp= PAGO_DOM; break;     // dia= 1 (domingo)
                   default: precio_tmp= PAGO_SEM; break;
              }
              precio= precio + precio_tmp;
              printf("t%s %2d: $ %0.2f\n", SEMANA[dia], i, precio_tmp);
 
         } else flag= YES;
    }
 
    printf("\n\tTOTAL MES %s: $ %0.2f\n\n", MES_NAME[mes_act], precio);
 
    return 0;
}
 
bool sera_bis(int ano)
{
    if (ano%4==0) {
         if (ano%100==0 && ano%400==0) return true;
         else if (ano%100!=0) return true;
         else return false;
    }
    else
         return false;
}
by ezequielher
  January 22, 2010 @ 6:31am
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate