我正在编写一个函数,该函数将读取一个输入文件并将其写入到链接列表中。输入文件中有两行数字,一个整数和一个双精度数:
诠释 1个 1个 1个 浮动 1.2 1.4 2.4
,但它们位于垂直行中。将其视为T图表,其中1列是整数,1列是浮点数。该列的每个行具有1个int和1个浮点,因此它们被分组在一起。我想将表的每个组存储在链接列表的节点中。 问题是,如何将它们读入链接列表?假设我已经创建了FlowList类之一。 FlowList具有 next 指针和 node ,其中包含整数和浮点存储。
struct ListItem{
int year;
double flow;
};
struct Node{
ListItem item;
Node *next;
};
class FlowList{
Node *headM;
因此,基本上,如果在row /中排列的数字属于同一组(即1和2.22相互排列),则它们将进入同一节点。
**year** **flow**
1 1.5
2 3.3 <--------this row goes into one node of the
linked list
这里是我到目前为止所写内容的一个示例(根本不多):
此外,确保输入文件中仅包含数字,因此不必担心。
int readData(FlowList& x){
int c=0; //counter which reads the number of nodes created.
ifstream fpr;
fpr.open("flow.txt");
if(!fpr){
cerr<<"Error in opening file";
exit(1);
}
//and then I have no idea what to do beyond here.
现在,我将创建一个名为x的新FlowList。访问整数和浮点数的方法是使用以下语法:
x.head->item.year
x.head->item.flow
我对C ++还是很陌生,所以让我知道我是否需要澄清任何内容。 谢谢