程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> 用C寫的停車收費代碼

用C寫的停車收費代碼

編輯:關於C

某市停車場收費標准為:

小型車:首小時內每15分鐘收費:2.5元,首小時後每15分鐘收費:3.75

大型車:首小時內每15分鐘收費:5元,首小時後每15分鐘收費:7.5

夜間收費標准(21:00---7:00)一律執行小型車每兩小時1元,大型車每兩小時2元

請編寫程序,輸入車型和進場時間(XXXX-XX-XX XX:XX)以及出場時間,計算停車時間,然後算出應收金額(請注意白天和晚上跨時間段問題,比如早上6.30進場,7.15出場;白天不夠15分鐘部分按照15分鐘計算;夜間不夠2小時部分也按2小時計算)。

 

// To_park_car.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <math.h>
#include <time.h>
 const int   Bigcar_rule   = 5;     //day per 15 minutes
 const float Smallcar_rule = 2.5;   //day per 15 minutes

 const float Bigcar_att   = 7.5;    //past 15 minutes in day
 const float Smallcar_att =3.75;    //past 15 minutes in day

 const int   Bigcar_night = 2;      //night per an hour
 const int   Smallcar_night = 1;    //night per an hour

 const int   Per_time = 15;         // 15 minutes

//Check input value
bool Compare(int sy,int sm,int sd,int sh,int smi,int ey,int em,int ed,int eh,int emi)
{
 if(ey > sy ||(ey>=sy && em> sm) || (ey>=sy && em>= sm && ed>sd) ||(ey>=sy && em>= sm && ed>=sd && eh>sh)||(ey>=sy && em>= sm && ed>=sd && eh>=sh && emi>smi))
 {
  return true;
 }
 else return false;
}

/// Generates all month of year
int *GenerateMonth(int year)
{
 int *month =new int[13];
 
  month[0] = 0;
  month[1] = 31;
  if((year%4==0&&year%100!=0)||(year%100==0&&year%400==0))
        {
      month[2] = 29;
  }
  else
  {
   month[2] =28;
  }
  month[3] = 31;
  month[4] = 30;
  month[5] = 31;
  month[6] = 30;
  month[7] = 31;
  month[8] = 31;
  month[9] = 30;
  month[10]= 31;
  month[11]= 30;
  month[12]= 31;
 return month;
}
//Check now of hour  or less than target to park car hour
bool CheckHour(int sy,int sm,int sd,int sh,int smi,int ey,int em,int ed,int eh,int emi)
{
 int hour = sh;
 if(hour >24)
 {
  hour=1;
  sd++;
 }
 if(hour >=21 || sh<7)
 {
  if(em>sm || ed>sd || ed>=sd && eh>hour || ed>=sd && eh>=hour && emi>=smi)
  {
   return true;
  }
 }
    if(ey > sy || ed>sd || ed>=sd && eh>hour+1 || ed>=sd && eh>hour && emi>=smi|| em>sm)
 {
  return true;
 }
 return false;
}
//Check now of day  or less than target to park car date
bool CheckDay(int yy,int mm,int dd,int hh,int mi,int ey,int em,int ed,int eh,int emi)
{
    int *point;
 int month_content_day = 0;
 int month_mod_value = 0;
    point =GenerateMonth(yy);
 //count days per month
 month_content_day = point[mm];
 dd++;
 if(dd > month_content_day )
 {
  mm++;
  dd=1;
 }
 //current month
 if(mm > 12)
 {
  month_mod_value = 1;
  yy++;
  mm =1;
 }
 else
 {
  month_mod_value = mm;
 }
 if(ey>yy || em>=month_mod_value && ed>dd || em>=month_mod_value && ed>=dd && eh>hh || em>=month_mod_value && ed>=dd && eh>=hh && emi>=mi)
 {
  return true;
 }
 return false;
}

//Check now of month  or less than target to park car month
bool CheckMonth(int  yy,int mm,int dd,int hh,int mi,int ey,int em,int ed,int eh,int emi)
{
 int sm = mm;
 if (sm+1>12)
 {
  sm =dd = 1;
  yy++;


 }
 
 if((ey>= yy && em>sm && ed>dd) || (ey>= yy && em>sm && ed>=dd &&eh>hh) || (ey>= yy && em>sm && ed>=dd &&eh>=hh && emi>=mi) || (ey>yy && em != 1) || (ey>yy && em == 1 && sm != 12))
 {
  return true;
 }
    return false;

 
}
//statistics how many days  in complete year
int StatisticsYearDays(int yy,int mm,int dd,int ed)
{
 
 int sumday  = 0;
 int *cnt ;
 int idx = 0;
 
 cnt =GenerateMonth(yy);
 //sum current year day

 while (idx <=12)
 {
  if(idx == 0)
  {
   sumday = cnt[mm] - dd;
  }
  else if (idx == 12 && mm != 12)
  {
   sumday = sumday +ed;
  }
  else
  {
   if(mm >12)
   {
    mm=1;
    yy++;
       cnt =GenerateMonth(yy);
   }
   sumday = sumday +cnt[mm];
  }
  idx++;
  mm++;
 }

 //while (idx <= 12)
 //{
 // sumday = sumday+cnt[idx];
 // idx++;
 //}
 //idx = 1;
 //cnt =GenerateMonth(yy+1);
 ////sum next year day
 //while (idx < em)
 //{
 // sumday = sumday+cnt[idx];
 // idx++;
 //}

 return sumday;
}

