| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 673 人关注过本帖
标题:利用栈实现表达式求值的问题(c语言数据结构)
只看楼主 加入收藏
夜清纯
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-6-12
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
利用栈实现表达式求值的问题(c语言数据结构)
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define STACK_SIZE 20
#define STACKINCREMENT 1
typedef struct stack_num{
    int *top_n;
    int *base_n;
    int stacksize1;
}sqstack_N;
typedef struct stack_op{
    char *top_o;
    char *base_o;
    int stacksize2;
}sqstack_O;


void Initstack_optr(sqstack_O *s);
void Initstack_opnd(sqstack_N *s);
void push(int n,sqstack_N *s);
int  pop(sqstack_N *s);
void enter_stack(char ch,sqstack_O *s);
char pop_stack(sqstack_O *s);
char get_top(sqstack_O *s);
char comp(char p,char q);
int operate(int a,char theta,int b);

int main(void)
{
    sqstack_N  s1;
    sqstack_O  s2;
    int ch;
    char theta;
    int n,a,b;
    Initstack_optr(&s2);
    Initstack_opnd(&s1);
    puts("enter the data");
    ch=getchar();
    while(ch!='#'||get_top(&s2)!='#')
    {
        if(isdigit(ch))
        {
            n=ch-48;
            push(n,&s1);
            ch=getchar();
        }
        else
            switch(comp(get_top(&s2),ch))
        {
            case '<':{
                enter_stack(ch,&s2);
                ch=getchar();
                break;
                     }
            case '=':{
                ch=getchar();
                break;
                     }
            case '>':{
                theta=pop_stack(&s2);
                a=pop(&s1);
                b=pop(&s1);
                push(operate(a,theta,b),&s1);
                ch=getchar();
                break;
                     }
        }
    }
    printf("the result is %d\n", pop(&s1));
    return 0;
}
void Initstack_optr(sqstack_O *s)
{
    s->base_o=(char *)malloc(STACK_SIZE*sizeof (char));
    if(!s->base_o)
        exit(1);
    s->stacksize2=STACK_SIZE;
    s->base_o[0]='#';
    s->top_o=s->base_o+1;
}
void Initstack_opnd(sqstack_N *s)
{
    s->base_n=(int *)malloc(STACK_SIZE*sizeof (int));
    if(!s->base_n)
        exit(2);
    s->top_n=s->base_n;
    s->stacksize1=STACK_SIZE;
}
void push(int n,sqstack_N *s)
{
    if(s->top_n-s->base_n>=s->stacksize1)
    {
        s->base_n=(int *)realloc(s->base_n,(s->stacksize1+STACKINCREMENT)*sizeof(int));
        if(!s->base_n)
            exit(3);
        s->top_n=s->base_n+s->stacksize1;
        s->stacksize1+=STACKINCREMENT;
    }
    *(s->top_n)++=n;
}
int pop(sqstack_N *s)
{
    int n;
    if(s->top_n==s->base_n)
        return 0;
    n=*--(s->top_n);
    return n;
}
void enter_stack(char ch,sqstack_O *s)
{
    if(s->top_o-s->base_o>=s->stacksize2)
    {
        s->base_o=(char *)realloc(s->base_o,(s->stacksize2+STACKINCREMENT)*sizeof(char));
        if(!s->base_o)
            exit(3);
        s->top_o=s->base_o+s->stacksize2;
        s->stacksize2+=STACKINCREMENT;
    }
    *(s->top_o)++=ch;
}
char pop_stack(sqstack_O *s)
{
    char ch;
    if(s->top_o==s->base_o)
        return 0;
    ch=*--(s->top_o);
    return ch;
}
char get_top(sqstack_O *s)
{
    char ch;
    if(s->top_o==s->base_o)
        return 0;
    ch=*(s->top_o-1);
    return ch;
}

