用c语言实现简易计算器的四则运算

chengluxiao 2011-11-23 12:02:32
要求用上数据结构的栈方法,应该是用数组和指针实现的吧,请高手们赐教!!谢啦
...全文
198 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
chengluxiao 2011-11-24
  • 打赏
  • 举报
回复
谢啦!
尘缘udbwcso 2011-11-24
  • 打赏
  • 举报
回复

//表达式求值
#include <stdio.h>
#include <malloc.h>
#include <string.h>
/*
*功能:根据运算符计算
*参数:a, b参与运算的数, ch运算符
*返回值:计算结果,操作符错误则返回0
*/
int cal(int a, char ch, int b)
{
switch(ch)
{
case '+':
return a+b;
break;
case '-':
return a-b;
break;
case '*':
return a*b;
break;
case '/':
return a/b;
break;
}
return 0;
}
/*
*功能:计算表达式的值(用数组模拟栈)
*参数:表达式字符串
*返回值:计算结果
*/
int evaluateExpression(char *str)
{
int i = 0, result, numSub = 0, operSub = 0;
int tmp, len = strlen(str);
int *operand = (int*)malloc(sizeof(int)*len);
char *operat = (char*)malloc(sizeof(char)*len);
while(str[i] != '\0')
{
switch(str[i])
{
case '+':
while(operSub > 0 && operat[operSub-1] != '(')
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
operat[operSub++] = '+';
break;
case '-':
while(operSub > 0 && operat[operSub-1] != '(')
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
operat[operSub++] = '-';
break;
case '*':
if(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = 0;
while(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = tmp * 10 + str[i+1] - '0';
++i;
}
--i;
printf("%d * %d = ", operand[numSub-1], tmp);
operand[numSub-1] = cal(operand[numSub-1], '*', tmp);
printf("%d\n", operand[numSub-1]);
++i;
}
else
operat[operSub++] = '*';
break;
case '/':
if(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = 0;
while(str[i+1] >= '0' && str[i+1] <= '9')
{
tmp = tmp * 10 + str[i+1] - '0';
++i;
}
--i;
printf("%d / %d = ", operand[numSub-1], tmp);
operand[numSub-1] = cal(operand[numSub-1], '/', tmp);
printf("%d\n", operand[numSub-1]);
++i;
}
else
operat[operSub++] = '/';
break;
case '(':
operat[operSub++] = '(';
break;
case ')':
while(operat[operSub-1] != '(')
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
--operSub;
break;
default:
tmp = 0;
while(str[i] >= '0' && str[i] <= '9')
{
tmp = tmp * 10 + str[i] - '0';
++i;
}
--i;
operand[numSub++] = tmp;
break;
}
++i;
}
while(numSub > 1 && operSub >= 1)
{
printf("%d %c %d = ", operand[numSub-2], operat[operSub-1], operand[numSub-1]);
operand[numSub-2] = cal(operand[numSub-2], operat[operSub-1], operand[numSub-1]);
printf("%d\n", operand[numSub-2]);
--numSub;
--operSub;
}
result = operand[numSub-1];
free(operand);
free(operat);
return result;
}
int main()
{
char *str = "225/15-20+(4-3)*2";
int result;
printf("计算过程:\n");
result = evaluateExpression(str);
printf("计算结果:result = %d\n", result);
return 0;
}

计算过程:
225 / 15 = 15
15 - 20 = -5
4 - 3 = 1
1 * 2 = 2
-5 + 2 = -3
计算结果:result = -3
编程点滴 2011-11-23
  • 打赏
  • 举报
回复

# include <stdio.h>
# include <stdlib.h>

# define STACK_INIT_SIZE 20
# define INCREASE_STACK_SIZE 10
# define ERROR 0
# define OK 1
typedef int SElemType;

struct SqStack
{
SElemType *base;
SElemType *top;
int size;
};

int initStack(SqStack &s)
{
s.base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if(! s.base)
return ERROR;
s.top = s.base;
s.size = STACK_INIT_SIZE;
return OK;
}

int push(SqStack &s, SElemType e)
{
if(s.top - s.base >= s.size)
{
s.base = (SElemType*)realloc(s.base, (s.size + INCREASE_STACK_SIZE) * sizeof(SElemType));
if(! s.base)
return ERROR;
s.top = s.base + s.size;
s.size += INCREASE_STACK_SIZE;
}
* s.top ++ = e;
return OK;
}

int pop(SqStack &s, SElemType &e)
{
if(s.top == s.base)
return ERROR;
e = * -- s.top;
return OK;
}

int getTop(SqStack s, SElemType &e)
{
if(s.top == s.base)
return ERROR;
e = * (s.top - 1);
return OK;
}

bool isOperator(SElemType e)
{
switch(e)
{
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '=': return true;
default : return false;
}
}

SElemType operate(SElemType a, SElemType op, SElemType b)
{
SElemType c;
switch(op)
{
case '+': c = a + b; break;
case '-': c = a - b; break;
case '*': c = a * b; break;
case '/': c = a / b; break;
}
return c;
}

SElemType precede(SElemType a, SElemType b)
{
SElemType f = '=';
switch(b)
{
case '+':
case '-':
if(a == '(' || a == '=')
f = '<';
else
f = '>';
break;
case '*':
case '/':
if(a == '*' || a == '/' || a == ')')
f = '>';
else
f = '<';
break;
case '(':
if(a == ')')
{
printf("表达式有错误\n");
exit(0);
}
else
f = '<';
break;
case ')':
if(a == '(')
f = '=';
else if(a == '=')
{
printf("表达式有错误\n");
exit(0);
}
else
f = '>';
break;
case '=':
if(a == '(')
{
printf("表达式有错误\n");
exit(0);
}
else if(a == '=')
f = '=';
else
f = '>';
break;
}
return f;
}

SElemType expressionValue()
{
char c;
SElemType s1, s2, op, x;
SqStack opera, num;
initStack(opera);
initStack(num);
push(opera, '=');
c = getchar();
getTop(opera, x);
while((c != '=') || x != '=')
{
if(isOperator(c))
{
switch(precede(x, c))
{
case '<':
push(opera, c);
c = getchar();
break;
case '=':
pop(opera, x);
c = getchar();
break;
case '>':
pop(num, s2);
pop(num, s1);
pop(opera, op);
push(num, operate(s1, op, s2));
break;
}
}
else if(c >= '0' && c <= '9')
{
s1 = c - '0';
c = getchar();
while(c <= '9' && c >= '0')
{
s1 = s1 * 10 + c - '0';
c = getchar();
}
push(num, s1);
}
else
{
printf("表达式有错误\n");
exit(0);
}
getTop(opera, x);
}
pop(num, x);
return x;
}

int main()
{
printf("%d\n", expressionValue());
return 0;
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