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/DataStructure/Code/ex07/ex7.cpp

69 lines
1.5 KiB
C++
Raw Permalink Normal View History

2021-11-11 12:45:47 +00:00
#include <stdio.h>
2021-11-12 09:06:39 +00:00
#include <string>
#include <iostream>
#include "stack.hpp"
2021-11-11 12:45:47 +00:00
int main()
{
2021-11-12 09:06:39 +00:00
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 '}':
2021-11-17 02:14:28 +00:00
'{' != S.pop() ? flag = false : 0;
2021-11-12 09:06:39 +00:00
break;
default:
break;
}
}
(flag==true&&S.empty()) ? printf("匹配成功\n") : printf("匹配失败\n");
}