📄 调度.cpp
字号:
#include<iostream.h>
#include<stdio.h>
int n;
int path[45];
struct pathss{
int paths[45][45];
int number;
}AllPath;
struct SNode{
int data[45];
int top;
}S;
void InitStack()
{
S.top=-1;
}
void Push(int q)
{
S.data[++S.top]=q;
}
int Pop()
{
int temp;
temp=S.data[S.top--];
return temp;
}
int StackEmpty()
{
if(S.top==-1)
return 1;
else
return 0;
}
void Attemper(int pos,int path[],int cur)
{
int m,i;
if(pos<n)
{
Push(pos+1);
Attemper(pos+1,path,cur);
Pop();
}
if(!StackEmpty())
{
m=Pop();
path[cur++]=m;
Attemper(pos,path,cur);
Push(m);
}
if(pos==n && StackEmpty())
{
cout<<"\t\t\t";
cout<<AllPath.number+1<<":";
for(i=0;i<cur;i++)
{
cout<<path[i]<<" ";
AllPath.paths[AllPath.number][i]=path[i];
}
cout<<endl;
AllPath.number++;
}
}
void Print()
{
int i;
for(i=S.top;i>=0;i--)
cout<<"\t\t\t "<<"|"<<S.data[i]<<"|\n";
}
void DisplayOnly(int k,int Output,int Input)
{
int j;
getchar();
for(j=0;j<=Output;j++)
cout<<"<----"<<AllPath.paths[k][j];
cout<<endl;
Print();
cout<<"\t\t\t\t\t";
for(j=Input;j<=n;j++)
cout<<endl;
}
void ChooseDisplay()
{
int k,Output,Input=1;
cout<<"请输入你想要查看序列状态变化过程的序号:";
cin>>k;
k=k-1;
InitStack();
cout<<"\n 输出序列\t 栈\t\t 输入序列";
DisplayOnly(k,-1,1);
for(Output=0;Output<n;Output++)
{
if(AllPath.paths[k][Output]>=Input)
{
while(AllPath.paths[k][Output]>=Input)
{
Push(Input);
Input++;
DisplayOnly(k,Output-1,Input);
}
Pop();
DisplayOnly(k,Output,Input);
}
else
{
Pop();
DisplayOnly(k,Output,Input);
}
}
}
void message()
{
cout<<endl;
cout<<"*******************************"<<endl;
cout<<"* 车厢调度 *"<<endl;
cout<<"* 1:输入火车的长度 *"<<endl;
cout<<"* 2:输出所有可能序列 *"<<endl;
cout<<"* 3:演示一个序列变化 *"<<endl;
cout<<"* 4:退出本程序 *"<<endl;
cout<<"*******************************"<<endl;
cout<<endl;
}
void InputNumber()
{
cout<<"请输入火车车厢的长度N:";
cin>>n;
}
void DisplayAll()
{
AllPath.number=0;
InitStack();
Push(1);
cout<<"所有可能的输出序列如下:\n";
Attemper(1,path,0);
}
void main()
{
int flag;
message();
cin>>flag;
while(flag!=4)
{
switch(flag)
{
case 1: InputNumber();break;
case 2: DisplayAll();break;
case 3: ChooseDisplay();break;
}
message();
cin>>flag;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -