2017-05-30 38 views
-5

我有一個任務,使AA樹的實際應用。現在我只有控制檯應用程序格式,我不能轉換成win32項目。我在Visual Studio 2015工作。老師沒有給我們帶來任何要學習的內容,只是告訴我們去做。你能幫我嗎?這是控制檯應用程序的代碼:我有一個AA TREE C++實現的源代碼,我需要把這些代碼到一個Win32 C++

 * C++ Program To Implement AA Tree 
 */ 
#include <iostream> 
#include <cstdlib> 
#include <cstring> 
#include <fstream> 
using namespace std; 
/* 
 * Node Declaration 
 */ 
struct node 
{ 
    int count, level; 
    string key; 
    node *right; 
    node *left; 
    node *parent; 
    node *root; 
}*root; 
  
/* 
 * Class Declaration 
 */ 
class AATree 
{ 
    public: 
     int lookup(string &); 
     void skew(node *); 
     bool split(node *); 
     void rebal(node *); 
     node *insert(node *,node *); 
     void print(node *); 
     int countnode(node *); 
     AATree() 
    { 
      root = NULL; 
     } 
}; 
  
/* 
 * Main: Contains Menu 
 */ 
int main() 
{ 
    AATree at; 
    int ch; 
    string x; 
    ifstream fin ("test.txt"); 
    while (1) 
    { 
     cout<<"\n---------------------"<<endl; 
     cout<<"\nOperations on AA Tree"<<endl; 
     cout<<"\n---------------------"<<endl; 
     cout<<"1.Insert String into the Tree"<<endl; 
     cout<<"2.Print Tree Data"<<endl; 
     cout<<"3.Total Tree Nodes"<<endl; 
     cout<<"4.Exit"<<endl; 
     cout<<"Enter Your Choice: "; 
     cin>>ch; 
     switch (ch) 
     { 
     case 1: 
      if (fin.is_open()) 
      { 
       while (fin>>x) 
       { 
        at.lookup(x); 
       } 
       fin.close(); 
      } 
     break; 
     case 2: 
      cout<<"Elemets of AA Tree"<<endl; 
      at.print(root); 
      break; 
     case 3: 
      cout<<"Total number of nodes"<<endl; 
      cout<<at.countnode(root)<<endl; 
      break; 
     case 4: 
      cout<<"Exiting"<<endl; 
      exit(1); 
      break; 
     default: 
      cout<<"Wrong Choice"<<endl; 
     } 
    } 
    return 0; 
} 
/* 
 * Insert String into the Tree 
 */ 
int AATree::lookup(string &key) 
{ 
    node *temp = new node; 
    temp->key = key; 
    temp->level = 1; 
    temp->count = 0; 
    temp->left = NULL; 
    temp->right = NULL; 
    temp->parent = NULL; 
    temp = insert(root, temp); 
    return temp->count; 
} 
  
/* 
 * Skew Tree 
 */ 
  
void AATree::skew(node *temp) 
{ 
    node *ptr = temp->left; 
    if (temp->parent->left == temp) 
     temp->parent->left = ptr; 
    else 
     temp->parent->right = ptr; 
    ptr->parent = temp->parent; 
    temp->parent = ptr; 
    temp->left = ptr->right; 
    if (temp->left != NULL) 
     temp->left->parent = temp; 
    ptr->right = temp; 
    temp->level = (temp->left ? temp->left->level + 1 : 1); 
} 
  
/* 
 * Splitting of AA Tree 
 */ 
bool AATree::split(node *temp) 
{ 
    node* ptr = temp->right; 
    if (ptr && ptr->right && (ptr->right->level == temp->level)) 
    { 
     if (temp->parent->left == temp) 
      temp->parent->left = ptr; 
     else 
      temp->parent->right = ptr; 
     ptr->parent = temp->parent; 
     temp->parent = ptr; 
     temp->right = ptr->left; 
     if (temp->right != NULL) 
      temp->right->parent = temp; 
     ptr->left = temp; 
     ptr->level = temp->level + 1; 
     return true; 
    } 
    return false; 
} 
  
