<分区>
我的程序应该使用 .txt 文件作为输入(时间从 1 秒到 10 秒)来计算坠落物体的距离。文本文件内容如下:
1 2个 3个 4个 5个 6个 7 8个 9 10
这是我到目前为止的代码。
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;
//function prototype
double fallingDistance (int);
void main()
{
ifstream inputFile;
int time;
double distance;
//open the file
inputFile.open("05.txt");
inputFile >> time;
{
distance = fallingDistance (time);
cout << time << "\t\t" << distance << endl;
}
}
double fallingDistance (int time)
{
double distance, gravity=9.8;
distance = static_cast<double>(0.5 * gravity * pow(time,2));
return distance;
}
这是我的程序编译的内容:
1 4.9
press any key to continue...
提前致谢!