資源簡介
數據結構課程設計 表達式求值 完整的程序和代碼

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#define?TRUE?1
#define?FALSE?0
#define?Stack_Size?50
char?ops[7]={‘+‘‘-‘‘*‘‘/‘‘(‘‘)‘‘#‘};???/*運算符集合*/
int???cmp[7][7]={{2211122}?????/*用來進行比較運算符優先級的矩陣3代表‘=‘2代表‘>‘1代表‘<‘0代表不可比*/
?????????????????{2211122}
?????????????????{2222122}
?????????????????{2222122}
?????????????????{1111130}
?????????????????{2222022}
?????????????????{1111103}};
typedef?struct
{?
char?elem[Stack_Size];
int?top;
}SeqStack;??????/*運算符棧的定義*/
typedef?struct
{
int?elem[Stack_Size];
int?top;
}nSeqStack;????/*?運算數棧的定義*/
void?InitStack(SeqStack?*S)???
{
S->top?=-1;
}
void?InitStackn(nSeqStack?*S)???
{
S->top?=-1;
}
int?IsEmpty(SeqStack?*S)????
{
return(S->top==-1?TRUE:FALSE);
}
int?IsEmptyn(nSeqStack?*S)????
{
return(S->top==-1?TRUE:FALSE);
}
/*判棧滿*/
int?IsFull(SeqStack?*S)?????
{
return(S->top==Stack_Size-1?TRUE:FALSE);
}
int?IsFulln(nSeqStack?*S)?????
{
return(S->top==Stack_Size-1?TRUE:FALSE);
}
int?Push(SeqStack?*S?char?x)????/*運算符棧入棧函數*/
{
if?(S->top==Stack_Size-1)
{
???printf(“Stack?is?full!\n“);
???return?FALSE;
}
else
{
???S->top++;
???S->elem[S->top]=x;
???return?TRUE;
}
}
int?Pushn(nSeqStack?*S?int?x)????/*運算數棧入棧函數*/
{
if?(S->top==Stack_Size-1)
{
???printf(“Stack?is?full!\n“);
???return?FALSE;
}
else
{
???S->top++;
???S->elem[S->top]=x;
???return?TRUE;
}
}
int?Pop(SeqStack?*S?char?*x)?????/*運算符棧出棧函數*/
{
if?(S->top==-1)
{
???printf(“運算符棧空!\n“);
???return?FALSE;
}
else
{
???*x=S->elem[S->top];
???S->top--;
???return?TRUE;
}
}
int?Popn(nSeqStack?*S?int?*x)?????/*運算數棧出棧函數*/
{
if?(S->top==-1)
{
???printf(“運算符棧空!\n“);
???return?FALSE;
}
else
{
???*x=S->elem[S->top];
???S->top--;
???return?TRUE;
}
}
char?GetTop(SeqStack?*S)?????/*運算符棧取棧頂元素函數*/?????
{
if?(S->top?==-1)
{
???printf(“運算符棧為空!\n“);
???return?FALSE;
}
else
{
???return?(S->elem[S->top]);
}
}
int?GetTopn(nSeqStack?*S)?????/*運算數棧取棧頂元素函數*/?????
{
if?(S->top?==-1)
{
???printf(“運算符棧為空!\n“);
???return?FALSE;
}
else
{
???return?(S->elem[S->top]);
}
}
int?Isoperator(char?ch)?????????/*判斷輸入字符是否為運算符函數是返回TRUE不是返回FALSE*/
{
int?i;
for?(i=0;i<7;i++)
{
???if(ch==ops[i])
????return?TRUE;
}
return?FALSE;
}
/*
int?isvariable(char?ch)
{?if?(ch>=‘a‘&&ch<=‘z‘)
???????return?true;
????else?
?????return?false;
}*/
char?Compare(char?ch1?char?ch2)????/*比較運算符優先級函數*/
{
int?imn;
char?pri;
int?priority;
for(i=0;i<7;i++)??????????????
{
???if(ch1==ops[i])?
????m=i;
???if?(ch2==ops[i])
????n=i;
}
priority?=?cmp[m][n];
switch(priority)
{
case?1:
???pri=‘<‘;
???break;
case?2:
???pri=‘>‘;
???break;
case?3:
???pri=‘=‘;
???break;
case?0:
???pri=‘$‘;
???printf(“表達式錯誤!\n“);
???break;
}
return?pri;
}
int?Execute(int?a?char?op?int?b)?????/*運算函數*/
{
int?result;
switch(op)
{
case?‘+‘:
???result=a+b;
??
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4615??2008-07-10?09:33??數據結構課程設計\表達式求值問題.c
?????文件?????225792??2008-11-16?12:13??數據結構課程設計\表達式求值問題.doc
?????目錄??????????0??2008-11-16?12:10??數據結構課程設計
-----------?---------??----------?-----??----
???????????????230407????????????????????3
- 上一篇:高壓直流輸電
- 下一篇:ImapiService.reg
評論
共有 條評論