RE:como generar un calendario en dev c++
Publicado por
alex (1 intervención) el 22/11/2006 15:40:34
// program C_15.cpp
// calendario perpetuo.
// 29/4/2005 - written in borland c++ - ver 4.52
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
struct Date
{
unsigned int day,
month,
year;
Date(unsigned int D = 0,unsigned int M = 0,unsigned int Y = 0):
day(D),month(M),year(Y){}; // CONSTRUCTOR
}origen(1,1,1990);
const unsigned int BISIESTO = 1,
COMUN = 0,
x[2][13] = {{365,31,28,31,30,31,30,31,31,30,31,30,31},// comun
{366,31,29,31,30,31,30,31,31,30,31,30,31}};// bisiesto
const char *meses[13] = {\\\"none\\\",\\\"enero\\\",\\\"febrero\\\",\\\"marzo\\\",\\\"abril\\\",
\\\"mayo\\\",\\\"junio\\\",\\\"julio\\\",\\\"agosto\\\",
\\\"septiembre\\\",\\\"octubre\\\",\\\"noviembre\\\",\\\"deciembre\\\"},
*dias[7] = {\\\"..domingo\\\",
\\\"....lunes\\\",
\\\"...martes\\\",
\\\"miercoles\\\",
\\\"...jueves\\\",
\\\"..viernes\\\",
\\\"...sabado\\\"};
unsigned int GetYearType(unsigned int year)
{
if(year%100 == 0) // si year es divisible por 4 pero termina en 00
year /= 100; // es bisiesto solamente cuando es divisible por 400
if(year%4) // year no es divisible por 4,
return COMUN; // por eso no es bisiesto.
return BISIESTO;
} // GET YEAR TYPE
unsigned int DiaEnLaSemana(Date &d)
{
unsigned int qty = 0;
for(unsigned int y = origen.year;y < d.year;y++)
qty += x[GetYearType(y)][0];
int index = GetYearType(d.year);
for(unsigned int m = 1;m < d.month;m++)
qty += x[index][m];
qty += d.day;
return qty%7;
} // DIA EN LA SEMANA
unsigned int GetNum(char *msg,unsigned int bottom,unsigned int top)
{
int num = -1;
while(num < bottom || num > top)
{
cout << setw(20) << msg << setw(4) << bottom << \\\" to \\\"
<< setw(4) << top << \\\"> : \\\";
cin >> num;
}
return num;
} // GET NUM
void GetDate(Date &d)
{
d.year = GetNum(\\\"get year <from \\\",1990,2100);
d.month = GetNum(\\\"get month <from \\\",1,12);
d.day = 1;
} // GET DATE
void MostrarCalendario()
{
cout << \\\"mostrar mes : \\\" << endl << endl;
Date d;
GetDate(d);
cout << endl;
cout << \\\"calendario para el mes de \\\" << meses[d.month] << \\\" del \\\"
<< d.year << \\\" : \\\" << endl << endl;
for(int i = 0;i < 9;i++)
{
for(int j = 0;j < 7;j++)
cout << setw(7) << dias[j][i];
cout << endl;
}
cout << endl;
d.day = 1;
int index = DiaEnLaSemana(d);
for(int counter = 0;counter < index;counter++)
cout << setw(7) << \\\"---\\\";
for(int dia = 1;dia <= x[GetYearType(d.year)][d.month];counter++,dia++)
{
if(counter%7 == 0)
cout << endl << endl;
cout << setw(7) << dia;
}
for(;;counter++)
{
if(counter%7 == 0)
break;
cout << setw(7) << \\\"---\\\";
}
cout << endl << endl;
} // MOSTRAR CALENDARIO
char Menu()
{
cout << \\\"para salir marque \\\'s\\\', cualquier otra tecla para continuar : \\\";
char choice = getche();
cout << endl << endl;
return choice;
} // MENU
int main()
{
for(char choice = Menu();choice != \\\'s\\\' && choice != \\\'S\\\';choice = Menu())
MostrarCalendario();
cout <<\\\"end of program - good bye ! ! !\\\" << endl;
return 0;
} // MAIN