PDA

View Full Version : تابع گرفتن تاریخ از کاربر



Arash m
یک شنبه 16 تیر 1387, 00:14 صبح
سلام.

این تابع که نوشتم قابلیت گرفتن تاریخ به صورت روز/ماه/سال رو داره

این هم تابع:


#include<conio.h>
#include<math.h>

struct s_date{
int day;
int month;
int year;
};

s_date get_date(){
int x_start,c_year=0,c_month=0,c_day=0;
char ch,str_year[5],str_month[3],str_day[3];
s_date rtn_date;

str_year[4]='\0';
str_month[2]='\0';
str_day[2]='\0';

x_start=wherex();

textcolor(4);
cprintf(" / / ");
textcolor(3);
gotoxy(x_start,wherey());

do{
ch=getch();
if(ch==8&&c_year>0){
if((c_year==4&&c_month==0)||(c_month==2&&c_day==0))
cprintf("\b\b \b");
else
cprintf("\b \b");

if(c_day>0)
c_day--;
else if(c_month>0)
c_month--;
else
c_year--;

}
else if((ch>=48&&ch<=57)&&c_day<2){
if(c_year<4){
c_year++;
putch(ch);
if(c_year==4)
gotoxy(wherex()+1,wherey());
str_year[c_year-1]=ch;
}
else if(c_month<2){
c_month++;
putch(ch);
if(c_month==2)
gotoxy(wherex()+1,wherey());
str_month[c_month-1]=ch;
}
else{
c_day++;
putch(ch);
str_day[c_day-1]=ch;
}
}

}while((ch!=13)||(c_day<2)||(atof(str_month)>12)||(atof(str_day)>31));
cprintf("\n");
gotoxy(1,wherey());
normvideo();

rtn_date.year=atof(str_year);
rtn_date.month=atof(str_month);
rtn_date.day=atof(str_day);
return rtn_date;
}توضیح تابع :

برای استاندارد تر کردن کار اول برای روز/ماه/سال یک ساختمان struct تعریف کردم . بعد از تعریف متغییر از نوع ساختمان با استفاده از تابع ساختمان رو پرمیکنیم.

مثال:


s_date t;
t=get_date();