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

📄 pair.cpp

📁 常用算法与数据结构原代码
💻 CPP
字号:
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include "stack.h"

const int MaxLength = 100;	//最大字符串长度

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

	// 从表达式中搜索(和)
	for (i=0; i<length; i++)
	{
		if (expr[i] == '(')
			s.Add(i);
		else if (expr[i] == ')')
		{
			try{
				s.Delete(j);
				cout<<j+1<<' '<<i+1<<endl;
			}
			catch(OutOfBounds)
			{
				cout<<"No match for right parenthesis at "<<i+1<<endl;
			}
		}
	}

	while(!s.IsEmpty())
	{
		s.Delete(j);
		cout<<"No match for left parenthesis at "<<j+1<<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;
	cout<<expr;
	cout<<"are"<<endl;
	PrintMatchedPairs(expr);
}

⌨️ 快捷键说明

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