This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/ex7/ex7.cpp
2021-11-12 17:06:39 +08:00

69 lines
1.5 KiB
C++

#include <stdio.h>
#include <string>
#include <iostream>
#include "stack.hpp"
int main()
{
Stack<int> stack;
int op = 0;
while (1)
{
op = 0;
printf("选择操作:1入栈2出栈3深度4是否空5栈顶6退出\n");
scanf("%d", &op);
switch (op)
{
case 1:
printf("输入\n");
scanf("%d", &op);
stack.push(op);
break;
case 2:
printf("出%d \n", stack.pop());
break;
case 3:
printf("深度%d \n", stack.depth());
break;
case 4:
printf("%d \n", stack.empty());
break;
case 5:
printf("%d \n", stack.top());
break;
case 6:
goto Out;
default:
break;
}
}
Out:
Stack<char> S;
std::string exp;
bool flag = true;
printf("输入算式\n");
std::cin >> exp;
for (int i = 0; i < sizeof(exp); i++)
{
switch (exp[i])
{
case '(':
case '[':
case '{':
S.push(exp[i]);
break;
case ')':
'(' != S.pop() ? flag = false : 0;
break;
case ']':
'[' != S.pop() ? flag = false : 0;
break;
case '}':
'}' != S.pop() ? flag = false : 0;
break;
default:
break;
}
}
(flag==true&&S.empty()) ? printf("匹配成功\n") : printf("匹配失败\n");
}