C++程序,关于堆栈的~

来源:百度知道 编辑:UC知道 时间:2024/06/08 18:32:05
请定义一个顺序存储结构的堆栈类型,通过该类型说明三个堆栈A、B、C,在程序出设计出堆栈的入栈函数和出栈函数。在主程序中,将200以内的偶数、大于3的素数、其他所有非素数的奇数,分别放到所定义的三个栈A、B、C中,程序最后分别按出栈的顺序输出各个栈的数据。
不想答的别来掺和……

#include<iostream>
#include<stack>
#include<cmath>
using namespace std;
int f1(int n)
{
int k,m=(int)sqrt((double)n);
for(k=2;k<=m;k++)
if(n%k==0)break;
if(k>m&&n>1)return 1;
else return 0;
}

int main()
{
stack<int>A,B,C;
for(int i=1;i<=200;i++)
{
if(i%2==0)A.push(i);
else if(f1(i)) B.push(i);
else C.push(i);
}
cout<<"A:偶数:"<<endl;
while(!A.empty())
{
cout<<A.top()<<" ";
A.pop();
}
cout<<endl<<"B:素数:"<<endl;
while(!B.empty())
{
cout<<B.top()<<" ";
B.pop();
}
cout<<endl<<"C:其他奇数:"<<endl;
while(!C.empty())
{
cout<<C.top()<<" ";
C.pop();
}
cout<<endl;
return 0