2011-03-03 279 views
3

我正在学习Intro to Programming类。我们不必编写实际的代码,但是如果我们这样做,我们会获得额外的功劳。我们使用Raptor作为流程图,您可以从那里生成C++代码,并通过一些修改获取工作代码。我使用Visual Studio 2008进行代码修改和构建。在中期,我有一个停车问题。的注释说明了该程序的功能,并且除了一个错误它建立罚款:我得到一个消息说,上线70在这里没有发现“天花板”识别代码如下:在C++中使用ceiling函数的问题

// midterm part 2.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <iostream> 
#include <string> 

void getTime (float &time); 
void getAge (int &age); 
void calcFee (float time, double &fee); 

using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    double discount; 
    double fee; 
    float time; 
    int age; 

    cout << "This program gets the amount of time parked and calculates the total fee for parking." <<endl; 
    cout << "The rate is $1.00 for the first hour or part thereof, and $0.75 for each additional" <<endl; 
    cout << "hour or part thereof. The minimum fee is $1.00. The maximum fee is $10.00." <<endl; 
    cout << "Those over 55 years of age receive a 10% discount, except on the minimum." << endl; 
    getTime(time); 
    getAge(age); 
    calcFee(time,fee); 
    if (fee>10) 
    { 
     fee = 10; 
    } 
    else 
    { 
     fee = fee; 
    } 
    if (age>=55 && fee>1) 
    { 
     discount =fee*0.1; 
    } 
    else 
    { 
     discount =0; 
    } 
    fee =fee-discount; 
    cout << "Your total fee for parking is $"<<fee<<"." << endl; 
    return 0; 
} 

void getTime (float &time) 
{ 
    string raptor_prompt_variable_zzyz; 

    raptor_prompt_variable_zzyz ="Enter the number of hours parked."; 
    cout << raptor_prompt_variable_zzyz << endl; 
    cin >> time; 
} 

void getAge (int &age) 
{ 
    string raptor_prompt_variable_zzyz; 

    raptor_prompt_variable_zzyz ="What is the age of the driver?"; 
    cout << raptor_prompt_variable_zzyz << endl; 
    cin >> age; 
} 

void calcFee (float time, double &fee) 
{ 
    float HOURLY_RATE = 0.75; 

    time = ceiling(time); 
    if (time==0) 
    { 
     fee =0; 
    } 
    else 
    { 
     fee =1+((time-1)*HOURLY_RATE); 
    } 
} 

我没有代码专家,所以如果这个代码不完美,我不会知道它。我可以采取行“时间=天花板(时间);”在代码之外,它的构建很好。它只是不会计算它应该的方式。教练喜欢在这种情况下使用天花板功能,但如果有另一种方法,我会看看。预先感谢任何可以提供的帮助。

+0

欢迎来到StackOverflow! – 2011-03-03 20:35:04

回答

7

Ceiling函数在C++中被称为ceil#include <cmath>来使用它。

+0

感谢您的帮助!我做了这些改变,现在它的构建和运行都非常完美。 – gil 2011-03-04 17:03:46

+0

不客气。 – 2011-03-04 18:22:25

2

C++的ceil函数与“ceiling”完全相同。要使用它,你只需要导入它的库,像这样:

/* ceil example */ 
#include <stdio.h> 
#include <math.h> 

int main() 
{ 
    printf ("ceil of 2.3 is %.1lf\n", ceil (2.3)); 
    return 0; 
} 
+0

我认为downvote需要一个解释。 – 2011-03-03 20:36:33

+0

-1:有点苛刻。 rlb可能会根据评论进行更新。我猜你应该更喜欢而不是 2011-03-03 20:37:40

+0

是的,这是一个C++问题。将C与C++混合是一种不应该被提升的可怕做法。 – 2011-03-03 20:44:27