//顺序栈实现 数值转换
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include<string.h>
#define MAXQSIZE 100 //最大长度
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef char ElemType;
//-------------------循环队列的顺序存储 表示--------------
//初始化的动态分配存储空间、头指针、尾指针
typedef struct {
ElemType *base;
int front;
int rear;
}SqQueue;
//-------------------------子函数-------------------------
//初始化 函数
int InitQueue (SqQueue &Q){
Q.base =new ElemType[MAXQSIZE];
if(!Q.base) exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
//返回队列的长度
int QueueLength (SqQueue Q)
{
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
//队列入队 -在队尾
int EnQueue(SqQueue &Q,ElemType e)
{
if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
return OK;
}
//队列出队 -在队首
int DeQueue (SqQueue &Q,ElemType &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
return OK;
}
//-----------------------------主函数--------------------------------
//主函数
int main()
{
SqQueue F,M; F,M; //定义 F:女队队列 M:男队队列
char f,m; f,m; //定义 f:女队队员编码 m:男队队员编码
int n=0; //定义 出场次序数
InitQueue(F);
InitQueue(M); //初始化男士队列
printf("输入女士编码(单字符,输入#结束):\n");
while(1)
{
scanf(" %c",&f);
//scanf("%c",&f); 会吃掉回车符 ,scanf在一次输入结束后,回车符会残留在数据缓冲区中。解决方法:%c前加空格
if(f=='#') break;
EnQueue(F,f);
}
printf("%d\n",QueueLength(F));
printf("输入男士编码(单字符,输入#结束):\n");
while(1)
{
scanf(" %c",&m);
if(m=='#') break;
EnQueue(M,m);
}
printf("%d\n",QueueLength(M));
printf("输入当前场次序号:");
scanf("%d", &n);
//队首出队,在队尾插入。根据出场场次n,确定当前舞者
for (int i = 1; i <= n; i++)
{
DeQueue(F,f); //女士出队
EnQueue(F,f); //跳完的女士,队尾重新排队
DeQueue(M,m); //男士出队
EnQueue(M,m); //跳完的男士,队尾重新排队
}
printf("当前跳舞者:\n");
printf("女士%c\n男士%c",f,m);
return 0;
}