| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 886 人关注过本帖
标题:用C语言求中缀表达式 找错误。。
只看楼主 加入收藏
QIAOCSGO
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-8-25
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
用C语言求中缀表达式 找错误。。
使用了栈 抄了书  编译无错误 运行时会R6002 求大神帮忙找一下为什么不行,是不是后面的main函数出错了?
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100        //栈的最大容量100//
#define TRUE 1
#define FALSE 0
#define NULL 0
typedef int elemtype;

typedef struct
{ elemtype stack[MAXSIZE];
int top;
}sqstack;                 //定义顺序栈类型sqstack//
sqstack *s;                //定义sqstack类型的变量 s//
void InitStack(sqstack *s);
int push(sqstack *s,elemtype x);
elemtype pop(sqstack *s);  
elemtype gettop(sqstack *s);
int empty(sqstack *s);
void display_stack(sqstack *s);

void InitStack(sqstack *s)            //初始化栈//
{s->top=-1;}
int push(sqstack *s,elemtype x)      //入栈操作//
{if(s->top >=MAXSIZE-1)
{printf("stack overflow!\n");return(FALSE);}
else
{s->top++;
s->stack[s->top]=x;
return(TRUE);}
}
elemtype pop(sqstack *s)       //出栈操作//
{ if(s->top<0)
{printf("stack underflow!\n");
return(NULL);
}
else
{s->top--;return(s->stack[s->top+1]);}
}
elemtype gettop(sqstack *s)    //获取栈顶元素//
{if (s->top<0)
{printf("stack is empty\n");
return(NULL);}
else
return(s->stack[s->top]);}
int empty(sqstack *s)         
{if(s->top<0)
return(TRUE);
else
return(FALSE);}
void display_stack(sqstack *s)
{int i;
printf("the elements is the stack are:\n");
for(i=s->top;i>=0;i--)
printf("%d",s->stack[i]);
}







char exp[MAXSIZE];          //存储转换的后缀表达式//

void transform(sqstack *s,char exp[MAXSIZE])
{                                                                   //将算术表达式改为后缀表达式//
char str[MAXSIZE];                   //存储原来的算术表达式//
char ch;
int sum,i,j,t;
s->top=0;
printf("算术表达式是:\n");
i=0;
do
{i++;                                //获取输入的表达式//
scanf("%c",&str[i]);}
while(str[i]!='#'&&i!=MAXSIZE);
sum=i;                         //记录总的数目//
t=1;
i=1;
ch=str[i];
i++;
while(ch!='#')
{switch(ch)
{ case'(':                                //判定为左括号//
        s->top++;
        s->stack[s->top]=ch;
        break;
  case')':                                //判定为右括号//
        while(s->stack[s->top]!='(')
        {exp[t]=s->stack[s->top];
        s->top--;
        t++;
        }
        s->top--;
        break;
  case'+':                              //判定为加减号//
  case'-':
      while(s->top!=0 && s->stack[s->top]!='(')
      {
          exp[t]=s->stack[s->top];
          s->top--;
          t++;
      }
      s->top++;
      s->stack[s->top]=ch;
      break;
  case'*':                                   //判定为* /   //
  case'/':
      while (s->stack[s->top]=='*'||s->stack[s->top]=='/')
      {
         exp[t]=s->stack[s->top];
         s->top--;
         t++;
      }
      s->top++;
      s->stack[s->top]=ch;
      break;
  case' ':
      break;
  default:
       while (ch>='0'&&ch<='9')                      //判定为数字//
       {exp[t]=ch;
       t++;
       ch=str[i];
       i++;}
       i--;
       exp[t]='#';
       t++;
}
    ch=str[i];
    i++;}
while (s->top!=0)
{exp[t]=s->stack[s->top];
t++;
s->top--;}
exp[t]='#';
printf("\n\t 原来表达式:");
for(j=1;j<sum;j++)
 printf("%c",str[j]);
printf("\n\t后缀表达式:",exp);
for(j=1;j<t;j++)
 printf("%c",exp[j]);
}
void compute_value(sqstack *s,char exp[MAXSIZE])
{int d;
char ch;
int t=1;
s->top=0;
ch=exp[t];                //t是exp下标//
t++;
while(ch!='#')
{      switch(ch)
{  case'+':
      s->stack[s->top-1]=s->stack[s->top-1]+s->stack[s->top];
      s->top--;
      break;
   case'-':
      s->stack[s->top-1]=s->stack[s->top-1]-s->stack[s->top];
      s->top--;
      break;
   case'*':
      s->stack[s->top-1]=s->stack[s->top-1]*s->stack[s->top];
      s->top--;
      break;
   case'/':
       if(s->stack[s->top]!=0)
      s->stack[s->top-1]=s->stack[s->top-1]/s->stack[s->top];
      else
      {printf("\n\t出错!\n");
      exit(0);}                          //异常退出//
      s->top--;
      break;
   default:
       d=0;
       while(ch>='0'&&ch<='9')
       {
            d=10*d-ch-'0';             //数字字符改为数值//
            ch=exp[t];
            t++;
       }
       s->top++;
       s->stack[s->top]=d;
}
          ch=exp[t];
          t++;}
          printf("\n\t计算结果:%f\n",s->stack[s->top]);
}
 

void main(void)
 {sqstack stack,*s;
  s=&stack;
 transform(s,exp);
 compute_value(s,exp);}
搜索更多相关主题的帖子: include 表达式 C语言 
2016-08-25 23:59
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
能不能举个出问题的例子?先把告警去掉再调:
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100        //栈的最大容量100//
#define TRUE 1
#define FALSE 0
#define NULL 0
typedef int elemtype;

