본문 바로가기

Studies/A Structured Approach Using C++

Problem 4-35 print the ceiling, floor, and rounded value of a floating-point number

//============================================================================
//  제출일 : 2005. 3. 12
//  작업환경 :  Windows XP SP1, VC++ 6.0, Pentium4
//  연습문제 4-35
//============================================================================
#include <iostream.h>
#include <math.h>

float round(float num);

int main()
{
 float num;
 cout << "아무수나 입력하세요\n";
 cin >> num;

 cout << "Ceiling : " << ceil(num) << "\n";
 cout << "Floor : " << floor(num) << "\n";
 cout << "Round : " << round(num) << "\n";
 

 return 0;
}

float round(float num)
{
 float temp;
 temp = (int)num;
 temp = num - temp;
 
 if (num > 0)
 {
  if (temp >= 0.5)
   return ceil(num);
  else
   return floor(num);
 }
 else
 {  
  if (temp >= -0.5)
   return ceil(num);
  else
   return floor(num);
 }
}