重读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 ...
随机推荐
- Ubuntu 12.04(所有ubuntu发行版都适用)sudo免输入密码
首先执行以下命令(该命令用来修改 /etc/sudoers 文件): $ sudo gedit /etc/sudoers 然后把 %sudo ALL=(ALL:ALL) ALL 这行注释掉, ...
- Linux内核 hlist_head/hlist_node结构解析
内核中的定义: struct hlist_head { struct hlist_node *first;}; struct hlist_node { struct hlist_node ...
- Java 使用httpclient Post与cxf 发布的Webservice通信
使用cxf发布的webservice不知道什么情况总会有时管用有时不管用,对于项目来说这肯定不行.又不想改动webservice因为代码太多.人懒! 于是便使用httpclient与webservic ...
- this和$(this)
this指的是javascript对象而$(this)就是就jquery对象 jQuery.prototype.test=function(){ this.css("color", ...
- Hadoop入门实例——WordCount统计单词
首先要说明的是运行Hadoop需要jdk1.6或以上版本,如果你还没有搭建好Hadoop集群,请参考我的另一篇文章: Linux环境搭建Hadoop伪分布模式 马上进入正题. 1.启动Hadoop集群 ...
- 使用spring-data-redis操作redis
redis.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
- 使用百度富文本编辑器UEditor碰到的问题
前面使用的是kindEditor,但是发现这个已经不再维护,并且bug不少,而我又不会改,哈哈,所以我就放弃了. 原来想过要用百度的这个UEditor,但是在配置的时候遇到了很多问题,基本上加载不出来 ...
- UIView类绘图出现错误提示
一:问题: Jan 16 15:49:53 CUBOT Band Ⅲ[2082] <Error>: CGContextSetLineWidth: invalid context 0x0. ...
- soa服务治理-dubbo
dubbo官网:http://dubbo.io/Home-zh.htm 学习点: 1. 日志的配置
- 开通域名绑定DDNS
一.初衷 我想要有一个自己的域名,然后有自己的server,在server上搭一个网站或者开通一个ftp服务,我想通过这个域名来访问它. 二.什么是DDNS DDNS 动态dns,电信宽带采用拨号联网 ...