我有一個任務,使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;
}
「win32 project」是什麼意思?你需要一個GUI嗎?你想通過製作一個「win32項目」來解決什麼是*實際*問題? –
呀一個圖形用戶界面,像內襯和按鈕使插入,用於刪除按鈕等。我在YouTube上發現了一些教程,但不能幫我...請 –
最簡單的方法是MFC與Visual Studio IDE。這只是拖放和添加事件處理程序按鈕。我會考慮的一些替代方案是SFML,SDL,QT Creator,或者只是簡單的好的「OpenGL」。這真的取決於你需要什麼,沒有用於C++的標準GUI庫。 –