| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 679 人关注过本帖
标题:写了个求表达式的程序但是不能编译请求答案
只看楼主 加入收藏
中国
Rank: 1
等 级:新手上路
帖 子:218
专家分:0
注 册:2009-1-4
结帖率:90.91%
收藏
已结贴  问题点数:20 回复次数:4 
写了个求表达式的程序但是不能编译请求答案
#include<stdio.h>
#define max 100
typedef struct
{
    int top;
    char b[max];
}node;
void chushihua(node * s)//初始化堆栈
{
    s->top=0;
}
int ruzhan(node * s,char a)//入栈
{
    if(s->top>=max)
        return 0;
    else
    {
        s->b[s->top]=a;
        s->top++;
        return 1;
    }
    

}
int chuzhan(node * s,char * d)//出栈
{
    if(s->top<=0)
        return 0;
    else
    {
            s->top--;
        * d=s->b[s->top];
            return 1 ;
    }
}
int qu(node s,char * d)//取头结点
{
   if(s.top<=0)
       return 0;
   else
   {
       * d=s.b[s.top];
            return 1 ;
   }
}
int panduan(char c)//判断c是否是计算符号
{
    if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='=')
        return 1;
    else
        return 0;
}
int calcute(int x,int y,char c)//计算x与y通过c运算符求出的结果
{
    int result;
    switch(c)
    {
    case'+': result=x+y;break;
    case'-': result=x-y;break;
    case '*': result = x * y;break;
    case '/':
             if (y == 0)
             {
                printf("Divided by zero!\n");
                return 0;
             }
             else
             {
                 result = x / y;
                 break;
             }
    default:
             printf("Bad Input.\n");
             return 0;
    }
    return result;
}
int daxiao(char x1,char x2)//比较运算符x1与x2的优先级
{
    switch(x1)
    {
    case'+':
    case'-':
        if(x2=='+'||x2=='-')
            return 1;
        else if(x2=='*'||x2=='/')
            return 0;
        else if(x2=='(')
            return 0;
        else if(x2==')')
            return 1;
        else
            return 1;break;
    case'*':
    case'/':
        if(x2=='+'||x2=='-')
            return 1;
        else if(x2=='*'||x2=='/')
            return 1;
        else if(x2=='(')
            return 0;
        else if(x2==')')
            return 1;
        else
            return 1;break;
    case'(':
        if(x2=='+'||x2=='-')
            return 0;
        else if(x2=='*'||x2=='/')
            return 0;
        else if(x2=='(')
            return 0;
        else if(x2==')')
            return 2;
        else
            return 1;break;
    case')':
            if(x2=='+'||x2=='-')
            return 1;
        else if(x2=='*'||x2=='/')
            return 1;
        
        else if(x2==')')
            return 1;
        else
            return 1;break;
    case'#':
            if(x2=='+'||x2=='-')
            return 0;
        else if(x2=='*'||x2=='/')
            return 0;
        
        else if(x2=='(')
            return 0;
        else
            return 0;break;
    default:break;
    }
}
void jisuan(node * s1,node * s2,node * s3)//把s1的中缀表达式借助s2转换为s3的后缀表达式
{

    char c,d;
    char abc[max];
    int i=0;
    scanf("%s",abc);
    while(abc[i]!='\0')
    {
        ruzhan(s1,abc[i]);
        i++;
    }
    ruzhan(s1,'#');
    ruzhan(s2,'#');
    while(s1->top>=0)
    {
        chuzhan(s1,&c);
        if((panduan(c))==0)
            ruzhan(s3,c);
        else
        {
            qu(*s2,&d);
            if((daxiao(d,c))==0)
                ruzhan(s2,c);
        
          else if((daxiao(d,c))==1)
        {
            chuzhan(s2,&d);
            ruzhan(s3,d);
        }
          else
          {
              chuzhan(s2,&d);
          }
        }
    }
}
char zhuji(node * s3)//用堆栈计算后缀表达式
{
    char d,a,b,,f;
         char e;
    node s4;
    char c;
    qu(*s3,&f);
    while((panduan(f))==0)
    {
        chuzhan(s3,&d);
        ruzhan(&s4,d);
    }
    while(s4.top>=0)
    {
        chuzhan(s3,&d);
        chuzhan(&s4,&a);
        chuzhan(&s4,&b);
        c=calcute(a,b,d);
        ruzhan(&s4,c);
    }
    qu(s4,&e);
    return e;
}
void main()
{
  node s1,s2,s3,s4;
  chushihua(&s1);
  chushihua(&s2);
  chushihua(&s3);
  chushihua(&s4);
  jisuan(&s1,&s2,&s3);
  printf("%d",zhuji(&s3));
}
搜索更多相关主题的帖子: 堆栈 表达式 
2009-08-11 11:30
中国
Rank: 1
等 级:新手上路
帖 子:218
专家分:0
注 册:2009-1-4
收藏
得分:0 
谁帮忙看下啊
感谢啊
2009-08-11 17:51
中国
Rank: 1
等 级:新手上路
帖 子:218
专家分:0
注 册:2009-1-4
收藏
得分:0 
我将程序修改如下,但是还是编译出来什么东西也没有
程序只有一个警告warning C4715: 'daxiao' : not all control paths return a value
我觉得应该没有关系吧,请大家帮帮我吧
#include<stdio.h>
#define max 100
typedef struct
{
    int top;
    char b[max];
}node;
 
