Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Project: prob set 2-1
- * Created by CS106 C++ Assignment Wizard 0.1
- *
- * Name: [TODO: enter name here]
- * Section: [TODO: enter section leader here]
- * [TODO: Describe assignment]
- */
- #include "genlib.h"
- #include "simpio.h"
- #include <iostream>
- #include <conio.h>
- #include <fstream>
- #include <sstream>
- #include "vector.h"
- #include "map.h"
- #include "set.h"
- #include "stack.h"
- #include "queue.h"
- #include "scanner.h"
- #include "grid.h"
- struct eMailMsg {
- string to; // i.e. "[email protected]"
- string from; // i.e. "[email protected]"
- string message; // body of message
- string subject; // i.e. "CS106 Rocks!"
- int date; // date email was sent
- int time; // time email was sent
- };
- // 1A: Vector<eMailMsg> v;
- //1B:
- void deleteSpam(Vector<eMailMsg> &v)
- {
- int count = 0;
- for (int i = 0; i<v.size(); i++)
- {
- if (v[i].subject.find("SPAM") == 0){
- v.removeAt(i);
- count++;
- }
- }
- cout << count << " messages removed." << endl;
- };
- //1C: you could use a set to store these values. But if order was important,
- // it might be better to use a vector. It depends on what you want.
- // 2:
- void ShowQueue (Queue<int> q)
- {
- while (!q.isEmpty())
- {
- cout << q.peek() << endl;
- q.dequeue();
- };
- };
- void ShowStack (Stack<string> s)
- {
- while (!s.isEmpty())
- {
- cout << s.peek() << endl;
- s.pop();
- };
- };
- void ReverseQueue (Queue<int> &q)
- {
- //cout << "Input of size " << q.size() << endl;
- Stack<int> s;
- int count = 0;
- while (!q.isEmpty())
- {
- s.push(q.dequeue());
- count++;
- }
- while (!s.isEmpty())
- q.enqueue(s.pop());
- //cout << "Successfully moved " << count << " items." << endl;
- }
- //3:
- bool MatchTag(string opentag, string closetag)
- {
- string new_closetag = closetag.erase(1,1);
- if (opentag == new_closetag)
- return true;
- else return false;
- }
- bool isProperlyNested(string html)
- {
- Scanner scanner;
- Vector<string> tags;
- Stack<string> tagStack;
- scanner.setInput(html);
- int count = 0;
- int countlessthan = 0;
- bool proper = true;
- while (scanner.hasMoreTokens())
- {
- string current = scanner.nextToken();
- if (current == "<")
- {
- bool isOpeningTag = true;
- while (scanner.hasMoreTokens()){
- string next = scanner.nextToken();
- if (next == "/")
- isOpeningTag = false;
- current += next;
- if (next == ">") break;
- }
- tags.add(current);
- if (isOpeningTag){
- cout << "adding new tag " << current << " to stack." << endl;
- tagStack.push(current);
- }
- else {
- if (MatchTag(tagStack.peek(),current)){
- cout << "Closing tag " << current << " found. Removing tag from stack." << endl;
- tagStack.pop();
- }
- else {
- cout << "Wrong tag order!!" << endl;
- proper = false;
- break;
- }
- }
- }
- count++;
- }
- cout << "You entered " << count << " tokens, of which " << tags.size() << " were tags, namely:" << endl;
- for (int i = 0; i<tags.size(); i++)
- cout << tags[i] << endl;
- cout << "There are " << tagStack.size() << " items in the tag stack." << endl;
- if (!tagStack.isEmpty())
- ShowStack(tagStack);
- if (proper && tagStack.size() == 0) return true;
- else return false;
- }
- //4
- void MostFrequentCharacter(ifstream &in, Map<int> &countMap, string &mostOccurred, int &maxOccurrence)
- {
- Scanner scanner;
- int count = 0;
- while (!in.fail())
- {
- in.clear();
- string line;
- getline(in,line);
- scanner.setInput(line);
- while (scanner.hasMoreTokens()){
- string word = scanner.nextToken();
- if (word != " " && word != "." && word != ","){
- if (countMap.containsKey(word))
- countMap[word] += 1;
- else
- countMap.add(word, 1);
- }
- }
- cout << line << endl;
- count++;
- }
- //find the maximum
- Map<int>::Iterator itr = countMap.iterator();
- while (itr.hasNext()) {
- string key = itr.next();
- cout << key << " = " << countMap[key] << endl;
- if (countMap[key] > maxOccurrence){
- mostOccurred = key;
- maxOccurrence = countMap[key];
- }
- }
- in.clear();
- }
- //5
- Grid<int> CountBombs(Grid<bool> &mines)
- {
- int sum = 0;
- Grid<int> mineCount(mines.numRows(),mines.numCols());
- for (int row = 0; row < mines.numRows(); row++){
- for (int col = 0; col < mines.numCols(); col++){
- cout << "Looking at row " << row << ", column " << col << endl;
- if (mines.getAt(row,col)){
- cout << "Self is true." << endl;
- sum = 1;
- }
- else sum = 0;
- bool hasUpper = false;
- bool hasLower = false;
- bool hasLeft = false;
- bool hasRight = false;
- if (row > 0){
- cout << "Has upper." << endl;
- hasUpper = true;
- }
- if (row < mines.numRows() - 1){
- cout << "Has lower." << endl;
- hasLower = true;
- }
- if (col > 0){
- cout << "Has left." << endl;
- hasLeft = true;
- }
- if (col < mines.numCols() - 1){
- cout << "Has right." << endl;
- hasRight = true;
- }
- if (hasUpper){
- if (mines.getAt(row-1,col)){
- sum += 1;
- cout << "Bomb above. Sum: " << sum << endl;
- }
- if (hasLeft){
- if (mines.getAt(row-1,col-1)){
- sum += 1;
- cout << "Bomb above-left. Sum: " << sum << endl;
- }
- }
- if (hasRight){
- if (mines.getAt(row-1,col+1)){
- sum += 1;
- cout << "Bomb above-right. Sum: " << sum << endl;
- }
- }
- }
- if (hasLower){
- if (mines.getAt(row+1,col)){
- sum += 1;
- cout << "Bomb below. Sum: " << sum << endl;
- }
- if (hasLeft){
- if (mines.getAt(row+1,col-1)){
- sum += 1;
- cout << "Bomb below-left. Sum: " << sum << endl;
- }
- }
- if (hasRight){
- if (mines.getAt(row+1,col+1)){
- sum += 1;
- cout << "Bomb below-right. Sum: " << sum << endl;
- }
- }
- }
- if (hasRight){
- if (mines.getAt(row,col+1)){
- sum += 1;
- cout << "Bomb to the right. Sum: " << sum << endl;
- }
- else cout << "Bomb not found to the right." << endl;
- }
- if (hasLeft){
- if (mines.getAt(row,col-1)){
- sum += 1;
- cout << "Bomb to the left. Sum: " << sum << endl;
- }
- else cout << "Bomb not found to the left." << endl;
- }
- mineCount.setAt(row,col,sum);
- cout << "Total sum: " << sum << endl << endl;
- };
- };
- return mineCount;
- }
- int main()
- {
- //1
- eMailMsg a, b, c;
- a.subject = "SPAM looking for fun in SHIZUOKA?";
- b.subject = "How are you?";
- c.subject = "SPAM live nude girls!";
- Vector<eMailMsg> v;
- v.add(a);
- v.add(b);
- v.add(c);
- cout << "Before:" << endl;
- for (int i = 0; i<v.size(); i++)
- cout << v[i].subject << endl;
- cout << "After:" << endl;
- deleteSpam(v);
- for (int i = 0; i<v.size(); i++)
- cout << v[i].subject << endl;
- //2
- Queue<int> q;
- q.enqueue(1);
- q.enqueue(2);
- q.enqueue(3);
- cout << "Before:" << endl;
- ShowQueue(q);
- ReverseQueue(q);
- cout << "After:" << endl;
- ShowQueue(q);
- //3
- //cout << "Please enter an html snippet: ";
- //string html = GetLine();
- //if (isProperlyNested(html))
- // cout << "It's OK." << endl;
- //else
- // cout << "dame da ne." << endl;
- //
- //4
- ifstream in;
- Map<int> countMap;
- string mostOccurred;
- int maxOccurrence = 0;
- bool endProgram = false;
- while (true)
- {
- cout << "Enter file name, -1 to exit: ";
- string s = GetLine();
- if (s == "-1")
- break;
- in.open(s.c_str());
- while (in.fail())
- {
- in.clear();
- cout << "Invalid file. Enter file name, -1 to exit: ";
- s = GetLine();
- if (s == "-1")
- {
- endProgram = true;
- break;
- }
- in.open(s.c_str());
- }
- if (!endProgram)
- {
- //cout << "Please enter the number of lines to read: ";
- //int numLines = GetInteger();
- cout << "Computing..." << endl;
- MostFrequentCharacter(in, countMap, mostOccurred, maxOccurrence);
- in.close();
- }
- }
- cout << "The most popular word was " << mostOccurred << ", which occurred " << maxOccurrence << " times." << endl;
- //5
- Grid<bool> mines(6,6);
- mines.setAt(0,0,true);
- mines.setAt(0,1,false);
- mines.setAt(0,2,false);
- mines.setAt(0,3,false);
- mines.setAt(0,4,false);
- mines.setAt(0,5,true);
- mines.setAt(1,0,false);
- mines.setAt(1,1,false);
- mines.setAt(1,2,false);
- mines.setAt(1,3,false);
- mines.setAt(1,4,false);
- mines.setAt(1,5,true);
- mines.setAt(2,0,true);
- mines.setAt(2,1,true);
- mines.setAt(2,2,false);
- mines.setAt(2,3,true);
- mines.setAt(2,4,false);
- mines.setAt(2,5,true);
- mines.setAt(3,0,true);
- mines.setAt(3,1,false);
- mines.setAt(3,2,false);
- mines.setAt(3,3,false);
- mines.setAt(3,4,false);
- mines.setAt(3,5,false);
- mines.setAt(4,0,false);
- mines.setAt(4,1,false);
- mines.setAt(4,2,true);
- mines.setAt(4,3,false);
- mines.setAt(4,4,false);
- mines.setAt(4,5,false);
- mines.setAt(5,0,false);
- mines.setAt(5,1,false);
- mines.setAt(5,2,false);
- mines.setAt(5,3,false);
- mines.setAt(5,4,false);
- mines.setAt(5,5,false);
- cout << "Created minefield:" << endl;
- for (int row = 0; row < mines.numRows(); row++){
- for (int col = 0; col < mines.numCols(); col++){
- cout << "Row: " <<row +1 << " Column: " << col +1 << " Value: " << mines.getAt(row,col) << endl;
- }
- }
- Grid<int> mineCount = CountBombs(mines);
- for (int row = 0; row < mineCount.numRows(); row++){
- for (int col = 0; col < mineCount.numCols(); col++){
- cout << "Row: " <<row +1 << " Column: " << col +1 << " Value: " << mineCount.getAt(row,col) << endl;
- }
- }
- cout << endl << "Press Enter to exit. Goodbye!";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment