⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paren.cpp

📁 data struct algorithm and application in c++ 一书的课后答案源码
💻 CPP
字号:
// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -