// ConsoleApplication7.cpp : main project file. #include "stdafx.h" #include #include using namespace std; string merge(string a, string b) { if (a.length() == 0) return b; if (b.length() == 0) return a; if (a[0] < b[0]) return a[0] + merge(a.substr(1, a.length()-1), b); else return b[0] + merge(a, b.substr(1, b.length()-1)); } string dedup(string in) { if (in.length() < 2) return in; if (in[0] == in[1]) return in[1] + dedup(in.substr(2, in.length() - 2)); else return in[0] + dedup(in.substr(1, in.length() -1)); } int count(string s) { if (s.length() == 0) return 0; if (s[0] == 'A') return 1 + count(s.substr(1,s.length()-1)); else return count(s.substr(1,s.length()-1)); } int sum(int a, int b) { if (a > b) return 0; else return a + sum(a+1, b); } int div10(int num) { if (num < 10) return 0; else return 1 + div10(num-10); } int main(array ^args) { // DO ANY THREE OF THESE. // ONLY DO THREE OF THESE. // THERE ARE FIVE. YOU CAN SKIP TWO! // Make a recursive function that returns the sum of the // integers from A .. B. cout << sum(1,3) << " -- sum(1,3) Should be 1+2+3 = 6" << endl; cout << sum(5,10) << " -- sum(5,10) Should be 5+6+7+8+9+10 = 45" << endl; cout << sum(-2,2) << " -- sum(-2,2) Should be -2 + -1 + 0 + 1 + 2 = 0" << endl; cout << endl << endl; // Make a function that divided by 10. // You may not use the "/" operator. // The argument will be non-negative. // You must round down. cout << div10(0) << " -- div10(0) = 0" << endl; cout << div10(34) << " -- div10(34) = 3" << endl; cout << div10(117) << " -- div10(117) = 17" << endl; cout << div10(9) << " -- div10(9) = 0" << endl; cout << endl << endl; // Make a recusive function that counts the number of 'A's in the string. cout << count("A") << " -- count(\"A\") -- Should be 1" << endl; cout << count("") << " -- count(\"\") -- Should be 0" << endl; cout << count("ABBA") << " -- count(\"ABBA\") -- Should be 2" << endl; cout << count("A large dog AA") << " -- count(\"A large dog AA\") -- Should be 3" << endl; cout << count("recusion is fun") << " -- count(\"recursion is fun\") -- Should be 0" << endl; cout << endl << endl; // Make a recursive method that eliminates the second letter of duplicate letters // that are next to each other. cout << dedup("AB") << " -- dedup(\"AB\") -- Should produce AB" << endl; cout << dedup("") << " -- dedup(\"\") -- Should produce the empty string" << endl; cout << dedup("mississippi") << " -- dedup(\"mississippi\") -- Should produce misisipi" << endl; cout << dedup("ABBA") << " -- dedup(\"ABBA\") -- Should produce ABA" << endl; cout << endl << endl; // Merge two strings that are ordered alphabetically. // The results should also be alphabetic. cout << merge("abd", "cef") << " -- merge(\"abe\", \"cef\") should be abcdef " << endl; cout << merge("", "") << " -- merge(\"\", \"\") -- should be the empty string." << endl; cout << merge("xy", "abc") << " -- merge(\"xy\", \"abc\") -- should be abcxy." << endl; cout << merge("abxy", "dd") << " -- merge(\"abxy\", \"dd\") -- should be abddxy." << endl; system("pause"); }