| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 555 人关注过本帖
标题:请大家帮忙看一下这个程序吗,怎么修改一下,是算术表达式的
只看楼主 加入收藏
黄若蓝
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-6-28
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:4 
请大家帮忙看一下这个程序吗,怎么修改一下,是算术表达式的
#include<stdio.h>
#include<stdlib.h>



typedef char ElemType;
typedef struct
{
    ElemType *base;
    ElemType *top;
    int StackSize;
}Stack;


int InitStack(Stack *s,int n)
{
    s->base=(ElemType*)malloc(n*sizeof(ElemType));
    if(s->base==NULL)return 0;
    s->top=s->base;
    s->StackSize=n;
    return 1;
}


ElemType GetTop(Stack s)
{
    if(s.top==s.base)return 0;
    return *(s.top-1);
}



int Push(Stack *s,ElemType e)
{
    if(s->top-s->base>=s->StackSize)
        return 0;
    *s->top++=e;
    return 1;
}


int Pop(Stack *s,ElemType *e)
{
    if(s->top==s->base)return 0;
    *e=*--s->top;
    return 1;
}
int Precede(char a)
{
    if(a=='(')
        return 0;
    else if(a=='+'||a=='-')
        return 1;
    else if(a=='*'||a=='/')
        return 2;
    else if(a==')')
        return 3;
}
float operate(char a,char b,char c)
{
    float anwser;
    switch(b)
    {
    case '+':
    return  anwser=((int)a-48)+((int)c-48);break;
    case '-':
    return  anwser=((int)a-48)-((int)c-48);break;
    case '*':
    return  anwser=((int)a-48)*((int)c-48);break;
    case '/':
    return  anwser=((int)a-48)/((int)c-48);break;
    }
   
}



char EvaluateExpressiom()
{
    char c,a,b,theta;
    Stack optr,opnd;
    InitStack(&optr,100);
    Push(&optr,'#');
    InitStack(&opnd,100);
    c=getchar();
    while(c!='#'||GetTop(optr)!='#')
    {
        if(c!='+'||c!='-'||c!='*'||c!='/'||c!='('||c!=')')
        {
            Push(&opnd,c);c=getchar();
        }
        else if(Precede(GetTop(optr))<Precede(c))
        {
                Push(&optr,c);
                c=getchar();
        }
        else if(Precede(GetTop(optr))==Precede(c))
        {
                Pop(&optr,&c);c=getchar();
        }
        else if(Precede(GetTop(optr))>Precede(c))
        {
                theta=GetTop(optr);
                Pop(&optr,&theta);
                a=GetTop(opnd);
                Pop(&opnd,&a);
                b=GetTop(opnd);
                Pop(&opnd,&b);
                Push(&opnd,(char)operate(a,theta,b));
        }
        
    }return GetTop(opnd);
}


int main()
{
    char a;
    printf("请输入算术表达式:");
    a=EvaluateExpressiom();
    printf("%c",a);

    return 1;
}
搜索更多相关主题的帖子: 表达 算术 
2010-06-29 19:01
一口三个汉堡
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:155
专家分:525
注 册:2010-3-21
收藏
得分:14 
发代码的时候,最好能把该注释的都注释了,还有你的思路,不然别人想为解答,还要看你代码,还要了解你要干什么?如过你发出200多行的代码。。要看多久啊。。。就算别人看得下去 ,你可能也等不下去。

坚持做对的事情,而不是容易的事情。
2010-06-29 21:19
黄若蓝
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-6-28
收藏
得分:0 
不好意思啊!一定改
2010-07-01 19:39
黄若蓝
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-6-28
收藏
得分:0 
#include<stdio.h>
#include<stdlib.h>



typedef char ElemType;
typedef struct//设置一个栈
{
    ElemType *base;
    ElemType *top;
    int StackSize;
}Stack;


int InitStack(Stack *s,int n)//定义栈
{
    s->base=(ElemType*)malloc(n*sizeof(ElemType));
    if(s->base==NULL)return 0;
    s->top=s->base;
    s->StackSize=n;
    return 1;
}


ElemType GetTop(Stack s)//取栈顶元素
{
    if(s.top==s.base)return 0;
    return *(s.top-1);
}



int Push(Stack *s,ElemType e)//入栈
{
    if(s->top-s->base>=s->StackSize)
        return 0;
    *s->top++=e;
    return 1;
}


int Pop(Stack *s,ElemType *e)、//出栈
{
    if(s->top==s->base)return 0;
    *e=*--s->top;
    return 1;
}
int Precede(char a)//设置运算符优先级
{
    if(a=='(')
        return 0;
    else if(a=='+'||a=='-')
        return 1;
    else if(a=='*'||a=='/')
        return 2;
    else if(a==')')
        return 3;
}
float operate(char a,char b,char c)//把运算符由字符转为实际运算操作
{
    float anwser;
    switch(b)
    {
    case '+':
    return  anwser=((int)a-48)+((int)c-48);break;
    case '-':
    return  anwser=((int)a-48)-((int)c-48);break;
    case '*':
    return  anwser=((int)a-48)*((int)c-48);break;
    case '/':
    return  anwser=((int)a-48)/((int)c-48);break;
    }
   
}



char EvaluateExpressiom()//计算操作
{
    char c,a,b,theta;
    Stack optr,opnd;
    InitStack(&optr,100);
    Push(&optr,'#');
    InitStack(&opnd,100);
    c=getchar();
    while(c!='#'||GetTop(optr)!='#')
    {
        if(c!='+'||c!='-'||c!='*'||c!='/'||c!='('||c!=')')
        {
            Push(&opnd,c);c=getchar();
        }
        else if(Precede(GetTop(optr))<Precede(c))
        {
                Push(&optr,c);
                c=getchar();
        }
        else if(Precede(GetTop(optr))==Precede(c))
        {
                Pop(&optr,&c);c=getchar();
        }
        else if(Precede(GetTop(optr))>Precede(c))
        {
                theta=GetTop(optr);
                Pop(&optr,&theta);
                a=GetTop(opnd);
                Pop(&opnd,&a);
                b=GetTop(opnd);
                Pop(&opnd,&b);
                Push(&opnd,(char)operate(a,theta,b));
        }
        
    }return GetTop(opnd);
}


int main()
{
    char a;
    printf("请输入算术表达式:");
    a=EvaluateExpressiom();
    printf("%c",a);

    return 1;
}
在麻烦各位高手看一下
2010-07-01 19:59
黄若蓝
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-6-28
收藏
得分:0 
回复 2楼 一口三个汉堡
能不能麻烦您在看一下啊
2010-07-01 20:01
快速回复:请大家帮忙看一下这个程序吗,怎么修改一下,是算术表达式的
数据加载中...
 
   



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

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