0
#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;


struct sphere_t  { float radius; }; sphere_t sph = { 5.8 };

void SphereDescription(sphere_t &sph) {     cout << "Sphere's
description: "; cin >> sph.radius; }


double SphereSurfaceArea(const sphere_t &sph) {     return
4*M_PI*sph.radius*sph.radius;  }

double SphereVolume(const sphere_t &sph) {  return
4.0/3.0*M_PI*sph.radius*sph.radius*sph.radius; }

//the volume and surface area of cylinder 

struct cylinder_t { float
radius ; float height ; }; cylinder_t cylr,cylh = { 6.6,1.3 };


double SurfaceAreaCylinder(const cylinder_t &cylr,const cylinder_t&cylh)
{   return (2*M_PI*cylr.radius)*2 +2*M_PI*cylr.radius*cylh.height; }

double CylinderVolume(const cylinder_t &cylr,const cylinder_t &cylh) {  return 2*M_PI*cylr.radius*cylr.radius*cylh.height; }


int main() { cout << "Sphere's description radius: " << sph.radius << endl; cout << "Surface Area: " << SphereSurfaceArea(sph) << endl; cout << "Volume :" << SphereVolume(sph) << endl;

cout << "Cylinder's description radius: " << cylr.radius << endl; cout << "Cylinder's description height: " << cylh.height << endl; cout << "Surface Area: " << SurfaceAreaCylinder(cylr,cylh) << endl; cout << "Volume :" << CylinderVolume(cylr,cylh) << endl;

system("pause");   return(0); }

//输出

Sphere's description radius: 5.8

Surface Area: 422.733

Volume : 817.283

Cylinder's description radius: 0

Cylinder's description height: 1.3

Surface Area: 0 

Volume : 0

为什么半径、SA 和体积处的输出为零?

4

2 回答 2

1

cylr没有被初始化,只有cylh.

cylinder_t cylr,cylh = { 6.6,1.3 };

试试这个:

cylinder_t cylr = { 1.1, 2.2 }, cylh = { 6.6,1.3 };
于 2013-04-04T02:07:08.220 回答
0

您的代码中有一些问题: struct cylinder_t { float radius ; 浮动高度;}; 圆柱体_t cylr,cylh = { 6.6,1.3 };

在这里,您声明了两个结构变量(您打算这样做吗?)只有 cylh 用值 (6.6 , 1.3) 初始化, cylr 未初始化

所以, cylh.height 返回 1.3(因为它是用它初始化的),但是 cylr.radius 在未初始化时返回 0 .. 这个问题导致其他值是 0

于 2013-04-04T02:19:26.340 回答