当前位置:
文档之家› C语言_算术表达式求值_代码
C语言_算术表达式求值_代码
源代码:
//用来存储字符的结点类型
typedef struct CharNode
{
char c;
struct CharNode *next;
}CharNode;
//用来存储数的结点类型
typedef struct IntNode
{
long double i;
struct IntNode *next;
n++;
}
if(mark_1==1)
{
b[i]=q->c;
q=q->next;
i++;
}
}
for(h=n-1;h>=0;h--)
{
x=(a[n-h-1]-48)*pow(JiWei,h)+x;//
}
for(h=0;h<i;h++)
{
y=y+(b[h]-48)*pow(JiWei,(-(h+1)));//
case '/':m->n =m->n / n->n ;break;
}
m->next=n->next;
n=m;
}
if(m->next->c=='#')
{
m=n=p;
mark=1;
}
}
return m;
}
//求用户输入表达式的值
Node *GetOutcome(Node *head)
{
Node *p,*q,*R;
p->next->c='#';
return top;
}
//将数与运算符分开,并将其他进制转化为10进制
Node *StackChange(CharNode *top,int m)
{
CharNode *p,*q;
long double x=0,y=0;
char a[10],b[10];
int n=0,i=0,JiWei,max,min=47,mark_1=0,mark_2=0,h,k=0;
};//定义一个二维数组存放算术符号的优先级
switch(x)
{
case '+':i=0;break;
case '-':i=1;break;
case '*':i=2;break;
case '/':i=3;break;
case '#':i=4;break;
}
switch(y)
{
case '+':j=0;break;
{
m=n;
}
if(Precede(max_char,min_char)=='>'||mark==1)
{
switch(m->next->c)
{
case '+':m->n =m->n + n->n ;break;
case '-':m->n =m->n - n->n ;break;
case '*':m->n =m->n * n->n ;break;
node_char->c=q->c;
node->next=node_char;
node_char->next_c=NULL;
node_char->next=NULL;
if(q->c=='#')
{
node->next=(Node_ys_char *)malloc(sizeof(Node_ys_char));
if(m->c=='(')
{
t=k;
k=m;
}
}
q=m->next ;
}
if(q->next->c==')')
{
n=q->next;
}
}
if(n->c==')')
{
p=k->next;
q->next->c='#';
R=Compute(p);
t->next =R;
t->next_c=NULL;
R->next=n->next_c;
printf("████\n");
printf("██表达式求值系统██\n");
printf("████\n");
printf("███████████████████████████████████████\n");
printf("本程序可分别进行2进制,8进制,10进制,16进制的加减乘除运算:\n");
Node_ys_char *m,*n,*t,*k;
n=(Node_ys_char *)malloc(sizeof(Node_ys_char));
n->c='(';
p=q=head;
while((n->c!=')')&&(q->next->c!='#'))
{
if(q->next->c=='(')
{
m=q->next;
node_char->next=NULL;
}
p=q;
n=0;
x=0;
y=0;
if(mark_2==0)
{
head=node;
mark_2=1;
}
}
return head;
}
//作只有)
{
int mark=0;
Node *m,*n;
char max_char,min_char;
GetOutcome(head);
}
else
{
R=Compute(head);
return R;
}
}
main()
{
int m;//进制
char a;
CharNode *top_1;
Node *top_2,*R;
printf("\n\n");
printf("███████████████████████████████████████\n");
q=q->next ;
//node->next=node_char;
node_char=node_char->next_c;
node_char->next_c=NULL;
node_char->next=NULL;
}
else
{
node->next=node_char;
node_char->next_c=NULL;
m=n=p;
while(p->next->c!='#')
{
max_char=n->next->c;
n=n->next->next;
min_char=n->next->c;
if((Precede(max_char,min_char)=='<'||Precede(max_char,min_char)=='=')&&mark!=1)
struct Node *next;
}Node_ys_char;
char Precede(char x,char y)//运算符优先级判断
{
int i,j;
int from[5][5]
={
{0,0,-1,-1,0},
{0,0,-1,-1,0},
{1,1,0,0,1},
{1,1,0,0,1},
{0,0,-1,-1,0}
k=m;
}
else if(q->next->next_c==NULL)
q=q->next->next;
else
{
m=q->next->next_c;
k=q->next;//t=q->next;
if(m->c=='(')
{
t=k;
k=m;
}
while(m->next_c!=NULL)
{
m=m->next_c;
}IntNode;
//用来存储数的结点类型
typedef struct Node
{
long double n;
struct Node_ys_char *next;
}Node;
//用来存储运算符的结点类型
typedef struct Node_ys_char
{
char c;
struct Node_ys_char *next_c;
max=103;
break;
}
p=q=top;
while(p->c !='#')
{
while((q->c>min)&&(q->c<max)||(q->c==46))
{
if(q->c==46)
{
mark_1=1;
q=q->next;
}
if(mark_1==0)
{
a[n]=q->c;