⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档.txt

📁 这是数据结构的四则运算的c语言程序
💻 TXT
字号:
                      #include<stdio.h>
struct Bitree
{
int judge;
union operater
  {
  int data;
  char op;
  }key;
struct Bitree *lchild;
struct Bitree *rchild;
};
struct Bitree *insert(struct Bitree *Root,struct Bitree *q,struct Bitree *p)
{
char x;
struct Bitree *p1;
scanf("%c",&x);
if(x=='\n')return(Root);
switch(x)
  {
  case '+':p=(struct Bitree*)malloc(sizeof(struct Bitree));p->key.op=x;p->lchild=Root;p->rchild=NULL;p->judge=1;Root=p;break;
  case '-':p=(struct Bitree*)malloc(sizeof(struct Bitree));p->key.op=x;p->lchild=Root;p->rchild=NULL;p->judge=1;Root=p;break;
  case '*':
          if(Root->lchild==NULL||Root->key.op=='*'||Root->key.op=='/'){p=(struct Bitree*)malloc(sizeof(struct Bitree));p->key.op=x;p->lchild=Root;p->rchild=NULL;Root=p;}
          else if(q->key.op=='*'||q->key.op=='/'){p=(struct Bitree*)malloc(sizeof(struct Bitree));p->key.op=x;p->lchild=q;p->rchild=NULL;Root->rchild=p;}
          else {p1=(struct Bitree*)malloc(sizeof(struct Bitree));p1=p;p=(struct Bitree*)malloc(sizeof(struct Bitree));p->key.op=x;p->lchild=p1;p->rchild=NULL;q->rchild=p;}
          p->judge=1;break;
  case '/':
          if(Root->lchild==NULL||Root->key.op=='*'||Root->key.op=='/'){p=(struct Bitree*)malloc(sizeof(struct Bitree));p->key.op=x;p->lchild=Root;p->rchild=NULL;Root=p;}
          else if(q->key.op=='*'||q->key.op=='/'){p=(struct Bitree*)malloc(sizeof(struct Bitree));p->key.op=x;p->lchild=q;p->rchild=NULL;Root->rchild=p;}
          else {p1=(struct Bitree*)malloc(sizeof(struct Bitree));p1=p;p=(struct Bitree*)malloc(sizeof(struct Bitree));p->key.op=x;p->lchild=p1;p->rchild=NULL;q->rchild=p;}
          p->judge=1;break;
  default:if(p->judge!=0&&Root!=NULL){p1=(struct Bitree*)malloc(sizeof(struct Bitree));p1->key.data=x-48;p1->judge=0;p1->lchild=NULL;p1->rchild=NULL;p->rchild=p1;
        q=(struct Bitree*)malloc(sizeof(struct Bitree));q=p;p=p->rchild;}
        else if(Root==NULL){p=Root=(struct Bitree*)malloc(sizeof(struct Bitree));Root->key.data=x-48;Root->lchild=NULL;Root->rchild=NULL;Root->judge=0;p=Root;}
        else if(p->judge==0&&Root!=NULL)
              {p->key.data=p->key.data*10+x-48;p->judge=0;}
  }
Root=insert(Root,q,p);
}

void display(struct Bitree *s)
{
if(s!=NULL)
  {
  display(s->lchild);
  if(s->judge==1)printf("%c",s->key.op);
  if(s->judge==0)printf("%d",s->key.data);
  display(s->rchild);
  }
}

int result(struct Bitree *s)
{
int op1,op2;
if(s->judge==0)return(s->key.data);
else
  {
  op1=result(s->lchild);
  op2=result(s->rchild);
  return cal(op1,op2,s->key.op);
  }
}

int cal(int op1,int op2,char op)
{
switch(op)
  {
  case '+':return(op1+op2);
  case '-':return(op1-op2);
  case '*':return(op1*op2);
  case '/':return(op1/op2);
  }
}

main()
{
struct Bitree *Root;
int sel;
while(1)
  {
  Root=(struct Bitree *)malloc(sizeof(struct Bitree));
  Root=NULL;
  clrscr();
  printf("***********************************************************************");
  printf("\nInput a expression:");
  Root=insert(Root,Root,Root);
  printf("\nThe result :");
  display(Root);
  printf("=%d",result(Root));
  printf("\n\n...Would you like to continue(y/n)...\n");
  printf("***********************************************************************");
  sel=getch();
  if(sel=='n')exit(1);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -