16S-Section-Solution

16S-Section-Solution - CS106X Winter 2008 Handout 16S...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 16S Winter 2008 January 23-25, 2007 Section Solution Problem 1: Using the Scanner and Stack classes #include "stack.h" #include "scanner.h" bool ProcessOpenTag(Scanner& scanner, Stack<string>& tagStack) { string tag = scanner.nextToken(); tagStack.push(tag); return true; } bool ProcessCloseTag(Scanner& scanner, Stack<string>& tagStack) { string tag = scanner.nextToken(); if (!tagStack.isEmpty() && tag == tagStack.pop()) { return true; } else { return false; } } bool ProcessTag(Scanner& scanner, Stack<string>& tagStack) { // read the next token to see if we found an // opening or closing tag string token = scanner.nextToken(); if (token == "/") return ProcessCloseTag(scanner, tagStack); scanner.saveToken(token); // So ProcessOpenTag can use it return ProcessOpenTag(scanner, tagStack); } bool IsCorrectlyNested(string htmlStr) { Scanner scanner; scanner.setSpaceOption(Scanner::IgnoreSpaces); scanner.setInput(htmlStr); Stack<string> tagStack; while (scanner.hasMoreTokens()) { string token = scanner.nextToken(); if (token == "<") { if (!ProcessTag(scanner, tagStack)) return false; scanner.nextToken(); // skip over ">" } } return tagStack.isEmpty(); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Problem 2: The Writing Center void ImprovePaper(ifstream& in, ofstream& out, Map<Set<string> >& thes) { while (true) { string line; getline(in, line);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/18/2008 for the course CS 106X taught by Professor Cain,g during the Winter '08 term at Stanford.

Page1 / 4

16S-Section-Solution - CS106X Winter 2008 Handout 16S...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online