你好。。。能否帮我看看这道题呢?非常感谢

2024-11-28 22:40:21
推荐回答(1个)
回答1:

clc
clear all
n=0;
while 1
n=n+1;
fprintf('Cylinder #%d\n',n)
[heightOut,radiusOut]=getCylinderInfo;
[surfAreaOut,VolOut]=calcAreaAndVolume(heightOut,radiusOut);
dispAreaAndVolume(surfAreaOut,VolOut)
Str=input('Calculate info for another cyclinder?[Y/y]es [N/n]o:\n','s');
if(Str=='N'||Str=='n')
break;
else
continue;
end
end

function [heightOut,radiusOut]=getCylinderInfo
hr(1)=-1;hr(2)=-1;
while(hr(1)<0||hr(2)<0)
hr=input('Please Insert height and radius of the circular cylinder:\n');
end
if(hr(1)>=0&&hr(2)>=0)
fprintf('Successfully data saved!\n')
heightOut=hr(1);radiusOut=hr(2);
else
fprintf('Non-negative number should be applied!')
end
end
function [surfAreaOut,VolOut]=calcAreaAndVolume(heightIn,radiusIn)
surfAreaOut=2*pi*radiusIn*heightIn+2*pi*radiusIn^2;
VolOut=pi*radiusIn^2*heightIn;
end
function dispAreaAndVolume(surfAreaIn,VolIn)
fprintf('surfArea=%f\nVolume=%f\n',surfAreaIn,VolIn)
end