繁体   English   中英

C ++中天花板功能的问题

[英]problem with ceiling function in C++

我正在参加编程课程的介绍。 我们不必编写实际的代码,但如果我们这样做,我们会得到额外的信任。 我们使用Raptor作为流程图,您可以从那里生成C ++代码,并通过一些修改获得工作代码。 我使用Visual Studio 2008进行代码mod和构建。 对于期中考试,我有停车问题。 这些评论解释了程序的功能,除了一个错误之外它构建得很好:我收到一条消息,说第70行找不到'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);
   }
}

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

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

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM