Thursday, August 30, 2012

Miscellaneous – VIII – Modifying a File.



Program to remove all kind of Comments (Single line and Multi-Line Comments) from a ‘C’ or same type of file.




#include <stdio.h>
#include <conio.h>
#include <string.h>

FILE *fp,*fp1;
char fname[15];
int j;

  void print()
      {
           char line[200];
           fp=fopen(fname,"r");
           while((fgets(line, 200, fp))!=NULL)
              {
                   printf("%s",line);
              }
           fclose(fp);
      }

  void Scomment()
      {
           char line[200];
           int i=0;
           fp=fopen(fname,"r");
           fp1=fopen("temp.c","w");
           while((fgets(line, 200, fp))!=NULL)
              {
                   for(i=0;line[i]!='\0';i++)
                        {
                            if(line[i]=='/' && line[i+1]=='/')
                                {
                                      break;
                                }
                            putc(line[i],fp1);
                        }
              }
           fclose(fp);
           fclose(fp1);
           remove(fname);
           rename("temp.c",fname);
      }


  int Mcomment(char line[],int k)
      {
                   for(j=k;line[j]!='\0';j++)
                        {
                            if(line[j]=='*' && line[j+1]=='/')
                                {
                                      j=j+2;
                                      return 1;
                                }
                        j++;
                        }
               return 0;
      }

  void main()
      {
           char line[200];
           int i=0,k=1;
           clrscr();
           printf("\n\tEnter Source 'C' Filename : ");
           gets(fname);
           printf("\n\tContents of the File %s :\n",fname);
           print();
           printf("\nPress Any Key to Continue : ");
           getch();
           clrscr();
           Scomment();
           fp=fopen(fname,"r");
           fp1=fopen("temp.c","w");
           while((fgets(line, 200, fp))!=NULL)
              {
                   for(i=0;line[i]!='\0';i++)
                        {
                            if(line[i]=='/' && line[i+1]=='*')
                                {
                                      k=Mcomment(line,i+2);
                                      i=j;
                                }
                            if(k==0)
                                {
                                      k=Mcomment(line,i);
                                      i=j;
                                }
                            if(k==1)
                                {
                                      putc(line[i],fp1);
                                }
                        }
                   if(k==0)
                        {
                            k=Mcomment(line,0);
                        }
              }
           fclose(fp);
           fclose(fp1);
           remove(fname);
           rename("temp.c",fname);
           printf("\n\tContents of the File %s :\n",fname);
           print();
           printf("\nPress Any Key to Continue : ");
           getch();
           clrscr();
      }

Thursday, August 23, 2012

Miscellaneous – VII – Date Related Programs



Some Date Manipulation program and a program for generating Calendar.



#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

//Print the day of the week from given date

  int valid(long dd,long mm,long yy,int mn[])
      {
           if(mm>12 || mm<1)
              {
                   return 1;
              }
           if(yy<1)
              {
                   return 1;
              }
           if(dd>mn[mm-1])
              {
                   return 1;
              }
           return 0;
      }

  void main()
      {
           char days[8][12]={"Monday","Tusesday","Wednesday","Thrusday","Friday","Saturday","Sunday"};
           long m[12]={31,28,31,30,31,30,31,31,30,31,30,31};
           long dd,mm,yy,i,d,td=0;
           clrscr();
           printf("\n\tEnter Day : ");
           scanf("%ld",&dd);
           printf("\n\tEnter Month : ");
           scanf("%ld",&mm);
           printf("\n\tEnter  Year : ");
           scanf("%ld",&yy);
           i=valid(dd,mm,yy,m);
           if(i==1)
           {
              printf("\n\n\t%ld-%ld-%ld is an invalid Date ",dd,mm,yy);
              getch();
              exit(0);
           }
           printf("\n\tWeek day for Date %2ld/%2ld/%4ld is ",dd,mm,yy);
           if(yy>1900)
              {
                   td=((yy-1)-1900)*365+((yy-1)-1900)/4;
              }
           if(yy==1900)
              {
                   td=td+dd-1;
              }
           else
              {
                   td=td+dd;
              }
           for(i=1;i<mm;i++)
              {
                   if(yy%4==0)
                        {
                            m[1]=29;
                        }
                   else
                        {
                            m[1]=28;
                        }
                   td=td+m[i-1];
              }
           d=td%7;
           printf(" %s ",days[d]);
           getch();
      }





#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

//Program for Generating Calendar

