- 10
- 0
- 约 11页
- 2017-10-06 发布于河南
- 举报
堆栈计算器(Stack calculator)
堆栈计算器(Stack calculator)
1. Define the stack structure of the postfix calculator
Because there are not many units to store, a sequential stack is used here, that is, to simulate the stack with a one-dimensional array:
#define MAX 100
Int stack[MAX];
Int top=0;
Therefore, the program defines a one-dimensional array of length MAX, where MAX is defined as constant 100 by macro, and we can modify the macro definition to redefine the size of the stack.
The integer data top indicates to the top of the stack that the top is initialized to 0 because there is no data element in the stack at the beginning of the program.
2, store the number of postfix calculator
After we have defined the stack stack[MAX], we can use the stack operation to store two operands entered successively.
Heres how it works:
Int push (int i) / * memory operand, stack operation.
{
If (top, MAX)
{
There is still room for stack[++top]=i; / * stack, the stack indicates a shift position * /
Return 0;
}
Else / * stack is full, giving the wrong information, return an error indication.
{
Printf (The stack is full);
Return ERR;
}
}
When we call the function push, if its return value is 0, the stack operation is successful; otherwise, if the return value is ERR (specified in the program as -1), the stack operation fails.
3. Take out operands from the stack
When the four operator has been read in the program, we can take out two operands that have been saved from the stack, form an expression, and calculate the result. The algorithm that extracts the operand is the stack algorithm. In this case, the function to implement the algorithm is pop ():
Int (POP); / * remove the operand stack, * /
{
Int VaR; / * defined to return the top of the stack.
If (top! =NULL) there are data elements / * stack
{
Var=stack[top--]; / * * / a position indicator down stack
Return var;
}
Else / * stack is empty, an error message, and returns the return value.
Printf (The, stack, is, cmpty, \n);
Return ERR;
}
Similarly, if the stack is n
您可能关注的文档
- 古风歌(Ancient song).doc
- 古龙经典名言(Gulong classic sayings).doc
- 句句是改革 字字有力度(The sentence is the word reform efforts).doc
- 古风耽美歌曲 歌词(Tanbi ancient song lyrics).doc
- 另类智慧(Alternative intelligence).doc
- 另类详解 windows xp 的闺中秘密(Windows XP's Secret offbeat.).doc
- 只需一分钟皮肤就变的白白嫩嫩(It takes only one minute to make the skin white, tender and tender).doc
- 句子练习三(转述句练习)(Sentence three).doc
- 可以吃的英文单词(English words that you can eat).doc
- 可以让你喊的文章(Articles that can make you shout).doc
- 伟明环保-市场前景及投资研究报告-境内业务稳健运行,印尼市场贡献边际增量.pdf
- 桂东县法院系统招聘考试真题2025.pdf
- 贵州省黔南布依族2026年中考三模物理试题及答案.pdf
- 贵州省黔南州2026年中考语文二模试卷附答案.pdf
- 贵州省铜仁市2026年中考语文二模试卷附答案.pdf
- 2026上半年安徽事业单位联考合肥市庐江县招聘36人备考题库及一套完整答案详解.docx
- 贵州省毕节市2026年中考语文一模试卷附答案.pdf
- 贵州省贵阳市南明区2026年中考语文一模试卷附答案.pdf
- 2026上半年安徽事业单位联考合肥市庐江县招聘36人备考题库及一套参考答案详解.docx
- 贵州省贵阳市白云区2026年中考二模物理试题附答案.pdf
最近下载
- 园区招商与先进制造业发展研究.pdf VIP
- 2025年拍卖师慈善拍卖现场竞拍氛围营造专题试卷及解析.pdf VIP
- 2025年信息系统安全专家工控协议基础概念与分类专题试卷及解析.pdf VIP
- 2025年金融风险管理师资产证券化风险暴露资本处理专题试卷及解析.pdf VIP
- 2025年无人机驾驶员执照电磁环境干扰风险评估专题试卷及解析.pdf VIP
- 小学至高中人工智能辅助下的学习时间管理模型构建与应用研究教学研究课题报告.docx
- IATF16949:ISO9001-2016中英对照版本.pdf VIP
- 某图书馆大楼供配电系统设计.doc VIP
- 国资私募基金合规风控实务研究报告(2024).pdf VIP
- 海信HZ55E5D说明书用户手册.pdf
原创力文档

文档评论(0)