資源簡介
堆棧 計算器 c語言寫主要思想是對每個輸入的字符進行檢測和分類,分成數字堆棧和符號堆棧然后判斷符號優先級。
統一先處理乘號和除號,(如果有括號先處理括號,將括號內的運算處理完)
然后最后會出現優先級低的運算符加號 減號還沒有運算完,字符串就結束了。
代碼片段和文件信息
#include?
#include?
#include?
#define?MAXSIZE?50
//定義一個順序存儲棧
typedef?struct
{
????int?data[MAXSIZE];
????int?top;
}SqStack;
/*******************棧的基本操作********************************/
int?init_stack(SqStack?*s)
{
????s->top?=?-1;
????return?1;
}
int?clear_stack(SqStack?*s)//清空?
{
????s->top?=?-1;
????return?1;
}
int?stack_empty(SqStack?s)//判斷?
{
????if?(s.top?==?-1)
????????return?1;
????else
????????return?0;
}
int?stack_length(SqStack?*s)//長度?
{
????return?s->top?+?1;
}
int?push(SqStack?*s?int?e)//入?
{
// printf(“%c\n“e);
????if?(s->top?==?MAXSIZE?-?1)
????????return?0;
????s->top++;
????s->data[s->top]?=?e;
????return?1;
}
int?pop(SqStack?*s?int?ee)//出?
{
int?f;
????if?(s->top?==?-1)
????????retu
評論
共有 條評論