char comp(char p,char q)
{
    char m[7]="+-*/()#";
    char n[7][7]=
    {
        {'>','>','<','<','<','>','>'},
        {'>','>','<','<','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'<','<','<','<','<','=',' '},
        {'>','>','>','>',' ','>','>'},
        {'<','<','<','<','<',' ','='}
    };
    int i=0,j=0;
    while(m[i]!=p)
        i++;
    while(m[j]!=q)
        j++;
    return n[i][j];
}
int operate(int a,char theta,int b)
{
    int res;
    switch(theta)
    {
        case '+':{
            res=a+b;
            break;}
        case '-':{
            res=a-b;
            break;
                 }
        case '*':{
            res=a*b;
            break;
                 }
        case '/':{
            if(b!=0)
                res=a/b;
            else
                printf("error in / with b=0");
            break;
                 }
    }
    return res;
}
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define STACK_SIZE 20
#define STACKINCREMENT 1
typedef struct stack_num{
    int *top_n;
    int *base_n;
    int stacksize1;
}sqstack_N;
typedef struct stack_op{
    char *top_o;
    char *base_o;
    int stacksize2;
}sqstack_O;


void Initstack_optr(sqstack_O *s);
void Initstack_opnd(sqstack_N *s);
void push(int n,sqstack_N *s);
int  pop(sqstack_N *s);
void enter_stack(char ch,sqstack_O *s);
char pop_stack(sqstack_O *s);
char get_top(sqstack_O *s);
char comp(char p,char q);
int operate(int a,char theta,int b);

int main(void)
{
    sqstack_N  s1;
    sqstack_O  s2;
    int ch;
    char theta;
    int n,a,b;
    Initstack_optr(&s2);
    Initstack_opnd(&s1);
    puts("enter the data");
    ch=getchar();
    while(ch!='#'||get_top(&s2)!='#')
    {
        if(isdigit(ch))
        {
            n=ch-48;
            push(n,&s1);
            ch=getchar();
        }
        else
            switch(comp(get_top(&s2),ch))
        {
            case '<':{
                enter_stack(ch,&s2);
                ch=getchar();
                break;
                     }
            case '=':{
                ch=getchar();
                break;
                     }
            case '>':{
                theta=pop_stack(&s2);
                a=pop(&s1);
                b=pop(&s1);
                push(operate(a,theta,b),&s1);
                ch=getchar();
                break;
                     }
        }
    }
    printf("the result is %d\n", pop(&s1));
    return 0;
}
void Initstack_optr(sqstack_O *s)
{
    s->base_o=(char *)malloc(STACK_SIZE*sizeof (char));
    if(!s->base_o)
        exit(1);
    s->stacksize2=STACK_SIZE;
    s->base_o[0]='#';
    s->top_o=s->base_o+1;
}
void Initstack_opnd(sqstack_N *s)
{
    s->base_n=(int *)malloc(STACK_SIZE*sizeof (int));
    if(!s->base_n)
        exit(2);
    s->top_n=s->base_n;
    s->stacksize1=STACK_SIZE;
}
void push(int n,sqstack_N *s)
{
    if(s->top_n-s->base_n>=s->stacksize1)
    {
        s->base_n=(int *)realloc(s->base_n,(s->stacksize1+STACKINCREMENT)*sizeof(int));
        if(!s->base_n)
            exit(3);
        s->top_n=s->base_n+s->stacksize1;
        s->stacksize1+=STACKINCREMENT;
    }
    *(s->top_n)++=n;
}
int pop(sqstack_N *s)
{
    int n;
    if(s->top_n==s->base_n)
        return 0;
    n=*--(s->top_n);
    return n;
}
void enter_stack(char ch,sqstack_O *s)
{
    if(s->top_o-s->base_o>=s->stacksize2)
    {
        s->base_o=(char *)realloc(s->base_o,(s->stacksize2+STACKINCREMENT)*sizeof(char));
        if(!s->base_o)
            exit(3);
        s->top_o=s->base_o+s->stacksize2;
        s->stacksize2+=STACKINCREMENT;
    }
    *(s->top_o)++=ch;
}
char pop_stack(sqstack_O *s)
{
    char ch;
    if(s->top_o==s->base_o)
        return 0;
    ch=*--(s->top_o);
    return ch;
}
char get_top(sqstack_O *s)
{
    char ch;
    if(s->top_o==s->base_o)
        return 0;
    ch=*(s->top_o-1);
    return ch;
}

char comp(char p,char q)
{
    char m[7]="+-*/()#";
    char n[7][7]=
    {
        {'>','>','<','<','<','>','>'},
        {'>','>','<','<','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'<','<','<','<','<','=',' '},
        {'>','>','>','>',' ','>','>'},
        {'<','<','<','<','<',' ','='}
    };
    int i=0,j=0;
    while(m[i]!=p)
        i++;
    while(m[j]!=q)
        j++;
    return n[i][j];
}
int operate(int a,char theta,int b)
{
    int res;
    switch(theta)
    {
        case '+':{
            res=a+b;
            break;}
        case '-':{
            res=a-b;
            break;
                 }
        case '*':{
            res=a*b;
            break;
                 }
        case '/':{
            if(b!=0)
                res=a/b;
            else
                printf("error in / with b=0");
            break;
                 }
    }
    return res;
}
搜索更多相关主题的帖子: include void c语言 表达式 
2012-09-28 22:17
夜清纯
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-6-12
收藏
得分:0 
求各位高手指点  运行不出结果
2012-09-28 22:18
遗矢的老人
Rank: 9Rank: 9Rank: 9
来 自:成都
等 级:蜘蛛侠
威 望:7
帖 子:325
专家分:1131
注 册:2012-7-20
收藏
得分:10 
错误提示也贴出来啊,要不很费时
2012-09-28 23:58
Demoon
Rank: 2
等 级:论坛游民
帖 子:16
专家分:34
注 册:2012-9-12
收藏
得分:10 
char comp(char p,char q)
{
    char m[7]="+-*/()#";           //空间不够,至少要m[8]
    char n[7][7]=
    {
        {'>','>','<','<','<','>','>'},
        {'>','>','<','<','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'<','<','<','<','<','=',' '},
        {'>','>','>','>',' ','>','>'},
        {'<','<','<','<','<',' ','='}
    };
2012-09-29 14:43
快速回复:利用栈实现表达式求值的问题(c语言数据结构)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017619 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved