Show full line of a CSV file C++

感情迁移 提交于 2020-06-17 13:07:29

问题


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:

1, Peter, 230151515
5, Merrick, 25551561
7, Lucky, 4301616199
2, luis, 2589191919
16, Alfredo, 25891919
8, Linda, 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.

output:
1
2
5
7
8
16

What I want to print would be something like:

1 Peter 230151515
2 Luis 2589191919
5 Merrick 25551561
7 Lucky 4301616199
8 Linda 129616919
16 Alfredo 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;
}


回答1:


When you are printing out the key, you can also print out the other information of each node. Here's a solution using cout:

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



回答2:


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;
  • Rename struct node to Node
  • Add a struct for Person next to the struct for Node
  • Add a constructor for Person to Node
  • Make insert a method
  • Use smart pointers. One problem of dynamic memory allocation is that you have to clean up but you didn't
#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;
}


来源:https://stackoverflow.com/questions/62121119/show-full-line-of-a-csv-file-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!