*本题要求实现一个表达式括号匹配判断函数,输入参数为一个指针型字符串,字符串长度不可超100,括号有( ),[ ],{ }三种形式(输入形式例如:{[(1+2)3]}-9)。如果匹配成功,则该函数应该返回1,匹配不成功则返回0
函数接口定义:
int matching(char* exp);
//提示:可以使用string.h中的"strlen()"函数,帮助判断用户输入的表达式的长度
裁判测试程序样例:
#include using namespace std;
#define STACKSIZE 10 typedef char DataType; typedef struct {
DataType *base;
DataType * top;
int stacksize; }SqStack; //初始化空顺序栈,细节在此不表 int InitStack(SqStack &S);//判顺序栈空,细节在此不表 int StackEmpty(SqStack S);
//入栈,细节在此不表 int Push(SqStack &S, DataType item);
//出栈,细节在此不表 int Pop(SqStack &S, DataType &item);
//取栈顶,细节在此不表 int GetTop(SqStack S, DataType &item);
//表达式括号匹配判断函数,自定义 int matching(char* exp);
int main() {
char expr[100];
//cout << “请输入表达式:”;
cin >> expr;
char* ex = expr;
if (matching(ex)) { cout << “匹配成功”; }
else { cout << “匹配不成功”; };
return 0; } /* 请在这里填写答案 */
输入样例:
{[1+2](3-2)+[3(1+2)]}
输出样例:
匹配成功
代码展示:
int matching(char* exp)
{
SqStack s;
InitStack(s);
for(int i=0;exp[i]!='\0';i++)
{
if((exp[i] =='{')||(exp[i]=='[') ||(exp[i] == '('))
Push(s,exp[i]);
if((exp[i] =='}')||(exp[i]==']') ||(exp[i] == ')'))
{
if(StackEmpty(s))
return 0;
char x;
GetTop(s,x);
if(exp[i] = x)
Pop(s,x);
else
return 0;
}
}
if(StackEmpty(s))
return 1;
else
return 0;
}