void main()
      {
           char b[12][12]={"January","February","March","April","May","June","July","August","September","October","November","December"};
           char a[8][4]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
           long m[12]={31,28,31,30,31,30,31,31,30,31,30,31};
           long y,i,d,j,td=0,k,n;
           clrscr();
           printf("\n\tEnter  Year : ");
           scanf("%ld",&y);
           td=((y-1)-1900)*365+((y-1)-1900)/4;
           d=(td+1)%7;
           printf("\n\n\t\tCalender of Year : %d ",y);
           n=d;
           for(i=0;i<12;i++)
              {
                   if(y%4==0)
                        {
                            m[1]=29;
                        }
                   else
                        {
                            m[1]=28;
                        }
                   printf("\n\n\t\tMonth : %s \n\n",b[i]);
                   for(j=0;j<7;j++)
                        {
                            printf("   %s\t",a[j]);
                        }
                   k=0;
                   printf("\n",n);
                   for(j=1;j<=n;j++)
                        {
                            printf("\t");
                        }
                   while(k<m[i])
                        {
                            k++;
                            n++;
                            printf("   %2d \t",k);
                            if(n==7)
                                {
                                      n=0;
                                      printf("\n");
                                }
                        }
                   getch();
              }
           getch();
      }

Thursday, August 16, 2012

Miscellaneous – VI – Date Related Programs



Some Date Manipulation programs.




#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

//Find Date after Numbers of Days

  int Leap(int y)
      {
           if(y%100!=0 && y%4==0)
              {
                   return 1;
              }
           if(y%100==0 && y%400==0)
              {
                   return 1;
              }
           return 0;
      }

  int valid(int dd,int mm,int yy,int mn[])
      {
           if(mm>12 || mm<1)
              {
                   return 1;
              }
           if(yy<1)
              {
                   return 1;
              }
           if(dd>mn[mm-1])
              {
                   return 1;
              }
           return 0;
      }

  void main()
      {
           int m[12]={31,28,31,30,31,30,31,31,30,31,30,31};
           int dd,mm,yy,n,c;
           clrscr();
           printf("\n\tEnter Day : ");
           scanf("%d",&dd);
           printf("\n\tEnter Month : ");
           scanf("%d",&mm);
           printf("\n\tEnter  Year : ");
           scanf("%d",&yy);
           c=valid(dd,mm,yy,m);
           if(c==1)
           {
              printf("\n\n\t%d-%d-%d is an invalid Date ",dd,mm,yy);
              getch();
              exit(0);
           }
           printf("\n\tEnter Nos. of Days to Add : ");
           scanf("%d",&n);
           printf("\n\tInput Date %2d/%2d/%4d is ",dd,mm,yy);
           printf("\n\tDate after %d Days ",n);
           while(n>0)
              {
                   c=Leap(yy);
                   if(c==1)
                        {
                            m[1]=29;
                        }
                   else
                        {
                            m[1]=28;
                        }
                   n--;
                   dd++;
                   if(dd>m[mm-1])
                        {
                            mm++;
                            dd=1;
                        }
                   if(mm>12)
                        {
                            yy++;
                            mm=1;
                        }
              }
           printf("  %d-%d-%d",dd,mm,yy);
           getch();
      }
 

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

//Find the Difference between two dates

  int valid(int dd,int mm,int yy,int mn[])
      {
           if(mm>12 || mm<1)
              {
                   return 1;
              }
           if(yy<1)
              {
                   return 1;
              }
           if(dd>mn[mm-1])
              {
                   return 1;
              }
           return 0;
      }
  void main()
   {
       int d,m,y,d1,m1,y1,d2,m2,y2,c;
       int mn[]={31,28,31,30,31,30,31,31,30,31,30,31};
       clrscr();
       printf("\nEnter the First Date dd mm yyyy : ");
       scanf("%d%d%d",&d,&m,&y);
       c=valid(d,m,y,mn);
       if(c==1)
           {
              printf("\n\n\t%d-%d-%d is an invalid date ",d,m,y);
              getch();
              exit(0);
           }
       printf("\nEnter the Second Date dd mm yyyy : ");
       scanf("%d%d%d",&d2,&m2,&y2);
       c=valid(d2,m2,y2,mn);
       if(c==1)
           {
              printf("\n\n\t%d-%d-%d is an invalid date ",d2,m2,y2);
              getch();
              exit(0);
           }

       printf("\n\First Date : %2d-%2d-%4d ",d,m,y);
       printf("\n\t\tSecond Date : %2d-%2d-%4d ",d2,m2,y2);
       if(y>y2)
           {
              printf("\nInvaid Input ");
              getch();
              exit(0);
           }

       d1=d2-d;
       if(d1<0)
           {
              d1=mn[m-1]-d;
              d1=d1+d2;
              m=m+1;
           }
       m1=m2-m;
       if(m1<0)
           {
              m1=12-m;
              m1=m1+m2;
              y=y+1;
           }

       y1=y2-y;
       if(y1<0)
       y1=0;
       printf("\n\t\t\t Difference = %d years %d months and %d days ",y1,m1,d1);
       getch();
  }