Banner of Calendar generating C program from 2012

Sharing My Old Programs: A Journey into Calendar Generation


Category: C/C++

Date: 8 months ago
Views: 636


Introduction

Welcome to this article where I take you on a journey into my early days of learning programming. In 2012, as a budding programmer, I created the C program presented below. It stands as a snapshot of my exploration into calendar generation, reflecting the challenges and triumphs of that learning phase. Though newer approaches and languages have since emerged, revisiting this piece of code allows me to share insights into the core ideas that fueled my early programming endeavors.

As we delve into the code, keep in mind that this is one of my programs from a time when I was still discovering the intricacies of programming. The calendar generator serves as an interesting example, showcasing the steps I took in understanding and implementing fundamental concepts. Join me in exploring the core ideas behind this C program, and let's uncover the learning experiences embedded within its code.

    
#include<stdio.h>
FILE *f;//,*tx;
unsigned long int  A[4];
char *mois[14]={"  ","January","February","March","April","May","June","July"
                  ,"August","September","October","November","December"};
char *stjours[9]={"Mo","Tu","We","Th","Fr","Sa","Su"};
char *stjoursxt[9]={"Dimenche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi"};
correction(unsigned long int a, int b)
{
    A[0]=a; // a= l'année
    A[1]=b; // b= le debut de janvier de l'année a

}
int demmande()
{
    printf("enter any year except 0 and 1 :");
    scanf("%D",&A[2]);
    if(A[2]<2)return 0;
    else return 1;
}
int nb_jours(unsigned long int a,int m) // a= année  , m= mois
{
    int t; // variable= nb_jours du mois m à retourner par la fonction
    if(m==2)
       if(a%4==0&&a%100!=0 || a%400==0) t=29;
       else t=28;
    else
      if(m%2==0 && m<=7 || m%2!=0 && m>7) t=30;
      else t=31;
    return t;
}
int debut(unsigned long int a,int m)
{
   int x,j,d;
   unsigned long int i;
   A[3]=0;
   i=A[0]; // la variable i prend la valeur de l'année de la 1ere correction du debut janvier
   x=A[1]; // la variable x prend l'index du jour du debout janvier
     do
      {
           correction(i,x); // en memorise l'année i  et l'index x du jour du  debut de son janvier
           // pour ne pas revenir à la valeur principale du debut janvier de l'année 2

           for(j=1;j<=12;j++)
           {
               if(i!=a || j!=m) // si on n'est pas aconre arriver à l'année saisis a et le mois m
                 {
                    x=x+nb_jours(i,j);
                    d=x%7;
                    x=d;
                    if(i!=a || j!=m)A[3]++;   // A[3] nbre d'operations avant arrivée à l'année désirée
                 }
               else break;
           }
           i++;
       }while(i<=a);
   if(m==1)   printf("\n    le programme a fais %d operations \n\n   pour trouver le debut de janvier %d \n",A[3],A[2]);
   return d;  // le debut du mois m de l'année a
}
remplissage(unsigned long int y)
{
  int i,j,a,n,k,m,v;

fprintf(f,"<html>\n");
fprintf(f,"<title>Calendrier Par MOSAID</title>\n");
fprintf(f,"<script language=%cjavascript%c>\n",34,34);//%chidepic()%c

fprintf(f,"  function hidepic()\n");
fprintf(f,"     {\n");
fprintf(f,"       if(p.style.display==%cnone%c)\n",34,34);
fprintf(f,"       p.style.display=%c%c\n",34,34);
fprintf(f,"       else\n");
fprintf(f,"       p.style.display=%cnone%c\n",34,34);
fprintf(f,"     }\n");

fprintf(f,"  function hidepic1()\n");
fprintf(f,"     {\n");
fprintf(f,"       if(pd.style.display==%cnone%c)\n",34,34);
fprintf(f,"       pd.style.display=%c%c\n",34,34);
fprintf(f,"       else\n");
fprintf(f,"       pd.style.display=%cnone%c\n",34,34);
fprintf(f,"     }\n");

fprintf(f,"  function blinkit()\n");
fprintf(f,"    {\n");
fprintf(f,"         intrvl=0;\n");
fprintf(f,"         for(nTimes=0;nTimes<20;nTimes++)\n");
fprintf(f,"           {\n");
fprintf(f,"            intrvl = intrvl+400;\n");
fprintf(f,"            setTimeout(%cdocument.bgColor='#0000FF';%c,intrvl);\n",34,34);
fprintf(f,"            intrvl =intrvl+ 400;\n");
fprintf(f,"            setTimeout(%cdocument.bgColor='#FFFFFF';%c,intrvl);\n",34,34);
fprintf(f,"           }\n");
fprintf(f,"     }\n");
fprintf(f,"var pos = 20;\n");

fprintf(f,"function initArray()\n");
fprintf(f," {\n");
fprintf(f,"    this.length = initArray.arguments.length;\n");
fprintf(f,"    for (var i = 0; i < this.length; i++)\n");
fprintf(f,"     {\n");
fprintf(f,"       this[i] = initArray.arguments[i];\n");
fprintf(f,"     }\n");
fprintf(f,"}\n");
fprintf(f,"var col=new initArray();\n");

fprintf(f,"function start()\n");
fprintf(f,"{\n");
fprintf(f,"   col[1]=%cblack%c\n",34,34);
fprintf(f,"   col[2]=%cyellowgreen%c\n",34,34);
fprintf(f,"   col[3]=%cyellow%c\n",34,34);
fprintf(f,"   col[4]=%cwhitesmoke%c\n",34,34);
fprintf(f,"   col[5]=%cwhite%c\n",34,34);
fprintf(f,"   col[6]=%cwheat%c\n",34,34);
fprintf(f,"   col[7]=%cviolet%c\n",34,34);
fprintf(f,"   col[8]=%cturquoise%c\n",34,34);
fprintf(f,"   col[9]=%ctomato%c\n",34,34);
fprintf(f,"   col[10]=%cthistle%c\n",34,34);
fprintf(f,"   col[11]=%clightgreen%c\n",34,34);
fprintf(f,"   col[12]=%cpurple%c\n",34,34);
fprintf(f,"   col[13]=%crose%c\n",34,34);
fprintf(f,"   col[14]=%cbluesky%c\n",34,34);
fprintf(f,"   col[15]=%cblue%c\n",34,34);
fprintf(f,"   col[16]=%cred%c\n",34,34);
fprintf(f,"   col[17]=%cviolet%c\n",34,34);
fprintf(f,"   col[18]=%cpurple%c\n",34,34);
fprintf(f,"   col[19]=%ctomato%c\n",34,34);
fprintf(f,"   col[20]=%cthistle%c\n",34,34);
fprintf(f,"   pos++;\n");
fprintf(f,"   if (pos>=20)\n");
fprintf(f,"    {\n");
fprintf(f,"    pos = 0;\n");
fprintf(f,"    } \n");
fprintf(f,"  document.bgColor =col[pos];\n");
fprintf(f,"  loopID = setTimeout(%cstart()%c,2000);\n",34,34);
fprintf(f,"}\n");

fprintf(f,"function op()\n");
fprintf(f,"     {\n");
fprintf(f,"       var m;\n");
fprintf(f,"       var today;\n");
fprintf(f,"       m=window.open(%c%c,%ca%c,%cheight=450,width=600,toolbar=0,top=100,left=200,title=hhhhhhhhh%c);\n",34,34,34,34,34,34);
//fprintf(f,"       m.document.write(%c<script language=javascript>%c\n",34,34);
fprintf(f,"       m.document.write(%c<body background='backk/varr.jpg' text='#Fe0010'>%c);\n",34,34);
fprintf(f,"       m.document.write(%c<center><br><br>%c);\n",34,34);
//fprintf(f,"       m.document.write(today.getYear());\n");
fprintf(f,"       for(i=0;i<=2;i++)\n");
fprintf(f,"       m.document.write(%c<marquee><font size=10><b>Happy year</b></font></marquee>%c);\n",34,34);
fprintf(f,"       m.document.write(%c<br><br><br><br><br><br>%c);\n",34,34);
fprintf(f,"       m.document.write(%c<input type=button value=close onclick=window.close() >%c);\n",34,34);
fprintf(f,"    }\n");

fprintf(f,"</script>\n");
fprintf(f,"<body onLoad=%cstart()%c   background=backk/bg6.jpg>\n\n",34,34);//<br><br><br><br><br><br><br><br><br><br><br>
fprintf(f,"<bgsound src=backk/4400.mp3 loop=400>\n");

  fprintf(f,"<center><marquee scrollamount=20 direction=left>\n<font size=6 color=black><b>  happy    year </b></font></marquee></center>\n");
  fprintf(f,"<center> <font size=12 color=red><blink><big><strong> %d </big></strong> </blink> </font></center>\n",y);
  fprintf(f,"<center><marquee scrollamount=20 direction=right>\n<font size=6 color=black><b> ÓäÉ    ÓÚíÏÉ </b></font></marquee></center>\n");
  fprintf(f,"<br><br>\n");
  fprintf(f,"<center>\n");
  for(k=1;k<=12;k++)
  {
      n=nb_jours(y,k);
      m=debut(y,k);
      if(y==2 && k==1)m=2;

      if(k==1)fprintf(f,"<table width=100 height=100  border=0><tr>\n");
      if(k<=4)fprintf(f,"<td><table width=100 height=100 border=0><tr><td>\n");
      if(k==5)fprintf(f,"</tr></table>\n");
      if(k==5)fprintf(f,"<table width=100 height=100 border=0>\n");
      if(k>4&&k<=8)fprintf(f,"<td><table width=100% height=100 border=0><tr><td >\n");
      if(k==9)fprintf(f,"</tr></table>\n");
      if(k==9)fprintf(f,"<table width=100 height=100 border=0>\n");
      if(k>8)fprintf(f,"<td><table  width=50 height=50     border=0><tr><td>\n");
      if(k==9||k==10||k==11)fprintf(f,"<td background=months/1.jpg>\n");
      if(k==12||k==1||k==2)fprintf(f,"<td background=months/Winter.jpg>");
      if(k==3||k==4||k==5)fprintf(f,"<td background=months/2.jpg>\n");
      if(k==6||k==7||k==8)fprintf(f,"<td background=months/4.bmp>\n");
      fprintf(f,"<table width=200 height=10 align=center border=0><tr><td>\n"); // tableau contenant le nom du mois
      fprintf(f,"<center><font size=5 color=#008000><b>%s</b></font><font size=4 color=#FF00FF><b>                  %d </b></font></center>\n",mois[k],k);
      fprintf(f,"</tr></table>");

      fprintf(f,"<table  width=200 height=10 align=center border=0><tr>\n");//tableau comtenant les jours de la semaine
      fprintf(f,"<i><td><font   color=tomato><b>Su</b><td><font   color=tomato><b>Mo</b><td><font   color=tomato><b>Tu</b><td>\n<font   color=tomato><b>We</b><td><font   color=tomato><b>Th</b><td>\n<font   color=tomato><b>Fr</b><td><font   color=tomato><b>Sa</b></font></i>\n");
      fprintf(f,"</tr></table>\n");

      a=1;  // variable allant de 1 au nb_jours n du mois k de l'année y


      fprintf(f,"<table width=200 height=10 align=center border=0>\n");    //tableau comtenant les jours du mois
      for(i=0;i<=5;i++)
       {
         fprintf(f,"<tr>");
         for(j=0;j<7;j++)
          {
            if(i==0 && j<m) //si on est avant le debut m du mois
             {
              fprintf(f,"<td>");
              fprintf(f,"<font size=3 color=#8A2BE2>  </font>\n");//
             }
           else
            {
              if(a<=n)// si l'iterations des jours a n'est pas encore arrivée au nb_jours (maximum) du mois k
               {
                        if(j==0) // si le jour j est un dimanche
                        {
                         fprintf(f,"<td>");
                         //fprintf(f,"<a href=m.html>");
                         fprintf(f," <font size=3 color=red><b>   %.2d</b></font>\n",a);
                        }
                        else{

                              if(k==9||k==10||k==11) // si les mois 9,10 ou 11
                              {
                               fprintf(f,"<td>");
                               //fprintf(f,"<a href=m.html>");
                               fprintf(f,"<font size=3 color=violet><b>   %.2d</b></font>\n",a);
                               }
                              if(k==3||k==4||k==5) // si les mois 3,4 ou 5
                              {
                               fprintf(f,"<td>");
                               //fprintf(f,"<a href=m.html>");
                               fprintf(f,"<font size=3 color=springgreen><b>   %.2d</b></font>\n",a);
                              }
                              if(k==6||k==7||k==8) // si les mois  6,7 ou 8
                              {
                               fprintf(f,"<td>");
                               //fprintf(f,"<a href=m.html>");
                               fprintf(f,"<font size=3 color=magenta><b>   %.2d</b></font>\n",a);
                              }
                              if(k==12||k==1||k==2) // si les mois  12,1 ou 2
                              {
                               fprintf(f,"<td>");
                               //fprintf(f,"<a href=m.html>");
                               fprintf(f,"<font size=3 color=white><b>   %.2d</b></font>\n",a);
                              }
                            }
                      }
              if(a>n)
                 {
                     fprintf(f,"<td>");
                     fprintf(f,"<font size=3 color=#8A2BE2>  </font>\n");
                 }
              a++;
            }
          }
		 fprintf(f,"</tr>\n");
       }
      fprintf(f,"</table>\n");
      fprintf(f,"</tr></table>\n");
  }
  fprintf(f,"</tr></table>\n");
  fprintf(f,"<table  width=160 height=240 align=center border=0><tr>\n");
  fprintf(f,"<td><center>\n<img src=%cmonths/hamd.gif%c align=%ctexttop%c title=%cone click here%c name=t  id=%cpd%c  OnMouseOver=t.src=%cmonths/73846972.gif%c OnMouseOut=t.src=%cmonths/hamd.gif%c Onclick=%chidepic()%c>\n",34,34,34,34,34,34,34,34,34,34,34,34,34,34);
  fprintf(f,"<td background=backk/mosaid.gif><center><i><b><font size=2 color=white>\n   &#169 mosaid radouan  <br>  E mail : mosaid_radouan@yahoo.fr </font> <b><i><br>  \n</center> \n");

  fprintf(f,"<td></center><img  id=%cp%c p.style.display=%cnone%c src=%cmonths/heart.gif%c name=m title=%cdouble click here%c onclick=%chidepic1()%c  ondblclick=%cop()%c >\n",34,34,34,34,34,34,34,34,34,34,34,34,34,34);
  fprintf(f,"</tr></table>\n");

  fprintf(f,"</body>\n");
  fprintf(f,"</html>\n");

}
main()
{
int t;
f=fopen("calendrier.html","w+");
//tx=fopen("index.log","w+");
correction(2,2);//correction(1900,1);   // le mois 1 de 1900 commence le lundi noté le jour 1
//fprintf(tx,"\n correction:  %d   --->(debut janvier)  %s = %d      ***** iterations=%d\n\n",A[0],stjoursxt[A[1]],A[1],A[3]);
do
 {
  t=demmande();
 }while(t==0);
remplissage(A[2]);
fclose(f);
//fclose(g);

printf("\n\n            produced by mosaid radouan\n\n");
printf("                                         you can see the html file now.\n\n");
system("pause");
}



    

