#include <stdio.h>
#include <time.h>
#define UNIT (30*60)
#define FEE 300
time_t get_caltime(char *tstr);
main()
{
time_t cal_stime, cal_etime;
char buff[100];
int fee, unit;
printf("駐車開始時間 YYYYMMDDHHMMSS : ");
scanf("%s", buff);
cal_stime = get_caltime(buff);
if(cal_stime == -1){
printf("変換失敗\n");
return;
}
printf("駐車終了時間 YYYYMMDDHHMMSS : ");
scanf("%s", buff);
cal_etime = get_caltime(buff);
if(cal_etime == -1){
printf("変換失敗\n");
return;
}
if(cal_etime <= cal_stime){
printf("入力した日時が不適切です.\n");
return;
}
printf("経過秒数 : %d\n", cal_etime - cal_stime);
unit = (cal_etime - cal_stime) / UNIT;
if((cal_etime - cal_stime) % UNIT != 0)
unit++;
fee = unit * FEE;
printf("料金 : %d円\n", fee);
}
time_t get_caltime(char *tstr)
{
char buf[20];
time_t cal_time;
struct tm
work_tm;
if(strlen(tstr) != 14)
return(-1);
strncpy(buf, tstr, 4);
buf[4] = '\0';
work_tm.tm_year = atoi(buf) - 1900;
strncpy(buf, tstr+4, 2);
buf[2] = '\0';
work_tm.tm_mon = atoi(buf) - 1;
strncpy(buf, tstr+4+2, 2);
work_tm.tm_mday = atoi(buf);
strncpy(buf, tstr+4+2+2, 2);
work_tm.tm_hour = atoi(buf);
strncpy(buf, tstr+4+2+2+2, 2);
work_tm.tm_min = atoi(buf);
strncpy(buf, tstr+4+2+2+2+2, 2);
work_tm.tm_sec = atoi(buf);
work_tm.tm_isdst = -1;
if((cal_time = mktime(&work_tm)) == -1){
return(-1);
}
return(cal_time);
}
|