typedef struct
{
    elemtype stack[MAXSIZE];
    int top;
}sqstack;                 //定义顺序栈类型sqstack//
sqstack *s;                //定义sqstack类型的变量 s//
void InitStack(sqstack *s);
int push(sqstack *s, elemtype x);
elemtype pop(sqstack *s);
elemtype gettop(sqstack *s);
int empty(sqstack *s);
void display_stack(sqstack *s);

void InitStack(sqstack *s)            //初始化栈//
{
    s->top = -1;
}
int push(sqstack *s, elemtype x)      //入栈操作//
{
    if (s->top >= MAXSIZE - 1)
    {
        printf("stack overflow!\n"); return(FALSE);
    }
    else
    {
        s->top++;
        s->stack[s->top] = x;
        return(TRUE);
    }
}
elemtype pop(sqstack *s)       //出栈操作//
{
    if (s->top<0)
    {
        printf("stack underflow!\n");
        return(NULL);
    }
    else
    {
        s->top--; return(s->stack[s->top + 1]);
    }
}
elemtype gettop(sqstack *s)    //获取栈顶元素//
{
    if (s->top<0)
    {
        printf("stack is empty\n");
        return(NULL);
    }
    else
        return(s->stack[s->top]);
}
int empty(sqstack *s)
{
    if (s->top<0)
        return(TRUE);
    else
        return(FALSE);
}
void display_stack(sqstack *s)
{
    int i;
    printf("the elements is the stack are:\n");
    for (i = s->top; i >= 0; i--)
        printf("%d", s->stack[i]);
}







char exp[MAXSIZE];          //存储转换的后缀表达式//

void transform(sqstack *s, char exp[MAXSIZE])
{                                                                   //将算术表达式改为后缀表达式//
    char str[MAXSIZE];                   //存储原来的算术表达式//
    char ch;
    int sum, i, j, t;
    s->top = 0;
    printf("算术表达式是:\n");
    i = 0;
    do
    {
        i++;                                //获取输入的表达式//
        scanf("%c", &str[i]);
    } while (str[i] != '#'&&i != MAXSIZE);
    sum = i;                         //记录总的数目//
    t = 1;
    i = 1;
    ch = str[i];
    i++;
    while (ch != '#')
    {
        switch (ch)
        {
        case'(':                                //判定为左括号//
            s->top++;
            s->stack[s->top] = ch;
            break;
        case')':                                //判定为右括号//
            while (s->stack[s->top] != '(')
            {
                exp[t] = s->stack[s->top];
                s->top--;
                t++;
            }
            s->top--;
            break;
        case'+':                              //判定为加减号//
        case'-':
            while (s->top != 0 && s->stack[s->top] != '(')
            {
                exp[t] = s->stack[s->top];
                s->top--;
                t++;
            }
            s->top++;
            s->stack[s->top] = ch;
            break;
        case'*':                                   //判定为* /   //
        case'/':
            while (s->stack[s->top] == '*' || s->stack[s->top] == '/')
            {
                exp[t] = s->stack[s->top];
                s->top--;
                t++;
            }
            s->top++;
            s->stack[s->top] = ch;
            break;
        case' ':
            break;
        default:
            while (ch >= '0'&&ch <= '9')                      //判定为数字//
            {
                exp[t] = ch;
                t++;
                ch = str[i];
                i++;
            }
            i--;
            exp[t] = '#';
            t++;
        }
        ch = str[i];
        i++;
    }
    while (s->top != 0)
    {
        exp[t] = s->stack[s->top];
        t++;
        s->top--;
    }
    exp[t] = '#';
    printf("\n\t 原来表达式:");
    for (j = 1; j<sum; j++)
        printf("%c", str[j]);
    printf("\n\t后缀表达式:");
    for (j = 1; j<t; j++)
        printf("%c", exp[j]);
}
void compute_value(sqstack *s, char exp[MAXSIZE])
{
    int d;
    char ch;
    int t = 1;
    s->top = 0;
    ch = exp[t];                //t是exp下标//
    t++;
    while (ch != '#')
    {
        switch (ch)
        {
        case'+':
            s->stack[s->top - 1] = s->stack[s->top - 1] + s->stack[s->top];
            s->top--;
            break;
        case'-':
            s->stack[s->top - 1] = s->stack[s->top - 1] - s->stack[s->top];
            s->top--;
            break;
        case'*':
            s->stack[s->top - 1] = s->stack[s->top - 1] * s->stack[s->top];
            s->top--;
            break;
        case'/':
            if (s->stack[s->top] != 0)
                s->stack[s->top - 1] = s->stack[s->top - 1] / s->stack[s->top];
            else
            {
                printf("\n\t出错!\n");
                exit(0);
            }                          //异常退出//
            s->top--;
            break;
        default:
            d = 0;
            while (ch >= '0'&&ch <= '9')
            {
                d = 10 * d - ch - '0';             //数字字符改为数值//
                ch = exp[t];
                t++;
            }
            s->top++;
            s->stack[s->top] = d;
        }
        ch = exp[t];
        t++;
    }
    printf("\n\t计算结果:%d\n", s->stack[s->top]);
}


void main(void)
{
    sqstack stack, *s;
    s = &stack;
    transform(s, exp);
    compute_value(s, exp);
}
2016-08-26 09:12
快速回复:用C语言求中缀表达式 找错误。。
数据加载中...
 
   



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

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