输入个数据后回车就说是某内存不能为读 #include <stdlib.h> #include <stdio.h>
typedef struct t_node { char data; struct t_node *left; struct t_node *right; }t_node;
typedef struct t_node *Tree;
Tree ReadExpr(Tree t) { char c; int d; t=(Tree)malloc(sizeof(t_node)); printf("input a letter of the Express\n"); scanf("%c",&c); getchar(); d=(int)c; t->data=(char)c; if(d==42||d==43||d==45||d==47||d==94) { t->left=ReadExpr(t->left); t->right=ReadExpr(t->right); } else {t->left=NULL; t->right=NULL;} return t; }
WriteExp(Tree t) { if(t==NULL) return 0; else { if(((t->left->data=='-')||(t->left->data=='+')) &&((t->data=='*')||(t->data=='/')||(t->data=='^')) &&(t->left!=NULL)) { printf("("); WriteExp(t->left); printf(")"); } else WriteExp(t->left); printf("%c",t->data); if(((t->right->data=='+')||(t->right->data=='-')) &&((t->data=='*')||(t->data=='/')||(t->data=='^')) &&(t->right!=NULL)) { printf("("); WriteExp(t->right); printf(")"); } else WriteExp(t->right); } return 1; }
Tree Assign(Tree t,char v,char c) { if(t==NULL) return NULL; if(t->data==v) t->data=c; Assign(t->left,v,c); Assign(t->right,v,c); return t; }
Tree AllAssign(Tree t,int m) { int i; char v,c; if(t==NULL) return NULL; for(i=0;i<m;i++) { printf("which letter do you want to assign?\n"); scanf("%c",&v); getchar(); printf("assign t"); scanf("%c",&c); getchar(); t=Assign(t,v,c); } return t; }
int Power(int oper1,int oper2) { int i,f=oper1; if(oper2==0) return 1; if(oper2==1) return f; for(i=1;i<oper2;i++) f*=oper1; return f; }
get_Value(char d,int oper1,int oper2) { switch(d) { case '+':return(oper1+oper2); case '-':return(oper1-oper2); case '*':return(oper1*oper2); case '/':return(oper1/oper2); case '^':return(Power(oper1,oper2)); } }
int Value(Tree t) { int oper1=0; int oper2=0; if(t==NULL) return NULL; else { if(t->left==NULL&&t->right==NULL) { if(t->data>47&&t->data<58) return(t->data-48); else return(0); } else{ oper1=Value(t->left); oper2=Value(t->right); return get_Value(t->data,oper1,oper2); } } }
Tree CompoundExpr(char e,Tree t1,Tree t2) { Tree t; t=(Tree)malloc(sizeof(t_node)); t->data=e; t->left=t1; t->right=t2; return t; }
Operate(Tree t) { int m,cal; printf("how many letter do you want to assign?\n"); /*表达式变量赋值*/ scanf("%d",&m); getchar(); t=AllAssign(t,m); WriteExp(t); printf("\n"); cal=Value(t); /*表达式求值输出*/ printf("the velue is : %d\n",cal); return 1; }
main() { int c,i; char e,s='y'; Tree temp=NULL; Tree t1=NULL; Tree t2=NULL; // while(s=='y') //{ //clrscr(); printf("please printf the letter of the first Expression:\n"); for(i=0;i<2;i++) { temp=ReadExpr(temp); /*建立,输出表达式*/ if(i==0) { t1=temp; printf("the first Express is:\n"); } else { t2=temp; printf("the second Express is:\n"); } WriteExp(temp); printf("\n"); Operate(temp); if(i==1) { printf("if want to combile ,please press '1';else press '2'\n");/*表达式复合*/ scanf("%d",&c);getchar(); if(c==1) { printf("please input the symbol:"); scanf("%c",&e);getchar(); temp=CompoundExpr(e,t1,t2); WriteExp(temp);printf("\n"); Operate(temp); } } if(i==0) printf("please printf the letter of the second Expression:\n"); } printf("rewrite the Express?\n"); printf("if yes ,please press 'y';else press 'n'\n"); scanf("%c",&s);getchar(); //} // getchar(); return 1; }