简体   繁体   English

显示 CSV 文件 C++ 的完整行

[英]Show full line of a CSV file C++

I am making a program that reads a.CSV file and loads it into binary search trees to sort its content, since I managed to load the.CSV and implement the tree however the content of the file is:我正在制作一个程序,它读取一个 .CSV 文件并将其加载到二进制搜索树中以对其内容进行排序,因为我设法加载了 .CSV 并实现了该树,但是文件的内容是:

1, Peter, 230151515 1、彼得,230151515
5, Merrick, 25551561 5、梅里克,25551561
7, Lucky, 4301616199 7、幸运,4301616199
2, luis, 2589191919 2、路易斯,2589191919
16, Alfredo, 25891919 16、阿尔弗雷多,25891919
8, Linda, 129616919 8、琳达,129616919

I am using the first data of each row as a key and with the code that I carry, it orders it correctly, however I want it to show the rest of the row, not just the key, someone could tell me how I could do that, to show all the data of each key ordered.我使用每行的第一个数据作为键,并且使用我携带的代码,它可以正确排序,但是我希望它显示该行的 rest,而不仅仅是键,有人可以告诉我我该怎么做即,显示排序的每个键的所有数据。

output: output:
1 1
2 2
5 5
7 7
8 8
16 16

What I want to print would be something like:我要打印的内容类似于:

1 Peter 230151515 1 彼得 230151515
2 Luis 2589191919 2 路易斯 2589191919
5 Merrick 25551561 5 梅里克 25551561
7 Lucky 4301616199 7 幸运 4301616199
8 Linda 129616919 8 琳达 129616919
16 Alfredo 25891919 16 阿尔弗雷多 25891919

Someone to help me correct my mistake please.请有人帮我纠正我的错误。

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

struct node {
    int key;
    string name;
    int num;
    struct node *left, *right;
};

vector<node> persons;


struct node *newNode(int item)
{
    struct node *temp =  (struct node *)malloc(sizeof(struct node));
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}

void inorder(struct node *root)
{
    if (root != NULL)
    {
        //cout<<"\t";

        inorder(root->left);
        printf("\t%d\n", root->key);
        inorder(root->right);
    }
}

struct node* insert(struct node* node, int key)
{
    /* If the tree is empty, return a new node */
    if (node == NULL) return newNode(key);

    /* Otherwise, recur down the tree */
    if (key < node->key)
        node->left  = insert(node->left, key);
    else if (key > node->key)
        node->right = insert(node->right, key);

    /* return the (unchanged) node pointer */
    return node;
}

struct node * minValueNode(struct node* node)
{
    struct node* current = node;

    /* loop down to find the leftmost leaf */
    while (current && current->left != NULL)
        current = current->left;

    return current;
}




int main()
{
    struct node *root = NULL;
    ifstream fin("data.txt");
    if (!fin)
    {
        cout << "File not open\n";
        return 1;
    }



    string line;
    const char delim = ',';


    while (getline(fin, line))
    {
        istringstream ss(line);
        node person;
        ss >> person.key; ss.ignore(10, delim);
        getline(ss, person.name,delim);
        ss >> person.num;
        if (ss)
            persons.push_back(person);
    }


   for (unsigned int i=0; i< persons.size(); i++)
   {
         cout << setw(5)  << persons[i].key<< setw(20) << persons[i].name<< setw(15)  << persons[i].num << '\n';

             root = insert(root, persons[i].key);
            insert(root, persons[i].key);


   }


    cout << "\n\nInorder:\n";
//    cout<<node.name;

    inorder(root);

    /*/cout<<"\nDelete 50\n";
    root = deleteNode(root, 50);
    cout<<"Inorder traversal of the modified tree \n";
    inorder(root);/*/


    /*
    insert(root, 80);*/

    return 0;
}

When you are printing out the key , you can also print out the other information of each node .在打印出key时,还可以打印出每个node的其他信息。 Here's a solution using cout :这是使用cout的解决方案:

void inorder(struct node *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        std::cout << root->key << " " << root->name << " " << root->num << "\n";
        inorder(root->right);
    }
}

There were some main problems and some other problems.有一些主要问题和一些其他问题。 The main problems were that you didn't store and print all data.主要问题是您没有存储和打印所有数据。 I did the following:我做了以下事情:

  • Clean up includes清理包括
  • Remove using namespace std; using namespace std;
  • Rename struct node to Node将结构node重命名为Node
  • Add a struct for Person next to the struct for NodeNode的结构旁边添加一个Person的结构
  • Add a constructor for Person to NodePerson的构造函数添加到Node
  • Make insert a method使insert方法
  • Use smart pointers.使用智能指针。 One problem of dynamic memory allocation is that you have to clean up but you didn't动态 memory 分配的一个问题是你必须清理但你没有
#include <iostream>
#include <iomanip>
#include <fstream>
#include <memory>
#include <string>
#include <sstream>
#include <vector>

struct Person {
  int key;
  std::string name;
  int num;
};

struct Node : Person {
  Node(const Person &person) : Person(person) {}
  std::unique_ptr<Node> left, right;
  void insert(const Person &person);
};

void Node::insert(const Person &person) {
  /* recur down the tree */
  if (key > person.key) {
    if (left)
        left->insert(person);
    else
        left = std::make_unique<Node>(person);
  } else if (key < person.key) {
    if (right)
        right->insert(person);
    else
        right = std::make_unique<Node>(person);
  }
}

std::vector<Person> persons;

void inorder(Node *root) {
  if (root) {
    // cout<<"\t";

    inorder(root->left.get());
    std::cout << '\t' << root->key << ' ' << root->name << ' ' << root->num << '\n';
    inorder(root->right.get());
  }
}

Node *minValueNode(Node *node) {
  Node *current = node;

  /* loop down to find the leftmost leaf */
  while (current && current->left) current = current->left.get();

  return current;
}

int main() {
  std::unique_ptr<Node> root;
  std::ifstream fin("data.txt");
  if (!fin) {
    std::cout << "File not open\n";
    return 1;
  }

  std::string line;
  const char delim = ',';

  while (std::getline(fin, line)) {
    std::istringstream ss(line);
    Person person;
    ss >> person.key;
    ss.ignore(10, delim);
    std::getline(ss, person.name, delim);
    ss >> person.num;
    if (ss) persons.push_back(person);
  }

  for (unsigned int i = 0; i < persons.size(); i++) {
    std::cout << std::setw(5) << persons[i].key << std::setw(20)
              << persons[i].name << std::setw(15) << persons[i].num << '\n';
    if (!root) root = std::make_unique<Node>(persons[i]);
    else root->insert(persons[i]);
  }

  std::cout << "\n\nInorder:\n";
  //    cout<<node.name;

  inorder(root.get());

  /*/cout<<"\nDelete 50\n";
  root = deleteNode(root, 50);
  cout<<"Inorder traversal of the modified tree \n";
  inorder(root);/*/

  /*
  insert(root, 80);*/

  return 0;
}

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

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