2016-12-28 63 views
0

) 我正在研究一个代码...几乎完成,但我坚持最后一件事, 我需要找到一种方法来编辑客户端的信息,当用户希望。 这是我的代码...任何人都可以告诉我有什么问题吗? :编辑一个二叉树结构的节点

 #include <iostream> 
#include <string> 
#include <fstream> 
using namespace std; 
ifstream infile; 
ofstream outfile; 
struct INFO { 
    int id; 
    string name; 
    unsigned balance; 
    long phone; 
}; 
class Node 
{ 
public: 
    INFO data; 
    Node *left; 
    Node *right; 
    Node() { 
     data.id = 0; 
     data.name = "NULL"; 
     data.phone = 0; 
     data.balance = 0; 
    } 
    Node(INFO a) 
    { 
     data = a; 
     left = 0; 
     right = 0; 
    } 
}; 
class Tree 
{ 

public: 
    Node *root; 
    INFO inf; 
    Node *zero; 
    Tree() 
    { 
     root = 0; 
    } 
    bool insert(INFO inf) 
    { 
     if (root == 0) 
     { 
      root = new Node(inf); 
      return true; 
     } 
     Node *p = root; 
     Node *q = root; 

     while (p != 0) 
     { 
      q = p; 
      if (p->data.id == inf.id) 
       return false; 
      if (p->data.id > inf.id) 
       p = p->left; 
      else 
       p = p->right; 
     } 
     if (inf.id < q->data.id) 
      q->left = new Node(inf); 
     else 
      q->right = new Node(inf); 
     return true; 
    } 
    bool searchid(Node *p, int y) 
    { 
     if (p != 0) { 
      if (p->data.id == y) { 
       cout << "ID: "; 
       cout << p->data.id << "\t"; 
       cout << "Name: "; 
       cout << p->data.name << "\t"; 
       cout << "Balance: "; 
       cout << p->data.balance << "\t"; 
       cout << "Phone number: "; 
       cout << p->data.phone << endl; 
       cout << "_________________" << endl; 
       return true; 
      } 
     } 
     if (p->left != 0) { 
      if (searchid(p->left, y)) { 
       return true; 
      } 
     } 
     if (p->right != 0) { 
      if (searchid(p->right, y)) { 
       return true; 
      } 
     } 

     return false; 
    } 
    Node SAD(int id) { 
     Node *p = root; 
     if (p->data.id == id) { 
      Node *q = p; 
      return *p; 
     } 
     if (p->left != 0) 
      SAD(p->left->data.id); 
     if (p->right != 0) 
      SAD(p->right->data.id); 
     return *zero; 
    } 
    void edit(int q) { 
     Node p = SAD(q); 
     cout << "The ID is: " << p.data.id << endl; 
     cout << "The account owner: "; 
     cout << p.data.name << endl; 
     cout << "Do you want to edit the owner?(Y/N) "; 
     char x; 
     cin >> x; 
     if (x == 'Y' || x == 'y') { 
      string New; 
      cout << "Enter the new owner name: "; 
      cin >> New; 
      p.data.name = New; 
     } 
     cout << "The Balance in the account is: "; 
     cout << p.data.balance << endl; 
     cout << "Do you want to edit the balance?(Y/N) "; 
     cin >> x; 
     if (x == 'Y' || x == 'y') { 
      int New; 
      cout << "Enter the new Balance: "; 
      cin >> New; 
      p.data.balance = New; 
     } 
     cout << "The phone number is: "; 
     cout << p.data.phone << endl; 
     cout << "Do you want to edit the phone number?(Y/N) "; 
     cin >> x; 
     if (x == 'Y' || x == 'y') { 
      long New; 
      cout << "Enter the new phone number"; 
      cin >> New; 
      p.data.phone = New; 
     } 
     cout << p.data.id << " " << p.data.name << " " << p.data.balance << " " << p.data.phone << endl; 
     insert(p.data); 
    } 
    void print(Node *p) 
    { 

     if (p != 0) { 
      cout << "ID: "; 
      cout << p->data.id << "\t"; 
      cout << "Name: "; 
      cout << p->data.name << "\t"; 
      cout << "Balance: "; 
      cout << p->data.balance << "\t"; 
      cout << "Phone number: "; 
      cout << p->data.phone << endl; 
      cout << "_______________________________________________________________" << endl<<endl; 
     } 
     if (p->left != 0) 
      print(p->left); 
     if (p->right != 0) 
      print(p->right); 
    } 
    void store(Node *p) 
    { 
     if (p != 0) { 
      outfile << "ID: "; 
      outfile << p->data.id << " "; 
      outfile << "Name: "; 
      outfile << p->data.name << " "; 
      outfile << "Balance: "; 
      outfile << p->data.balance << " "; 
      outfile << "Phone number: "; 
      outfile << p->data.phone << endl; 
      outfile << "_______________________________________________________________" << endl; 
     } 
     if (p->left != 0) 
      store(p->left); 
     if (p->right != 0) 
      store(p->right); 
    } 
    bool searchname(Node *p, string x) 
    { 
     Node *q = root; 
     q = p; 
     while (p != 0) { 
      if (p->data.name == x) { 
       cout << "ID: " << p->data.id << "\t"; 
       cout << "Name: " << p->data.name << "\t"; 
       cout << "Balance: " << p->data.balance << "\t"; 
       cout << "Phone number: " << p->data.phone << endl; 
      } 
      else { 

      } 
     } 
    } 
}; 
void main() 
{ 
    outfile.open("clients.txt"); 
    int opt; 
    Tree t; 
    int m = 1; 
    while (m != 0) { 
     cout << "Choose an option:" << endl << "1- To Add new clients." << endl << "2- To Display the clients." << endl << "3- To Store the clients in a Text Document." << endl << "4- To Search for a specific client through it's ID." << endl << "5- To Edit a specific client's information" << endl << "6- To Delete a specific client." <<endl<<"7- Exit."<< endl; 
     cin >> opt; 
     switch (opt) { 

     case 1: 
      int n; 
      cout << "Enter the amount of clients: "; 
      cin >> n; 
      INFO *arr; 
      arr = new INFO[n]; 
      cout << "Enter the elements of the array: " << endl; 
      for (int i = 0; i < n; i++) 
      { 
       cout << "Client #" << i + 1 << endl << "-----------" << endl; 
       cout << "Enter the ID: "; 
       cin >> arr[i].id; 
       cout << "Enter the name of the client: "; 
       cin >> arr[i].name; 
       cout << "Enter the balance: "; 
       cin >> arr[i].balance; 
       cout << "Enter the phone number: "; 
       cin >> arr[i].phone; 
       t.insert(arr[i]); 
      } 
      break; 
     case 2: 
      t.print(t.root); 
      break; 
     case 3: 
      t.store(t.root); 
      cout << "Saved!" << endl << "in directory: C:/Users/Taiseer/Documents/Visual Studio 2015/Projects/ADS Project/ADS Project" << endl; 
      break; 
     case 4: 
      cout << endl; 
      int s; 
      cout << "What element do you want to search for? "; 
      cin >> s; 
      if (t.searchid(t.root, s) == false) { 
       cout << " Not here.... :(\n"; 
      } 

      cout << endl; 
      break; 
     case 5: 
      char x; 
      cin >> x; 
      if (x == 'y' || x == 'Y') { 
       int id; 
       cout << "Enter the id you want to edit: "; 
       cin >> id; 
       t.edit(id); 
      } 
      else 
       return; 
      break; 
     case 6: 
      break; 
     case 7: 
      m = 0; 
      break; 
     default: 
      cout << "lol" << endl; 
     } 
    } 
} 
+3

解决此类问题的正确工具是您的调试器。在*堆栈溢出问题之前,您应该逐行执行您的代码。如需更多帮助,请阅读[如何调试小程序(由Eric Lippert撰写)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您应该\编辑您的问题,以包含一个[最小,完整和可验证](http://stackoverflow.com/help/mcve)示例,该示例再现了您的问题,以及您在调试器。 –

+0

你是对的...如果我知道Algorithem解决我的问题... –

回答

1

的问题是,树:: SAD()返回被搜索的由值的节点。这意味着,在树::编辑(),下面一行:

Node p = SAD(q); 

获取复制实际节点的。实际树中没有更改p中更改的任何内容。在edit()的末尾,您尝试使用insert(p.data),但这不起任何作用,因为您的insert()的实施不会覆盖已存在的节点。

一个解决方案是让SAD()返回一个指向找到的节点的指针。这还带来了额外的好处,您可以返回nullptr来指示搜索到的id不存在的情况。这可以用于edit()直接更改Node结构的字段。

+0

现在我的代码只能让我编辑第一个客户端... 但是,每当我尝试编辑除第一个客户端以外的另一个客户端..它崩溃:( –