古驰最新手表女士图片:这是我的一个小型科学计算器的代码.哪位高手帮忙看看是哪里有问题!

来源:百度文库 编辑:高考问答 时间:2024/04/26 08:58:09
我的代码是:
#include<stdio.h>
#include<stdlib.h>
union yuansu{
float shuzi;//存放数字
char fuhao;//存放符号
};
typedef struct node{
int biaoshi;//标志号
union yuansu data;//元素
struct node *lchild,*rchild;
}jd;//节点
typedef struct stacknode{
float data;
struct stacknode *next;
}stacknode;
typedef struct{
stacknode *top;
}linkstack;
void initial(linkstack *s){
s->top=NULL;
}
int isempty(linkstack *s){
return s->top==NULL;
}
void push(linkstack *s,float x){
stacknode *p=(stacknode *)malloc(sizeof(stacknode));
p->data=x;
p->next=s->top;
s->top=p;
}
float pop(linkstack *s){
float x;
stacknode *p=s->top;
if(isempty(s)){
printf("栈为空");
exit(1);
}
x=p->data;
s->top=p->next;
free(p);
return x;
}
jd *crt_bt_pre(jd *bt)//前序输入
{ int a;
float num;
char ch;
loop:printf("请输入标志符,用来标志是数字还是符号,0--数字,1--符号");
scanf("%d",&a);
if(a==0){
printf("数字=");
scanf("%d",&num);
getchar();
}
else if(a==1){
printf("符号=");
scanf("%c",&ch);
getchar();
if(ch=='('||ch==')')
goto loop;
}
if(a!=0&&a!=1) bt=NULL;
else
{ bt=(jd *)malloc(sizeof(jd));

if(a==0)
bt->data.shuzi=num;
else
bt->data.fuhao=ch;
bt->biaoshi=a;
bt->lchild=crt_bt_pre(bt->lchild);
bt->rchild=crt_bt_pre(bt->rchild);
}
return bt;
}
void jisuan(linkstack &s,float a,float b,char ch){
switch(ch){
case'+':push(&s,a+b);break;
case'-':push(&s,a-b);break;
case'*':push(&s,a*b);break;
case'/':if(b==0){
printf("分母不可以是0");
exit(0);
}
else
push(&s,a-b);
}
}
void inorder(linkstack &s,jd *p){//后序输出
float a,b;
if(p!=NULL){
inorder(s,p->lchild);
inorder(s,p->rchild);
if(p->biaoshi==0)
push(&s,p->data.shuzi);
else{
b=pop(&s);
a=pop(&s);
jisuan(s,a,b,p->data.fuhao);
}
}
}
void main(){
jd *head=NULL;
float end;
linkstack s;
initial(&s);
head=crt_bt_pre(head);
inorder(s,head);
end=pop(&s);
printf("计算结果是:");
printf("%f",end);

}
我用的是前序建立一个二叉树,最好是用中序建立,因为那样才更容易输入参数.请高手认真看下,问老师问题太难了(常常找不到她);拜托拉