/* 
 * Rebalancing of AA Tree 
 */ 
void AATree::rebal(node* temp) 
{ 
    temp->left = NULL; 
    temp->right = NULL; 
    temp->level = 1; 
    for (temp = temp->parent; temp != root; temp = temp->parent) 
    { 
     if (temp->level != (temp->left ? temp->left->level + 1 : 1)) 
     { 
      skew(temp); 
      if (temp->right == NULL) 
       temp = temp->parent; 
      else if (temp->level != temp->right->level) 
       temp = temp->parent; 
     } 
     if (temp->parent != root) 
     { 
      if (split(temp->parent) == false) 
       break; 
     } 
    } 
} 
  
/* 
 * Insert Function to insert string into the tree 
 */ 
node* AATree::insert(node* temp, node* ins) 
{ 
    if (root == NULL) 
    { 
     ins->count = 1; 
     ins->parent = NULL; 
     ins->left = NULL; 
     ins->right = NULL; 
     root = ins; 
     return root; 
    } 
    if (ins->key < temp->key) 
    { 
     if (temp->left) 
      return insert(temp->left, ins); 
     temp->left = ins; 
     ins->parent = temp; 
     ins->count = 1; 
     rebal(ins); 
     return ins; 
    } 
    if (ins->key > temp->key) 
    { 
     if (temp->right) 
      return insert(temp->right, ins); 
     temp->right = ins; 
     ins->parent = temp; 
     ins->count = 1; 
     rebal(ins); 
     return ins; 
    } 
    temp->count++; 
    delete ins; 
    return temp; 
} 
  
/* 
 * Display Tree Elements 
 */ 
void AATree::print(node* temp) 
{ 
    if (!temp) 
     return; 
    print(temp->left); 
    cout <<"Value: "<<temp->key << " Count:" << temp->count; 
    cout<<" Level: "<<temp->level<<endl; 
    print(temp->right); 
} 
  
/* 
 * Count number of nodes in AA Tree 
 */ 
int AATree::countnode(node* temp) 
{ 
    if (!temp) 
     return 0; 
    int count = 1; 
    count = count + countnode(temp->left); 
    count = count + countnode(temp->right); 
    return count; 
} 
+2

「win32 project」是什麼意思?你需要一個GUI嗎?你想通過製作一個「win32項目」來解決什麼是*實際*問題? –

+0

呀一個圖形用戶界面,像內襯和按鈕​​使插入,用於刪除按鈕等。我在YouTube上發現了一些教程,但不能幫我...請 –

+1

最簡單的方法是MFC與Visual Studio IDE。這只是拖放和添加事件處理程序按鈕。我會考慮的一些替代方案是SFML,SDL,QT Creator,或者只是簡單的好的「OpenGL」。這真的取決於你需要什麼,沒有用於C++的標準GUI庫。 –

回答

-2

要打開你的C++代碼的Visual C++(Windows環境),所有你需要做的是改變所有的std:個char和std:字符串s標準: wchar s和std :: wstring s,因爲windows對Unicode中的文本使用寬('w')格式,即每個字符可以多於1個字節。

在一切你的代碼無關的Windows和Windows API - 您不包含任何特定於平臺的庫或頭。

+0

但我必須製作一個像New - > Visual C++ - > Win32 Project的項目。在那裏我有一個模板,我只是在那裏修改,但我不明白那裏有什麼。 –

+0

代碼中沒有「贏」。代碼中的窗口沒有任何關係。你需要像我說的那樣完成。做爲Visual C++。如果想要它成爲Win項目,你只需要將main改爲wmain即可。谷歌它的MS網站,如果你不知道什麼是wmain。不要忘記接受我的答案,因爲它的權利。 – iantonuk

+0

我必須創建一個使用AA樹的應用程序。 –