| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 476 人关注过本帖
标题:这个程序哪里错了,求大神指教
只看楼主 加入收藏
讨厌数构
Rank: 1
等 级:新手上路
帖 子:32
专家分:4
注 册:2013-10-21
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:1 
这个程序哪里错了,求大神指教
#include<iostream>
using namespace std;
//求表达式
//============================
struct NumberNode
{
    float data;
    struct NumberNode *next;
};
//定义数据栈
//===========================
struct OPTRNode
{
    char ch;
    struct OPTRNode *next;
};
//=========================
//操作数栈
void push_number(NumberNode *&s,float x);
void pop_number(NumberNode *&s,float &x);
void create_number(NumberNode *&s);
float GetTop_number(NumberNode *&s);
//===========================
//操作符栈
void push_operator(OPTRNode *&r,char e);
void pop_operator(OPTRNode *&r,char &y);
void create_operator(OPTRNode *&r);
char GetTop_operator(OPTRNode * &r);
//==============================

//==============================
bool ISnumber(char e);
bool ISchar(char e);
char compare(char a1,char a2,char b[5],char c[5][5]);
float evaluate(float a,float b,char op);
float Getnumber(char e);
//=========================
int main()
{   
    char a,y;
    float x1,x2,sum;
    char b[5]={'+','-','*','/','#'};
    char c[5][5]={
        {'>','>','<','<','>'},
        {'>','>','<','<','>'},
        {'>','>','>','>','>'},
        {'>','>','>','>','>'},
        {'<','<','<','<','='},
    };
    //定义运算符优先级集合
    NumberNode *s;
    create_number(s);
    OPTRNode *r;
    create_operator(r);         
    cout<<"请输入一段以'#'结束的表达式!"<<endl;   
    push_operator(r,'#');
    a=getchar();   
     while(r->next!=NULL)
     {
        if(ISnumber(a))
        {
            push_number(s,Getnumber(a));
            //    a=getchar();
        }
        else
        {
            y=GetTop_operator(r);            
            switch(compare(y,a, b, c))
            {
            case '<':
                push_operator(r,a);
                cout<<GetTop_operator(r)<<endl;   
            //    a=getchar();
                break;
            case '>':
                {   
                    pop_operator(r,y);
                    pop_number(s,x1);
                    pop_number(s,x2);
                    sum=evaluate(x1,x2,y);
                    push_number(s,sum);
                //    a=getchar();
                    break;
                    
                }
            case'=':
                cout<<"运算结束!"<<endl;
                cout<<GetTop_number(s)<<endl;
                exit(1);
            }
        }
        a=getchar();
    }
    cout<<GetTop_number(s)<<endl;
    return 1;
}
//===================================
void push_number(NumberNode *&s,float x)
{
    NumberNode *p=new NumberNode;
    if(!p)
    {
        cout<<"内存分配不成功!"<<endl;
        exit(1);
    }
    p->data=x;
    p->next=s->next;
    s->next=p;
}
//============================

void pop_number(NumberNode *&s,float &x)
{
   
    if(s->next==NULL)
    {
        cout<<"栈空!"<<endl;
        exit(1);
    }
    NumberNode *p=new NumberNode;
    p=s->next;
    x=p->data;
    s->next=p->next;
    delete p;
}
//出栈
//============================

void create_number(NumberNode *&s)
{
    s=new NumberNode;
    s->next=NULL;
}
//===========================
float GetTop_number(NumberNode *&s)
{
    float e;
    if(s->next==NULL)
    {
        cout<<"栈空!"<<endl;
        exit(1);
    }
    e=s->next->data;
    return e;
}
//==================================
//============================================
//================================
//操作符栈
void push_operator(OPTRNode *&r,char e)
{
    OPTRNode *p=new OPTRNode;
    if(!p)
    {
        cout<<"内存分配不成功!"<<endl;
        exit(1);
    }
    p->ch=e;
    p->next=r->next;
    r->next=p;
}
//============================

void pop_operator(OPTRNode *&r,char &y)
{
   
    if(r->next==NULL)
    {
        cout<<"栈空!"<<endl;
        exit(1);
    }
    OPTRNode *p=new OPTRNode;
    p=r->next;
    y=p->ch;
    r->next=p->next;
   
}
//============================

void create_operator(OPTRNode *&r)
{
    r=new OPTRNode;
    r->next=NULL;
}
//===========================
char GetTop_operator(OPTRNode *&r)
{  
    char e;
    if(r->next==NULL)
    {
        cout<<"栈空!"<<endl;
        exit(1);
    }
    e=r->next->ch;
    return e;
}
//=======================
//操作符栈
//=============================

bool ISnumber(char e)
{
    if(e>=48&&e<=57)
        return true;
    else
        return false;
}
//判断是否是数字
//===============================
bool ISchar(char e)
{
    if(e=='+'||e=='-'||e=='*'||e=='/'||e=='#')
        return true;
    else
        return false;
}
//判断是否是运算符

float Getnumber(char e)
{
   
    return(e-48);
}
//将字符转换为数字
//===========================================
char compare(char a1,char a2,char b[5],char c[5][5] )
{
    char d;
    int i,j,m,n;
    for(i=0;i<5;i++)
    {
        if(b[i]==a1)
            m=i;
    }
    for(j=0;j<5;j++)
    {
        if(b[j]==a2)
            n=j;
    }
    d=c[m][n];
    return d;
}
//比较两个运算符的优先级
//======================================
float evaluate(float a,float b,char op)
{
    if(op=='+')
        return (a+b);
    if(op=='-')
        return (a-b);
    if(op=='*')
        return (a*b);
    if(op=='/')
        return (a/b);
    else
    {
        cout<<"输入错误,不能运算!"<<endl;
        exit(1);
    }
}
//定义四种运算
搜索更多相关主题的帖子: include 表达式 
2013-10-21 23:21
Osiris9
Rank: 4
来 自:九柱
等 级:业余侠客
帖 子:28
专家分:225
注 册:2013-10-25
收藏
得分:20 
他究竟是神仙的化身还是地狱的使者,没人知道!

干旱时死去................丰水时重生!
2013-10-26 01:29
快速回复:这个程序哪里错了,求大神指教
数据加载中...
 
   



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

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