ex18_04.cpp

来自「C程序设计经典教程第5版程序示例,比较适合初级c语言的学生」· C++ 代码 · 共 32 行

CPP
32
字号
// Exercise 18.4 Solution: Ex18_04.cpp
// Inline function that calculates the volume of a sphere.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <cmath>
using std::pow;

const double PI = 3.14159; // define global constant PI

// calculates volume of a sphere
inline double sphereVolume( const double radius ) 
{ 
   return 4.0 / 3.0 * PI * pow( radius, 3 ); 
} // end inline function sphereVolume

int main()
{
   double radiusValue;

   // prompt user for radius 
   cout << "Enter the length of the radius of your sphere: ";
   cin >> radiusValue; // input radius 

   // use radiusValue to calculate volume of sphere and display result
   cout << "Volume of sphere with radius " << radiusValue 
      << " is " << sphereVolume( radiusValue ) << endl;
   return 0; // indicates successful termination
} // end main

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?