| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3945 人关注过本帖
标题:谁能做表达式求值啊 求助!!!!!!!!!!!!!!
只看楼主 加入收藏
红色兔子
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-3-9
收藏
得分:0 
我是这样写的
#include<stdio.h>
#include<conio.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define OK 1
#define FALSE 0
#define OVERFLOW -2
#define ERROR 0
typedef int Status;
typedef char SElemType1;
typedef int SElemType2;
char OP[7]={'+','-','*','/','(',')','#'};
typedef struct
{
    SElemType1 *base;
    SElemType1 *top;
    int stacksize;
}CharStack;
typedef struct
{
    SElemType2 *base;
    SElemType2 *top;
    int stacksize;
}IntStack;
Status initIntStack(IntStack *I)
{
    (*I).base=(SElemType2 *)malloc(STACK_INIT_SIZE*sizeof(SElemType2));
        if(!(*I).base) exit(OVERFLOW);
        (*I).top=(*I).base;
        (*I).stacksize=STACK_INIT_SIZE;
        return OK;
}
Status initCharStack(CharStack *C)
{
    (*C).base=(SElemType1 *)malloc(STACK_INIT_SIZE*sizeof(SElemType1));
        if(!(*C).base) exit(OVERFLOW);
        (*C).top=(*C).base;
        (*C).stacksize=STACK_INIT_SIZE;
        return OK;
}
Status desintStack(IntStack *I)
{
    (*I).top=NULL;
    free((*I).base);
    (*I).base=NULL;
    return OK;
}
Status descharStack(CharStack *C)
{
    (*C).top=NULL;
    free((*C).base);
    (*C).base=NULL;
    return OK;
}
int getinttop(IntStack I)
{
    SElemType2 e;
    if(I.top==I.base) return ERROR;
    e=*(I.top-1);
    return e;
}
char getchartop(CharStack C)
{
    SElemType1 e;
    if(C.top==C.base) return ERROR;
    e=*(C.top-1);
    return e;
}
Status pushchar(CharStack *C,SElemType1 e)
{
    if((*C).top-(*C).base>=(*C).stacksize)
    {
        (*C).base=(SElemType1 *)realloc((*C).base,((*C).stacksize+STACKINCREMENT)*sizeof(SElemType1));
        if(!(*C).base) exit(OVERFLOW);
        (*C).top=(*C).base+(*C).stacksize;
    (*C).stacksize+=STACKINCREMENT;
    }
    *(*C).top++=e;
    return OK;
}
Status pushint(IntStack *I,SElemType2 e)
{
    if((*I).top-(*I).base>=(*I).stacksize)
    {
        (*I).base=(SElemType2 *)realloc((*I).base,((*I).stacksize+STACKINCREMENT)*sizeof(SElemType2));
        if(!(*I).base) exit(OVERFLOW);
        (*I).top=(*I).base+(*I).stacksize;
        (*I).stacksize+=STACKINCREMENT;
    }
    *(*I).top++=e;
    return OK;
}
Status popchar(CharStack *C,SElemType1 *e)
{
    if((*C).top==(*C).base) return ERROR;
    *e=*(--(*C).top);
    return OK;
}
Status popint(IntStack *I,SElemType2 *e)
{
    if((*I).top==(*I).base) return ERROR;
    *e=*(--(*I).top);
    return OK;
}
Status In(char c,char OP[])
{
    int i;
    for(i=0;i<=6;i++)
        if(c==OP[i]) return TRUE;
    return FALSE;
}
int Transfer(char c)
{
    int k;
    switch(c)
    {
        case '+':k=0;break;
        case '-':k=1;break;
        case '*':k=2;break;
        case '/':k=3;break;
        case '(':k=4;break;
        case ')':k=5;break;
        case '#':k=6;break;
    }
    return k;
}
char Precede(char c1,char c2)
{
    int i,j;
    char a[7][7]={{'>','>','<','<','<','>','>'},
                  {'>','>','<','<','<','>','>'},
                  {'>','>','>','>','<','>','>'},
                  {'>','>','>','>','<','>','>'},
                  {'<','<','<','<','<','=',''},
                  {'>','>','>','>','','>','>'},
                  {'<','<','<','<','<','','='}};
    i=Transfer(c1);
    j=Transfer(c2);
    return(a[i][j]);
}
int Operate(int a,char c,int b)
{
    int z;
    switch(c)
    {
         case '+':z=b+a;break;
         case '-':z=b-a;break;
         case '*':z=b*a;break;
         case '/':z=b/a;break;
    }
    return z;
}
 int EvaluateExpression()
 {
     int flag=0,num,a,b,answer;
     char c,x;
     IntStack NUM;
     CharStack OPTR;
     initCharStack(&OPTR);
     pushchar(&OPTR,'#');
     initIntStack(&NUM);
     c=getchar();
     while(c!='#'||getchartop(OPTR)!='#')
     {
         if(!In(c,OP))
         {
             if(flag==1)
             {
                 popint(&NUM,&num);
                 num=num*10+(c-'0');
                 pushint(&NUM,num);
             }
             else
             {
                 pushint(&NUM,c-'0');
                 flag=1;
             }
             c=getchar();
         }
         else
         {
             switch(Precede(getchartop(OPTR),c))
             {
             case '<':pushchar(&OPTR,c);c=getchar();break;
             case '=':popchar(&OPTR,&x);c=getchar();break;
             case '>':popchar(&OPTR,&x);
                      popint(&NUM,&a);
                      popint(&NUM,&b);
                      pushint(&NUM,Operate(a,x,b));
                      break;
             }/*switch*/
             flag=0;
         }
     }/*while*/
     answer=getinttop(NUM);
     desintStack(&NUM);
     descharStack(&OPTR);
     return answer;
 }
main()
{
    int loop=1,answer;
    while(loop)
    {
        clrscr();
        printf("\n\n\n\t\tPlease input your biaodashi:(end with '#')");
        answer=EvaluateExpression();
        printf("\n\t\tThe answer is : %d",answer);
        printf("\n\t\tOnce again?(1/0)");
        scanf("%d%*c",&loop);
    }
    printf("\n\n\n\t\tThank you for your useing!");
    getch();
}
2008-05-25 11:01
满江风
Rank: 1
等 级:新手上路
帖 子:102
专家分:0
注 册:2007-10-30
收藏
得分:0 
哈哈。我正找这个呢!
2008-05-25 16:57
快速回复:谁能做表达式求值啊 求助!!!!!!!!!!!!!!
数据加载中...
 
   



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

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