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

代碼片段和文件信息
#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)????/*判斷棧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)???/*運算符棧入棧函數*/
{
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?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????159799??2005-06-06?17:21??表達式求值_課程設計\Debug\ex
?????文件?????167416??2005-06-06?17:21??表達式求值_課程設計\Debug\ex
?????文件??????14684??2005-06-06?17:21??表達式求值_課程設計\Debug\ex
?????文件?????210892??2005-06-06?17:21??表達式求值_課程設計\Debug\ex
?????文件?????328704??2005-06-06?17:21??表達式求值_課程設計\Debug\ex
?????文件??????41984??2005-06-06?17:21??表達式求值_課程設計\Debug\vc60.idb
?????文件??????53248??2005-06-06?17:21??表達式求值_課程設計\Debug\vc60.pdb
?????文件?????159799??2005-06-06?17:18??表達式求值_課程設計\Debug\表達式求值.exe
?????文件?????183292??2005-06-06?17:18??表達式求值_課程設計\Debug\表達式求值.ilk
?????文件??????14630??2005-06-06?17:18??表達式求值_課程設計\Debug\表達式求值.obj
?????文件?????181644??2005-06-06?15:02??表達式求值_課程設計\Debug\表達式求值.pch
?????文件?????451584??2005-06-06?17:18??表達式求值_課程設計\Debug\表達式求值.pdb
?????文件???????4904??2005-06-06?17:19??表達式求值_課程設計\ex
?????文件???????3447??2005-06-06?17:21??表達式求值_課程設計\ex
?????文件????????545??2005-06-06?17:21??表達式求值_課程設計\ex
?????文件??????33792??2005-06-06?17:21??表達式求值_課程設計\ex
?????文件??????48640??2005-06-06?17:21??表達式求值_課程設計\ex
?????文件????????768??2005-06-06?17:21??表達式求值_課程設計\ex
?????目錄??????????0??2005-06-09?21:30??表達式求值_課程設計\Debug
?????目錄??????????0??2005-06-09?21:30??表達式求值_課程設計
-----------?---------??----------?-----??----
??????????????2059772????????????????????20
- 上一篇:計算機圖形學實驗報告
- 下一篇:計算機圖形學消隱實驗轉
評論
共有 條評論