Skip to content

Instantly share code, notes, and snippets.

@DanielDe
Created November 21, 2012 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielDe/4123132 to your computer and use it in GitHub Desktop.
Save DanielDe/4123132 to your computer and use it in GitHub Desktop.
Week 8 Lab Exercise Solutions
/*
This code is based on the first solution I posted for exercise 4 from Week 6. I placed a MODIFIED comment anywhere I changed the code.
*/
#include <iostream>
#include <vector>
using namespace std;
vector<string> extract_bold(string);
void print_vec(vector<string>&);
int main() {
string test1 = "To <b>succeed</b> in <b>life</b> one must be <b>bold</b>!";
vector<string> results = extract_bold(test1);
print_vec(results);
return 0;
}
// print function to print out the contents of a vector
void print_vec(vector<string> & v) {
for (int i = 0; i < v.size(); ++i)
cout << v.at(i) << endl;
}
vector<string> extract_bold(string htmlText) {
// these tags and their lengths will never change, so we'll make them const
const string OPEN_TAG = "<b>";
const int OPEN_LENGTH = OPEN_TAG.size();
const string CLOSE_TAG = "</b>";
const int CLOSE_LENGTH = CLOSE_TAG.size();
// this vector will hold our results
vector<string> results; // MODIFIED
// this is where we'll start searching for the next <b> tag
int findStartIndex = 0;
// we'll keep searching so long as there are more <b> tags in the string
while (htmlText.find(OPEN_TAG, findStartIndex) != -1) {
// find the beginning and end of our next result
int beginIndex = htmlText.find(OPEN_TAG, findStartIndex) + OPEN_LENGTH;
int endIndex = htmlText.find(CLOSE_TAG, findStartIndex);
// substr expects a length, so we'll calculate the length of our next result
int length = endIndex - beginIndex;
// extract the next result using substring
string nextResult = htmlText.substr(beginIndex, length);
// push the next result onto the end of the results vector
results.push_back(nextResult); // MODIFIED
// our new starting search index is after the </b> tag
findStartIndex = endIndex + CLOSE_LENGTH;
}
// MODIFIED
// return the answer
return results;
}
#include <iostream>
#include <vector>
using namespace std;
bool isOpeningBrace(char);
bool isClosingBrace(char);
bool braceTypeMatches(char, char);
bool bracesMatch(string);
int main() {
cout << "Enter a string: ";
string input;
cin >> input;
if (bracesMatch(input))
cout << "All braces match" << endl;
else
cout << "Braces are mismatched." << endl;
return 0;
}
bool isOpeningBrace(char c) {
return c == '{' || c == '(' || c == '[';
}
bool isClosingBrace(char c) {
return c == '}' || c == ')' || c == ']';
}
bool braceTypeMatches(char open, char closed) {
if (open == '{')
return closed == '}';
else if (open == '(')
return closed == ')';
else if (open == '[')
return closed == ']';
else
return false;
}
bool bracesMatch(string s) {
// create a stack to hold the opening braces
vector<char> bracesStack;
// iterate over every character of s
for (int i = 0; i < s.size(); ++i) {
// extract the next character
char c = s.at(i);
if (isOpeningBrace(c))
// if the character is an opening brace, push it on the stack
bracesStack.push_back(c);
else if (isClosingBrace(c)) {
if (s.size() == 0)
return false; // no more opening braces, so this must be a mismatch
else {
// if this closing brace doesn't match the last opening one, there's a mismatch
if (!braceTypeMatches(bracesStack.at(bracesStack.size() - 1), c))
return false;
// if the brace does match, pop the last one off of the stack
bracesStack.pop_back();
}
}
}
// if the stack isn't empty at this point, there's a mismatch
return bracesStack.size() == 0;
}
#include <iostream>
#include <vector>
using namespace std;
void sort_vec(vector<int>&);
void print_vec(vector<int>&);
int main() {
vector<int> v;
v.push_back(3);
v.push_back(1);
v.push_back(4);
v.push_back(1);
v.push_back(5);
v.push_back(9);
v.push_back(2);
v.push_back(6);
v.push_back(5);
v.push_back(3);
print_vec(v);
cout << endl;
sort_vec(v);
print_vec(v);
return 0;
}
void print_vec(vector<int> & v) {
for (int i = 0; i < v.size(); ++i)
cout << v.at(i) << " ";
}
void sort_vec(vector<int> & v) {
for (int i = 0; i < v.size(); ++i) {
for (int j = 0; j < v.size() - 1; ++j) {
if (v.at(j) > v.at(j + 1)) {
int temp = v.at(j);
v.at(j) = v.at(j + 1);
v.at(j + 1) = temp;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment