| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 550 人关注过本帖
标题:算术表达式求值
只看楼主 加入收藏
寻愁觅恨
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-11-23
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
算术表达式求值
写了一个算术表达式求值的代码
各位给指点下能否再精简点
#include<stdlib.h>
#include<stdio.h>
#define stack_init_size1 40
#define stack_init_size2 20
#define stackincrement1 40
#define stackincrement2 20
typedef struct{
    int *base;
    int *top;
    int stacksize;
}s_stack;
typedef struct{
    char *base;
    char *top;
    int stacksize;
}f_stack;

static char youxian[7][7]=
    { // '+' '-' '*' '/' '(' ')' '#'
        {'>','>','<','<','<','>','>'},
        {'>','>','<','<','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'>','>','>','>','<','>','>'},
        {'<','<','<','<','<','=','>'},
        {'>','>','>','>','>','>','>'},
        {'<','<','<','<','<','>','='}
    };

int in(char c)
{
    switch(c)
    {
    case '+': return 0;
    case '-': return 1;
    case '*': return 2;
    case '/': return 3;
    case '(': return 4;
    case ')': return 5;
    case '#':return 6;
    default:return 9;
    }

}

void s_push(s_stack *s,int e)
{

     if(s->top-s->base>=s->stacksize)
     {
         s->base=(int *)realloc(s->base,(s->stacksize+stackincrement1)*sizeof(int));
         if(!s->base) exit(0);
         s->top=s->base+s->stacksize;
         s->stacksize+=stackincrement1;
     }
    *s->top=e; s->top++;
    /*printf("the s_stack has the data:%d\n",*(s->top-1));*/
}
void  f_push(f_stack *f,char e)
{
     if(f->top-f->base>=f->stacksize)
     {
         f->base=(char *)realloc(f->base,(f->stacksize+stackincrement2)*sizeof(char));
         if(!f->base) exit (0);
         f->top=f->base+f->stacksize;
         f->stacksize+=stackincrement2;
     }
     *f->top=e;f->top++;
    /* printf("the f_stack has the sign:%c\n",*(f->top-1));*/
}

int s_gettop(s_stack *s)
{
    s->top--;
    return *s->top;
}

char f_gettop(f_stack *f)
{
    f->top--;
    return *f->top;
}

char panduan(f_stack *f,char c)
{
    int m,n;
    char w;
    m=in(c);
    w=*(f->top-1);
    n=in(w);
    return youxian[n][m];
}

int yunsuan(int p,char r,int q)
{
        switch(r)
    {
        case '+': return q+p;
        case '-': return q-p;
        case '*': return q*p;
        case '/': return q/p;
    }
}
int main()

{
    int h=0,jieguo,wrong;
    char c;
    s_stack s;
    f_stack f;
    s.base=(int*)malloc(stack_init_size1*sizeof(int));
    if(!s.base) return 0;
    s.top=s.base;
    s.stacksize=stack_init_size1;

    f.base=(char*)malloc(stack_init_size2*sizeof(char));
    if(!f.base) return 0;
    f.top=f.base;
    f.stacksize=stack_init_size2;
    *f.top='#';f.top++;

    printf("please press the shizi end by enter!\n");
    c=getchar();
    while(c!='\n')
    {   int t;
        int p,q;
        char r;
        if(in(c)==9)
        {   if(h>0)
         {
            t=s_gettop(&s);
            s_push(&s,t*10+c-48);
         }
         else
             s_push(&s,c-48);
         h++;
         c=getchar();
        }
        else
        switch(panduan(&f,c))
        {
          case '<':
               f_push(&f,c);c=getchar();h=0;break;
          case '=':
               f.top--;
               f.stacksize--;
               c=getchar();h=0;break;
          case '>':
               p=s_gettop(&s);
               q=s_gettop(&s);
               r=f_gettop(&f);
             if(r=='/'&&p==0)
               {wrong=1;break;}
             else
               { jieguo=yunsuan(p,r,q);
                 s_push(&s,jieguo);h=0;break;
               }
        }
    }
    while((c=='\n')&&(*(f.top-1)!='#'))
    {
        int p,q;
        char r;
        p=s_gettop(&s);
        q=s_gettop(&s);
        r=f_gettop(&f);
    if(r=='/'&&p==0)
        {wrong=1;break;}
    else
        jieguo=yunsuan(p,r,q);
        s_push(&s,jieguo);
    }
    if(wrong==1)
        printf("It is wrong!\n");
    else
        printf("the answer is %d\n",s_gettop(&s));
}
 
 
搜索更多相关主题的帖子: 算术 求值 表达 
2010-12-07 16:11
outsider_scu
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:3
帖 子:430
专家分:1333
注 册:2010-10-21
收藏
得分:20 
没注释,我就不看了,不过你可以让他处理符点数的。
/*数字字符串转换成实数*/
double StringToReal(char *s)
{
    double result;
    char dummy;

    if (s == NULL) printf("NULL string passed to StringToReal");
    sscanf(s, " %lg %c", &result, &dummy) ;
    return (result);
}

编程的道路上何其孤独!
2010-12-07 16:50
寻愁觅恨
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-11-23
收藏
得分:0 
回复 2楼 outsider_scu
请问如何能让程序实现负数相加
2010-12-08 07:32
快速回复:算术表达式求值
数据加载中...
 
   



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

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