| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
题目: 
/Computes the area of a circle and the volume of a sphere. 
//Uses the same radius for both calculations. 
//Code all function declarations and definitions with comments 
#include<iostream> 
#include<cmath> 
using namespace std; 
const double PI = 3.14159; 
// Add function declarations here 
int main() 
{ 
int radius_of_both; 
double area_of_circle, volume_of_sphere; 
radius_of_both = getInput(); 
area_of_circle = area(radius_of_both); 
volume_of_sphere = volume(radius_of_both); 
return 0; 
} 
// Function definitions go here 
 
正确输出: 
Enter a radius to use for both a circle 
and a sphere (in inches): 3 
Radius = 3 inches 
Area of circle = 28.2743 square inches 
Volume of sphere = 113.097 cubic inches 
C:\Users\Shaun\Desktop\code\middleTest\Debug\middleTest.exe (进程 21204)已退出,代码为 0。 
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。 
按任意键关闭此窗口. . . 
 
 
 
我的答案: 
#include<iostream> 
#include<cmath> 
using namespace std; 
 
const double PI = 3.14159; 
int getInput(); 
double area(int radius_of_both); 
double volume(int radius_of_both); 
 
int main() 
{ 
 
    int radius_of_both; 
    double area_of_circle, volume_of_sphere; 
 
    radius_of_both = getInput(); 
    area_of_circle = area(radius_of_both); 
    volume_of_sphere = volume(radius_of_both); 
 
    return 0; 
 
} 
    
    int getInput() 
{ 
     int radius_of_both; 
     cout << "Enter a radius to use for both a circle\n" 
         << "and a sphere (in inches): " << radius_of_both; 
    cin >> radius_of_both; 
    cout << "Radius = " << radius_of_both << " inchese\n"; 
 
    return radius_of_both; 
} 
 
    double area(int radius_of_both) 
{ 
     double area_of_circle; 
     area_of_circle = (PI * pow(radius_of_both, 2)); 
     cout << "Area of circle = " << area_of_circle << "\n"; 
 
     return area_of_circle; 
} 
 
    double volume(int radius_of_both) 
{ 
     
     double volume_of_sphere; 
     volume_of_sphere = ((4.0 / 3.0) * PI * pow(radius_of_both, 3)); 
     cout << "Volume of sphere = " << volume_of_sphere; 
 
     return volume_of_sphere; 
  } 
 
输出: 
Enter a radius to use for both a circle 
and a sphere (in inches):13 
Radius = 3 inches 
Area of circle = 28.2743 square inches 
Volume of sphere = 113.097 cubic inches 
C:\Users\Shaun\Desktop\code\middleTest\Debug\middleTest.exe (进程 21204)已退出,代码为 0。 
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。 
按任意键关闭此窗口. . . 
 本帖最后由 yuxijian2020 于 2021-3-25 11:06 编辑 
- int getInput()
 
 - {
 
 -      int radius_of_both;
 
 -      cout << "Enter a radius to use for both a circle\n"
 
 -          << "and a sphere (in inches): "; //从这里开始注释掉 << radius_of_both;  //这个变量你刚声明并没有初始化,直接输出,输出内容是不确定的
 
 -         //所以你的输出Enter a radius to use for both a circle
 
 -         //                   and a sphere (in inches):13 //->这里多了一个1
 
 -     cin >> radius_of_both;
 
 -     cout << "Radius = " << radius_of_both << " inchese\n";
 
  
-     return radius_of_both;
 
 - }
 
  复制代码 
 
 
 |   
 
 
 
 |