栈实现四则运算
2011-07-15 10:05
225 查看
#include <stdio.h>
#include <ctype.h> //header file of isdigit
#define MAXSIZE 100//the max length of the expresion
char ch[7] = {'+','-','*','/','(',')','#'};
int in[7] = {3,3,5,5,1,6,0};//the order of operator in the stack
int out[7] = {2,2,4,4,6,1,0};//the order of operator out the stack
struct mystack
{
int stack[MAXSIZE];
int top;
};
typedef struct mystack mystack;
void Initstack(mystack *s)//initial the stack
{
s->top = 0;
}
void Push(mystack *s,int x)//to push a number into a stack
{
s->top++;
s->stack[s->top] = x;
}
void Pop(mystack *s,int *x)//to pop a number out of a stack
{
*x = s->stack[s->top];
s->top--;
}
int Gettop(mystack s)//to get the top number of a stack
{
return s.stack[s.top];
}
int Trans(char c)//to translate the operator to a number in order
{
switch(c)
{
case '+':return 0;break;
case '-':return 1;break;
case '*':return 2;break;
case '/':return 3;break;
case '(':return 4;break;
case ')':return 5;break;
default :return 6;break;
}
}
char Compare(char c1,char c2)//compare the order of the operator according to the array given at the front and return a char
{
int i1 = Trans(c1);
int i2 = Trans(c2);
if(in[i1] < out[i2])
return '<';
else if(in[i1] > out[i2])
return '>';
else
return '=';
}
int Operate(int a,int op,int b)//do the calculate according to the number in the array
{
switch (op)
{
case 0:return a+b;break;
case 1:return a-b;break;
case 2:return a*b;break;
default:return a/b;break;
}
}
int Evaluate()//deal with the expression inputed char by char
{
char c;
int sum,op,a,b,x;
int j = 1,k = 1;
mystack operator,character;//operator for the operator stack,character for the number stack
Initstack(&operator);
Push(&operator,Trans('#'));
Initstack(&character);
c = getchar();
if (c == '+')
c = getchar();//ignore the '+' in front of the expression
else if (c == '-')
{
c = getchar();//ignore the '-' in front of the expresion
j = 0;//mark the negative number
}
while(c != '#'|| ch[Gettop(operator)] != '#')//the condition of the cycle
{
if(isdigit(c))//include in the ctype.h to judge whether it is a number
{
sum = 0;
while(isdigit(c))
{
if(j)
sum = sum * 10 + (c - '0');
else
sum = sum * 10 - (c - '0');
c = getchar();
}
Push(&character,sum);//push the inputed number into the character stack
j = 1;//back the mark
}
else
{
switch(Compare(ch[Gettop(operator)],c))
{
case '>'://do the calculte if the operator in the stack is bigger than the operator out of the stack
Pop(&operator,&op);
Pop(&character,&b);//notice the pop order
Pop(&character,&a);
Push(&character,Operate(a,op,b));
// c = getchar();//no need to getchar
break;
case '<':
Push(&operator,Trans(c));
c = getchar();
break;
case '='://very important //about '(' and ')'
Pop(&operator,&x);//pop the '(' in the stack
c = getchar();
break;
}
}
}
return Gettop(character);//the result of the expression
}
int main()
{ int result;
printf("Input the expression:\n");
result = Evaluate();
printf("The result is :%d\n",result);
}来公司自己写的第一个小程序,关于输入格式不正确等出错信息目前还没有调试。
#include <ctype.h> //header file of isdigit
#define MAXSIZE 100//the max length of the expresion
char ch[7] = {'+','-','*','/','(',')','#'};
int in[7] = {3,3,5,5,1,6,0};//the order of operator in the stack
int out[7] = {2,2,4,4,6,1,0};//the order of operator out the stack
struct mystack
{
int stack[MAXSIZE];
int top;
};
typedef struct mystack mystack;
void Initstack(mystack *s)//initial the stack
{
s->top = 0;
}
void Push(mystack *s,int x)//to push a number into a stack
{
s->top++;
s->stack[s->top] = x;
}
void Pop(mystack *s,int *x)//to pop a number out of a stack
{
*x = s->stack[s->top];
s->top--;
}
int Gettop(mystack s)//to get the top number of a stack
{
return s.stack[s.top];
}
int Trans(char c)//to translate the operator to a number in order
{
switch(c)
{
case '+':return 0;break;
case '-':return 1;break;
case '*':return 2;break;
case '/':return 3;break;
case '(':return 4;break;
case ')':return 5;break;
default :return 6;break;
}
}
char Compare(char c1,char c2)//compare the order of the operator according to the array given at the front and return a char
{
int i1 = Trans(c1);
int i2 = Trans(c2);
if(in[i1] < out[i2])
return '<';
else if(in[i1] > out[i2])
return '>';
else
return '=';
}
int Operate(int a,int op,int b)//do the calculate according to the number in the array
{
switch (op)
{
case 0:return a+b;break;
case 1:return a-b;break;
case 2:return a*b;break;
default:return a/b;break;
}
}
int Evaluate()//deal with the expression inputed char by char
{
char c;
int sum,op,a,b,x;
int j = 1,k = 1;
mystack operator,character;//operator for the operator stack,character for the number stack
Initstack(&operator);
Push(&operator,Trans('#'));
Initstack(&character);
c = getchar();
if (c == '+')
c = getchar();//ignore the '+' in front of the expression
else if (c == '-')
{
c = getchar();//ignore the '-' in front of the expresion
j = 0;//mark the negative number
}
while(c != '#'|| ch[Gettop(operator)] != '#')//the condition of the cycle
{
if(isdigit(c))//include in the ctype.h to judge whether it is a number
{
sum = 0;
while(isdigit(c))
{
if(j)
sum = sum * 10 + (c - '0');
else
sum = sum * 10 - (c - '0');
c = getchar();
}
Push(&character,sum);//push the inputed number into the character stack
j = 1;//back the mark
}
else
{
switch(Compare(ch[Gettop(operator)],c))
{
case '>'://do the calculte if the operator in the stack is bigger than the operator out of the stack
Pop(&operator,&op);
Pop(&character,&b);//notice the pop order
Pop(&character,&a);
Push(&character,Operate(a,op,b));
// c = getchar();//no need to getchar
break;
case '<':
Push(&operator,Trans(c));
c = getchar();
break;
case '='://very important //about '(' and ')'
Pop(&operator,&x);//pop the '(' in the stack
c = getchar();
break;
}
}
}
return Gettop(character);//the result of the expression
}
int main()
{ int result;
printf("Input the expression:\n");
result = Evaluate();
printf("The result is :%d\n",result);
}来公司自己写的第一个小程序,关于输入格式不正确等出错信息目前还没有调试。
相关文章推荐
- 利用System.Linq.Expressions实现四则运算计算器(一)
- 四则运算表达式实现
- week2-结对编程【网页实现四则运算】
- 支持多位数,括号,四则运算,的计算器算法c++实现
- 字符串四则运算表达式的求解(中缀表达式实现,模拟笔算的方法)
- 实现一个Add函数,让两个数相加,但是不能使用+、-、*、/等四则运算符。
- 作业:实现0-10的随机四则运算
- 分别用C++和JavaScript 实现四则运算表达式求值
- java实现一个整数和一个小数的四则运算和求最大值,平均值。
- java实现四则运算,难点主要在理解加减乘除优先级以及使用递归
- 程序实现自动生成30道四则运算题目(2)
- java 四则运算 栈的实现
- 3.结对编程成果报告(小学生四则运算的出题程序,Java实现)
- 用PHP实现的四则运算表达式计算实现代码
- 栈实现简单的四则运算表达式
- 位运算实现四则运算
- 如何处理加括号的四则混合运算表达式——基于二叉树的实现(Eclipse平台 Java版)
- 【实践】js实现简易的四则运算计算器
- C语言实现四则混合运算
- 基于51单片机设计简单计算器,实现6位有效四则运算的程序设计(汇编调用C)