📄 pair.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 + -