paren.cpp

来自「本文件为C++数据结构源代码」· C++ 代码 · 共 45 行

CPP
45
字号
// match parentheses

#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include "stack.h"

const int MaxLength = 100; // max expression length

void PrintMatchedPairs(char *expr)
{// Parenthesis matching.
   Stack<int> s(MaxLength);
   int j, length = strlen(expr);

   // scan expression expr for ( and )
   for (int i = 1; i <= length; i++) {
      if (expr[i - 1] == '(') s.Add(i);
      else if (expr[i - 1] == ')')
   	try {s.Delete(j);  // unstack match
             cout << j << ' ' << i << endl;}
        catch (OutOfBounds)
             {cout << "No match for right parenthesis"
                   << " at " << i << endl;}
      }

   // remaining ( in stack are unmatched
   while (!s.IsEmpty()) {
      s.Delete(j);
      cout << "No match for left parenthesis at "
           << j << endl;}
}

void main(void)
{
   char expr[MaxLength];
   cout << "Type an expression of length at most "
        << MaxLength << endl;
   cin.getline(expr, MaxLength);
   cout <<"The pairs of matching parentheses in"
        << endl;
   puts(expr);
   cout <<"are" << endl;
   PrintMatchedPairs(expr);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?