计算器(1)

#include "Calculator.h"
#include "error.h"
#include <stdio.h>

int Num_Init (Num *s)
{
	if(s == NULL)
	{
		return FALSE;
	}
	
	s->top = -1;
	return TRUE;
}

int Ch_Init (Ch *s)
{
	if(s == NULL)
	{
		return FALSE;
	}
	
	s->top = -1;
	return TRUE;
}

int Num_Empty (Num *s)
{
	if(s == NULL)
	{
		return FALSE;
	}
	
	return s->top == -1;
}

int Ch_Empty (Ch *s)
{
	if(s == NULL)
	{
		return FALSE;
	}
	
	return s->top == -1;
}

int Num_Full (Num *s)
{
	if(s == NULL)
	{
		return FALSE;
	}
	
	return s->top == (SIZE-1);
}

int Ch_Full (Ch *s)
{
	if(s == NULL)
	{
		return FALSE;
	}
	
	return s->top == (SIZE-1);
}

int Num_Push (Num *s, int x)
{
	if(s == NULL)
	{
		return FALSE;
	}
	if(Num_Full(s))
	{
		return FALSE;
	}
	
	s->num[++s->top] = x;
	
	return TRUE;
}

int Ch_Push (Ch *s, char x)
{
	if(s == NULL)
	{
		return FALSE;
	}
	if(Ch_Full(s))
	{
		return FALSE;
	}
	
	s->ch[++s->top] = x;
	
	return TRUE;
}

int Num_Pop (Num *s, int *x)
{
	if(s == NULL)
	{
		return FALSE;
	}
	if(Num_Empty(s))
	{
		return FALSE;
	}
	
	*x = s->num[s->top--];
	
	return TRUE;
}

int Ch_Pop (Ch *s, char *x)
{
	if(s == NULL)
	{
		return FALSE;
	}
	if(Ch_Empty(s))
	{
		return FALSE;
	}
	
	*x = s->ch[s->top--];
	
	return TRUE;
}

int Num_GetTop (Num *s, int *x)
{
	if(s == NULL)
	{
		return FALSE;
	}
	if(Num_Empty(s))
	{
		return FALSE;
	}
	
	*x = s->num[s->top];
	
	return TRUE;
}

int Ch_GetTop (Ch *s, char *x)
{
	if(s == NULL)
	{
		return FALSE;
	}
	if(Ch_Empty(s))
	{
		return FALSE;
	}
	
	*x = s->ch[s->top];
	
	return TRUE;
}

//判断是不是数字
int math (char s)
{
	if(s>='0' && s<='9')
		return TRUE;
	return FALSE;
}

//得到计算数
int getmath (int a, int sum)
{
	int b = sum;
	b = b*10 + a;
	return b;
}	

int Jud(Ch *s, char ch)//判断是否要入栈
{
	if(s->top == -1)//栈顶元素为空,入栈
	{
		return TRUE;
	}
	else
	{
		switch (s->ch[s->top])//对栈顶元素判断
		{
			case '+':
			case '-':
			{
				if(ch == '+' || ch == '-' || ch == ')')//加减和右括号不进栈,其余的进栈
				{
					return  FALSE;
				}
				else
				{
					return TRUE;
				}
				break;
			}
			
			case '*':
			case '/':
			{
				if ( ch =='(')//乘除除了左括号进栈其他不进栈
				{
					return TRUE;
				}
				else
				{
					return FALSE;
				}
				break;
			}
			case '(':
			{
				return TRUE;//左括号全部进栈
				break;
			}
		}
	}
}
//运算
int Calcu(int a, int b, char c)
{


	switch(c)
	{
		case '+':
			return a + b;
		case '-':
			return a - b;
		case '*':
			return a * b;
		case '/':
			return a / b;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值