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;
}