void chushihua(node * s)
{
    s->top=0;
}
int ruzhan(node * s,char a)
{
    if(s->top>=max)
        return 0;
    else
    {
        s->b[s->top]=a;
        s->top++;
        return 1;
    }
     
 
}
int chuzhan(node * s,char * d)
{
    if(s->top<=0)
        return 0;
    else
    {
            s->top--;
        * d=s->b[s->top];
            return 1 ;
    }
}
int qu(node s,char * d)
{
   if(s.top<=0)
       return 0;
   else
   {
       * d=s.b[s.top-1];
            return 1 ;
   }
}
int panduan(char c)
{
    if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='=')
        return 1;
    else  
        return 0;
}
int calcute(int x,int y,char c)
{
    int result;
    switch(c)
    {
    case'+': result=x+y;break;
    case'-': result=x-y;break;
    case '*': result = x * y;break;
    case '/':  
             if (y == 0)
             {
                printf("Divided by zero!\n");
                return 0;
             }
             else
             {
                 result = x / y;
                 break;
             }
    default:  
             printf("Bad Input.\n");  
             return 0;
    }
    return result;
}
int daxiao(char x1,char x2)
{
    switch(x1)
    {
    case'+':
    case'-':
        if(x2=='+'||x2=='-')
            return 1;
        else if(x2=='*'||x2=='/')
            return 0;
        else if(x2=='(')
            return 0;
        else if(x2==')')
            return 1;
        else
            return 1;break;
    case'*':
    case'/':
        if(x2=='+'||x2=='-')
            return 1;
        else if(x2=='*'||x2=='/')
            return 1;
        else if(x2=='(')
            return 0;
        else if(x2==')')
            return 1;
        else
            return 1;break;
    case'(':
        if(x2=='+'||x2=='-')
            return 0;
        else if(x2=='*'||x2=='/')
            return 0;
        else if(x2=='(')
            return 0;
        else if(x2==')')
            return 2;
        else
            return 1;break;
    case')':
            if(x2=='+'||x2=='-')
            return 1;
        else if(x2=='*'||x2=='/')
            return 1;
         
        else if(x2==')')
            return 1;
        else
            return 1;break;
    case'#':
            if(x2=='+'||x2=='-')
            return 0;
        else if(x2=='*'||x2=='/')
            return 0;
         
        else if(x2=='(')
            return 0;
        else
            return 0;break;
    default:break;
    }
}
void jisuan(node * s1,node * s2,node * s3)
{
 
    char c,d;
    char abc[max];
    int i=0;
    scanf("%s",abc);
    ruzhan(s1,'#');
    while(abc[i]!='\0')
          i++;
    while(i>=0)
    {
        ruzhan(s1,abc[i-1]);
        i--;
    }
    ruzhan(s2,'#');
    while(s1->top>0)
    {                     
        chuzhan(s1,&c);
        if((panduan(c))==0)
            ruzhan(s3,c);
        else
        {
            qu(*s2,&d);
            if((daxiao(d,c))==0)
                ruzhan(s2,c);
         
          else if((daxiao(d,c))==1)
        {
            chuzhan(s2,&d);
            ruzhan(s3,d);
        }
          else
          {
              chuzhan(s2,&d);
          }
        }
    }
}
char zhuji(node * s3)
{
    char d,a,b,f;
    char e;
    node s4,s5;
    char c;
     chushihua(&s4);
     chushihua(&s5);
    while(s3->top>0)
    {
        chuzhan(s3,&f);
        ruzhan(&s5,f);
    }
    qu(s5,&f);
    while((panduan(f))==0)
    {
        chuzhan(&s5,&d);
        ruzhan(&s4,d);
    }
    while(s4.top>1)
    {
        qu(s5,&d);
        if((panduan(d))==0)
        {
           chuzhan(&s5,&d);
            ruzhan(&s4,d);
        }
        else
        {
        chuzhan(&s5,&d);
        chuzhan(&s4,&a);
        chuzhan(&s4,&b);
        c=calcute(a,b,d);
        ruzhan(&s4,c);
        }
    }
    qu(s4,&e);
    return e;
}
void main()
{
  node s1,s2,s3;
  chushihua(&s1);
  chushihua(&s2);
  chushihua(&s3);
  jisuan(&s1,&s2,&s3);
  printf("%d",zhuji(&s3));
}
2009-08-14 11:52
中国
Rank: 1
等 级:新手上路
帖 子:218
专家分:0
注 册:2009-1-4
收藏
得分:0 
无人问津啊
2009-08-25 14:15
龙心
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:14
专家分:159
注 册:2009-8-20
收藏
得分:20 
没看完 不过感觉你这样的定义栈可能会消耗大量的内存
2009-08-25 15:34
快速回复:写了个求表达式的程序但是不能编译请求答案
数据加载中...
 
   



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

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