油罐车截面示意图:
#define M_PI 3.14159265358979323846
int tankVol(int h, int d, int vt)
{
float r ; //半径
float ab; //直角三角形一个边长
float q ; //直角三角形角度
float length; //罐车桶长度
float Vol;
int tag = 0;
r = (float)d/2;
if(h > r) /*判断高度是否大于半径*/
{
h = d - h;
tag = 1;
}
ab = (float)sqrt(r*r - (r-h)*(r-h));
q = asin(ab/r);
length = (float)vt / (M_PI*r*r);
float s = (float)M_PI * r * r * q / M_PI - ab * (r - h);
//printf("%f, %f, %f, %f, %f\n", r, ab, q, length, s);
if(tag)
{
Vol = vt - s*length;
}
else
{
Vol = s*length;
}
return Vol;
}
void dotest(int h, int d, int vt, int expr) {
int act = tankVol(h, d, vt);
if(act != expr)
printf("Error. Expected %d but got %d\n", expr, act);
//cr_assert_eq(act, expr, "");
}
int main(void) {
dotest(5, 7, 3848, 2940);
dotest(2, 7, 3848, 907);
dotest(3, 6, 3500, 1750);
}