此代码接受用户的输入(字符 C、T、B)和(整数 0-24 和 0-60),以根据用户输入的车辆类型计算 parking 费用。
程序中的最后一行代码应该打印函数“charged”的结果,该结果由用户输入的字符值声明的车辆类型决定,但当我运行时,它仅返回 0.00 而不是 flaot重视任何和所有的帮助:)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int total_minute_parked (int minute_in, int minute_left)
{
int minute_parked;
if (minute_in > minute_left)
{
minute_parked = (minute_left - minute_in + 60);
}
else
{
minute_parked = (minute_left - minute_in);
}
return minute_parked;
}
// func calc total hours parked
int total_hour_parked (int hour_in, int hour_left)
{
int hours_parked;
if (hour_left > hour_in)
{
hours_parked = abs((hour_left - 1) - hour_in);
}
else
{
hours_parked = abs(hour_left - hour_in);
}
return hours_parked ;
}
// -------------------funtion to calc charge based off type of vehicle------
float charged (char vehicle_type,int total_hour_parked)
{
char C;
char T;
char B;
float temp_charged;
if (vehicle_type == C) // -------------------------------CAR
{
if (total_hour_parked > 3)
{
float secondary_hour = total_hour_parked - 3;
temp_charged = secondary_hour * 1.5;
}
else
{
temp_charged = 0;
}
}
else if (vehicle_type == T) // ------------------------------TRUCK
{
if (total_hour_parked > 2)
{
float secondary_hour = total_hour_parked - 2;
temp_charged = (secondary_hour * 2.3) + 1.0;
}
else {
temp_charged = 1;
}
}
else if (vehicle_type == B) // -----------------------------------BUS
{
if (total_hour_parked > 1)
{
float secondary_hour = total_hour_parked - 1;
temp_charged = (secondary_hour * 3.7) + 2.0;
}
else {
temp_charged = 2;
}
}
return temp_charged;
}
//---------------------- end program upon invalid imput -------------------//
// --------------------- main that prints results and takes imput -----------//
int main()
{
int total_hour_parked (int hour_in,int hour_left);
float charged (char vehicle_type, int total_hour_parked);
char vehicle_type;
int hour_in = 0;
int minute_in = 0;
int hour_left = 0;
int minute_left = 0;
printf("Please enter the type of Vehicle:");
scanf("%c",&vehicle_type);
printf("Please enter the hour entered lot:");
scanf("%d", &hour_in);
printf("Please enter the minute entered lot:");
scanf("%d", &minute_in);
printf("Please enter the hour left lot:");
scanf("%d", &hour_left);
printf("Please enter the minute left lot:");
scanf("%d", &minute_left);
printf("------------------------------------\n");
printf("Parking time: %d:%d\n", total_hour_parked(hour_in,hour_left),total_minute_parked(minute_in,minute_left));
printf("Cost %f",charged(vehicle_type,total_hour_parked));
return 0;
}
最佳答案
我不确定它是否能解决代码中的所有问题,但这里有一个问题:
char C;
char T;
char B;
float temp_charged;
if (vehicle_type == C) // -------------------------------CAR
它的作用是声明三个char
并且不分配任何值(因此访问它们是未定义的并且会导致一些垃圾值)。然后,您将 vehicle_type
与这些字符进行比较。结果很可能是false
(或0
)。那不是你的本意。相反,请执行以下操作:
if (vehicle_type == 'C')
您可能误解了 char C;
的含义。它并不意味着“创建一个值为 'C'
的 char
”,而是“创建一个名为 C
的 char
> 并且不初始化它”。但在这种情况下,您无论如何都不需要这三个字符,因为您只需将 vehicle_type
与文字进行比较即可。
关于c - 我的程序不打印最后一行代码的函数结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52829143/