逆波兰表达式思想下的计算器
来源:互联网 发布:淘宝做工问题退货规则 编辑:程序博客网 时间:2024/05/02 03:56
K&R C Bible《C Programming Language》中采用reverse Polish notation思想的计算器代码:
calc.h
#define NUMBER '0'
void push(double);
double pop(void);
int getop(char []);
int getch(void);
void ungetch(int);
……………………………………………………………………………………………………………………………………………………………………………………………………………
main.c
#include<stdio.h>
#include<stdlib.h> /*for atof() */
#define MAXOP 100 /* max size of operand or operator*/
#define NUMBER '0' /*signal that a number was found*/
int getop(char []);
void push(double);
double pop(void);
/*reverse Polish calculator*/
main()
{
int type;
double op2;
char s[MAXOP];
while((type = getop(s))!= EOF){
switch(type){
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() +pop());
break;
case '*':
push(pop() + pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if(op2 != 0.0)
push(pop() / op2);
else
printf("error:zero divisor\n");
break;
case '\n':
printf("\t%.8g\n",pop());
break;
default:
printf("errro:unknown command%s\n",s);
break;
}
}
return 0;
}
……………………………………………………………………………………………………………………………………………………………………………………………………………
stack.c
#include<stdio.h>
#include"calc.h"
#define MAXVAL 100
int sp = 0;
double val[MAXVAL];
void push(double f)
{
if(sp < MAXVAL)
val[sp++] = f;
else
printf("error:stack full ,can't push %g\n",f);
}
double pop(void)
{
if(sp > 0)
return val[--sp];
else{
printf("error:stack empty\n");
return 0.0;
}
}
……………………………………………………………………………………………………………………………………………………………………………………………………………
getop.c
#include<ctype.h>
#include<stdio.h>
#include"calc.h"
int getch(void);
void ungetch(int);
/*getop: get next operator or numberic operand */
int getop(char s[])
{
int i,c;
while((s[0] = c = getch()) ==' ' || c == '\t')
;
s[1] = '\0';
if( ! isdigit(c) && c != '.')
return c; /*not a number*/
i = 0;
if(isdigit(c)) /*collect interger part*/
while(isdigit(s[++i] = c = getch()))
;
if(c == '.') /*collect fraction part*/
while(isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if(c != EOF)
ungetch(c);
return NUMBER;
}
…………………………………………………………………………………………………………………………………………………………………………………………………………
getch.c
#include<stdio.h>
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
int getch(void)
{
return(bufp > 0) ? buf[--bufp]:getchar();
}
void ungetch(int c)
{
if(bufp >= BUFSIZE)
printf("ungetch:too many characters\n");
else
buf[bufp++] = c;
}
上述代码为K&R的C BIBLE书籍中的源代码,经过编译运行,完全正确。不过要注意的是,想要能够正确运行此计算器,必须了解reverse Polish notation.
在此仅作简单叙述:
如要计算3 +4 ,则输入时应为:3 4 +。注意每个字符之间是有空格的,作为一个完整输入的区分。否则无法辨认是34还是3、4。
- 逆波兰表达式思想下的计算器
- 计算器 | 逆波兰表达式
- 逆波兰表达式计算器
- 实现逆波兰表达式的java计算器
- 基于逆波兰表达式的计算器
- 用逆波兰表达式的三角函数计算器
- 计算器(逆波兰运算表达式)
- HDU 1237 简单计算器 逆波兰表达式
- 逆波兰计算器的实现
- 栈的应用--中缀表达式转换为后缀表达式&逆波兰计算器的实现
- 逆波兰计算器与中缀表达式向后缀表达式的转化实例
- java 逆波兰表达式 (后缀表达式) 计算器
- 波兰、逆波兰表达式
- 分享一个Swift语言的逆波兰表达式计算器的实现
- 逆波兰计算器的C完整C代码(输入须为后缀表达式)
- 简单的逆波兰基本计算器
- 数据结构(6)--逆波兰计算器的运用
- 逆波兰式实现四则运算表达式计算器支持括号、十六进制
- poj 2777 Count color
- [CentOs 5.5 X64] CentOS 安裝 Broadcom BCM43225 無線網路卡驅動程式
- IT人员迅速提升自我效率的十大方法
- Js构造二维数组
- 拓扑排序 C++实现
- 逆波兰表达式思想下的计算器
- 强制转换指针类型
- static全局变量与普通的全局变量的区别/static局部变量和普通局部变量的区别/static函数与普通函数的区别
- 堆-栈-动态存储方式-静态存储方式
- Android中保存数据常用的方法
- 用C读取DXF文件
- 优秀程序设计的18大原则
- memset
- Asp.Net多附件上传