//简单计算器

 #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 笔记三:简单计算器程序的更多相关文章

  1. 重读The C programming Lanuage 笔记四:c预处理

    C预处理器执行宏替换.条件编译以及包含指定的文件.以#开头的命令行就是与处理器的对象.这些命令行的语法独立于语言的其他部分,它们可以出现在任何地方,其作用可延续到所在编译单元的末尾(与作用域无关).行 ...

  2. 重读The C programming Lanuage 笔记二:运算符优先级

    运算符的优先级和结合性有明确的规定,但是,除少数例外情况外,表达式的求值次序没有定义,甚至某些有副作用的子表达式也没有定义. 也就是说运算符的定义保证了其操作数按某一特定的顺序求值,否则具体实现可以自 ...

  3. 重读The C programming Lanuage 笔记一:类型转换

    首先说自动类型转换: 当一个运算符的几个操作数类型不同时,就需要吧他们转换位某种共同的类型.一般来说,自动转换把“较低”的类型转换为”较高“的类型.运算结果为较高的类型 以下是不严格的规则: 首先,如 ...

  4. JSP学习笔记(三):简单的Tomcat Web服务器

    注意:每次对Tomcat配置文件进行修改后,必须重启Tomcat 在E盘的DATA文件夹中创建TomcatDemo文件夹,并将Tomcat安装路径下的webapps/ROOT中的WEB-INF文件夹复 ...

  5. JAVA WEB学习笔记(三):简单的基于Tomcat的Web页面

    注意:每次对Tomcat配置文件进行修改后,必须重启Tomcat 在E盘的DATA文件夹中创建TomcatDemo文件夹,并将Tomcat安装路径下的webapps/ROOT中的WEB-INF文件夹复 ...

  6. Linux System Programming 学习笔记(三) 标准缓冲I/O

    1. partial block operations are inefficient. The operating system has to “fix up” your I/O by ensuri ...

  7. 加壳学习笔记(三)-简单的脱壳思路&amp;调试思路

    首先一些windows的经常使用API:   GetWindowTextA:以ASCII的形式的输入框   GetWindowTextW:以Unicaode宽字符的输入框   GetDlgItemTe ...

  8. Lex与Yacc学习(六)之lex & yacc (简单计算器程序) 运行

    词法分析程序ch3-01.l %{ #include "ch3-01.tab.h" extern int yylval; %} %% [0-9]+ { yylval = atoi( ...

  9. Learning ROS for Robotics Programming Second Edition学习笔记(三) 补充 hector_slam

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...

随机推荐

  1. 【deep learning学习笔记】注释yusugomori的RBM代码 --- 头文件

    百度了半天yusugomori,也不知道他是谁.不过这位老兄写了deep learning的代码,包括RBM.逻辑回归.DBN.autoencoder等,实现语言包括c.c++.java.python ...

  2. mysql之多列索引

    mysql的多列索引是经常会遇到的问题,怎样才能有效命中索引,是本文要探讨的重点. 多列索引使用的Btree,也就是平衡二叉树.简单来说就是排好序的快速索引方式.它的原则就是要遵循左前缀索引. 多个索 ...

  3. 用Canvas,画中国国旗(Canvas基本知识点)

    .getContext("2d")=======>获取绘图接口 //2d .beginPath()========>创建绘图路径开始点 .moveTo(x,y)==== ...

  4. Jquery datepicker 时间插件使用 js 时间相加,相减

    $(document).ready(function(){ //输入框事件 $('#probation').bind('input propertychange', function() { var ...

  5. grunt--自常用配置文件--js/样式压缩打包,sass工具整合使用

    // Project configuration. module.exports = function(grunt) { // 使用严格模式 'use strict'; // 这里定义我们需要的任务 ...

  6. js Checkbox 传递多个值给后台

    ------前台JS "<input class=\'jTabCheck2\' type=\'checkbox\' partvguid=" + obj + " pr ...

  7. socket select()模型

    转载:http://www.cnblogs.com/xiangshancuizhu/archive/2012/10/05/2711882.html 由于socket recv()方法是阻塞式的,当有多 ...

  8. linux下如何查询jdk安装路径

    1:echo $JAVA_HOME 使用$JAVA_HOME的话能定位JDK的安装路径的前提是配置了环境变量$JAVA_HOME,否则如下所示,根本定位不到JDK的安装路径 [root@localho ...

  9. ArrayList、HashSet、HashTable、List、Dictionary的区别

    在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...

  10. css学习之 display:inline-block;

    设置display:inline-block;后的元素 就是一个格式化为行内元素的块容器( Block container ):通俗讲就是:将对象呈递为内联对象,但是对象的内容作为块对象呈递.旁边的内 ...