Files
Data-Structure/Dev-C++/ExerciseBook/03.17/03.17.cpp
2019-11-04 23:04:29 +08:00

70 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <stdio.h>
#include "Status.h" //**▲01 绪论**//
#include "SqStack.h" //**▲03 栈和队列**//
/*
* 判定输入序列是否为"序列1&序列2"这种模式
* 其中序列2是序列1的逆置
*
* s待验证的序列以'@'符号结尾
*/
Status Algo_3_17(char* s);
int main(int argc, char* argv[]) {
char* s = "a+b-c&c-b+a@";
printf("判断序列 %s 是否合规...\n", s);
if(Algo_3_17(s)) {
printf("█ 序列满足题意!\n");
} else {
printf("█ 序列不满足题意!!\n");
}
return 0;
}
/*
* 判定输入序列是否为"序列1&序列2"这种模式
* 其中序列2是序列1的逆置
*
* s待验证的序列以'@'符号结尾
*/
Status Algo_3_17(char* s) {
SqStack S;
SElemType e;
int i;
InitStack(&S);
i = 0;
// 将'&'前的序列入栈
while(s[i] != '@' && s[i] != '&') {
Push(&S, s[i]);
i++;
}
// 如果遇到了'&'符号
if(s[i] != '@') {
i++; // 跳过&符号
// 将'&'后的序列出栈
while(!StackEmpty(S) && s[i] != '@') {
Pop(&S, &e);
if(s[i] != e) {
return ERROR;
}
i++;
}
}
// 如果栈为空,且序列恰好访问完,说明符合题意
if(StackEmpty(S) && s[i] == '@') {
return OK;
}
return ERROR;
}