//简单计算器

 #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. Eclipse相关集锦

    开场白,之前的个人博客写过很多细小的Eclipse的东西,这里将搬过来,作为整体一篇. 1.Eclipse提示失效 解决:window->Preferences->Java->Edi ...

  2. 密码配置配置SSH免密码登陆

    在本文中,我们主要介绍密码配置的内容,自我感觉有个不错的建议和大家分享下 我的用户名是master 1.安装ssh(若没安装的话) sudo apt-get install ssh 2.配置为可以免密 ...

  3. java参数传递(值传递还是引用传递)

    Java中的参数传递机制一直以来大家都争论不休,究竟是“传值”还是“传址(传引用)”,争论的双方各执一词,互不相让.不但“菜鸟”们一头雾水,一些“老鸟”也只知道结果却说不出所以然来.我相信看过下面的内 ...

  4. SmoOne——开源免费的企业移动OA应用,基于VS.Net

    一.SmoOne是什么一个开源的移动OA应用 二.语言.Net 三.开发环境Visual Studio 四.开发平台Smobiler Designer 五.功能该应用开源代码中包含注册.登录.用户信息 ...

  5. 学习web之路

    一些文章是转接过来,有些是自己原创的.希望大家喜欢,By:xiaohaimian' q:963892669

  6. apache2部署django以及静态文件

    django中的runserver只是一个很简单的web服务器,在开发中是不建议使用的,django在官方中建议是使用apache2等web服务器来配置,并且django会把静态文件交由apache2 ...

  7. openstack私有云布署实践【9.1 Glance镜像管理(科兴环境)】

    首先登录kxcontroller1创建kx_glance数据库,并赋于远程和本地访问的权限.      mysql -u root -p   CREATE DATABASE kx_glance; GR ...

  8. LintCode ---- 刷题总结

    对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始).如果不存在,则返回 -1. 基本:两重for循 ...

  9. 将Cygwin Emacs设为Windows explorer默认打开程序

    由于我在平日的学习与工作中会经常用到Cygwin中的Emacs,很自然地想到应该将emacsclient作为指定文件类型在Windows explorer中的默认打开程序.这样,便可以直接双击文件后在 ...

  10. 【Android-UI】包含多个子View时触发父节点的焦点事件

    今天遇到个问题: 在 LinearLayout 中添加了好几个其他视图 View 之后,点击时不能获得焦点,导致绑定的 onClick 事件不能触发. 解决办法: 对 LinearLayout 添加属 ...