你们试一下我的运行结果,为什么是这样的?
我想问 问题出在哪了啦?怎么没有运行结果#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct
{
char op;
char level;
}opNode;
typedef union
{
opNode opNode;
double value;
}Node;
typedef struct
{
char leixing;
Node* a[100];
int top;
}Stack;
Stack* init_sta()
{
int i;
Stack* s=(Stack*)malloc(sizeof(Stack));
s->top=0;
for(i=0;i<100;i++)
s->a[i]=NULL;
return s;
}
char* substr(char* s,int n1,int n2)
{
char* ss=(char*)malloc(sizeof(char)*(n2-n1+2));
int i,j=0;
for(i=n1;i<n2;i++)
{
ss[j++]=s[i];
}
ss[j]='\0';
return ss;
}
int empty(Stack*ss)
{
return ss->top;
}
Node*Top(Stack* ss)
{
if(ss->top<=0)
{
return 0;
}
else
{
return ss->a[ss->top-1];
}
}
Node*Pop(Stack* ss)
{
if(ss->top<=0)
{
return 0;
}
else
{
ss->top--;
return ss->a[ss->top];
}
}
void Push(Stack *p,Node*n)
{
Node*temp=(Node*)malloc(sizeof(Node));
if(p->top>=100)
{
printf("错误,栈为满!\n");
free(temp);
}
else
{
if(p->leixing==1)
{
temp->opNode.level=n->opNode.level;
temp->opNode.op=n->opNode.op;
}
else
temp->value=n->value;
p->a[p->top]=temp;
p->top=p->top+1;
}
}
double calc(char op,double m,double n)
{
switch(op)
{
case'+':return n+m;
case'-':return n-m;
case'*':return n*m;
case'/':return n/m;
case'%':return (int)n%(int)m;
}
return 0;
}
void math(char* exp)
{
Node*top;
Node*temp=(Node*)malloc(sizeof(Node));
Node*tempn=(Node*)malloc(sizeof(Node));
Node*tempm;
char c;
char*temps="";
unsigned int index=0,tempindex;
double a,b;
Stack*s1=init_sta();
Stack*s2=init_sta();
s1->leixing=2;
s2->leixing=1;
while(index<strlen(exp))
{
c=exp[index];
if(c>='0'&&c<='9'||c=='.')
{
tempindex=index+1;
while(tempindex<strlen(exp)&&(exp[tempindex]>='0'&&exp[tempindex]<='9'||exp[tempindex]=='.'))
{
tempindex++;
}
if(tempindex==strlen(exp)-1)
{
temps=substr(exp,index,index+1);
}
else
{
temps=substr(exp,index,tempindex);
}
index=tempindex;
temp->value=atof(temps);
Push(s1,temp);
}
if(c=='+'||c=='-')
{
tempn->opNode.op=c;
tempn->opNode.level=1;
top=Top(s2);
while(empty(s2)>0&&tempn->opNode.level<=top->opNode.level)
{
tempn=Pop(s2);
a=Pop(s1)->value;
b=Pop(s1)->value;
temp->value=calc(tempm->opNode.op,a,b);
Push(s1,temp);
top=Top(s2);
}
Push(s2,tempn);
index++;
}
if(c=='(')
{
tempn->opNode.op=c;
tempn->opNode.level=0;
Push(s2,tempn);
index++;
}
if(c==')')
{
top=Top(s2);
while(empty(s2)>0&&empty(s1)>0&&top->opNode.op!='(')
{
tempn=Pop(s2);
a=Pop(s1)->value;
b=Pop(s1)->value;
temp->value=calc(tempn->opNode.op,a,b);
Push(s1,temp);
top=Top(s2);
}
Pop(s2);
index++;
}
}
while(empty(s1)>0&&empty(s2)>0)
{
tempm=Pop(s2);
a=Pop(s1)->value;
b=Pop(s1)->value;
temp->value=calc(tempm->opNode.op,a,b);
Push(s1,temp);
}
sprintf(exp,"%1f",Pop(s1)->value);
}
void display(char* value)
{
char *temps="";
int i=0;
for(i=strlen(value)-1;i>=0;i--)
{
if(value[i]!='0'&&value[i]!='.')
break;
if(value[i]=='.')
{
i--;
break;
}
}
temps=substr(value,0,i+1);
printf("The Value is \"%s\".",temps);
}
void main()
{
char*calcstring=(char*)malloc(sizeof(char)*100);
strcpy(calcstring,"3.01*(4.23-2.16)-9%2");
printf("This string is %s\n",calcstring);
math(calcstring);
display(calcstring);
getch();
}