计算器(1)

xiaoxiao2021-02-28  92

#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; } }
转载请注明原文地址: https://www.6miu.com/read-70630.html

最新回复(0)