繁体   English   中英

将链接结构写入类

[英]Writing linked structure into class

我有一个程序可以读取txt文件,并打印出文件中的所有行。 我使用链接结构来做到这一点。 码:

#include <string>
struct ListNode{
std::string item;
int count;
ListNode* link;
};

#include <iostream>
#include <fstream>

int main(){
char filename[] = "src/inputdata";
std::cout << " Reading from file: " << filename << " . . . ";

std::ifstream in( filename );
if (in.fail()){
    std::cout << " ... not able to read it! Exiting." << std::endl;
    return -1;
}
std::cout << " ... OK, file opened." << std::endl << std::endl;

ListNode* first = new ListNode();
ListNode* p = first;
int count = 1;

while (!in.eof()){

       getline( in, p->item );

       p->count = count++;

       p->link = new ListNode();

       p = p->link;
}

count = 1;

p = first;

   while (count <= p->count)

   {

   p->count = count++;

   std::cout << p->count << ": " << p->item << std::endl;

   p = p->link;

   }
       }

要求是将该程序转换为3个文件,驱动程序,主cpp文件和头文件。 我对C ++很陌生。 我已经按照以下步骤设置了头文件,但是我想我想念其中的东西了,也许是吸气剂和吸气剂...

头文件:

//ListNode.h
#ifndef LISTNODE_H_
#define LISTNODE_H_
using namespace std;
#include <iostream>

class ListNode{
public:
ListNode();
virtual ~MyData();

private:
string item;
int count;
ListNode* link;
};
#endif /* LISTNODE_H_ */

我应该如何处理其他两个文件? 感谢您的回答。 谢谢

现在,当您使用类时。

在类中创建一个方法

class ListNode
{
....
        ListNode* CreateList();
....
};

读取文件并创建列表的操作; 与您在main()中所做的相同

ListNode.cpp包括ListNode.h文件,并为您在ListNode类中声明的方法提供定义

Cpp文件

#include "ListNode.h"
.... //remaining code

最后在第三个文件main.cpp 包括“ ListNode.h”

#include"ListNode.h"
...//code..

在main方法中,创建ListNode类的对象并执行您要执行的操作

头文件(.h):

//ListNode.h
#ifndef LISTNODE_H_
#define LISTNODE_H_
#include <string>
using namespace std;

class ListNode {
    public:
       ListNode();
       ~ListNode();

       // Accessor functions.
       string const& getItem() const;
       int getCount() const;
       ListNode* getLink() const;

       // Modifier functions.
       void setItem(string const& i);
       void setCount(int c);
       void setLink(ListNode* l);

    private:
       string item;
       int count;
       ListNode* link;
};
#endif /* LISTNODE_H_ */

实施文件(.cc):

// ListNode.cc
#include "ListNode.h"

ListNode::ListNode() : count(0), link(NULL) {}

ListNode::~ListNode() {}

string const& ListNode::getItem() const
{
   return item;
}

int ListNode::getCount() const
{
   return count;
}

ListNode* ListNode::getLink() const
{
   return link;
}

void ListNode::setItem(string const& i)
{
   item = i;
}

void ListNode::setCount(int c)
{
   count = c;
}

void ListNode::setLink(ListNode* l)
{
   link = l;
}

main.cc:

// main.cc
#include <iostream>
#include <fstream>
#include "ListNode.h"

int main()
{
   char filename[] = "src/inputdata";
   std::cout << " Reading from file: " << filename << " . . . ";

   std::ifstream in( filename );
   if (in.fail()){
       std::cout << " ... not able to read it! Exiting." << std::endl;
       return -1;
   }
   std::cout << " ... OK, file opened." << std::endl << std::endl;

   ListNode* first = new ListNode();
   ListNode* p = first;
   int count = 1;

   while (!in.eof()){

       string item;
       getline( in, item );
       p->setItem(item);
       p->setCount(count++);

       p->setLink(new ListNode());

       p = p->getLink();
   }

   count = 1;

   p = first;

   while (count <= p->getCount())

   {
      p->setCount(count++);

      std::cout << p->getCount() << ": " << p->getItem() << std::endl;

      p = p->getLink();
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM