编写一个程序,确定当您将岩石从悬崖上扔下时,它会移动多远和移动多长时间。单击此处将文件 toss.txt 复制到您的桌面(右键单击文件名并选择另存为)。该文件包含以米为单位的悬崖高度。
然后程序将:
打开文件 toss.txt 并将悬崖高度读入一个双精度变量,然后用适当的标签将悬崖高度的值回显到屏幕上。
询问用户投掷岩石的角度(90 度是笔直向上,0 度是笔直向前),以及投掷岩石的速度(以英里/小时为单位)。
检查以确保角度大于或等于 0 且小于或等于 90。如果不是,则程序终止并在屏幕上打印适当的错误消息。
检查以确保速度小于或等于 100 mph 且大于或等于 0 mph。如果不是,则程序终止并将适当的错误消息打印到屏幕上。
如果角度和速度有效,程序完成计算如下:
将英里每小时转换为米每秒。
将角度转换为弧度。
使用以下等式计算经过的时间:
在哪里
使用以下方法计算在水平方向上行驶的距离:
将水平方向上经过的时间和距离输出到带有适当标签的屏幕。
打印一条适当的消息,告诉用户在水平方向上行驶的距离是否大于、小于或等于悬崖的高度。
/* This program */ using namespace std; #include<iostream> #include<cmath> #include<iomanip> #include<fstream> int readit (); int calcit (double, double, double); int main() { readit (); system ("pause"); return 0; } int readit () { double hite, angl, v; ifstream datain ( "toss.txt" ); datain >> hite; cout << "The cliff height is " << hite << " meters"<< endl; cout << "Enter the angle in degrees (from horizontal) the rock is thrown: " << endl; cin >> angl; if (angl>=0 && angl<=90) { cout << endl << "The angle you have entered is "<<angl<< endl <<endl; } else { cout << "The angle you have entered is not acceptable" << endl; return 0; } cout << "Enter the velocity in mph the rock is thrown: " << endl; cin >> v; if (v>=0 && v<=100) { cout << endl << "The velocity at which the rock is thrown is "<<v<< " mph" << endl << endl; } else { cout << "The velocity you have entered is not acceptable" << endl; return 0; } calcit (hite, angl, v); } int calcit (double hite, double angl, double v) { double tyme, dist; v = v * (1609.344/3600); angl = angl*(M_PI/180); tyme = -v*sin(angl) + (sqrt((v*sin(angl)*v*sin(angl)) + 2*9.8*hite)/9.8) + (2*(v*sin(angl))/9.8); dist = (tyme * v) * cos(angl); cout << tyme << " " << dist <<endl; }
我试图在岩石撞到地面之前获得正确的移动时间,但我一直得到错误的答案。我不确定我是否正在转动等式来计算出岩石在空气中停留的时间,直到影响到 c++ 语言为止。有什么想法吗???我真的需要完成这个该死的项目。