資源簡介
[問題描述]
一個算術(shù)表達式是由操作數(shù)(operand)、運算符(operator)和界限符(delimiter)組成的。假設(shè)操作數(shù)是正整數(shù),運算符只含加減乘除等四種運算符,界限符有左右括號和表達式起始、結(jié)束符“#”,如:#(7+15)*(23-28/4)#。引入表達式起始、結(jié)束符是為了方便。編程利用“算符優(yōu)先法”求算術(shù)表達式的值。
[基本要求]
(1) 從鍵盤讀入一個合法的算術(shù)表達式,輸出正確的結(jié)果。
(2) 顯示輸入序列和棧的變化過程。
[選作內(nèi)容]
(1) 擴充運算符集合。
(2) 引入變量操作數(shù)。
(3) 操作數(shù)類型擴充到實數(shù)。

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#define?TRUE?1
#define?FALSE?0
#define?Stack_Size?50
char?ops[7]={‘+‘‘-‘‘*‘‘/‘‘(‘‘)‘‘#‘};??/*運算符數(shù)組*/
int??cmp[7][7]={{2211122}????/*用來進行比較運算符優(yōu)先級的矩陣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;???/*?運算數(shù)棧的定義*/
void?InitStack(SeqStack?*S)???/*初始化運算符棧*/
{
S->top?=-1;
}
void?InitStackn(nSeqStack?*S)???/*初始化運算數(shù)棧*/
{
S->top?=-1;
}
int?IsEmpty(SeqStack?*S)????/*判斷棧S為空棧時返回值為真,反之為假*/
{
return(S->top==-1?TRUE:FALSE);
}
int?IsEmptyn(nSeqStack?*S)????/*判斷棧S為空棧時返回值為真,反之為假*/
{
return(S->top==-1?TRUE:FALSE);
}
/*判棧滿*/
int?IsFull(SeqStack?*S) ????/*判斷棧S為滿棧時返回值為真,反之為假*/
{
return(S->top==Stack_Size-1?TRUE:FALSE);
}
int?IsFulln(nSeqStack?*S) ????/*判斷棧S為滿棧時返回值為真,反之為假*/
{
return(S->top==Stack_Size-1?TRUE:FALSE);
}
int?Push(SeqStack?*S?char?x)???/*運算符棧入棧函數(shù)*/
{
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)???/*運算數(shù)棧入棧函數(shù)*/
{
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)????/*運算符棧出棧函數(shù)*/
{
if?(S->top==-1)
{
printf(“運算符棧空!\n“);
return?FALSE;
}
else
{
*x=S->elem[S->top];
S->top--;
return?TRUE;
}
}
?
int?Popn(nSeqStack?*S?int?*x)????/*運算數(shù)棧出棧函數(shù)*/
{
if?(S->top==-1)
{
printf(“運算符棧空!\n“);
return?FALSE;
}
else
{
*x=S->elem[S->top];
S->top--;
return?TRUE;
}
}
char?GetTop(SeqStack?*S)????/*運算符棧取棧頂元素函數(shù)*/?????
{
if?(S->top?==-1)
{
printf(“運算符棧為空!\n“);
return?FALSE;
}
else
{
return?(S->elem[S->top]);
}
}
int?GetTopn(nSeqStack?*S)????/*運算數(shù)棧取棧頂元素函數(shù)*/?????
{
if?(S->top?==-1)
{
printf(“運算符棧為空!\n“);
return?FALSE;
}
else
{
return?(S->elem[S->top]);
}
}
int?Isoperator(char?ch)????????/*判斷輸入字符是否為運算符函數(shù)是返回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ǎn)‘&&ch<=‘z‘)
??????return?true;
???else?
???return?false;
}*/
char?Compare(char?ch1?char?ch2)???/*比較運算符優(yōu)先級函數(shù)*/
{
int?imn;
char?pri;
int?priority;
for(i=0;i<7;i++)??????????????/*找到相比較的兩個運算符在比較矩陣?yán)锏南鄬ξ恢?/
{
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?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????159799??2005-06-06?17:21??表達式求值_課程設(shè)計\Debug\ex
?????文件?????167416??2005-06-06?17:21??表達式求值_課程設(shè)計\Debug\ex
?????文件??????14684??2005-06-06?17:21??表達式求值_課程設(shè)計\Debug\ex
?????文件?????210892??2005-06-06?17:21??表達式求值_課程設(shè)計\Debug\ex
?????文件?????328704??2005-06-06?17:21??表達式求值_課程設(shè)計\Debug\ex
?????文件??????41984??2005-06-06?17:21??表達式求值_課程設(shè)計\Debug\vc60.idb
?????文件??????53248??2005-06-06?17:21??表達式求值_課程設(shè)計\Debug\vc60.pdb
?????文件?????159799??2005-06-06?17:18??表達式求值_課程設(shè)計\Debug\表達式求值.exe
?????文件?????183292??2005-06-06?17:18??表達式求值_課程設(shè)計\Debug\表達式求值.ilk
?????文件??????14630??2005-06-06?17:18??表達式求值_課程設(shè)計\Debug\表達式求值.obj
?????文件?????181644??2005-06-06?15:02??表達式求值_課程設(shè)計\Debug\表達式求值.pch
?????文件?????451584??2005-06-06?17:18??表達式求值_課程設(shè)計\Debug\表達式求值.pdb
?????文件???????4904??2005-06-06?17:19??表達式求值_課程設(shè)計\ex
?????文件???????3447??2005-06-06?17:21??表達式求值_課程設(shè)計\ex
?????文件????????545??2005-06-06?17:21??表達式求值_課程設(shè)計\ex
?????文件??????33792??2005-06-06?17:21??表達式求值_課程設(shè)計\ex
?????文件??????48640??2005-06-06?17:21??表達式求值_課程設(shè)計\ex
?????文件????????768??2005-06-06?17:21??表達式求值_課程設(shè)計\ex
?????目錄??????????0??2005-06-09?21:30??表達式求值_課程設(shè)計\Debug
?????目錄??????????0??2005-06-09?21:30??表達式求值_課程設(shè)計
-----------?---------??----------?-----??----
??????????????2059772????????????????????20
評論
共有 條評論