【问题标题】:I have an AA TREE c++ implementation source code and I need to put that code into a Win32 c++ [closed]我有一个 AA TREE c++ 实现源代码,我需要将该代码放入 Win32 c++ [关闭]
【发布时间】:2017-05-30 01:21:14
【问题描述】:

我有一个任务要真正应用 AA 树。目前我只有控制台应用程序格式,无法转换为 win32 项目。我在visual studio 2015工作。老师没有给我们带来任何学习的内容,只是告诉我们去做。请你帮帮我好吗?这是控制台应用程序的代码:

 * 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 项目”是什么意思?你需要图形用户界面吗?您想通过制作“win32 项目”来解决的实际问题是什么?
  • 是的,一个图形用户界面,例如插入和插入按钮、删除按钮等。我在 youtube 上找到了一些教程,但无法帮助我...请
  • 最简单的方法是使用 Visual Studio IDE 的 MFC。这只是拖放并将事件处理程序添加到按钮。我会研究的一些替代方案是 SFML、SDL、QT Creator,或者只是普通的 OpenGL。这真的取决于你需要什么,c++ 没有标准的 GUI 库。
  • 我如何向按钮添加事件处理程序?我什至如何添加按钮?或者它被称为坏运气,因为我在 YouTube 或 Google 上找不到可以帮助我的东西,或者我接受了不好的成绩..
  • 等等,你的老师告诉你把你的控制台程序转换成一个 GUI 程序,而不向你介绍任何 GUI 概念?本课程是否需要 GUI 课程? GUI 不是小事,你可以在没有任何事先介绍的情况下扔给学生,将控制台应用程序转换为 GUI 应用程序也不是一件小事,因为必须完全更改事件处理。最重要的是,您的老师是否要求您使用特定的 GUI 技术?如果是这样,他们应该先教它......

标签: c++ winapi tree


【解决方案1】:

要将您的 C++ 代码转换为 Visual C++(Windows 环境),您只需将所有 std:char 和 std:string 更改为 std:wchar 和 std::wstring ,因为 Windows 使用Unicode 中文本的宽('w')格式,即每个字符可以有超过 1 个字节。

在所有其他方面,您的代码与 Windows 和 Windows api 无关 - 您不包含任何特定于平台的库或标头。

【讨论】:

  • 但我必须创建一个像 New -> Visual C++ -> Win32 Project 这样的项目。我有一个模板,我只是在那里修改,但我不明白那里有什么。
  • 您的代码中没有“Win”。您的代码中与窗口无关。你需要完全按照我说的去做。像 Visual C++ 那样做。如果想让它成为 Win 项目,您只需将 main 更改为 wmain。如果您不知道 wmain 是什么,请在 MS 网站上用谷歌搜索它。并且不要忘记接受我的回答,因为它是正确的。
  • 我必须创建一个使用 AA 树的应用程序。
  • @bedbad 首先,πάντα ῥεῖ 是对的;由于历史原因,Microsoft 提供了大多数 API 函数的“ANSI”和“Unicode”版本。虽然很痛苦,但手动或使用 UTF-8 完成所有操作是完全可能的,因此项目设置告诉头文件使函数默认为 Unicode 而不是 ANSI。 MSDN 有更多关于这个问题的信息。而对于“没有问号”,仅仅因为它没有问号并不意味着它不是问题;这是基本逻辑。至于“你必须完全按照我说的那样”,你没有说任何关于 GUI 编程的事情。
  • @andlabs “仅仅因为它没有问号并不意味着它不是问题” 不,这正是定义的含义。 “首先,πάντα ῥεῖ 是对的;出于历史原因,Microsoft 提供了大部分 API 函数的“ANSI”和“Unicode”版本”msdn.microsoft.com/en-us/library/windows/desktop/… 你们都不知道自己在说什么。在那里找到一个 CHAR 类型并证明我错了。项目属性与文本无关。他们改变了 char 的宽度——在 L"string" 中用 L 控制的东西
猜你喜欢
  • 1970-01-01
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 2015-03-20
相关资源
最近更新 更多