#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
#define bool char
typedef char ElemType;
typedef struct node1
{
ElemType data;
struct node1 *next;
}LinkStackNode1,*LinkStack1;
//typedef LinkStackNode1 *LinkStack1;
typedef struct node2
{
int data;
struct node2 *next;
}LinkStackNode2,*LinkStack2;
//typedef LinkStackNode2 *LinkStack2;
void InitStack1(LinkStack1 *S)
{
*S=(LinkStackNode1 *)malloc(sizeof(LinkStackNode1));
(*S)->next=NULL;
}
void InitStack2(LinkStack2 *S)
{
(*S) = (LinkStackNode2 *)malloc(sizeof(LinkStackNode2));
(*S)->next=NULL;
}
bool Push1(LinkStack1 top,char e)
{
LinkStackNode1 *tmp;
tmp=(LinkStackNode1 *)malloc(sizeof(LinkStackNode1));
if(tmp==NULL)
{
printf("申请空间失败");
return false;
}
tmp->data=e;
tmp->next=top->next;
return true;
}
bool Push2(LinkStack2 top,int e)
{
LinkStackNode2 *tmp;
tmp=(LinkStackNode2 *)malloc(sizeof(LinkStackNode2));
if(tmp==NULL)
{
printf("申请空间失败");
return false;
}
tmp->data=e;
tmp->next=top->next;
return true;
}
bool Pop1(LinkStack1 top,char *e)
{
LinkStackNode1 *tmp;
tmp=top->next;
if(tmp==NULL)
{
printf("栈空error");
return false;
}
*e=tmp->data;
free(tmp);
return true;
}
bool Pop2(LinkStack2 top,int *e)
{
LinkStackNode2 *tmp;
tmp=top->next;
if(tmp==NULL)
{
printf("栈空error");
return false;
}
*e=tmp->data;
free(tmp);
return true;
}
bool In1(char ch,LinkStack1 OPSet)
{
LinkStackNode1 *tmp;
tmp=OPSet->next;
while(tmp!=NULL)
{
if(tmp->data==ch)
{
return true;
}
tmp=tmp->next;
}
return true;
}
char GetTop1(LinkStack1 S)
{
LinkStackNode1 *tmp;
tmp=S->next;
return tmp->data;
}
char Compare(char a,char b)
{
if(b=='*' || b=='/')
{
if(a=='*' || a=='/')
{
return '=';
}
else if(a=='-' || a=='+')
{
return '<';
}
}
else if(b=='+' || b=='-')
{
if(a=='+' || b=='-')
{
return '=';
}
else if(a=='*' || a=='/')
{
return '>';
}
}
}
int Execute(int a,char op,int b)
{
printf("未完成");
if(op=='+')
{
return a+b;
}
else if(op=='-')
{
return a-b;
}
else if(op=='*')
{
return a*b;
}
else if(op=='/')
{
return a/b;
}
else
{
printf("??");
}
}
int ExpEvaluation()
{
LinkStack1 OPTR,OPSet;
LinkStack2 OVS;
int n,v,a,b;
char ch,op;
//
InitStack1(&OPSet);
Push1(OPSet,'+');
Push1(OPSet,'-');
Push1(OPSet,'*');
Push1(OPSet,'/');
Push1(OPSet,'^');
Push1(OPSet,'%');
ch=GetTop1(OPSet);
printf("haha");
putchar(ch);
InitStack1(&OPTR);
InitStack2(&OVS);
Push1(OPTR,'#');
printf("Input(end with #): ");
while(true)
{
scanf("%d",&n);
Push2(OVS,n);
ch=getchar();
switch(Compare(ch,GetTop1(OPTR)))
{
case '>':
Push1(OPTR,ch);
break;
case '=':
case '<':
Pop1(OPTR,&op);
Pop2(OVS,&b);
Pop2(OVS,&a);
v=Execute(a,op,b);
Push2(OVS,v);
}
if(ch=='#')
{
break;
}
}
/*
while(ch!='#' || GetTop(OPTR)!='#')
{
if(!In(ch,OPSet))
{
n=GetNumber(ch);
push(&OVS,n);
ch=getchar();
}
else
{
switch(Compare(ch,GetTop(OPTR)))
{
case '>':
Push(&OPTR,ch);
ch=getchar();
break;
case '=':
case '<':
Pop(&OPTR,&op);
Pop(&OVS,&b);
Pop(&OVS,&a);
v=Execute(a,op,b);
Push(&OVS,v);
break;
}
}
}
*/
return true;
}
int main()
{
if(ExpEvaluation())
{
printf("Yes\n");
}
return 0;
}
链式栈 C语言练习
最新推荐文章于 2022-08-02 20:34:43 发布