Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define isleapyear(year) ((!(year % 4) && (year % 100)) || (!(year % 400) && (year % 1000)))
FILE *fstud;
FILE *fteacher;
typedef struct {
int id;
int psswrd;
} idpass;
typedef struct {
char ders;
int mark;
char ogrt_yorum;
} lesson_reprt;
typedef struct {
int month;
int day;
int year;
} date;
typedef struct {
char name[30];
char mahalle[30];
char city[20];
int tc_no;
idpass info;
date kayit;
} record;
typedef struct {
int stud_no;
char stud_bolum;
lesson_reprt ders[7];
} stud_info;
record readscreen1(record stud);
record readscreen2(record teacher);
void writefile1(record stud);
void writefile2(record teacher);
int isdatevalid(int day,int month,int year);
int main()
{
record stud,teacher;
int n;
printf("Ogrenci [1]/Ogretmen[2]");
scanf("%d",&n);
switch (n)
{
case 1:
fstud=fopen("studrecords.txt","w+");
stud=readscreen1(stud);
writefile1(stud);
fclose(fstud);
break;
case 2:
fteacher=fopen("teacherrecords.txt","w+");
teacher=readscreen2(teacher);
writefile2(teacher);
fclose(fteacher);
break;
}
}
record readscreen1(record stud)
{
beginning1:
printf("Tarih(gg/aa/yyyy): ");
scanf("%d/%d/%d",&stud.kayit.day,&stud.kayit.month,&stud.kayit.year);
if(isdatevalid(stud.kayit.day, stud.kayit.month, stud.kayit.year))
{
printf("Isim: ");
scanf("%s",&stud.name[30]);
printf("T.C. numarasi: ");
scanf("%d",&stud.tc_no);
printf("Sehir: ");
scanf("%s",&stud.city[20]);
printf("Mahalle: ");
scanf("%s",&stud.mahalle[30]);
printf("ID: ");
scanf("%d",&stud.info.id);
printf("Parolla: ");
scanf("%d",&stud.info.psswrd);
}
else
{
printf("Girdiginiz tarih gecerli degil.");
goto beginning1;
}
return (stud);
}
record readscreen2(record teacher)
{
beginning2:
printf("Tarih(gg/aa/yyyy: ");
scanf("%d/%d/%d",&teacher.kayit.day,&teacher.kayit.month,&teacher.kayit.year);
if(isdatevalid(teacher.kayit.day,teacher.kayit.month,teacher.kayit.year))
{
printf("Isim: ");
scanf("%s",&teacher.name[30]);
printf("T.C. numarasi: ");
scanf("%d",&teacher.tc_no);
printf("Sehir: ");
scanf("%s",&teacher.city[20]);
printf("Mahalle: ");
scanf("%s",&teacher.mahalle[30]);
printf("ID: ");
scanf("%d",&teacher.info.id);
printf("Parolla: ");
scanf("%d",&teacher.info.psswrd);
}
else
{
printf("Girdiginiz tarih gecerli degil.");
goto beginning2;
return (teacher);
}
void writefile1(record stud)
{
fprintf(fstud,"%d/%d/%d\n",stud.kayit.day,stud.kayit.month,stud.kayit.year);
fprintf(fstud,"Isim: %s\n",stud.name);
fprintf(fstud,"T.C. No: %d\n",stud.tc_no);
fprintf(fstud,"Sehir: %s\n",stud.city);
fprintf(fstud,"Mahalle: %s\n",stud.mahalle);
fprintf(fstud,"ID: %d\n",stud.info.id);
fprintf(fstud,"Parolla: %d\n",stud.info.psswrd);
return;
}
void writefile2(record teacher)
{
fprintf(fteacher,"%d/%d/%d\n",teacher.kayit.day,teacher.kayit.month,teacher.kayit.year);
fprintf(fteacher,"Isim: %s\n",teacher.name);
fprintf(fteacher,"T.C. No: %d\n",teacher.tc_no);
fprintf(fteacher,"Sehir: %s\n",teacher.city);
fprintf(fteacher,"Mahalle: %s\n",teacher.mahalle);
fprintf(fteacher,"ID: %d\n",teacher.info.id);
fprintf(fteacher,"Parolla: %d\n",teacher.info.psswrd);
return;
}
int isdatevalid(int day,int month,int year)
{
if (day <= 0) return 0 ;
switch( month )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 : if (day > 31) return 0 ; else return 1 ;
case 4 :
case 6 :
case 9 :
case 11 : if (day > 30) return 0 ; else return 1 ;
case 2 :
if ( day > 29 ) return 0 ;
if ( day < 29 ) return 1 ;
if (isleapyear(year)) return 1 ; // leap year
else return 0 ;
}
return 0 ;
}
return 0;
}