2007年12月28日 星期五

C 程式設計作業五,解題練習:解答


/* C Programming, Project 5 */
#include <stdio.h>
#include <stdlib.h>

int getZoneNo(int);
int compZoneMin(int, int, int, int);
int main(void)
{
char phoneNo[20];
double zonePrice1=5.4, zonePrice2=3.6;
int startHr, startMin, endHr, endMin;
int zone1, zone2, zoneMin1, zoneMin2;
while (scanf("%s %d %d %d %d",phoneNo, &startHr, &startMin, &endHr, &endMin)==5)
{
printf("%s %6d %6d %6d %6d\n",phoneNo, startHr, startMin, endHr, endMin);
zone1 = getZoneNo(startHr);
zone2 = getZoneNo(endHr);
if (zone1==zone2)
{
if (zone1==1)
{
zoneMin1 = compZoneMin(startHr, startMin, endHr, endMin);
zoneMin2 = 0;
}
else
{
zoneMin1 = 0;
if (startHr<endHr)
zoneMin2 = compZoneMin(startHr, startMin, endHr, endMin);
else
zoneMin2 = compZoneMin(startHr, startMin, endHr+24, endMin);
}
}
else
{
if (zone1==1)
{
if (endHr<8)
endHr += 24;
zoneMin1 = compZoneMin(startHr, startMin, 18, 0);
zoneMin2 = compZoneMin(18, 0, endHr, endMin);
}
else
{
endHr += 24;
if (startHr<8)
startHr += 24;
zoneMin1 = compZoneMin(startHr, startMin, 32, 0);
zoneMin2 = compZoneMin(32, 0, endHr, endMin);
}
}
printf("--------------------------->%6d%6d%10.0lf\n", zoneMin1, zoneMin2, zoneMin1*zonePrice1+zoneMin2*zonePrice2);
}

system("pause");
return 0;
}

int getZoneNo(int hr)
{
if (hr>=8 && hr<18)
return 1;
else
return 2;
}
int compZoneMin(int startHr, int startMin, int endHr, int endMin)
{
printf("%6d %6d %6d %6d\n",startHr, startMin, endHr, endMin);
if (startMin<endMin)
return (endHr-startHr)*60+endMin-startMin;
else
return (endHr-startHr-1)*60+endMin-startMin+60;
}

作業五題目
回到首頁

沒有留言: