Topic Description:
In a string (not exceeding 100 in length) there is an opening parenthesis, a closing parenthesis, and a uppercase and lowercase letter; a rule (as with a common arithmetic formula) any opening parenthesis is matched from inside to outside with the nearest right bracket to the right. Write a program that finds the opening and closing parentheses that cannot be matched, outputs the original string, and marks an unmatched bracket on the next line. The opening parenthesis cannot be matched with the "$" callout, and the closing parenthesis cannot be matched with the "?" Marking. Input:
The input includes multiple sets of data, one row for each set of data, containing a string containing only the left and right brackets and the uppercase and lowercase letters, and the string length not exceeding 100.
Note: Cin.getline (str,100) can enter up to 99 characters. Output:
For each set of output data, output two lines, the first row contains the original input characters, the second line by "$", "?" and spaces, "$" and "?" Indicates that the corresponding opening and closing parentheses do not match. Sample input:
) (Rttyy ()) SSS) (
Sample output:
) (Rttyy ()) SSS) (
? ? $
#include <stdio.h>
#include <stack>
using namespace std;
Stack<int> S;
Char str[110];
Char ans[110];
int main ()
{
while (scanf ("%s", str)!=eof)
{
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;
}
#include <cstdio>
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main () {
string str;
while (CIN >> str) {
stack<int> s;
String ans = "";
for (int i = 0; i < str.size (); i++) {
if (str[i] = = ' (') {
s.push (i);
Ans + = "";
}
else if (str[i] = = ") {
if (!s.empty ()) {
s.pop ());
Ans + = "";
}
else
ans = "?";
}
else
ans + = "";
}
while (!s.empty ()) {
ans[s.top ()] = ' $ ';
S.pop ();
}
cout << str << "\ n" << ans << endl;
}
return 0;
}