
//简单计算器 #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