机试-括号匹配

 1 #include <iostream>
 2 #include <stack>
 3 #include <stdio.h>
 4 
 5 using namespace std;
 6 
 7 stack<int> S;
 8 char str[110];
 9 char ans[110];
10 
11 int main()
12 {
13     while(scanf("%s",str)!=EOF)
14     {
15         int i;
16         for(i=0;str[i]!=0;i++)
17         {
18             if(str[i]=='(')
19             {
20                 S.push(i);
21                 ans[i]=' ';
22             }
23             else if(str[i]==')')
24             {
25                 if(S.empty()==false)
26                 {
27                     S.pop();
28                     ans[i]=' ';
29                 }
30                 else
31                     ans[i]='?';
32             }
33             else
34                 ans[i]=' ';
35         }
36         while(!S.empty())
37         {
38             ans[S.top()]='$';
39             S.pop();
40         }
41         ans[i]=0;
42         puts(str);
43         puts(ans);
44     }
45     return 0;
46 }
原文地址:https://www.cnblogs.com/Xbert/p/5109980.html