需要将 fstream 输入文件转换为可以循环和解析的格式
我将有一个按以下方式组成的数据文件。 第一行是文件中的行数 [N] 下一行和每个连续行将具有三个值。 如果值三是 >0
则该订单项是三角形。 如果值二和三是 <0
则该订单项是一个圆圈。 如果只有值三是<0
,则该行项目是一个矩形。
一旦我将它们分成圆圈,我就可以编写算法来操纵这些项目, 矩形和三角形,但问题是我不知道该怎么做。
这是我到目前为止编写的代码。
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
// MAIN
int main(void)
{
int N=0;
ifstream Hinfo;
// ........................................................
Hinfo.open("Holes");
if (Hinfo.fail())
{
cerr << "Error: open input file error" << endl;
exit(-1);
}
else
cout << "Success: input files opened" << endl;
// ........................................................
Hinfo >> N;
float A[N];
float B[N];
float C[N];
float Rec[];
float Cir[];
float Tri[];
// ........................................................
for(int i=0; i<N; i++)
Hinfo >> A[i] >> B[i] >> C[i];
Hinfo.close();
}
我真的不知道下一步该做什么,也不知道我到目前为止所做的是否是正确的道路 采取。
接下来我想用这个来找到矩形、三角形和圆形。
for (i=0; i<N; i++)
{
if (C[i] < 0)
{
if (B[i] < 0)
A[i] >> B[i] >> C[i] >> Circle[];
else
A[i] >> B[i] >> C[i] >> Rectangle[];
}
else
A[i] >> B[i] >> C[i] >> Triangle[];
}
我能做到吗?请帮忙,我很难管理所有这些阵列。有没有更简单的方法将所有这些数据分成三角形、圆形和矩形数组?请记住,我是初学者,因此复杂或高级的语法会让我感到困惑 脆弱的n00b心灵
谢谢,卢克。
I'm going to have a data file that will be made up in the following manner.
Line one will be number of lines in the file [N]
The next line and each successive line will have three values.
If value three is >0
then that line item is a triangle.
If value two and three are <0
then that line item is a circle.
If only value three is <0
then that line item is a rectangle.
I can write algorithms to manipulate these items once I have separated them into circles,
rectangles and triangles but the problem is I don't know how to do that.
here's the code I have written so far.
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
// MAIN
int main(void)
{
int N=0;
ifstream Hinfo;
// ........................................................
Hinfo.open("Holes");
if (Hinfo.fail())
{
cerr << "Error: open input file error" << endl;
exit(-1);
}
else
cout << "Success: input files opened" << endl;
// ........................................................
Hinfo >> N;
float A[N];
float B[N];
float C[N];
float Rec[];
float Cir[];
float Tri[];
// ........................................................
for(int i=0; i<N; i++)
Hinfo >> A[i] >> B[i] >> C[i];
Hinfo.close();
}
I don't really know what to do next or if what I've done so far is the correct path
to take.
next I was thinking this to find rectangles, triangles and circles.
for (i=0; i<N; i++)
{
if (C[i] < 0)
{
if (B[i] < 0)
A[i] >> B[i] >> C[i] >> Circle[];
else
A[i] >> B[i] >> C[i] >> Rectangle[];
}
else
A[i] >> B[i] >> C[i] >> Triangle[];
}
Would I be able to do this? Please help, I'm having a hard time managing all these arrays. Is there a simpler way to separate all this data into triangle, circle and rectangle arrays? Keep in mind that I'm a beginner so complex or advanced syntax will confuse my
fragile n00b mind
Thanks, Luke.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我要做的就是编写一个结构来保存单个数据条目,可能是这样的:
这样,每次读取一行值时,您都可以将它们放入新的条目中并相应地设置类型。那么你只需要一个数组或向量来保存条目。或者您可以为每个条目类型创建一个数组/向量。
沿着这些思路做的事情应该可以做到。
What I would do, is write a struct to hold a single data entry, probably like this:
This way, each time you read a line of values you can put them in a new Entry and set the type accordingly. Then you only need one array or vector to hold the entries. Or you can create an array/vector for each entry type.
Something along these lines should do it.
一种简单的方法是拥有三个数组,以及一个额外的类型数组,用于存储表示类型的整数。(注意,您应该使用枚举作为类型,但常量也可以工作)
这样您就不需要三种形状中的每一种都需要单独的数组。
然后您可以执行循环来查找类型
然后您可以再次循环遍历数组:
当然您甚至不需要第二次循环,但如果您的老师想要它,这将是一种方法。
One simple way to do this is to have your three arrays, and an extra type array that stores an integer that signifies the type.(Note, you should use an enum for the type, but constants will also work)
This way you do not need separate arrays for each of the three types of shapes.
Then you can do your loop to find the types
Then you can loop through the arrays again:
Of course you do not even need the second looping, but if your teacher wants it, this would be one way to do it.