实验6 说明文档
- 完成以下实验题目:
第七章:树和二叉树 ,实验题8
1.首先需要建树
2.需要计算求值
1.1步骤 首先需要构建足够大的数组存放用户输入的值,此时的值为char类型得到array数组(设置Maxsize为100,得到array[Maxsize]
1.2步骤 将array[Maxsize]分为两部分,一部分为存粹的数字,一部分为存粹的符号得到数字figure[Maxsize]和symbol[Maxsize](由于此时数字大小为0到9,不需要另外转换,也因此可以设置这种类型的二叉树节点,否则需要在此时的二叉树节点上加上int 类型
1.3步骤 分别得到figure数组大小figureptr和symbol数组大小symbolptr
1.4步骤 设置二叉树节点,为一个char word,两个左右指针
1.5步骤 设置Node*stack设置栈并且得到栈指针,将stackptr设置为0
1.6步骤 此时写apply函数,该函数为主要内容
1.7步骤 在apply函数中采用while循环读取符号和数字,假如符号和数字数组即figureptr==-1&&symbolptr==-1说明已经全部完成就退出循环
1.8步骤 主要思路
我们想的是观察算式,得出应该首先将symbol数组最后的值从后向前读取,假如是*或者/则在figure数组中取两个值组成一个三个节点的二叉树。然后将此时符号打头的节点地址进栈。
那么symbolptr--,figureptr-2后会发生什么呢?
此时符号可能碰到+,-
假如是加减法,那么我们就申请一个Node节点用来存储+,-符号,并且将上一次进栈的值出栈,和此时的Node节点构成一个全新的二叉树,然后将新的head节点进栈。
(但是此时有一个问题,就是连乘或者连除会发生什么)
比如2*3*4
那么必须先判断此时是不是连乘除情况,假如是的话,就申请一个空间,并且将原本已经进栈的值出栈,然后构成一个新的二叉树在进栈。
最后还有一种情况,就是假如2+3怎么办呢
此时我们需要考虑这个+符号后面是不是*或者/,假如是的话就按照之前的做法,假如不是的话,那么我们就要自己构建三个新节点的二叉树了。
1.9步骤 成功构建了几个二叉树,并且将这些二叉树的头head放进了栈里面,这时候我们要考虑怎么将它组合起来。
1.10步骤 之前我们构建二叉树的时候留下了最后三种可能
一是2*3*4+1,对于这种情况我们需要特殊判断symbol[0]是不是*或者/
二是 2+3*4-1对于这种情况我们留下了一个2
三是2*3+4 对于这种情况我们什么都没留下(但是前面head已经记录下了2*3二叉树的头
1.11步骤 在isempty的循环中我们将二叉树组合起来,最终用inorder(中序表达式)将其输出
2.1步骤 我们得到了一个二叉树
2.2步骤 我们想到先序遍历,采用递归的方式,如果节点的左右子树都为NULL那么就返回word-‘0’的值(这点很重要)
2.3步骤 如果不是我们就依据此节点的符号,返回左右子树做*/+-后的值
2.4步骤 最终得到一个double值打印
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#define Maxsize 100
typedef struct PNode{
char word;
PNode*l;
PNode*r;
}Node;
void pushstack(Node*p,Node*stack[],int &stackptr)
{
stackptr++;
stack[stackptr]=p;
}
void popstack(Node*&e,Node*stack[],int &stackptr)
{
e=stack[stackptr];
stackptr--;
}
void gettopstack(Node*&e,Node*stack[],int &stackptr)
{
e=stack[stackptr];
}
bool isempty(int stackptr)
{
if(stackptr==-1)
return 1;
else
return 0;
}
void preorder(Node*head)
{
if(head!=NULL){
printf("%c ",head->word);
preorder(head->l);
preorder(head->r);
}
}
void inorder(Node*head)
{
if(head!=NULL){
inorder(head->l);
printf("%c ",head->word);
inorder(head->r);
}
}
void init(char str[],char symbol[],char figure[],int mount,int &j,int &k)
{
int i=0;
for(;i<mount;i++)
{
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
{
symbol[j]=str[i];
j++;
}
else
{
figure[k]=str[i];
k++;
}
}
for(int i=0;i<j;i++)
{
printf("%c ",symbol[i]);
}
printf("\n");
for(int i=0;i<k;i++)
{
printf("%c ",figure[i]);
}
printf("\n");
}
Node* apply(char symbol[],char figure[],Node*stack[],int &symbolptr,int &figureptr,int &stackptr)
{
Node*head=NULL;
figureptr--;
symbolptr--;
stackptr--;
int a=symbolptr;
printf("%d \n",figureptr);
while(figureptr!=-1&&symbolptr!=-1)
{
if(symbol[symbolptr]=='*'||symbol[symbolptr]=='/')
{
if((symbol[symbolptr+1]=='*'||symbol[symbolptr+1]=='/'))
{
Node*temp=(Node*)malloc(sizeof(Node));
temp->word=symbol[symbolptr];
symbolptr--;
popstack(head,stack,stackptr);
temp->r=head;
Node*ltemp=(Node*)malloc(sizeof(Node));
ltemp->word=figure[figureptr];
figureptr--;
ltemp->l=NULL;
ltemp->r=NULL;
temp->l=ltemp;
head=temp;
pushstack(head,stack,stackptr);
}
else{
Node*temp=(Node*)malloc(sizeof(Node));
temp->word=symbol[symbolptr];
symbolptr--;
Node*ltemp=(Node*)malloc(sizeof(Node));
Node*rtemp=(Node*)malloc(sizeof(Node));
rtemp->word=figure[figureptr];
rtemp->l=NULL;
rtemp->r=NULL;
figureptr--;
ltemp->word=figure[figureptr];
ltemp->l=NULL;
ltemp->r=NULL;
figureptr--;
temp->r=rtemp;
temp->l=ltemp;
head=temp;
pushstack(head,stack,stackptr);
}
}
else if(symbol[symbolptr]=='+'||symbol[symbolptr]=='-')
{
if(symbol[symbolptr+1]=='*'||symbol[symbolptr+1]=='/')
{
popstack(head,stack,stackptr);
Node*temp1=(Node*)malloc(sizeof(Node));
temp1->word=symbol[symbolptr];
temp1->r=head;
temp1->l=NULL;
head=temp1;
pushstack(head,stack,stackptr);
symbolptr--;
}
else
{
Node*temp2=(Node*)malloc(sizeof(Node));
temp2->word=symbol[symbolptr];
Node*rtemp2=(Node*)malloc(sizeof(Node));
rtemp2->word=figure[figureptr];
figureptr--;
rtemp2->l=NULL;
rtemp2->r=NULL;
temp2->r=rtemp2;
head=temp2;
pushstack(head,stack,stackptr);
symbolptr--;
}
}
for(int i=0;i<=stackptr;i++)
{
printf("%c ",(stack[i])->word);
}
printf("%d %d",figureptr,symbolptr);
printf(" %c ",head->word);
printf("\n");
}
{
if(symbol[0]=='*'||symbol[0]=='/')
{
popstack(head,stack,stackptr);
}
Node*p=NULL;
if(figureptr==-1&&symbolptr==-1)
{
while(!isempty(stackptr))
{
popstack(p,stack,stackptr);
p->l=head;
head=p;
}
}
else
{
Node*temp3=(Node*)malloc(sizeof(Node));
temp3->l=NULL;
temp3->r=NULL;
temp3->word=figure[figureptr];
figureptr--;
head=temp3;
while(!isempty(stackptr))
{
popstack(p,stack,stackptr);
p->l=head;
head=p;
inorder(head);
printf("\n");
}
}
}
printf("\n");
return head;
}
void buildtree(char str[],int mount,Node*&head)
{
Node*stack[mount];
char symbol[mount]={0};
char figure[mount]={0};
int j=0;
int k=0;
init(str,symbol,figure,mount,j,k);
int strptr=mount;
int symbolptr=j;
int figureptr=k;
int stackptr=0;
printf("hh");
head=apply(symbol,figure,stack,symbolptr,figureptr,stackptr);
}
double call(Node*head)
{
if(head==NULL)
return 0;
if(head->r==NULL&&head->l==NULL)
return (head->word-'0');
if(head->word=='*')
return (call(head->l)*call(head->r));
if(head->word=='/')
return (call(head->l)/call(head->r));
if(head->word=='+')
return (call(head->l)+call(head->r));
if(head->word=='-')
return (call(head->l)-call(head->r));
}
int main()
{
char array[Maxsize];
scanf("%s",array);
int mount=strlen(array);
Node*head=NULL;
buildtree(array,mount,head);
printf("先序遍历:");
preorder(head);
double count=call(head);
printf("\n%f",count);
}