thinkpad20

Stanford CS106B Problem set 2

Apr 10th, 2012
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.28 KB | None | 0 0
  1. /*
  2.  * Project: prob set 2-1
  3.  * Created by CS106 C++ Assignment Wizard 0.1
  4.  *
  5.  * Name: [TODO: enter name here]
  6.  * Section: [TODO: enter section leader here]
  7.  * [TODO: Describe assignment]
  8.  */
  9. #include "genlib.h"
  10. #include "simpio.h"
  11. #include <iostream>
  12. #include <conio.h>
  13. #include <fstream>
  14. #include <sstream>
  15. #include "vector.h"
  16. #include "map.h"
  17. #include "set.h"
  18. #include "stack.h"
  19. #include "queue.h"
  20. #include "scanner.h"
  21. #include "grid.h"
  22.  
  23. struct eMailMsg {
  24.   string to;        // i.e. "[email protected]"
  25.   string from;      // i.e. "[email protected]"
  26.   string message;   // body of message
  27.   string subject;   // i.e. "CS106 Rocks!"
  28.   int date;         // date email was sent
  29.   int time;         // time email was sent
  30. };
  31. // 1A: Vector<eMailMsg> v;
  32. //1B:
  33. void deleteSpam(Vector<eMailMsg> &v)
  34. {
  35.     int count = 0;
  36.     for (int i = 0; i<v.size(); i++)
  37.     {
  38.         if (v[i].subject.find("SPAM") == 0){
  39.             v.removeAt(i);
  40.             count++;
  41.         }
  42.     }
  43.     cout << count << " messages removed." << endl;
  44. };
  45. //1C: you could use a set to store these values. But if order was important,
  46. //    it might be better to use a vector. It depends on what you want.
  47.  
  48. // 2:
  49. void ShowQueue (Queue<int> q)
  50. {
  51.     while (!q.isEmpty())
  52.     {
  53.         cout << q.peek() << endl;
  54.         q.dequeue();
  55.     };
  56. };
  57. void ShowStack (Stack<string> s)
  58. {
  59.     while (!s.isEmpty())
  60.     {
  61.         cout << s.peek() << endl;
  62.         s.pop();
  63.     };
  64. };
  65. void ReverseQueue (Queue<int> &q)
  66. {
  67.     //cout << "Input of size " << q.size() << endl;
  68.     Stack<int> s;
  69.     int count = 0;
  70.     while (!q.isEmpty())
  71.     {
  72.         s.push(q.dequeue());
  73.         count++;
  74.     }
  75.     while (!s.isEmpty())
  76.         q.enqueue(s.pop());
  77.     //cout << "Successfully moved " << count << " items." << endl;
  78. }
  79.  
  80. //3:
  81. bool MatchTag(string opentag, string closetag)
  82. {
  83.     string new_closetag = closetag.erase(1,1);
  84.     if (opentag == new_closetag)
  85.         return true;
  86.     else return false;
  87. }
  88.  
  89. bool isProperlyNested(string html)
  90. {
  91.     Scanner scanner;
  92.     Vector<string> tags;
  93.     Stack<string> tagStack;
  94.     scanner.setInput(html);
  95.     int count = 0;
  96.     int countlessthan = 0;
  97.     bool proper = true;
  98.     while (scanner.hasMoreTokens())
  99.     {
  100.         string current = scanner.nextToken();
  101.         if (current == "<")
  102.         {
  103.             bool isOpeningTag = true;
  104.             while (scanner.hasMoreTokens()){
  105.                 string next = scanner.nextToken();
  106.                 if (next == "/")
  107.                     isOpeningTag = false;
  108.                 current += next;
  109.                 if (next == ">") break;
  110.             }
  111.             tags.add(current);
  112.             if (isOpeningTag){
  113.                 cout << "adding new tag " << current << " to stack." << endl;
  114.                 tagStack.push(current);
  115.             }
  116.             else {
  117.                 if (MatchTag(tagStack.peek(),current)){
  118.                     cout << "Closing tag " << current << " found. Removing tag from stack." << endl;
  119.                     tagStack.pop();
  120.                 }
  121.                 else {
  122.                     cout << "Wrong tag order!!" << endl;
  123.                     proper = false;
  124.                     break;
  125.                 }
  126.             }
  127.         }
  128.         count++;
  129.     }
  130.     cout << "You entered " << count << " tokens, of which " << tags.size() << " were tags, namely:" << endl;
  131.     for (int i = 0; i<tags.size(); i++)
  132.         cout << tags[i] << endl;
  133.     cout << "There are " << tagStack.size() << " items in the tag stack." << endl;
  134.     if (!tagStack.isEmpty())
  135.         ShowStack(tagStack);
  136.     if (proper && tagStack.size() == 0) return true;
  137.             else return false;
  138. }
  139.  
  140. //4
  141. void MostFrequentCharacter(ifstream &in, Map<int> &countMap, string &mostOccurred, int &maxOccurrence)
  142. {
  143.     Scanner scanner;
  144.     int count = 0;
  145.     while (!in.fail())
  146.     {
  147.         in.clear();
  148.         string line;
  149.         getline(in,line);
  150.         scanner.setInput(line);
  151.         while (scanner.hasMoreTokens()){
  152.             string word = scanner.nextToken();
  153.             if (word != " " && word != "." && word != ","){
  154.                 if (countMap.containsKey(word))
  155.                     countMap[word] += 1;
  156.                 else
  157.                     countMap.add(word, 1);
  158.             }
  159.         }
  160.         cout << line << endl;
  161.         count++;
  162.     }
  163.     //find the maximum
  164.     Map<int>::Iterator itr = countMap.iterator();
  165.     while (itr.hasNext()) {
  166.         string key = itr.next();
  167.         cout << key << " = " << countMap[key] << endl;
  168.         if (countMap[key] > maxOccurrence){
  169.             mostOccurred = key;
  170.             maxOccurrence = countMap[key];
  171.         }
  172.     }
  173.     in.clear();
  174. }
  175.  
  176. //5
  177.  
  178. Grid<int> CountBombs(Grid<bool> &mines)
  179. {
  180.     int sum = 0;
  181.     Grid<int> mineCount(mines.numRows(),mines.numCols());
  182.     for (int row = 0; row < mines.numRows(); row++){
  183.         for (int col = 0; col < mines.numCols(); col++){
  184.             cout << "Looking at row " << row << ", column " << col << endl;
  185.             if (mines.getAt(row,col)){
  186.                 cout << "Self is true." << endl;
  187.                 sum = 1;
  188.             }
  189.             else sum = 0;
  190.             bool hasUpper = false;
  191.             bool hasLower = false;
  192.             bool hasLeft = false;
  193.             bool hasRight = false;
  194.             if (row > 0){
  195.                 cout << "Has upper." << endl;
  196.                 hasUpper = true;
  197.             }
  198.             if (row < mines.numRows() - 1){
  199.                 cout << "Has lower." << endl;
  200.                 hasLower = true;
  201.             }
  202.             if (col > 0){
  203.                 cout << "Has left." << endl;
  204.                 hasLeft = true;
  205.             }
  206.             if (col < mines.numCols() - 1){
  207.                 cout << "Has right." << endl;
  208.                 hasRight = true;
  209.             }
  210.             if (hasUpper){
  211.                 if (mines.getAt(row-1,col)){
  212.                     sum += 1;
  213.                     cout << "Bomb above. Sum: " << sum << endl;
  214.                 }
  215.                 if (hasLeft){
  216.                     if (mines.getAt(row-1,col-1)){
  217.                         sum += 1;
  218.                         cout << "Bomb above-left. Sum: " << sum << endl;
  219.                     }
  220.                 }
  221.                 if (hasRight){
  222.                     if (mines.getAt(row-1,col+1)){
  223.                         sum += 1;
  224.                         cout << "Bomb above-right. Sum: " << sum << endl;
  225.                     }
  226.                 }
  227.             }
  228.             if (hasLower){
  229.                 if (mines.getAt(row+1,col)){
  230.                     sum += 1;
  231.                     cout << "Bomb below. Sum: " << sum << endl;
  232.                 }
  233.                 if (hasLeft){
  234.                     if (mines.getAt(row+1,col-1)){
  235.                         sum += 1;
  236.                         cout << "Bomb below-left. Sum: " << sum << endl;
  237.                     }
  238.                 }
  239.                 if (hasRight){
  240.                     if (mines.getAt(row+1,col+1)){
  241.                         sum += 1;
  242.                         cout << "Bomb below-right. Sum: " << sum << endl;
  243.                     }
  244.                 }
  245.             }
  246.             if (hasRight){
  247.                 if (mines.getAt(row,col+1)){
  248.                     sum += 1;
  249.                     cout << "Bomb to the right. Sum: " << sum << endl;
  250.                 }
  251.                 else cout << "Bomb not found to the right." << endl;
  252.             }
  253.             if (hasLeft){
  254.                 if (mines.getAt(row,col-1)){
  255.                     sum += 1;
  256.                     cout << "Bomb to the left. Sum: " << sum << endl;
  257.                 }
  258.                 else cout << "Bomb not found to the left." << endl;
  259.             }
  260.             mineCount.setAt(row,col,sum);
  261.             cout << "Total sum: " << sum << endl << endl;
  262.         };
  263.     };
  264.     return mineCount;
  265. }
  266.  
  267. int main()
  268. {
  269.     //1
  270.     eMailMsg a, b, c;
  271.     a.subject = "SPAM looking for fun in SHIZUOKA?";
  272.     b.subject = "How are you?";
  273.     c.subject = "SPAM live nude girls!";
  274.     Vector<eMailMsg> v;
  275.     v.add(a);
  276.     v.add(b);
  277.     v.add(c);
  278.     cout << "Before:" << endl;
  279.     for (int i = 0; i<v.size(); i++)
  280.         cout << v[i].subject << endl;
  281.     cout << "After:" << endl;
  282.     deleteSpam(v);
  283.     for (int i = 0; i<v.size(); i++)
  284.         cout << v[i].subject << endl;
  285.  
  286.     //2
  287.     Queue<int> q;
  288.     q.enqueue(1);
  289.     q.enqueue(2);
  290.     q.enqueue(3);
  291.     cout << "Before:" << endl;
  292.     ShowQueue(q);
  293.     ReverseQueue(q);
  294.     cout << "After:" << endl;
  295.     ShowQueue(q);
  296.  
  297.     //3
  298.     //cout << "Please enter an html snippet: ";
  299.     //string html = GetLine();
  300.     //if (isProperlyNested(html))
  301.     //  cout << "It's OK." << endl;
  302.     //else
  303.     //  cout << "dame da ne." << endl;
  304.     //
  305.     //4
  306.     ifstream in;
  307.     Map<int> countMap;
  308.     string mostOccurred;
  309.     int maxOccurrence = 0;
  310.     bool endProgram = false;
  311.     while (true)
  312.     {
  313.         cout << "Enter file name, -1 to exit: ";
  314.         string s = GetLine();
  315.         if (s == "-1")
  316.             break;
  317.         in.open(s.c_str());
  318.         while (in.fail())
  319.         {
  320.             in.clear();
  321.             cout << "Invalid file. Enter file name, -1 to exit: ";
  322.             s = GetLine();
  323.             if (s == "-1")
  324.             {
  325.                 endProgram = true;
  326.                 break;
  327.             }
  328.             in.open(s.c_str());
  329.         }
  330.         if (!endProgram)
  331.         {
  332.             //cout << "Please enter the number of lines to read: ";
  333.             //int numLines = GetInteger();
  334.             cout << "Computing..." << endl;
  335.             MostFrequentCharacter(in, countMap, mostOccurred, maxOccurrence);
  336.             in.close();
  337.         }
  338.     }
  339.     cout << "The most popular word was " << mostOccurred << ", which occurred " << maxOccurrence << " times." << endl;
  340.  
  341.     //5
  342.  
  343.     Grid<bool> mines(6,6);
  344.     mines.setAt(0,0,true);
  345.     mines.setAt(0,1,false);
  346.     mines.setAt(0,2,false);
  347.     mines.setAt(0,3,false);
  348.     mines.setAt(0,4,false);
  349.     mines.setAt(0,5,true);
  350.     mines.setAt(1,0,false);
  351.     mines.setAt(1,1,false);
  352.     mines.setAt(1,2,false);
  353.     mines.setAt(1,3,false);
  354.     mines.setAt(1,4,false);
  355.     mines.setAt(1,5,true);
  356.     mines.setAt(2,0,true);
  357.     mines.setAt(2,1,true);
  358.     mines.setAt(2,2,false);
  359.     mines.setAt(2,3,true);
  360.     mines.setAt(2,4,false);
  361.     mines.setAt(2,5,true);
  362.     mines.setAt(3,0,true);
  363.     mines.setAt(3,1,false);
  364.     mines.setAt(3,2,false);
  365.     mines.setAt(3,3,false);
  366.     mines.setAt(3,4,false);
  367.     mines.setAt(3,5,false);
  368.     mines.setAt(4,0,false);
  369.     mines.setAt(4,1,false);
  370.     mines.setAt(4,2,true);
  371.     mines.setAt(4,3,false);
  372.     mines.setAt(4,4,false);
  373.     mines.setAt(4,5,false);
  374.     mines.setAt(5,0,false);
  375.     mines.setAt(5,1,false);
  376.     mines.setAt(5,2,false);
  377.     mines.setAt(5,3,false);
  378.     mines.setAt(5,4,false);
  379.     mines.setAt(5,5,false);
  380.     cout << "Created minefield:" << endl;
  381.     for (int row = 0; row < mines.numRows(); row++){
  382.         for (int col = 0; col < mines.numCols(); col++){
  383.             cout << "Row: " <<row +1 << " Column: " << col +1 << " Value: " << mines.getAt(row,col) << endl;
  384.         }
  385.     }
  386.     Grid<int> mineCount = CountBombs(mines);
  387.     for (int row = 0; row < mineCount.numRows(); row++){
  388.         for (int col = 0; col < mineCount.numCols(); col++){
  389.             cout << "Row: " <<row +1 << " Column: " << col +1 << " Value: " << mineCount.getAt(row,col) << endl;
  390.         }
  391.     }
  392.  
  393.     cout << endl << "Press Enter to exit. Goodbye!";
  394.     return 0;
  395. }
Advertisement
Add Comment
Please, Sign In to add comment