8586 括号匹配测试

今天的数据结构学习代码和朋友来分享,测试应该能生活!


8586 括号匹配测试


Time Limit:1000MS  Memory Limit:1000K
Total Submit:679 Accepted:182

Type: Program   Language: Not Limited
Description

利用栈编写满足下列要求的括号匹配检验程序:如果表达式中同意包括两种括号:圆括号和方括号,其嵌套的顺序任意,即([]())或[([][])]等为正确的格式,[(]或([())或(()])均为不对的格式。

输入一个包括上述括号的表达式,检验括号是否配对。

本题给出部分check()函数。要求将check()函数补充完整。并完毕整个程序。

typedef char SElemType;
#include"malloc.h"
#include"stdio.h"
#include"math.h"
#include"process.h" // exit()
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码。如OK等
#define STACK_INIT_SIZE 10 // 存储空间初始分配量
#define STACKINCREMENT 2 // 存储空间分配增量
struct SqStack
{
 SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
 SElemType *top; // 栈顶指针
 int stacksize; // 当前已分配的存储空间,以元素为单位
 }; // 顺序栈
Status InitStack(SqStack &S)
{
 }

Status StackEmpty(SqStack S)
{

 }
Status Push(SqStack &S,SElemType e)
{
 }
 Status Pop(SqStack &S,SElemType &e)
{
 }
void check()
 { // 对于输入的随意一个字符串,检验括号是否配对
   SqStack s;
   SElemType ch[80],*p,e;
   if(InitStack(s)) // 初始化栈成功
   {
    //printf("请输入表达式
");
     __________________________________;
     p=ch;
     while(*p) // 没到串尾
       switch(*p)
       {
         case '(':
         case '[':_______________________;
                  break; // 左括号入栈,且p++
         case ')':
         case ']':if(!StackEmpty(s)) // 栈不空
                  {
                   _________________________; // 弹出栈顶元素
                    if(*p==')'&&e!='('||___________________&&___________________)
                                                // 弹出的栈顶元素与*p不配对
{
                      printf("isn't matched pairs
");
                      exit(ERROR);
                    }
                    else
                    {
                     __________________________;
                      break; // 跳出switch语句
                    }
                  }
                  else // 栈空
                  {
                    printf("lack of left parenthesis
");
                    exit(ERROR);
                  }
         default: ______________________; // 其他字符不处理,指针向后移
       }
     if(StackEmpty(s)) // 字符串结束时栈空
       printf("matching
");
     else
       printf("lack of right parenthesis
");
   }
 }
void main()
 {
   check();
 }



Input

第一行:输入一个包括圆括号或方括号、不超过80个字符的表达式串。

Output

第一行:若输入表达式括号匹配。输出"matching"; 若不匹配,输出详细信息:"isn't matched pairs", 或"lack of left parenthesis"或"lack of right parenthesis"
Sample Input

8*[3*(35-23)]
Sample Output

matching
Author

yqm

答案:

typedef char SElemType;
#include"malloc.h"
#include"stdio.h"
#include"math.h"
#include"stdlib.h" // exit()
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
#define STACK_INIT_SIZE 10 // 存储空间初始分配量
#define STACKINCREMENT 2 // 存储空间分配增量
struct SqStack
{
 SElemType *base; // 在栈构造之前和销毁之后。base的值为NULL
 SElemType *top; // 栈顶指针
 int stacksize; // 当前已分配的存储空间,以元素为单位
 }; // 顺序栈
Status InitStack(SqStack &S)
{
    S.base =(SElemType*)malloc(STACK_INIT_SIZE *sizeof(SElemType));
    if(!S.base ) return FALSE;
    S.top =S.base ;
    S.stacksize =STACK_INIT_SIZE;
    return OK;
}

Status StackEmpty(SqStack S)
{
    if(S.base ==S.top ) return 1;
    else return 0;
 }
Status Push(SqStack &S,SElemType e)
{
    if(S.top -S.base >=S.stacksize  )
    {
        S.base =(SElemType*)realloc(S.base ,(S.stacksize +STACKINCREMENT)*sizeof(SElemType));
        if(!S.base )  return FALSE;
        S.top =S.base +S.stacksize ;
        S.stacksize +=STACKINCREMENT;
    }
    *(S.top )=e;
    S.top ++;
    return OK;
    

 }
 Status Pop(SqStack &S,SElemType &e)
{
    if(S.base ==S.top )
    return FALSE;
    --S.top ;
    e=*(S.top );
    return OK;

 }
void check()
 { // 对于输入的随意一个字符串,检验括号是否配对
   SqStack s;
   SElemType ch[80],*p,e;
   if(InitStack(s)) // 初始化栈成功
   {
    //printf("请输入表达式
");
     gets(ch);
     p=ch;
     while(*p) // 没到串尾
       switch(*p)
       {
         case '(':
         case '[':Push(s,*p++);
                  break; // 左括号入栈,且p++
         case ')':
         case ']':if(!StackEmpty(s)) // 栈不空
                  {
                   Pop(s,e); // 弹出栈顶元素
                    if(*p==')'&&e!='('||*p==']'&&e!='[')
                                                // 弹出的栈顶元素与*p不配对
                    {
                      printf("isn't matched pairs
");
                      exit(ERROR);
                    }
                    else
                    {
                     p++;
                      break; // 跳出switch语句
                    }
                  }
                  else // 栈空
                  {
                    printf("lack of left parenthesis
");
                    exit(ERROR);
                  }
         default: p++;; // 其他字符不处理,指针向后移
       }
     if(StackEmpty(s)) //字符串结束时栈空
       printf("matching
");
     else
       printf("lack of right parenthesis
");
   }
 }
int main()
 {
   check();
 }



 

版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/mfrbuaa/p/4733784.html