Understanding the Code

The C program begins with the function call correction(2, 2), which sets the starting point for the calendar generation. In this context, it means that January 1st of year 2 is considered day 2 (Tuesday). This initial correction is crucial as it serves as the reference point for calculating the days of the week for subsequent years and months. this correction was correction(1900,1); meaning January 1 of the year 1900 was day "1" ie Monday. I remember getting it from my phone at the time (Motorola or nokia I don't remember). by scrolling down untill the limit (1900). I don't remember where I got the other correction.

The calendar generation logic continues within the while loop in the function int debut(unsigned long int a, int m). This function calculates the starting day of month "m" of year "a" while storing the January 1st of year "a" in the function correction(unsigned long int a, int b). The correction function memorizes the year "a" and the index "b" of the day of the week for the beginning of its January.

The iterative nature of the code ensures accurate calculation of days and months, taking into account leap years and variations in the number of days in each month. The nb_jours function is used to determine the number of days in a given month, considering leap years.

How to Use the Code

To use this calendar generation code, follow these steps:

  1. Compile the C program.
  2. Run the executable and input the desired year when prompted.
  3. Open the generated HTML file to view the calendar for the specified year.
Screenshot calendar generated by c program
Calendar of the year 105 generated by this C program

Customization and Further Development

The provided C program is a starting point that you can customize and expand upon. Consider incorporating modern HTML and CSS techniques to enhance the visual presentation of the calendar. Additionally, explore how you can adapt the core logic for different use cases or integrate it into other projects.

Conclusion

In conclusion, exploring old C programs like this one can be a valuable learning experience. The principles demonstrated in this calendar generator can be applied and adapted to various programming languages. Feel free to experiment, modify, and build upon this foundation to create your own calendar generation projects.



636 views

Next Article

0 Comments, latest

No comments.