📄 demo04_2.cpp
字号:
//{ Demo04_2 }
#include"sstack.h"
int n,i;
bb1 A;
SeqStack S;
string S1="";
GrpArr g;
boolean Valid_Element(bb1& A,int n)
{int i;
boolean ok;
set ISet;
//ISet:set of 1..10;
ok=true;
for (i=1;i<=n;i++)
if (!(A[i]>=1&&A[i]<=n)||ISet.contain(A[i]) )
ok=false;
else
ISet.insert(A[i]);
boolean Valid_Element=ok;
return Valid_Element;
}
boolean judge_Stack_Sequence(bb1& A,int n)
{int i,j,x;
boolean ok=true;
i=1; j=1; ok=true;
SeqStack_setnull(S);
while ( (i<=n)&&ok)
{
cur_elmn_onoff(g,i);
while ( (SeqStack_empty(S)||(A[i]>SeqStack_top(S))) &&(j<=n) )
{ SeqStack_push(S,j);
j=j+1;
}
if (!SeqStack_empty(S)&& (SeqStack_top(S)==A[i]) )
{ cur_elmn_onoff(g,i);
i=i+1;
Wait();
SeqStack_pop(S,x);
Wait();
}
else
ok=false;
}
boolean judge_Stack_Sequence=ok;
return judge_Stack_Sequence;
}
void main()
{
set_SeqStack(S,"Stack",1,9); // {设置栈的下标范围}
move_SeqStack_mid(S,getmaxx()/2,getmaxy()/2-80);
create_grp_Arrbb(g,1,1,true,A,"Input",1,9);
initial_GrpArr(g);
do
{
Inputintinwnd(30,4,50,"num of elements: ",n); // {控制输入长度}
if (n<=0)
exit(0);
do// {控制输入有效的数据序列}
{ strcpy(S1,"");
Inputstrinwnd(30,6,50+n,"Array of elements: ",S1);
// {输入数组元素的值}
for( i=1;i<=strlen(S1);i++)
A[i]=S1[i-1]-'0';
for( i=n+1;i<=9;i++)
A[i]=0;
move_GrpArr_mid(g,getmaxx()/2,getmaxy()/2+100);
if (!Valid_Element(A,n) )
Error("The Sequence is not valid, Input again ");
}
while(!Valid_Element(A,n) );
if (judge_Stack_Sequence(A, n) )// {调用算法进行判断,并输出相应结果}
Dispstrinwnd(20,4,"This sequence is valid output of stack ");
else
Dispstrinwnd(20,4,"This sequence is not valid output of stack ");
}
while(!(n==0) );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -