您的位置:首页 > 其它

栈的应用--简单计算器---加减乘除

2015-08-14 18:52 246 查看
源文件 1
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#define emptytos (-1)//空栈
#define minstacksize (5)//最小栈大小
//typedef char element;
#define element int
//顺序栈的实现
typedef struct stackrecord
{
        int capacity;
        int topofstack;
        element *array;
}*stack, stackrecord;
void calloc_error(const stack s)
/**
*判断内存分配是否正常
*/
{
        if (s == NULL)
        {
                printf("calloc error");
                //exit(0);
        }
}
void array_error(const element* a)
/**
*判断分配是否正常
*/
{
        if (a == NULL)
        {
                printf("calloc error");
                //exit(0);
        }
}
void makeempty(stack s)//使栈为空
/**
*使栈为空
*/
{
        s->topofstack = emptytos;
}
int isempty(const stack s)//判断栈是否为空
/**
*判断栈是否为空
*/
{
        return s->topofstack == emptytos;
}
int isfull(const stack s)
/**
*判断栈是否满
*/
{
        return (s->topofstack + 1) >= s->capacity;
}
int stack_size(const stack s)
/**
*判断栈的大小
*/
{
        return s->capacity;
}
void free_stack(stack s)
/**
*销毁栈
*/
{
        if (s != NULL)
        {
                free(s->array);
                free(s);
        }
}
stack createstack(const int maxelement)
/**
*创建栈
*/
{
        stack s;
        if (maxelement < minstacksize)
        {
                printf("maxelement is too small!!");
                //exit(1);
        }
        s = calloc(1, sizeof(stackrecord));
        calloc_error(s);
        s->array = (element*)calloc(maxelement, sizeof(element));
        array_error(s->array);
        s->capacity = maxelement;
        makeempty(s);
        return s;
}
int error(const char *__format, ...)
/**
*报错printf
*/
{
        register int __retval;
        __builtin_va_list __local_argv;
        __builtin_va_start( __local_argv, __format );
        __retval = __mingw_vprintf( __format, __local_argv );
        __builtin_va_end( __local_argv );
        return __retval;
        //exit(0);
}
void push(element data, stack s)
/**
*进栈
*/
{
        if (isfull(s))
        {
                error("stack is full!error!!");
        }
        else if (s->capacity < 1)
        {
                error("stack is not initialize!!");
        }
        else
        {
                s->topofstack++;
                s->array[s->topofstack] = data;
        }
}
element top(const stack s)
/**
*返回栈顶的数据
*/
{
        if (!isempty(s))
        {
                return s->array[s->topofstack];
        }
        error("s is error");
        return 0;
}
void pop(stack s)
/**
*出栈
*/
{
        if (isempty(s))
        {
                error("stack is empty");
        }
        else
        {
                s->topofstack--;
        }
}
element top_and_pop(stack s)
/**
*返回栈顶的数据 并弹出栈
*/
{
        if ((!isempty(s)))
        {
                return s->array[s->topofstack--];
        }
        error("s is error");
        return 0;
}
int priority(const char a)
/**
*算数表达式优先级
*/
{
        switch (a)
        {
        case '+':
        case '-':
                return 3;
                break;
        case '#':
                return 0;
                break;
        case '*':
        case '/':
        case '%':
                return 4;
                break;
        case '(':
        case ')':
                return 5;
                break;
        }
}
int char_of_intsize(const char a)//返回给定字符的asill值
{
        return a - '0';
}
void display_stack(const stack s)//显示栈元素
{
        int index = s->topofstack;
        while (index > -1)
        {
                printf("%c ", s->array[index--]);
        }
}
void display_int_stack(const stack s)//显示栈元素
{
        int index = s->topofstack;
        while (index > -1)
        {
                printf("%d ", s->array[index--]);
        }
}
extern void infix_to_suffix(const char *s, stack z1, stack z2)//中缀转后缀
{
        int b = 0;
        while (s[b] != '#')
        {
                if (char_of_intsize(s[b]) >=0)//如果是操作数
                {
                        push(s[b], z1);
                        b++;
                }
                else if (isempty(z2))//如果临时栈为空
                {
                        push(s[b], z2);
                        b++;
                }
                else
                {
                        if (priority(z2->array[z2->topofstack]) < priority(s[b]))//如果当前数的优先级大于栈顶的优先级
                        {
                                if (s[b] != ')')//如果当前数不是右圆括号
                                {
                                        push(s[b], z2);
                                        b++;
                                }
                                else if (s[b] == ')')//如果当前数是右圆括号
                                {
                                        while (top(z2) != '(')//括号处理
                                        {
                                                push(top_and_pop(z2), z1);//括号里面的操作符 处理
                                        }
                                        if (top(z2) == '(')//从临时栈中弹出 右括号
                                        {
                                                pop(z2);
                                        }
                                        b++;
                                }
                        }
                        else if (priority(z2->array[z2->topofstack]) >= priority(s[b]))
                        {
                                if (z2->array[z2->topofstack] == '(') //如果存在括号
                                {
                                        push(s[b], z2);
                                        b++;
                                }
                                else
                                {
                                        while (priority(z2->array[z2->topofstack]) >= priority(s[b]) && z2->array[z2->topofstack] != '(')
                                        {
                                                push(top_and_pop(z2), z1);
                                                push(s[b], z2);
                                                b++;
                                        }
                                }
                        }
                }
        }
        while (!isempty(z2))//临时栈的数据 放到数据栈
        {
                push(top_and_pop(z2), z1);
        }
        while (!isempty(z1))//数据栈的数据 倒序放到 临时栈
        {
                push(top_and_pop(z1), z2);
        }
}
static int operator(const stack s)
{
        return char_of_intsize(s->array[s->topofstack]) < 0;
}
extern void switch_type_stackrecord(const int type)
{
        switch (type)
        {
        case 1://栈数据类型转换为int
#ifdef element
#undef element
#define element int
#else
#define element int
#endif // element
                break;
        case 2://栈数据类型转换为char
#ifdef element
#undef element
#define element char
#else
#define element char
#endif // element
                break;
        case 3://栈数据类型转换为float
#ifdef element
#undef element
#define element float
#else
#define element float
#endif // element
                break;
        case 4://栈数据类型转换为double
#ifdef element
#undef element
#define element double
#else
#define element double
#endif // element
                break;
        default://转换错误
                printf("switch_type_stackrecord of the parameter is error !!");
                break;
        }
}
int calculator(char a, stack s)
{
        int result;//保存结果
        switch (a)
        {
        case'-':
                result = (s->array[s->topofstack - 1]) - (s->array[s->topofstack]);
                pop(s);
                pop(s);
                push(result, s);
                break;
        case'+':
                result = (s->array[s->topofstack - 1]) + (s->array[s->topofstack]);
                pop(s);
                pop(s);
                push(result, s);
                break;
        case'*':
                result = (s->array[s->topofstack - 1]) * (s->array[s->topofstack]);
                pop(s);
                pop(s);
                push(result, s);
                break;
        case'/':
                if ( s->array[s->topofstack] != 0)
                {
                        result = ( s->array[s->topofstack - 1]) / (s->array[s->topofstack]);
                }
                else
                {
                        result = 0;
                        printf("除数不能为0!");
                }
                pop(s);
                pop(s);
                push(result, s);
                break;
        case'%':
                if ( s->array[s->topofstack] != 0)
                {
                        result = (s->array[s->topofstack - 1] ) % ( s->array[s->topofstack]);
                }
                else
                {
                        result = 0;
                        printf("取余数不能为0!!");
                }
                pop(s);
                pop(s);
                push(result, s);
                break;
        default://遇到无法识别的操作符
                printf("operator error!!");
                return 0;
                break;
        }
}
extern void simple_calculator(stack z1, stack z2) //中缀计算器--简单
{
        free_stack(z1);//clean栈
        switch_type_stackrecord(1);//栈数据类型换成int
        z1 = createstack(15);//创建一个数据类型为int的栈
        while (!isempty(z2))
        {
                if (!operator(z2))//判断栈顶是否为操作数
                {
                        //printf("s=%d\n",char_of_intsize(top(z2)));
                        push(char_of_intsize(top_and_pop(z2)), z1);//把操作数弹出 并压入z1栈

                }
                else
                {
                        calculator(top_and_pop(z2), z1);//遇到操作符 计算
                        //printf("sum=%d\n",top(z1));
                }
        }
        free_stack(z2);
}


源文件二

#include "20150518.c"
int main()
{
        stack z1, z2;
        z1 = createstack(15);//数据栈
        z2 = createstack(15);//临时栈
        char *s = "2-2*4/(2*0)#";//3*3-3+3*(7-9)
        infix_to_suffix(&s[0], z1, z2);
        printf("z2=");
        display_stack(z2);
        simple_calculator(z1, z2);
        printf("\nz1=");
        display_int_stack(z1);
        return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: