Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Project: name
- * 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"
- // Problem 1
- // Part 1-A
- struct entryT {
- string firstName;
- string lastName;
- string phoneNumber;
- };
- int compareEntry(entryT a, entryT b)
- {
- if (a.lastName==b.lastName){
- if (a.firstName == b.firstName) return 0;
- else if (a.firstName<b.firstName) return -1;
- else return 1;
- }
- else {
- if (a.lastName < b.lastName) return -1;
- else return 1;
- };
- };
- // 1-B
- string stringToUpper (string input)
- {
- int i = 0;
- string output = "";
- while (input[i])
- {
- char c = input[i];
- output += toupper(c);
- i++;
- }
- return output;
- }
- int CaseInsensitive(string name1, string name2)
- {
- if (stringToUpper(name1) == stringToUpper(name2)) return 0;
- if (stringToUpper(name1) < stringToUpper(name2)) return -1;
- else return 1;
- }
- // Problem 2
- string convertInt(int number)
- {
- stringstream ss;//create a stringstream
- ss << number;//add number to the stream
- return ss.str();//return a string with the contents of the stream
- }
- struct pointT {
- int x, y;
- };
- struct cityT {
- string name;pointT coordinates;
- };
- // Problem 3
- struct pairT
- {
- string first, second;
- };
- string printPairT(pairT a)
- {
- string stringToReturn = "(" + a.first + ", " + a.second + ")";
- return stringToReturn;
- };
- int comparePairT (pairT a, pairT b)
- {
- if (a.first==b.first){
- if (a.second == b.second) return 0;
- else if (a.second < b.second) return -1;
- else return 1;
- }
- else {
- if (a.first < b.first) return -1;
- else return 1;
- };
- };
- Set<pairT> CartesianProduct(Set<string> & one, Set<string> & two)
- {
- Set<string>::Iterator itr1 = one.iterator();
- Set<pairT> output(comparePairT);
- for (int i = 0; i<one.size(); i++){
- string first = itr1.next();
- Set<string>::Iterator itr2 = two.iterator();
- for (int j = 0; j<two.size(); j++){
- pairT blobs;
- string second = itr2.next();
- blobs.first = first;
- blobs.second = second;
- cout << "Adding " << printPairT(blobs) << endl;
- output.add(blobs);
- }
- }
- return output;
- };
- // Problem 4
- int PrintCannonballs(int height)
- {
- if (height == 1) return 1;
- else return height*height + PrintCannonballs(height-1);
- }
- // Problem 5
- string reversestring(string input)
- {
- string output = "";
- int i = input.size() - 1;
- while (i >= 0){
- output += input[i];
- i--;
- }
- return output;
- };
- string reversestringR(string input)
- {
- if (input.size() <= 1) return input;
- else return reversestringR(input.substr(1)) + input[0];
- }
- // Problem 6
- int remainder(int a, int b)
- {
- return a - (a/b)*b;
- }
- int GCD(int x, int y)
- {
- int max, min;
- if (x>y){
- max = x;
- min = y;
- }
- else{
- max = y;
- min = x;
- }
- if ((max/min) * min == max) return min;
- else return GCD(min, remainder(max,min));
- }
- // Problem 7
- bool IsMeasurable(int target, Vector<int> weights, int index)
- {
- if (target == 0) return true;
- if (index >= weights.size()) return false;
- else return (IsMeasurable(target, weights, index + 1)||IsMeasurable(target - weights[index],
- weights, index + 1)||IsMeasurable(target + weights[index], weights, index + 1));
- }
- // Problem 8
- Vector<string> updateVector(string str, Vector<string> v)
- {
- Vector<string> output;
- for (int i = 0; i<str.size(); i++){
- Vector<string> subOut;
- for (int index = 0; index<v.size(); index++){
- string mod = v[index];
- mod += str[i];
- subOut.add(mod);
- }
- for (int k = 0; k<subOut.size(); k++)
- output.add(subOut[k]);
- }
- return output;
- }
- void ListMnemonics(string str)
- {
- Map<string> beluga;
- beluga.add("2", "ABC");
- beluga.add("3", "DEF");
- beluga.add("4", "GHI");
- beluga.add("5", "JKL");
- beluga.add("6", "MNO");
- beluga.add("7", "PRS");
- beluga.add("8", "TUV");
- beluga.add("9", "WYZ");
- Vector<string> finalOut;
- finalOut.add("");
- for (int i = 0; i<str.size(); i++){
- string key(1,str[i]);
- if (beluga.containsKey(key)){
- string nums = beluga.getValue(key);
- finalOut = updateVector(nums, finalOut);
- }
- else cout << "Bad input: " << str[i] << endl;
- }
- cout << str << " can represent the following letters:" << endl;
- for (int i = 0; i<finalOut.size(); i++)
- cout << finalOut[i] << " ";
- }
- int main ()
- {
- //Problem 1
- // 1-A
- entryT Bob,Tom;
- Bob.firstName = "Bob";
- Bob.lastName = "Jones";
- Tom.firstName = "Bob";
- Tom.lastName = "Jones";
- if (compareEntry(Bob,Tom) == 0) cout << "Problem 1-A Passed!";
- else cout << "Problem 1-A Failed!";
- //Naturally, if Bob and Tom have the same name, our comparator outputs 0.
- // 1-B
- cout << endl << stringToUpper("ooga Booga.") << endl;
- Set<string> oogabooga(CaseInsensitive);
- oogabooga.add("henry");
- oogabooga.add("higgins");
- oogabooga.add("HIGGINS");
- if (oogabooga.size()==2) cout << "Problem 1-B Passed!" << endl;
- else cout << "Problem 1-B Failed!" << endl;
- //Problem 2
- // the above code should satisfy the requirements to make the Map.
- //Problem 3
- Set<string> set1;
- Set<string> set2;
- set1.add("A");
- set1.add("B");
- set1.add("C");
- set2.add("X");
- set2.add("Y");
- Set<pairT> ans = CartesianProduct(set1, set2);
- Set<pairT>::Iterator itr3 = ans.iterator();
- cout << "{";
- while (itr3.hasNext())
- cout << printPairT(itr3.next()) << ", ";
- cout << "}" << endl;
- cout << "Problem 3 Passed!" << endl;
- // Problem 4
- if (PrintCannonballs(4) == 30) cout << "Problem 4 Passed!" << endl;
- else cout << "Problem 4 Failed!" << endl;
- // Problem 5
- string input = "ooga booga";
- if (reversestring(input) == "agoob agoo") cout << "Problem 5-1 Passed!" << endl;
- else cout << "Problem 5-1 Failed!" << endl;
- if (reversestringR(input) == "agoob agoo") cout << "Problem 5-2 Passed!" << endl;
- else cout << "Problem 5-2 Failed!" << endl;
- // Problem 6
- cout << remainder(25,12) << " " << remainder(12,25) << endl;
- cout << GCD(15,18) << endl;
- // Problem 7
- Vector<int> weights;
- weights.add(3);
- weights.add(1);
- cout << IsMeasurable(5,weights,0) << endl;
- // Problem 8
- ListMnemonics("346");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment