数据结构第二章理解

 第二章 栈

1、在栈中有关类模板库的一段程序:

问题描述:

输入数n,代表测试数据的数目,输入n个数并逆序输出;

#include<stack>  //定义栈类

#include<iostream>

using namespace std;

int main()

{

   int n;

   double item;

   stack<double>numbers;

 cout<<"Type in an integer n followed by n decimal numbels."<<endl<<"The numberswill be printed in reverse order."<<endl;

   cin>>n;

   for(int i=0;i<n;i++)

   {

     cin>>item;

     numbers.push(item);

   }

   cout<<endl<<endl;

   while(!numbers.empty())

   {

     cout<<numbers.top()<<" "; //栈顶元素;此处用于逆序输出

     numbers.pop();

   }

   cout<<endl;

   return 0;

}  

2、桌面计算器:(逆波兰计算器)

问题描述:

在这样的计算器中,操作数在操作被指定前输入,操作数放入栈中。当操作执行时,操作数从栈中弹出并且把操作结果入栈。

 #include<stack.h>
#include<iostream>
typedef double Stack_entry;
using namespace std;
int main()
{
 Stack stored_numbers;
 introduction();
 instructions();
 while(do_command(get_command(),stored_numbers));
}
char get_command()
{
 char command;
 bool waiting=true;
 cout<<"Secelt command and press<Enter>:";
 while(waiting)
 {
  cin>>command;
  command=tolower(command);
  if(command=='?'||command=='='||command=='+'||command=='-'||command=='*'||command=='/'||command=='q')
   waiting=false;
  else
  {
   cout<<"Please enter valid command:"<<endl<<"[?]push to stack [=]print top"<<endl<<"[+][-][*][/] are arithmetic operations"<<endl<<"[Q]uit."<<endl;
  }
 }
 return command;
}
bool do_command(char command,Stack &numbers)
{
 double p,q;
 switch(command)
 {
 case'?':
  cout<<"Enter a real number:"<<flush;
  cin>>p;
  if(numbers.push(p)==overflow)
   cout<<"Warning:Stack full,lost number"<<endl;
  break;
 case'=':
  if(numbers.top(p)==underflow)
   cout<<"Stack empty"<<endl;
  else
   cout<<p<<endl;
  break;
 case'+':
  if(numbers.top(p)==underflow)
   cout<<"Stack empty"<<endl;
  else
  {
   numbers.pop();
   if(numbers.top(q)==underflow)
   {
   cout<<"Stack has just one entry"<<endl;
   numbers.push(p);
   }
   else
   {
    numbers.pop();
    if(numbers.push(q+p)==overflow)
     cout<<"Warning:Stack full,lost result"<<endl;
   }
   case'-':
  if(numbers.top(p)==underflow)
   cout<<"Stack empty"<<endl;
  else
  {
   numbers.pop();
   if(numbers.top(q)==underflow)
   {
   cout<<"Stack has just one entry"<<endl;
   numbers.push(p);
   }
   else
   {
    numbers.pop();
    if(numbers.push(q-p)==overflow)
     cout<<"Warning:Stack full,lost result"<<endl;
   }
   case'*':
  if(numbers.top(p)==underflow)
   cout<<"Stack empty"<<endl;
  else
  {
   numbers.pop();
   if(numbers.top(q)==underflow)
   {
   cout<<"Stack has just one entry"<<endl;
   numbers.push(p);
   }
   else
   {
    numbers.pop();
    if(numbers.push(q*p)==overflow)
     cout<<"Warning:Stack full,lost result"<<endl;
   }
  }
  case'/':
  if(numbers.top(p)==underflow)
   cout<<"Stack empty"<<endl;
  else
  {
   numbers.pop();
   if(numbers.top(q)==underflow)
   {
   cout<<"Stack has just one entry"<<endl;
   numbers.push(p);
   }
   else
   {
    numbers.pop();
    if(numbers.push(q/p)==overflow)
     cout<<"Warning:Stack full,lost result"<<endl;
   }
  break;
 case'q':
  cout<<"Calculation finished.\n";
  return false;
 }
 return ture;
}

3、括号匹配:

功能介绍:

检查一个输入文本文件中的括号是否正确匹配。

#include<iostream>
#include<stack>
using namespace std;
int main()
{
 Stack openings;
 char symbol;
 bool is_matched=true;
 while(is_matched&&(symbol=cin.get())!='\n')
 {
  if(symbol=='{'||symbol=='('||symbol=='[')
   openings.push(symbol);
  if(symbol=='}'||symbol==')'||symbol==']')
  {
   if(openings.empty())
   {
    cout<<"Unmatched closing bracket"<<symbol<<"detched."<<endl;
    is_matched=false;
   }
   else
   {
    char match;
    opengings.top(match);
    opengings.pop();
    is_matched=(symbol=='}'&&match=='{')||(symbol==')'&&match=='(')||(symbol==']'&&match=='[');
    if(!is_matched)
     cout<<"Bad match"<<match<<symbol<<endl;
   }
  }
 }
 if(!openings.empty())
  cout<<"Unmatched opening bracket(s) detected."<<endl;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值