/***************************************************************************
* 如果你发现错误,请指教!
* car.c
*
* Fri Dec 15 14:16:49 2006
* Copyright 2006 Elliot
* storysnail@gawab.com
****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Compile:
gcc car.c -o car
某市不同车牌的出租车3KM的起步价和计费分别为:
夏利7元 3KM以外2.1元/KM
富康8元 3KM以外2.4元/KM
桑塔纳9元 3KM以外2.7元/KM
编程实现:从键盘输入乘车的车型既及行车的公里数,输出应付车资
*/
#include <stdio.h>
#include <string.h>
/* defines*/
#define XIALI_ID1
#define FUKANG_ID2
#define SANGTANA_ID3
#define UNDEFINED_ID0
/***************************************************************************************
Displays the proper usage
***************************************************************************************/
void DisplayProperUsage(FILE * pFile)
{
fprintf(pFile, "Proper Usage: [car] [Car modes] [Kilometres]\n\n");
fprintf(pFile, "Car modes: \n");
fprintf(pFile, " XIALI: 1\n");
fprintf(pFile, " FUKANG: 2\n");
fprintf(pFile, " SANGTANA: 3\n");
fprintf(pFile, "Examples:\n");
fprintf(pFile, " car 1 2000\n");
}
/***************************************************************************************
Main (the main function)
***************************************************************************************/
int main(int argc, char * argv[])
{
/* variable declares*/
char CarMode[8+8];
int cMode = UNDEFINED_ID;
char Kilometres[256+8];
int Kilo = 0;
double expense = 0;
/* make sure there are at least 3 arguments*/
if (argc < 2)
{
DisplayProperUsage(stderr);
exit(-1);
}
memset(CarMode,'\0',8+8);
strncpy(CarMode, argv[1],8);
cMode = atoi(&CarMode[0]);
if (cMode != XIALI_ID && cMode != FUKANG_ID && cMode != SANGTANA_ID)
{
DisplayProperUsage(stderr);
return -1;
}
memset(Kilometres,'\0',256+8);
strncpy(Kilometres, argv[2],256);
Kilo = atoi(Kilometres);
if(Kilo<=0) {
DisplayProperUsage(stderr);
return -1;
}
// process
if (cMode == XIALI_ID)
{
if(Kilo<=3) {
printf("XIALI:\nKilometres:[%d],expense:[7]\n",Kilo);
}
else {
expense = (Kilo-3)*2.1+7;
printf("XIALI:\nKilometres:[%d],expense:[%f]\n",Kilo,expense);
}
}
else if (cMode == FUKANG_ID)
{
if(Kilo<=3) {
printf("FUKANG:\nKilometres:[%d],expense:[8]\n",Kilo);
}
else {
expense = (Kilo-3)*2.4+8;
printf("FUKANG:\nKilometres:[%d],expense:[%f]\n",Kilo,expense);
}
}
else if (cMode == SANGTANA_ID)
{
if(Kilo<=3) {
printf("SANGTANA:\nKilometres:[%d],expense:[9]\n",Kilo);
}
else {
expense = (Kilo-3)*2.7+9;
printf("SANGTANA:\nKilometres:[%d],expense:[%f]\n",Kilo,expense);
}
}
return 0;
}