以图形方式表示C++代码
Represent C++ code graphically
我已经为表达式树的图形着色编写了一个C++代码(这是我项目模拟的一部分,与寄存器分配有关)。
我需要用图形来展示它。我的代码采用中缀表达式并创建一个表达式树,然后生成它的邻接矩阵并对其进行着色。
对于任何给定的表达式,我需要以图形方式显示为图像中的显示(未上传)。
请给我一些可以使用这个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 fulln" ;
else
{
top++ ;
stack[top] = c ;
}
}
char infix :: pop( )
{
if ( top == -1 )
{
cout << "nStack is emptyn" ;
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 = ' ' ;
}
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<<"nBFSn";
bfs(root1[0]);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
adj[i][j]=0;
}
}
adjgen(root1[0]);
cout<<"adjagenn";
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<<"nNODEtCOLORn";
show_color(root1[0],x);
// for(i=0;i<n;i++) //displaying color of each vertex
//cout<<"nVertex["<<i<<"] : "<<x[i];
}
图形必须类似于:
一/\=+/\b c
它应该用图形表示,并按照上述程序进行着色。节点应为圆形
如果您想要一个GUI,请查找一些库,例如SDL和SDL_gfx来对其进行排序。或者,尝试windows.h
附带的windows API。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值