重读The C programming Lanuage 笔记三:简单计算器程序
//简单计算器 #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h> #define MAXOP 100 //max size of operand or operator
#define NUMBER '0' //sign of a number was found
#define NAME 'n' //sign of a mathfunc was found
#define MAXVAL 100 //max size of the stack
#define BUFSIZE 100 //buf io int sp = ; //the postion of the stack
double val[MAXVAL]; // the stack
double variable[]; //26 a~z
void clear(void); int getop(char[]); //get operand or operator
void push(double);
double pop(void);
void mathfnc(char[]); //math function int main()
{
int type,var = ;
double op1, op2, v;
char s[MAXOP]; for (int i = ; i<; i++)
{
variable[i] = 0.0; while ((type = getop(s)) != EOF)
switch (type)
{ case NUMBER:
push(atof(s));
break;
case NAME:
mathfnc(s);
break;
case '+':
push(pop() + pop());
break;
case '-':
op2 = pop(); push(pop() - op2);
break;
case '*':
push(pop()*pop());
break;
case '/':
op2 = pop();
if (op2 != 0.0)
push(pop() / op2);
else printf("error:zero divisor");
break;
case '%':
op2 = pop();
if (op2 != 0.0)
push(fmod(pop(), op2));
else printf("error:zero divisor");
break;
//对栈操作 打印栈顶元素,交换栈值,清空栈
case '?': // printf top of element of the stack
op2 = pop();
printf("\t%.8g", op2);
push(op2);
break;
case 'c': //clear the stack
clear();
break;
case 's': //swap the top of the stack
op1 = pop();
op2 = pop();
push(op2);
push(op1);
break;
case '\n':
v = pop();
printf("\t%8f\n", v);
break;
case '=':
pop();
if (var >= 'A' && var <= 'Z')
variable[var - 'A'] = pop();
else
printf("error: no variable name");
break;
default:
if (type >= 'A' || type <= 'Z')
push(variable[type - 'A']);
else if (type == 'v')
push(v);
else
printf("error:unknown command %s\n", s);
break; }
var = type;
}
return ;
} //出入栈函数
void push(double f)
{
if (sp<MAXVAL)
val[sp++] = f;
else
printf("error :stack full,can push %s\n", f);
} double pop(void)
{
if (sp > )
return val[--sp];
else
printf("error:stack empty");
return 0.0;
} //getop()函数
int getch(void);
void ungetch(int); int getop(char s[])
{
int c, i; while ((s[] = c = getch()) == ' ' || c == '\t')
;
s[] = '\0';
i = ; if (islower(c)) //commend or NAME
{
while (islower(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
if (strlen(s)>)
return NAME;
else
return c;
}
if (!isdigit(c) && c != '.' && c != '-')
return c; //not a number
if (c == '-')
if (isdigit(c = getch()) || c == '.')
s[++i] = c; //negetive number
else
{
if (c != EOF)
ungetch(c);
return '-'; //minus sign
}
if (isdigit(c))
while (isdigit(s[++i] = c = getch()))
;
if (c == '.')
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return NUMBER;
} //getch().ungetch()
int buf[BUFSIZE]; //不是char *buf 以能正确处理 EOF
int bufp = ; //buf中下一个空闲位置 int getch(void) //取一个字符(可能是压回的字符)
{
return (buf > ) ? buf[--bufp] : getchar();
} void ungetchar(int c) //把字符压回输入中
{
if (bufp >= BUFSIZE)
printf("error :too many characters");
else
buf[bufp++] = c;
} void clear(void)
//reverse polish calculator
{
sp = ;
} void mathfnc(char s[])
{
double op2;
if (strcmp(s, "sin") == )
push(sin(pop()));
else if (strcmp(s, "cos") == )
push(cos(pop()));
else if (strcmp(s, "exp") == )
push(exp(pop()));
else if (strcmp(s, "pow") == )
{
op2 = pop();
push(pow(pop(), op2));
}
else printf("error:%s not supported\n", s);
} //push string back onto the input
void ungets(char s[])
{
int len = strlen(s);
void ungetch(int); while (len > )
ungetch(s[--len]);
}
int getline(char line[], int lim)
{
int c, i;
for (i = ; i < MAXLINE - && c != '\n'; ++i)
{
line[i] = c;
if (c == '\n')
{
line[i] = c;
++i;
}
line[i] = '\0';
}
return ; }
逆波兰表示法计算器(vs2013)
可以完成简单运算(+ - * / %等)以及sin,cos,幂运算和对数运算
以及例如:
3 A = 将3的值复制给A
此后 2 A + 则A的值为5
计算器的换行操作符将输出数值5,同时把5赋值给变量v
如下一个操作是 v 1 + 则结果将是 6
重读The C programming Lanuage 笔记三:简单计算器程序的更多相关文章
- 重读The C programming Lanuage 笔记四:c预处理
C预处理器执行宏替换.条件编译以及包含指定的文件.以#开头的命令行就是与处理器的对象.这些命令行的语法独立于语言的其他部分,它们可以出现在任何地方,其作用可延续到所在编译单元的末尾(与作用域无关).行 ...
- 重读The C programming Lanuage 笔记二:运算符优先级
运算符的优先级和结合性有明确的规定,但是,除少数例外情况外,表达式的求值次序没有定义,甚至某些有副作用的子表达式也没有定义. 也就是说运算符的定义保证了其操作数按某一特定的顺序求值,否则具体实现可以自 ...
- 重读The C programming Lanuage 笔记一:类型转换
首先说自动类型转换: 当一个运算符的几个操作数类型不同时,就需要吧他们转换位某种共同的类型.一般来说,自动转换把“较低”的类型转换为”较高“的类型.运算结果为较高的类型 以下是不严格的规则: 首先,如 ...
- JSP学习笔记(三):简单的Tomcat Web服务器
注意:每次对Tomcat配置文件进行修改后,必须重启Tomcat 在E盘的DATA文件夹中创建TomcatDemo文件夹,并将Tomcat安装路径下的webapps/ROOT中的WEB-INF文件夹复 ...
- JAVA WEB学习笔记(三):简单的基于Tomcat的Web页面
注意:每次对Tomcat配置文件进行修改后,必须重启Tomcat 在E盘的DATA文件夹中创建TomcatDemo文件夹,并将Tomcat安装路径下的webapps/ROOT中的WEB-INF文件夹复 ...
- Linux System Programming 学习笔记(三) 标准缓冲I/O
1. partial block operations are inefficient. The operating system has to “fix up” your I/O by ensuri ...
- 加壳学习笔记(三)-简单的脱壳思路&调试思路
首先一些windows的经常使用API: GetWindowTextA:以ASCII的形式的输入框 GetWindowTextW:以Unicaode宽字符的输入框 GetDlgItemTe ...
- Lex与Yacc学习(六)之lex & yacc (简单计算器程序) 运行
词法分析程序ch3-01.l %{ #include "ch3-01.tab.h" extern int yylval; %} %% [0-9]+ { yylval = atoi( ...
- Learning ROS for Robotics Programming Second Edition学习笔记(三) 补充 hector_slam
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...
随机推荐
- centos设置服务开机自启动
在CentOS或者RedHat其他系统下,如果是后面安装的服务,如httpd.mysqld.postfix等,安装后系统默认不会自动启动的.就算手动执行/etc/init.d/mysqld start ...
- Hibernate缓存配置
一级缓存 Hibernate的一级缓存是由Session提供的,因此它只存在于Session的生命周期中,当程序调用save(),update(),saveorupdate()等方法 及调用查询接口l ...
- Linux中的info指令
Info 是什么?info是一种文档格式,也是阅读此格式文档的阅读器:我们常用它来查看Linux命令的info文档.它以主题的形式把几个命令组织在一起,以便于我们阅读:在主题内以node(节点)的形式 ...
- 如何安装并使用bower包依赖工具
什么是bower Bower是一个客户端技术的软件包管理器,它可用于搜索.安装和卸载如JavaScript.HTML.CSS之类的网络资源.其他一些建立在Bower基础之上的开发工具,如YeoMan和 ...
- 【Backbone】 Backbone初探
前言 在此之前研究了一段React,但是不得不承认React.Vue等MVVM框架相对于原有的Jquery来说,简直是翻天覆地的不同.它们之间的差异不仅仅体现在框架思维的不同,而是ES5到ES6的编程 ...
- 干货云集 WOT 2017全球架构与运维技术峰会揭密技术难点
WOT,World Of Tech专注互联网IT技术领域,是一场不容错过的技术盛会!WOT 2017全球架构与运维技术峰会三大章节,15大技术专场,60+国内外一线互联网精英大咖站台,打造兼顾技术视野 ...
- 背景background
background简写:http://www.cnblogs.com/dunken/p/4380194.html
- ListView的局部刷新
有的列表可能notifyDataSetChanged()代价有点高,最好能局部刷新. 局部刷新的重点是,找到要更新的那项的View,然后再根据业务逻辑更新数据即可. private void upda ...
- Token注解防止表单的重复提交
注解的一些基础: 参见http://blog.csdn.net/duo2005duo/article/details/50505884和 http://blog.csdn.net/duo2005duo ...
- DOM学习笔记--入门1
HTML DOM 是关于如何获取.修改.添加或删除 HTML 元素的标准. 首先节点有很多种,不仅仅HTML元素是节点,尤其 要注意文本节点的存在. 根据 W3C 的 HTML DOM 标准,HTML ...