我的程序没有为我的最后一行代码显示函数的结果

时间:2018-10-16 06:26:53

标签: c

此代码接受用户(字符C,T,B)和(int 0-24和0-60)的输入,以根据用户输入的车辆类型计算停车费用。

该程序的最后一行代码可以打印“ charged”功能的结果,该功能由用户输入的char值声明的车辆类型决定,但是当我运行它时,它仅重新运行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;
}

2 个答案:

答案 0 :(得分:2)

我不确定是否要解决代码中的所有问题,但是这里有一个问题:

string[] sentences = {"Hi 123 it is a 564 and 678, so let's work.",
                      "123 asd asd 123",
                      "123 asd 123 asd"};
for (int i = 0; i < sentences.Length; i++)
{
    var sliced = Regex.Split(sentences[i], @"(\d+)").Where(s => !string.IsNullOrEmpty(s)).ToArray();
}

它的作用是声明三个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;制作char”,而是“使名为'C'的{​​{1}}而不初始化它”。但是在这种情况下,因为您可以将char与文字进行比较,所以无论如何都不需要这三个字符。

答案 1 :(得分:0)

它们在上面的代码中有几个问题。

1)。您正在将vehicle_type与不是char的{​​{1}}进行比较,因此首先将值分配给此assign变量< / p>

char

或者您可以直接比较

char C = 'C';
char T = 'T';
char B = 'B';

2)。 if (vehicle_type == 'T') 是一个函数,而不是total_hour_parked变量,因此您可以将integer的{​​{1}}存储在任意位置变量

例如:-

return value

希望有帮助