``## 栈的应用
进制转换
#include<iostream>
#include<stack>
using namespace std;
int main()
{
int n;
int m;
cout<<"输入数字还有转换的进制"<<endl;
cin>>n>>m;
stack<int>re;
while(n!=0)
{
int a,b;//a代表结果,b代表余数
a=n/m;
b=n%m;
re.push(b);
n=a;
}
while(!re.empty())
{
int t;
t=re.top();
re.pop();
cout<<t;
}
cout<<endl;
return 0;
}
括号匹配
#include<iostream>
#include<stack>
#include<string.h>
using namespace std;
int main()
{
stack<char>a;
char b[50];
cin>>b;
int flag=1;
for(int i=0;i<strlen(b);i++)
{
while(!a.empty())
{
a.pop();
}
if(b[i]=='('||b[i]=='{')
{
a.push(b[i]);
}
else if(b[i]==')'||b[i]=='}')
{
if(a.empty())
{
flag=0;
break;
}
char t;
t=a.top();
a.pop();
if(b[i]==')'&&t=='{')
{
flag=0;
break;
}
else if(b[i]=='}'&&t=='(')
{
flag=0;
break;
}
}
else
{
continue;
}
}
if(!a.empty())
{
flag=0;
}
if(flag==1)
{
cout<<"true"<<endl;
}
else
{
cout<<"false"<<endl;
}
return 0;
}