仅使用C功能编码简单的停车费计算器

时间:2012-11-07 06:05:53

标签: c function calculator

我获得了以下任务:

  

编写一个给出条目的函数(包括文档)和   停车场停车时间和小时费率,   计算到期金额。

     

假设:

     
      
  1. 没有过夜停车
  2.   
  3. 一小时内不收费
  4.   
  5. 时间是军事风格(下午1:20是1320)
  6.         

    你还必须为你的函数编写一个测试驱动程序:在main()中   根据需要声明并初始化尽可能多的变量,然后调用   功能并显示金额。

这是代码:

int calcRate (int entry , int exit);

int main (void)
{
    // Local Declarations
    int entry;
    int exit;

    //Statements
    printf("Please Enter Entry and Exit time(In military style. For example : 9.30am as
           0930)\n");
    scanf("%d %d\n",&entry , &exit);

    double fee = calcRate(entry,exit);

    printf("Your Parking Fees are %f\n", fee);

    return 0;
}   //main

/*==============calcRate================
 This function calculates the cost of parking
 */

double calcRate (int entry,int exit,double cost)
{
    int hours;
    double rate = 2.00;
    //Statements
    hours = (exit-entry)/100;
    cost = hours * rate;

    return (cost);
}
//calcRate

我无法建立它,我面临着它的问题。例如,我收到以下错误:

  

架构x86_64的未定义符号:“calcRate(int,int)”,引自:Parking.o中的_main(也许你的意思是:calcRate(int,int,double)

我现在被困了近2个小时。 那里有什么样的灵魂?

3 个答案:

答案 0 :(得分:1)

功能声明和定义之间的区别

功能声明

int calcRate (int entry , int exit);

定义标题

double calcRate (int entry,int exit,double cost)

因此要么更改声明或定义标题

表示例如: 将定义标题更改为

int calcRate(int entry,int exit)

并添加声明

double cost

在函数calcRate中。

而不是

  printf("Please Enter Entry and Exit time(In military style. For example : 9.30am as 0930)\n");
  scanf("%d %d\n",&entry , &exit);

待办事项

 printf("Please Enter Entry (In military style. For example : 9.30am as 0930)\n");
 scanf("%d",&entry);
 printf("\n Please exit Entry (In military style. For example : 9.30am as 0930)\n");  
 scanf("%d",&exit);        

扫描中没有“\ n”。

修改

在夜间停车的情况下

而不是

hours = (exit-entry)/100;

使用

if(exit>entry)
    hours = (exit-entry)/100;
else
    {
        int overnightHours;
        overnightHours = (entry-exit)/100;
        hours=2400-overnightHours;
    }

这将消除负值。

答案 1 :(得分:0)

试试这个::

#include <stdio.h>

void calcRate (int entry , int exit, double *cost);

int main (void)
{
    int entry;
    int exit;
    double cost=0 ;

    printf("Please Enter Entry time(In military style. For example : 9.30am as 0930)\n");
    scanf("%d", &entry);
    printf("Please Enter Exit time(In military style. For example : 9.30am as 0930)\n");
    scanf("%d", &exit);

    calcRate(entry,exit,&cost);
    printf("Your Parking Fee is %lf\n", cost);
    return 0;
}   

void calcRate (int entry , int exit, double *cost)
{
    double hours;
    double rate = 2.00;

    hours = (double)(exit-entry)/100 ; //you need to convert your HRS correctly, ex-     entry=0930, exit=1000. cost should be 1.000 but your code will print 1.4000
    *cost = hours * rate;
}

答案 2 :(得分:0)

int calcRate (int entry , int exit);

int main (void)
{
// Local Declarations
int entry;
int exit;

//Statements
printf("Please Enter Entry time (In military style :  0930)\n");
scanf("%d",&entry);

printf("Please Enter Exit time (In military style :  0930)\n");
scanf("%d"&exit);

\n

中也不需要scanf()
double fee = calcRate(entry,exit);

printf("Your Parking Fees are %lf\n", fee); 

需要%lf,因为费用为double

return 0;
}  

double calcRate (int entry,int exit)
{
double cost;
double hours;
double rate = 2.00;
//Statements
hours = ((double)(exit-entry))/100;

您已完成integer division,可能会导致错误。 因此,请将时间设为double

cost = hours * rate;

return cost;

}