//总而言之一系列出入栈空合法,否则不合法
//调试完了
#ifndef PCH_H
#define PCH_H
#include<cstdlib>
#include<iostream>
#include<stdio.h>
#include<malloc.h>
#include<math.h>
typedef int Status;
constexpr auto OK = 1;
constexpr auto ERROR = 0;
constexpr auto MAXSIZE = 8;
typedef struct
{
char *base;
char *top;
int stacksize;
}SqStack;
// TODO: 添加要在此处预编译的标头
Status InitStack(SqStack &S);
Status Pop(SqStack &S, char &e);
Status Push(SqStack &S, char e);
Status EmptyStack(SqStack S);
void Judgement(char line[]);
#endif //PCH_H
// 一般情况下,忽略此文件,但如果你使用的是预编译标头,请保留它。
Status InitStack(SqStack &S)
{
S.base = (char *)malloc(MAXSIZE * sizeof(char));
if (!S.base) exit(OVERFLOW);
S.top = S.base;//初始top指向base
S.stacksize = MAXSIZE;
return OK;
}
Status Pop(SqStack & S, char & e)
{
if (EmptyStack(S)) return ERROR;
e = *--S.top;//ご注意
return OK;
}
Status Push(SqStack & S, char e)
{
if (S.top - S.base == S.stacksize) return ERROR;
*S.top++ = e;//ご注意
return OK;
}
Status EmptyStack(SqStack S)
{
if(S.base==S.top) return OK;
return ERROR;
}
void Judgement(char line[])
{
SqStack S;
InitStack(S);
int i = 0;
Status flag=1;
while (i<MAXSIZE && flag)
{
switch (line[i])
{
case 'I':
Push( S, line[i]);
break;
case 'O':
if (EmptyStack(S))
{
flag = 0;
}
Pop(S,line[i]);
break;
default:
break;
}
i++;
}
if (flag&&EmptyStack(S)) std::cout << "legal!";
else std::cout << "illegal!!";
}
//初始化
-------------------------------------------
// ConsoleApplication5.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
int main()
{
char line[MAXSIZE];
std::cout << "put in line : \n";
for(int i=0;i< MAXSIZE;i++)
std::cin >> line[i];
Judgement(line);
}
判别合法序列
最新推荐文章于 2023-08-02 10:35:49 发布