标题:程序产生一个访问违例 这个程序到底哪儿出问题了?
只看楼主
李姗姗
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-10-21
结帖率:0
已结贴  问题点数:20 回复次数:1 
程序产生一个访问违例 这个程序到底哪儿出问题了?
#include<iostream>
#include<math.h>

using namespace std;

typedef struct
{int e[20];
int top;}
SeqStack;


void InitStack(SeqStack *S);
void Push(SeqStack *S,int x);
void Pop(SeqStack *S,int *x);
int Gettop(SeqStack *S);
int Execute(int a,int op,int b);
void ExpEvaluation();
int Compare(int ch,int gettopS1);
 
SeqStack S1;
SeqStack S2;

int main()
{  
   
    ExpEvaluation();
      
   
  system("pause");
  return 0;
 
}
 
 
 

void ExpEvaluation()
{
    InitStack(&S1);
    InitStack(&S2);
    Push(&S1,'#');
    int op,a,b,d,ch,v;
   
printf("please input expression(ending with '#')");
ch=getchar();

while(ch!='#'||Gettop(&S1)!='#')
{

if(ch=='(') Push(&S1,ch);
else
    if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='#')     // 如果是左括号或者操作符,则进栈S1;
        switch(Compare(ch,Gettop(&S1)))
        {
         case '>':
             Push(&S1,ch);
             ch=getchar();
             break;
   
         case '<':  Pop(&S1,&op);
                 Pop(&S2,&b);
                 Pop(&S2,&a);
                 v=Execute(a,op,b);
                 Push(&S2,v);
               
                 break;
        }
    else
   
        if(ch>='0'&&ch<='9')         //是操作数
        {   
            int t=ch-'0';
            ch=getchar();
             while (ch>='0'&& ch<='9')      //对多位整数的处理
                {   
                     t=t*10+(ch-'0');  
                       ch=getchar();
                 }     
             Push(&S2,t);
         }

        else
        
          if(ch==')')
          {     while(Gettop(&S1)!='(')
                 {
                 Pop(&S1,&op);
                 Pop(&S2,&b);
                 Pop(&S2,&a);
                 v=Execute(a,op,b);
                 Push(&S2,v);
                 }
          Pop(&S1,&op);
          ch=getchar();
          }

}//while 结束

 printf("%d",v);
}//


void InitStack(SeqStack *S)     //初始化
{
    S->top=-1;
}


void Push(SeqStack *S,int x)    //入栈
{
     S->top++;
     S->e[S->top]=x;
}


void Pop(SeqStack *S,int *x)   //出栈
{
     *x=S->e[S->top];
     S->top--;
}


int Gettop(SeqStack *S)  //取栈顶元素
{
     int x;
     x=S->e[S->top];
     return (x);
     
}


int Execute(int a,int op,int b)
{
    switch(op)
    {
        case'+':    return a+b;
        case'-':    return a-b;
        case'*':    return a*b;
        case'/':    return a/b;
    }

}

int Compare(int ch,int gettopS1)
{    if(ch=='#') return '<';
    else
        if(gettopS1=='( '||gettopS1=='#')return '>';
        else
            if (ch=='+'||ch=='-')
            {
                if(gettopS1=='*'||gettopS1=='/') return '<';
                else return '>';
            }

            else
            {
                if(gettopS1=='+'||gettopS1=='-') return '>';
                else return '<';
            }
            
            
}



只有在进行有括号运算的时候才出错
搜索更多相关主题的帖子: include system return void 
2012-10-31 16:58
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
得分:20 
程序代码:
        if(ch=='(')
        {//左括号进符号栈
            Push(&S1,ch);
            ch = getchar();
        }
2012-11-01 09:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-385331-1-1.html




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

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