以图形方式表示C ++代码

时间:2015-03-19 22:48:38

标签: c++ tree graph-coloring

我已经为表达式树的图形着色编写了一个C ++代码(这是我的项目模拟的一部分,与寄存器分配有关)。

我需要以图形方式显示它。我的代码采用中缀表达式并创建表达式树,然后生成它的邻接矩阵并为其着色。

对于任何给定的表达式,我需要以图形方式显示在图像{not getting upload>中。

请建议我使用这种C ++代码的方法。

这是我的代码:

#include <iostream>
#include <string.h>
#include <ctype.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;
struct node{
    char data;
    int num;
    node *left;
    node *right;
};
int top1=-1;
node *arr\[35\],*root1\[35\];
int x\[50\],n,tot_colors=0,c=0;
int adj\[20\]\[20\];
class infix
{
    private :
        char target\[50\], stack\[50\] ;
        char *s, *t ;
        int top ;
    public :
        infix( ) ;
        void setexpr ( char *str ) ;
        void push ( char c ) ;
        char pop( ) ;
        void convert( ) ;
        int priority ( char c ) ;
        char* show( ) ;
        friend string infixToPostfix(char exp\[\]);
} ;
infix :: infix( )
{
    top = -1 ;
    strcpy ( target, "" ) ;
    strcpy ( stack, "" ) ;
    t = target ;
    s = ""  ;
}
void infix :: setexpr ( char *str )
{
    s = str ;
}
void infix :: push ( char c )
{
    if ( top == 50 )
        cout << "\nStack is full\n" ;
    else
    {
        top++ ;
        stack\[top\] = c ;
    }
}
char infix :: pop( )
{
    if ( top == -1 )
    {
        cout << "\nStack is empty\n" ;
        return -1 ;
    }
    else
    {
        char item = stack\[top\] ;
        top-- ;
        return item ;
    }
}
void infix :: convert( )
{
    while ( *s )
    {
        if ( *s == ' ' || *s == '\t' )
        {
            s++ ;
            continue ;
        }
        if ( isdigit ( *s ) || isalpha ( *s ) )
        {
            while ( isdigit ( *s ) || isalpha ( *s ) )
            {
                *t = *s ;
                s++ ;
                t++ ;
            }
        }
        if ( *s == '(' )
        {
            push ( *s ) ;
            s++ ;
        }
        char opr ;
        if ( *s == '*' || *s == '+' || *s == '/' || *s == '%' || *s == '-' || *s == '$' || *s=='=' )
        {
            if ( top != -1 )
            {
                opr = pop( ) ;
                while ( priority ( opr ) >= priority ( *s ) )
                {
                    *t = opr ;
                    t++ ;
                    opr = pop( ) ;
                }
                push ( opr ) ;
                push ( *s ) ;
            }
            else
                push ( *s ) ;
            s++ ;
        }
        if ( *s == ')' )
        {
            opr = pop( ) ;
            while ( ( opr ) != '(' )
            {
                *t = opr ;
                t++ ;
                opr =  pop( ) ;
            }
            s++ ;
        }
    }
    while ( top != -1 )
    {
        char opr = pop( ) ;
        *t = opr ;
        t++ ;
    }
    *t = '\0' ;
}
int infix :: priority ( char c )
{   if(c=='=')
        return -1;
    if ( c == '$' )
        return 3 ;
    if ( c == '*' || c == '/' || c == '%' )
        return 2 ;
    else
    {
        if ( c == '+' || c == '-' )
            return 1 ;
        else
            return 0 ;
    }
}
char* infix :: show( )
{
    return target ;
}
string infixToPostfix(char exp\[\])
{
    infix q;
    q.setexpr ( exp ) ;
    q.convert( ) ;
    string a=q.show();
    return a ;

}
//----------------------------------------------------------------------------------------------------------------------
int  r(char inputchar) //for checking symbol is operand or operator for tree
{
      if(inputchar=='+' || inputchar=='-' || inputchar=='*' || inputchar=='/' || inputchar=='='|| inputchar=='$')
      return(-1);
     else if(inputchar>='a' || inputchar<='z')
      return(1);
     else if(inputchar>='A' || inputchar<='Z')
      return(1);
     else
    return(-99);  //for error
}
//it is used for inserting an single element in a tree, i.e. is pushing of single element. FOR TREE
void push1(node *tree){
    top1++;
    arr\[top1\]=tree;
}

node *pop1(){
    top1--;
    return(arr\[top1+1\]);
}
//FOR TREE
void create_expr_tree(string suffix)
{
    char symbol;
    node *newl,*ptr1,*ptr2;
    int flag; //flag=-1 when operator and flag=1 when operand
    symbol = suffix\[0\]; //Read the first symbol from the postfix expr.
    for(int i=1;symbol!=NULL;i++)
    {   //continue till end of the expr.
        flag=r(symbol); //check symbol is operand or operator.
        if(flag == 1)   //if symbol is operand.
        {
            newl = new node;
            //new1->num=0;
            newl->data = symbol;
            newl->left = NULL;
            newl->right = NULL;
            push1(newl);
        }
        else{    //If the symbol is operator//pop two top elements.
            ptr1=pop1();
            ptr2=pop1();
            newl = new node;
            newl->data = symbol;
            newl->left = ptr2;
            newl->right = ptr1;
            push1(newl);
        }
        symbol=suffix\[i\];
    }
}

void inOrder(node *tree){
    if( tree!=NULL){
        inOrder( tree->left);
        cout<< tree->data;
        inOrder(tree->right);
    }
}
node* postfixToTree(string postfix)
{
    create_expr_tree(postfix);
    return arr\[0\];
}
//----------------------------------------------------------------------------------------------------/+

void bfs(node *root)
{
    if(c<n)
    {

        root->num=c++;
        cout<<root->data<<"\t"<<root->num<<"\n";
        if(root->left!=NULL)
        {
            bfs(root->left);
        }
        if(root->right!=NULL)
        {
            bfs(root->right);
        }
    }
}
void adjgen(node *root)
{
    node *n1,*n2;
    n1=root->left;
    n2=root->right;
    if(root->left!=NULL)
    {   adj\[root->num\]\[n1->num\]=1;
        adj\[n1->num\]\[root->num\]=1;
        adjgen(root->left);
    }
    if(root->right!=NULL)
    {   adj\[root->num\]\[n2->num\]=1;
        adj\[n2->num\]\[root->num\]=1;
        adjgen(root->right);
    }
}
//-------------------------------------------------gc
int graph_color(int k)
{
   int i;
   x\[k\]=1;  //coloring vertex with color1
   for(i=0;i<k;i++){ //checking all k-1 vertices-backtracking
     if(adj\[i\]\[k\]!=0 && x\[k\]==x\[i\])  //if connected and has same color
       {    x\[k\]=x\[i\]+1;//assign higher color than x\[i\]
            if(x\[k\]>tot_colors)
            {
                cout<<"\nThe given graph is not "<<tot_colors<<"-colorable!!!";
                exit(0);
            }
   }
}
}
int o=0;
void show_color(node *root,int a\[\])
{       cout<<root->data<<"\t"<<a\[o++\]<<"\n";
        if(root->left!=NULL)
        {
            show_color(root->left,a);
        }
        if(root->right!=NULL)
        {
            show_color(root->right,a);
        }

}
int main( )
{
    char expr\[50\];
    int i,j;
    //string p1;
    cout << "\nEnter an expression in infix form: " ;
    cin>>expr ;
    string p1=infixToPostfix(expr);
    n=p1.size();
    cout << "\nThe postfix expression is: " <<p1<<endl;
    root1\[0\]=postfixToTree(p1);
    cout<<"\nIn-Order Traversal :   ";
    inOrder(root1\[0\]);
    cout<<"\nBFS\n";
    bfs(root1\[0\]);
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            adj\[i\]\[j\]=0;
        }

    }
    adjgen(root1\[0\]);
    cout<<"adjagen\n";
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            cout<<adj\[i\]\[j\];
        }
        cout<<"\n";
    }
//-----------------------------------------------------GC
    cout<<"\nEnter the no. of colors available(no. of registers available): ";
    cin>>tot_colors;
    int fl=1;
    for(i=0;i<n;i++)
        graph_color(i);
    cout<<"\nNODE\tCOLOR\n";
    show_color(root1\[0\],x);
  //  for(i=0;i<n;i++)  //displaying color of each vertex
    //cout<<"\nVertex\["<<i<<"\] : "<<x\[i\];
}

图表必须如下:

         a
       /  \
      =    +
          / \
         b   c

应该根据上述程序用图形表示。节点应该是圆的

1 个答案:

答案 0 :(得分:0)

如果您需要GUI,请留意某些库,例如SDL和SDL_gfx将其排序。或者,尝试使用windows.h附带的Windows API。