Skip to content

Commit

Permalink
Finished ch09 🍻
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 5, 2014
1 parent 78c571f commit f790448
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ch09/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,6 @@ what does numbers.find(name) return?
string::npos

## [Exercise 9.49](ex9_49.cpp)
## [Exercise 9.50](ex9_50.cpp)
## [Exercise 9.51](ex9_51.cpp)
## [Exercise 9.52](ex9_52.cpp)
45 changes: 45 additions & 0 deletions ch09/ex9_52.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// ex9_52.cpp
// Exercise 9.52
//
// Created by pezy on 12/5/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief Use a stack to process parenthesized expressions.
// When you see an open parenthesis, note that it was seen.
// When you see a close parenthesis after an open parenthesis,
// pop elements down to and including the open parenthesis off the stack.
// push a value onto the stack to indicate that a parenthesized expression was replaced.

#include <stack>
#include <string>
#include <iostream>

using std::string; using std::cout; using std::endl; using std::stack;

int main()
{
string expression{"This is (pezy)."};
bool bSeen = false;
stack<char> stk;
for (const auto &s : expression)
{
if (s == '(') { bSeen = true; continue; }
else if (s == ')') bSeen = false;

if (bSeen) stk.push(s);
}

string repstr;
while (!stk.empty())
{
repstr += stk.top();
stk.pop();
}

expression.replace(expression.find("(")+1, repstr.size(), repstr);

cout << expression << endl;

return 0;
}

0 comments on commit f790448

Please sign in to comment.