//statistics how many days  in complete month
int StatisticsMonthDays(int yy,int mm,int dd,int hh,int mi,int ey,int em,int idx)
{
 int sumday  = 0;
 int *cnt ;
 cnt =GenerateMonth(yy);

 //statistics current month
 if(idx == 0 && dd==0  && hh==0 && mi==0)
 {
  //complete month
  sumday = sumday+cnt[mm];
 
 }
 else if( mm < em)
 {
  sumday = sumday+cnt[mm];
 }
 else if(ey>yy)
 {
  sumday = sumday+cnt[mm];
 }
 idx++;
 return sumday;
}
//Statistics all Hour of day
double StatisticsHourOfDay(int car_type,int sumday)
{
 double result  = 0;
 if(car_type == 1)
 {
  //small car
  result = 60/Per_time * Smallcar_att * 14 * sumday;
 }
 else
 {
  result = 60/Per_time * Bigcar_att * 14 * sumday;
 }
 return result;
}
// Statistics all Hour of night
double StatisticsHourOfNight(int car_type,int sumday)
{
 double result = 0;
 if(car_type ==1)
 {
  result =  Smallcar_night * 10 * sumday;
 }
 else
 {
  result =  Bigcar_night   * 10*  sumday;
 }
 return result;
}
//Statistics per hour * sumhour of day
double StatisticsHourDayCost(int car_type,int sumhour,double pay)
{
 double result = 0;
 if(car_type == 1)
 {
  //small car
  result = 60/Per_time * pay  * sumhour;
 }
 else
 {
  result = 60/Per_time * pay  * sumhour;
 }
 return result;
}
//
double StatisticsHourNightCost(int car_type,int sumhour)
{
 double result = 0;
 if(car_type == 1)
 {
  //small car
  result =  Smallcar_night  * sumhour;
 }
 else
 {
  result =  Bigcar_night  * sumhour;
 }
 return result;
}

