鱼C论坛

 找回密码
 立即注册
查看: 2858|回复: 8

栈应用

[复制链接]
发表于 2011-10-13 11:08:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main()
{
  stack<char> sexp;//处理表达式的stack对象
  string exp;
  //读入表达式
  cout<<"Enter  a  expression:"<<endl;
  cin>>exp;
  //处理表达式
  string::iterator iter = exp.begin();
  while(iter!=exp.end())
  {
     if(*iter !=')')//读到的字符不是右圆括号
     sexp.push(*iter);
  else
  {
     //读到的字符是右圆括号,弹出元素,知道栈顶为左圆括号或栈为空
  while(sexp.top()!='(' && !sexp.empty())
  {
           sexp.pop();
   }
  if(sexp.empty())//栈为空
     cout<<"parenthese  are  not matched "<<endl;
  else{      //栈顶为左圆括号
     sexp.pop();
     sexp.push('@');
     }
  }
  ++iter;
}
return 0;
};




程序正确、但是输入字母是读不出来、还有一开始就出现右括号直接出现不能读取内存的代码错误、大侠看看啊?


小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-10-13 17:10:30 | 显示全部楼层
这个问题其实很好解决,如果你第一次输入的是右括号那么它是不会压入栈中的,这点你可以认同吧,那么这个栈是个空栈,既然是空栈的话栈顶指针是不会指向栈空的位置,因为栈顶指针永远是指向栈顶的那个元素,也就是最后一个元素。同意吧。那么在sexp.top()!='(' 时就会出现指针指向内存错误编译器自然报错异常退出而不会运行到  !sexp.empty()这句话,明白我意思么?
分析完原因后解决办法知道了么?没错,循环判断时先判断是否为空在判断栈顶元素是否为左括号!
不知道这么讲你能够明白,不明白你就静等甲鱼大侠讲解吧!呵呵
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-10-13 22:08:32 | 显示全部楼层
改了还是不行。。还是没有输出。大哥你把能输出的代码贴一下嘛、谢谢。
这个源代码是C++primer书上的、我无语了啊
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2011-10-13 22:09:15 | 显示全部楼层

改了还是不行。。还是没有输出。大哥你把能输出的代码贴一下嘛、谢谢。
这个源代码是C++primer书上的、我无语了啊
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-10-13 23:18:53 | 显示全部楼层
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main()
{
        stack<char> sexp;//处理表达式的stack对象
        string exp;//读入表达式
        cout<<"Enter  a  expression:"<<endl;
        cin>>exp; //处理表达式
        string::iterator iter = exp.begin();
        while(iter!=exp.end())
        {
                if(*iter !=')')//读到的字符不是右圆括号
                sexp.push(*iter);
                else
                {
                        //读到的字符是右圆括号,弹出元素,知道栈顶为左圆括号或栈为空
                        while(!sexp.empty() && sexp.top()!='('  )
                {
                         sexp.pop();
                }
                if(sexp.empty())//栈为空
                        cout<<"parenthese  are  not matched "<<endl;
                else
                {      //栈顶为左圆括号
                        sexp.pop();
                        sexp.push('@');
                }
         }
  ++iter;
}
return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-10-13 23:19:52 | 显示全部楼层
你代码的缩进写的不好,这不是一个好的习惯。。。。慢慢培养好的编程风格
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-10-13 23:30:12 | 显示全部楼层
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main()
{
        stack<char> sexp;//处理表达式的stack对象
        string exp;//读入表达式
        cout<<"Enter  a  expression:"<<endl;
        cin>>exp; //处理表达式
        string::iterator iter = exp.begin();
        while(iter!=exp.end())
        {
                if(*iter !=')')//读到的字符不是右圆括号
                sexp.push(*iter);
                else
                {
                        //读到的字符是右圆括号,弹出元素,知道栈顶为左圆括号或栈为空
                        while(!sexp.empty() && sexp.top()!='('  )
                        {
                         sexp.pop();
                        }
                }
                if(sexp.empty())//栈为空
                        cout<<"parenthese  are  not matched "<<endl;
                else
                {      //栈顶为左圆括号
                        cout<<"parenthese are matched"<<endl;
                }
                ++iter;
        }
        return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-10-14 00:06:20 | 显示全部楼层
不好意思,我发现了这个代码问题很多,不仅仅是你说的那个问题,现在把它改好了,你看看这个吧,前面我发的你就别看了,如果你的意思是判断括号是否匹配的话这个代码基本上还算正确。。。
#include<iostream>
#include<stack>
#include<string>
#include <stdlib.h>
using namespace std;
int main()
{
stack<char> sexp;//处理表达式的stack对象
string exp;//读入表达式
cout<<"Enter  a  expression:"<<endl;
cin>>exp; //处理表达式
string::iterator iter = exp.begin();
while(iter!=exp.end())
{
  if(*iter !=')')//读到的字符不是右圆括号
  {
   sexp.push(*iter);
   
  }
  else
  {
   //读到的字符是右圆括号,弹出元素,直到栈顶为左圆括号或栈为空
   while(!sexp.empty() && sexp.top()!='('  )
   {
    sexp.pop();
   }
   if(sexp.empty())//栈为空
   {
    cout<<"parenthese  are  not matched "<<endl;
    exit(0);
   }
   sexp.pop();
  }
  ++iter;
}
while(!sexp.empty() && sexp.top()!='(' )
{
  sexp.pop();
}
if(sexp.top()=='(')
  cout<<"parenthese are not matched"<<endl;
else
  cout<<"paraenthese are mathed"<<endl;
return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2011-10-14 00:08:22 | 显示全部楼层
调戏林黛玉 发表于 2011-10-13 22:09
改了还是不行。。还是没有输出。大哥你把能输出的代码贴一下嘛、谢谢。
这个源代码是C++primer书上的、我 ...

#include<iostream>
#include<stack>
#include<string>
#include <stdlib.h>
using namespace std;
int main()
{
stack<char> sexp;//处理表达式的stack对象
string exp;//读入表达式
cout<<"Enter  a  expression:"<<endl;
cin>>exp; //处理表达式
string::iterator iter = exp.begin();
while(iter!=exp.end())
{
  if(*iter !=')')//读到的字符不是右圆括号
  {
   sexp.push(*iter);
   
  }
  else
  {
   //读到的字符是右圆括号,弹出元素,直到栈顶为左圆括号或栈为空
   while(!sexp.empty() && sexp.top()!='('  )
   {
    sexp.pop();
   }
   if(sexp.empty())//栈为空
   {
    cout<<"parenthese  are  not matched "<<endl;
    exit(0);
   }
   sexp.pop();
  }
  ++iter;
}
while(!sexp.empty() && sexp.top()!='(' )
{
  sexp.pop();
}
if(sexp.top()=='(')
  cout<<"parenthese are not matched"<<endl;
else
  cout<<"paraenthese are mathed"<<endl;
return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-8 23:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表