您的位置:首页 > 编程语言 > C语言/C++

括号匹配问题1153

2017-04-30 19:53 253 查看
#include "stdafx.h"

#include <stack>

#include <stdio.h>

#include<iostream>

using namespace std;

stack<int> s;

char str[100];

char ans[110];

int _tmain(int argc, _TCHAR* argv[])

{
while (cin >> str)
{
int i;
for (i = 0; str[i] != 0; i++){
if (str[i] == '('){
s.push(i);
ans[i] = ' ';
}
else if (str[i] == ')'){
if (s.empty() == false){
s.pop();
ans[i] = ' ';

}
else ans[i] = '?';

}
else ans[i] = ' ';
}
while (!s.empty()){
ans[s.top()] = '$';
s.pop();
}
ans[i] = 0;
puts(str);
puts(ans);

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  1153 C++ 括号匹配