int StatisticsHour()
{
 int sumhour = 0;
     return ++sumhour;
}
//Statistic Cost of Parking car
int StatisticsCost(int sumday,int sumhour_night,int sumhour_day,int summinute,int car_type)
{
 double cost = 0;
 if (sumday > 0)
 {
  cost = cost + StatisticsHourOfDay(car_type,sumday);
  cost = cost + StatisticsHourOfNight(car_type,sumday);
 }
 if(sumhour_day > 1 || sumhour_day >=1 && summinute > 0 )
 {
  if(car_type ==1 )
  {
   cost = cost +StatisticsHourDayCost(car_type,sumhour_day,Smallcar_att);
  }
  else
  {
   cost = cost +StatisticsHourDayCost(car_type,sumhour_day,Bigcar_att);
  }
 
 }
 else if( sumhour_day == 1 && summinute == 0 || sumhour_day<=1 && summinute>0)
 {
  if(car_type ==1 )
  {
   cost = cost +StatisticsHourDayCost(car_type,sumhour_day,Smallcar_rule);
  }
  else
  {
   cost = cost +StatisticsHourDayCost(car_type,sumhour_day,Bigcar_rule);
  }
 }
 if(sumhour_night > 0)
 {
  cost = cost + StatisticsHourNightCost(car_type,sumhour_night);
 }
 if(cost >0 )
 {
  //pass N hour
  if(car_type ==1)
  {
   cost = cost + summinute * Smallcar_att;
  }
  else
  {
   cost = cost + summinute * Bigcar_att;
  }
 }
 else
 {
  //first hour
  if(car_type ==1)
  {
   cost = cost + summinute * Smallcar_rule;
  }
  else
  {
   cost = cost + summinute * Bigcar_rule;
  }
 }
 printf("The car total Cost of parking car is %f",cost);
 return 0;
}
//Statistic Time of parking car
int StatisticsTime(int sy,int sm,int sd,int sh,int smi,int ey,int em,int ed,int eh,int emi,int car_type)
{
 int sumday  = 0;
 int sumhour_day = 0;
 int sumhour_night = 0;
 int summinute=0;
 int idx = 0;
 while (ey> sy || em> sm || ed> sd ||eh > sh|| emi > smi)
 {
     if(sy>ey)
  {
   break;
  }
  if((ey-sy>=2)||(ey>sy && em> sm) || (ey>sy && em>= sm && ed>sd) ||(ey>sy && em>= sm && ed>=sd && eh>sh)||(ey>sy && em>= sm && ed>=sd && eh>=sh && emi>=smi))
  {
   //start year less than end year  indicate to park a year
   sumday = sumday + StatisticsYearDays(sy,sm,sd,ed);
   sy++;
  }
  else if(CheckMonth(sy,sm,sd,sh,smi,ey,em,ed,eh,emi))
  {
   //start year month than end month  indicate to park a month
   sumday = sumday + StatisticsMonthDays(sy,sm,sd,sh,smi,ey,em,idx);
   sm++;
   if(sm >12)
   {
    sm = 1;
    sy++;
   }
   idx++;
  }
  else if (CheckDay(sy,sm,sd,sh,smi,ey,em,ed,eh,emi))
  {

    sd++;
    if(sd > GenerateMonth(sy)[sm])
    {
     sd = 1;
     sm ++;
     if(sm > 12)
     {
      sm = 1;
      sy ++;
     }
    }
    sumday = sumday ++;
  }
  else if(CheckHour(sy,sm,sd,sh,smi,ey,em,ed,eh,emi))
  {
   if(sh>=21 || sh<7 )
   {
    //night
    sumhour_night = sumhour_night + StatisticsHour();
    //all hour in night Times
    if((eh >=21 || eh<7) && sd>ed && sh>eh || (eh >=21 || eh<7) && sd>ed && sh>=eh&& smi>=emi)
    {
     smi = 0;
    }
    sh=sh+2;
    if(sh >= 7 && sh <9)
    {
     sh =7;
     smi =0;
    }
   }
   else
   {
    //day
    sumhour_day = sumhour_day + StatisticsHour();
    sh++;
    if(sh>=21)
    {
     smi = 0;
    }
   }
   if(sh >24)
   {
    if(sh == 25)
    {
       sh =1;
    }
    else
     sh =2;
    sd++;
    if(sd >  GenerateMonth(sy)[sm])
    {
     sd = 1;
     sm++;
     if(sm >12)
     {
      sm = 1;
      sy ++;
     }
    }
   }

  }
  else if(ey>=sy && em>=sm && ed>=sd && eh >=sh )
  {
   if(smi >60)
   {
    smi = smi%60;
    sh++;
   }
   summinute = summinute++;
   smi = smi+Per_time;
  }
  else
  {
   break;
  }
 }

 printf("The Car is parking %d days and approximate %d hours of day \n and approximate %d hours of night and approximate %d minutes \n",sumday,sumhour_day,sumhour_night,summinute*Per_time);
 StatisticsCost( sumday,sumhour_night,sumhour_day,summinute,car_type);
 return 0;
}

 

int _tmain(int argc, _TCHAR* argv[])
{
 
  //start yyyy  mmm dd  hh24 mi and car_type
  int sy,sm,sd,sh,smi,car_type;
  //end   yyyy  mmm dd  hh24 mi and car_type
  sy=sm=sd=sh=smi=car_type = 0;
  int ey,em,ed,eh,emi;
  ey=em=ed=eh=emi = 0;
  int result =0;
  printf("Please input to park car start time,format is yyyy mm dd hh24 mi \n");
  scanf("%d %d %d %d %d",&sy,&sm,&sd,&sh,&smi);
  printf("Please input to park car type,when 1 then small car when 2  big car else 1 \n");
  scanf("%d",&car_type);

  printf("Please input to park car start time,format is yyyy mm dd hh24 mi \n");
  scanf("%d %d %d %d %d",&ey,&em,&ed,&eh,&emi);
  ///////////////////////////////////////////
  if(Compare(sy,sm,sd,sh,smi,ey,em,ed,eh,emi))
  {
   //input correctly
   printf("[ Parking car charge standard! \n");
   printf("Start 7 to 21 per %d minutes bigCar is %d  and smallCar is %f \n",Per_time,Bigcar_rule,Smallcar_rule);
   printf("Pass an Hour  per %d minutes bigCar is %f  and smallCar is %f \n",Per_time,Bigcar_att,Smallcar_att);
   printf("Start 21 to 7 per 2 hours    bigCar is %d  and smallCar is %d ] \n",Bigcar_night,Smallcar_night);
   printf("\n");
   time_t lt;
   lt = time(NULL);
            printf("%s\n",ctime(&lt));
   result = StatisticsTime(sy,sm,sd,sh,smi,ey,em,ed,eh,emi,car_type);
   time_t et;
   et=time(NULL);
   printf("%s\n",ctime(&et));
   printf("%d",ctime(&et) - ctime(&lt));
  }
  else
  {
   printf("Input error please check your input value");
  }
 scanf("%d") ;
 //printf("%d %d %d %d %d",y,m,d,h,mi);
 
 return 0;
}


摘自 sql_xep的專欄
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved