thinkpad20

Stanford CS106 Problem set 3

Apr 19th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.33 KB | None | 0 0
  1. /*
  2.  * Project: name
  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. // Problem 1
  24. // Part 1-A
  25. struct entryT {
  26.     string firstName;
  27.     string lastName;
  28.     string phoneNumber;
  29. };
  30.  
  31. int compareEntry(entryT a, entryT b)
  32. {
  33.     if (a.lastName==b.lastName){
  34.         if (a.firstName == b.firstName) return 0;
  35.         else if (a.firstName<b.firstName) return -1;
  36.         else return 1;
  37.     }
  38.     else {
  39.         if (a.lastName < b.lastName) return -1;
  40.         else return 1;
  41.     };
  42. };
  43. // 1-B
  44. string stringToUpper (string input)
  45. {
  46.     int i = 0;
  47.     string output = "";
  48.     while (input[i])
  49.     {
  50.         char c = input[i];
  51.         output += toupper(c);
  52.         i++;
  53.     }
  54.     return output;
  55. }
  56.  
  57.  
  58. int CaseInsensitive(string name1, string name2)
  59. {
  60.     if (stringToUpper(name1) == stringToUpper(name2)) return 0;
  61.     if (stringToUpper(name1) < stringToUpper(name2)) return -1;
  62.     else return 1;
  63. }
  64.  
  65. // Problem 2
  66.  
  67. string convertInt(int number)
  68. {
  69.     stringstream ss;//create a stringstream
  70.     ss << number;//add number to the stream
  71.     return ss.str();//return a string with the contents of the stream
  72. }
  73.  
  74. struct pointT {
  75.     int x, y;
  76. };
  77. struct cityT {
  78.     string name;pointT coordinates;
  79. };
  80.  
  81. // Problem 3
  82.  
  83. struct pairT
  84. {
  85.     string first, second;
  86. };
  87.  
  88. string printPairT(pairT a)
  89. {
  90.     string stringToReturn = "(" + a.first + ", " + a.second + ")";
  91.     return stringToReturn;
  92. };
  93.  
  94. int comparePairT (pairT a, pairT b)
  95. {
  96.     if (a.first==b.first){
  97.         if (a.second == b.second) return 0;
  98.         else if (a.second < b.second) return -1;
  99.         else return 1;
  100.     }
  101.     else {
  102.         if (a.first < b.first) return -1;
  103.         else return 1;
  104.     };
  105. };
  106.  
  107. Set<pairT> CartesianProduct(Set<string> & one, Set<string> & two)
  108. {
  109.     Set<string>::Iterator itr1 = one.iterator();
  110.     Set<pairT> output(comparePairT);
  111.     for (int i = 0; i<one.size(); i++){
  112.         string first = itr1.next();
  113.         Set<string>::Iterator itr2 = two.iterator();
  114.         for (int j = 0; j<two.size(); j++){
  115.             pairT blobs;
  116.             string second = itr2.next();
  117.             blobs.first = first;
  118.             blobs.second = second;
  119.             cout << "Adding " << printPairT(blobs) << endl;
  120.             output.add(blobs);
  121.         }
  122.     }
  123.     return output;
  124. };
  125.  
  126. // Problem 4
  127.  
  128. int PrintCannonballs(int height)
  129. {
  130.     if (height == 1) return 1;
  131.     else return height*height + PrintCannonballs(height-1);
  132. }
  133.  
  134. // Problem 5
  135.  
  136. string reversestring(string input)
  137. {
  138.     string output = "";
  139.     int i = input.size() - 1;
  140.     while (i >= 0){
  141.         output += input[i];
  142.         i--;
  143.     }
  144.     return output;
  145. };
  146.  
  147. string reversestringR(string input)
  148. {
  149.     if (input.size() <= 1) return input;
  150.     else return reversestringR(input.substr(1)) + input[0];
  151. }
  152.  
  153. // Problem 6
  154.  
  155. int remainder(int a, int b)
  156. {
  157.     return a - (a/b)*b;
  158. }
  159.  
  160. int GCD(int x, int y)
  161. {
  162.     int max, min;
  163.     if (x>y){
  164.         max = x;
  165.         min = y;
  166.     }
  167.     else{
  168.         max = y;
  169.         min = x;
  170.     }
  171.     if ((max/min) * min == max) return min;
  172.     else return GCD(min, remainder(max,min));
  173. }
  174.  
  175. // Problem 7
  176.  
  177. bool IsMeasurable(int target, Vector<int> weights, int index)
  178. {
  179.     if (target == 0) return true;
  180.     if (index >= weights.size()) return false;
  181.     else return (IsMeasurable(target, weights, index + 1)||IsMeasurable(target - weights[index],
  182.         weights, index + 1)||IsMeasurable(target + weights[index], weights, index + 1));
  183. }
  184.  
  185. // Problem 8
  186.  
  187. Vector<string> updateVector(string str, Vector<string> v)
  188. {
  189.     Vector<string> output;
  190.     for (int i = 0; i<str.size(); i++){
  191.         Vector<string> subOut;
  192.         for (int index = 0; index<v.size(); index++){
  193.             string mod = v[index];
  194.             mod += str[i];
  195.             subOut.add(mod);
  196.         }
  197.         for (int k = 0; k<subOut.size(); k++)
  198.             output.add(subOut[k]);
  199.     }
  200.     return output;
  201. }
  202.  
  203. void ListMnemonics(string str)
  204. {
  205.     Map<string> beluga;
  206.     beluga.add("2", "ABC");
  207.     beluga.add("3", "DEF");
  208.     beluga.add("4", "GHI");
  209.     beluga.add("5", "JKL");
  210.     beluga.add("6", "MNO");
  211.     beluga.add("7", "PRS");
  212.     beluga.add("8", "TUV");
  213.     beluga.add("9", "WYZ");
  214.     Vector<string> finalOut;
  215.     finalOut.add("");
  216.     for (int i = 0; i<str.size(); i++){
  217.         string key(1,str[i]);
  218.         if (beluga.containsKey(key)){
  219.             string nums = beluga.getValue(key);
  220.             finalOut = updateVector(nums, finalOut);
  221.         }
  222.         else cout << "Bad input: " << str[i] << endl;
  223.     }
  224.     cout << str << " can represent the following letters:" << endl;
  225.     for (int i = 0; i<finalOut.size(); i++)
  226.         cout << finalOut[i] << " ";
  227. }
  228.  
  229.  
  230. int main ()
  231. {
  232.     //Problem 1
  233.     // 1-A
  234.     entryT Bob,Tom;
  235.     Bob.firstName = "Bob";
  236.     Bob.lastName = "Jones";
  237.     Tom.firstName = "Bob";
  238.     Tom.lastName = "Jones";
  239.     if (compareEntry(Bob,Tom) == 0) cout << "Problem 1-A Passed!";
  240.     else cout << "Problem 1-A Failed!";
  241.     //Naturally, if Bob and Tom have the same name, our comparator outputs 0.
  242.     // 1-B
  243.     cout << endl << stringToUpper("ooga Booga.") << endl;
  244.     Set<string> oogabooga(CaseInsensitive);
  245.     oogabooga.add("henry");
  246.     oogabooga.add("higgins");
  247.     oogabooga.add("HIGGINS");
  248.     if (oogabooga.size()==2) cout << "Problem 1-B Passed!" << endl;
  249.     else cout << "Problem 1-B Failed!" << endl;
  250.  
  251.     //Problem 2
  252.     // the above code should satisfy the requirements to make the Map.
  253.    
  254.     //Problem 3
  255.     Set<string> set1;
  256.     Set<string> set2;
  257.     set1.add("A");
  258.     set1.add("B");
  259.     set1.add("C");
  260.     set2.add("X");
  261.     set2.add("Y");
  262.     Set<pairT> ans = CartesianProduct(set1, set2);
  263.     Set<pairT>::Iterator itr3 = ans.iterator();
  264.     cout << "{";
  265.     while (itr3.hasNext())
  266.         cout << printPairT(itr3.next()) << ", ";
  267.     cout << "}" << endl;
  268.     cout << "Problem 3 Passed!" << endl;
  269.  
  270.     // Problem 4
  271.  
  272.     if (PrintCannonballs(4) == 30) cout << "Problem 4 Passed!" << endl;
  273.     else cout << "Problem 4 Failed!" << endl;
  274.    
  275.     // Problem 5
  276.     string input = "ooga booga";
  277.     if (reversestring(input) == "agoob agoo") cout << "Problem 5-1 Passed!" << endl;
  278.     else cout << "Problem 5-1 Failed!" << endl;
  279.     if (reversestringR(input) == "agoob agoo") cout << "Problem 5-2 Passed!" << endl;
  280.     else cout << "Problem 5-2 Failed!" << endl;
  281.  
  282.     // Problem 6
  283.     cout << remainder(25,12) << " " << remainder(12,25) << endl;
  284.     cout << GCD(15,18) << endl;
  285.  
  286.     // Problem 7
  287.     Vector<int> weights;
  288.     weights.add(3);
  289.     weights.add(1);
  290.    
  291.     cout << IsMeasurable(5,weights,0) << endl;
  292.     // Problem 8
  293.  
  294.     ListMnemonics("346");
  295.  
  296.     return 0;
  297. }
Advertisement
Add Comment
Please, Sign In to add comment