使用递归的曲线下面积
Area under the curve using recursive
因此,我得到了一个要创建的程序,该程序涉及读取一个外部文件,该文件输出一个类似于x-y字段的图像,我能够为其生成如此多的图像,但我陷入了死胡同。。。我删除了#以便可以读取库
数据文件包含
x values y values
20.00 0
20.02 15
20.04 27
20.06 39
20.08 54
20.10 65
20.12 75
20.14 84
20.16 93
20.18 101
20.20 108
20.22 113
20.24 116
20.26 115
20.28 112
20.30 107
20.32 100
20.34 92
20.36 83
20.38 74
20.40 64
20.42 53
20.44 39
20.46 27
20.48 15
20.50 0
/* This program reads numbers from a file. */
#include< iostream >
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
ifstream inFile;
int x1, x2, y1, y2;
//Open the File.
inFile.open("xydata.txt");
//Read the numbers from the file
inFile >> x1; //x1 =20
inFile >> y1; // y1 = 0
inFile >> x2; // x2 =20.02
inFile >> y2; //y2 = 15
//Close File.
inFile.close();
//Calculate the total Area underneath the curve
double h, b, a, trap, final_trap;
a = (x2 - x1);
b = (y2 - y1);
trap = ((a+b)/2);
final_trap = trap*b;
cout<<final_trap<<endl ;
return 0;
}
/*
// Writing data into a File
int main()
{
ofstream outputFile
outputFile.open("xydata.dat")
cout << "Now writing data into the file" <<endl;
//Writing Area into the file
outputFile <<
//close this file
outputFile.close();
cout << "Done." << endl;
return 0;
*/
以下是整个程序(但从stdin读取-我相信你可以修复它):
#include <iostream>
using namespace std;
int main() {
bool first_iteration = true;
float area = 0.0;
float ox, oy, x, y;
while (cin>>x && cin>>y) {
if (!first_iteration)
area += (x-ox) * (y+oy)/2.0;
ox=x; oy=y;
first_iteration=false;
}
cout << "Area is " << area << endl;
}
如果输入文件中确实有这些列标签,那么一开始就必须吃掉它们。
相关文章:
- 通过递归进行因子分解
- 递归函数计算序列中的平方和(并输出过程)
- 使用递归的数组的最小值.这是怎么回事
- 递归列出所有目录中的C++与Python与Ruby的性能
- 递归计数给定目录的文件和所有目录
- 如何在BST的这个简单递归实现中消除警告
- C++:正在检查LinkedList中的回文-递归方法-错误
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 递归无序映射
- TSP递归解的迭代形式
- 如何在Elixir中调用递归函数并行
- 返回递归调用和仅递归调用的区别
- 数组元素打印的递归方法
- 使用递归时获取变量的奇怪值
- 如何在C++中递归地按相反顺序打印集合
- 到连接组件算法的问题(递归)
- 如何使用递归打印修改后的星号三角形图案
- 使用递归模板动态分配的多维数组
- 递归函数有效,但无法记忆
- 使用递归的曲线下面积