-
大小: 11KB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2021-05-24
- 語言: C/C++
- 標(biāo)簽:
資源簡介
表達(dá)式求值的c語言版,能求出后綴,并根據(jù)后綴求表達(dá)式的值。僅供參考與交流

代碼片段和文件信息
#include
#include
#include
#define?TRUE?1
#define?FALSE?0
#define?STACK_SIZE?30
char?optr[7]={‘+‘‘-‘‘*‘‘/‘‘(‘‘)‘‘#‘};
int?cmp[7][7]={{3311133}{3311133}{3333?133}{3333?133}{1111120}{3333033}{1111102}};
//算符優(yōu)先矩陣,3代表>2代表=,1代表<0代表不可比
typedef?struct
{
char?optr[STACK_SIZE];
int?top;
}stack;//操作符棧
typedef?struct
{
int?opnd[STACK_SIZE];
int?top;
}nstack;//操作數(shù)棧
void?initstack(stack?*s)//初始化運(yùn)算符棧
{
s->top=-1;
};
void?initstackn(nstack?*s)//初始化操作數(shù)棧
{
s->top=-1;
}
int?isempty(stack?*s)//判斷運(yùn)算符棧是否為空,為空返回1
{
if(s->top==-1)return?TRUE;
else?return?FALSE;
}
int?isemptyn(nstack?*s)//判斷操作數(shù)棧是否為空,為空返回1
{
return(s->top==-1?TRUE:FALSE);
}
int?isfull(stack?*s)//判斷運(yùn)算符棧是否為滿,為空返回1
{
return(s->top==(STACK_SIZE-1)?TRUE:FALSE);
}
int?isfulln(nstack?*s)//判斷操作數(shù)棧是否為滿,為空返回1
{
return(s->top==(STACK_SIZE-1)?TRUE:FALSE);
}
int?push(stack?*schar?c)//運(yùn)算符入棧
{
if(s->top==STACK_SIZE-1)
{
printf(“stack?is?full!\n“);?
return?FALSE;
}
else?
{
s->top++;
s->optr[s->top]=c;
return?TRUE;
}
}
int?pushn(nstack?*sint?x)//操作數(shù)入棧
{
if(s->top==STACK_SIZE-1)
{
printf(“stack?is?full!\n“);?
return?FALSE;
}
else?
{
s->top++;
s->opnd[s->top]=x;
return?TRUE;
}
}
int?pop(stack?*schar?*c)//?運(yùn)算符出棧
{
if(s->top==-1)
{
printf(“stack?is?empty!\n“);
return?FALSE;
}
else
{
*c=s->optr[s->top];
s->top--;
return?TRUE;
}
}
int?popn(nstack?*sint?*x)//操作數(shù)出棧
{
if(s->top==-1)
{
printf(“stack?is?empty!\n“);
return?FALSE;
}
else
{
*x=s->opnd[s->top];
s->top--;
return?TRUE;
}
}
char?gettop(stack?*s)//取操作符棧頂元素
{
if(s->top==-1)
{
printf(“stack?is?empty!\n“);
return?FALSE;
}
else
{
return?s->optr[s->top];
}
}
int?gettopn(nstack?*s)//取操作數(shù)棧頂元素
{
if(s->top==-1)
{
printf(“stack?is?empty!\n“);
return?FALSE;
}
else
{
return?s->opnd[s->top];
}
}
int?isoptr(char?ch)//?判斷是否為運(yùn)算符,是則返回1
{
int?i;
for(i=0;i<7;i++)
{
if(optr[i]==ch)return?TRUE;
}
return?FALSE;
}
int?compare(char?ch1char?ch2)//??運(yùn)算符優(yōu)先級比較
{
int?imn;
for(i=0;i<7;i++)
{
if(ch1==optr[i])
m=i;
if(ch2==optr[i])
n=i;
}
return?cmp[m][n];
}
int?execute(int?achar?opint?b)//運(yùn)算函數(shù)
{
int?result;
switch(op)
{
case‘+‘:
result=a+b;
break;
case‘-‘:
result=b-a;
break;
case‘*‘:
result=a*b;
break;
case‘/‘:
result=b/a;
break;
}
return?result;
}
int?expevalution()//讀入表達(dá)式并求其值
{ int?i=0tempabv;
char?chop;
char?*str;
stack?optr;
nstack?opnd;
initstack(&optr);
initstackn(&opnd);
push(&optr‘#‘);
printf(“請輸入表達(dá)式(以#結(jié)束)\n“);
str=(char*)malloc(50*sizeof(char));
gets(str);
ch=str[i];
i++;
while(ch!=‘#‘||gettop(&optr)!=‘#‘)
{
if(!isoptr(ch))//字符轉(zhuǎn)化為十進(jìn)制數(shù)
{
temp=ch-‘0‘;
ch=str[i];
i++;
while(!isoptr(ch))
{
temp=temp*10+ch-‘0‘;
ch=str[i];
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4180??2009-10-10?23:58??DS-10010804-2008302503-明懷平\源程序\轉(zhuǎn)化為后綴并求值.cpp
?????文件???????3783??2009-10-10?17:01??DS-10010804-2008302503-明懷平\源程序\中綴求值.cpp
?????文件??????61440??2009-10-11?00:31??DS-10010804-2008302503-明懷平\數(shù)據(jù)結(jié)構(gòu)實驗報告.doc
?????目錄??????????0??2009-10-11?00:32??DS-10010804-2008302503-明懷平\源程序
?????目錄??????????0??2009-10-11?00:33??DS-10010804-2008302503-明懷平
-----------?---------??----------?-----??----
????????????????69403????????????????????5
評論
共有 條評論