CopyPastor

Detecting plagiarism made easy.

Score: 0.8069249987602234; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2022-11-08
by JustLearning

Original Post

Original - Posted on 2018-02-16
by Danilo Ramos



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Welcome to C++!
Modern C++ takes optional parameters. In addition, you should probably revisit the basics of how functions `return` in the language. Multiple `return` statements are ignored after the first one.
This should work: ``` File Edit Options Buffers Tools C++ Help #include <iostream> #include <string> #include <string.h> using namespace std; string initials(string first, string second, string third = "", string fourth = "", string fifth = ""); int main() { string a = "This"; string b = "Are"; string c = "My"; string d = "Name"; string e = "Initials"; cout << initials(a, b, c, d, e) << endl; cout << initials("John", "Smith") << endl; } string initials(string first, string second, string third, string fourth, string fifth) { string result = string(1, first.at(0)) + ". "; result += string(1, second.at(0)) + ". "; result += third == "" ? "" : string(1, third.at(0)) + ". "; result += fourth == "" ? "" : string(1, fourth.at(0)) + ". "; result += fifth == "" ? "" : string(1, fifth.at(0)) + ". "; return result; } ```
If you've learned arrays already, you should notice that the implementation of `initials` could easily be generalized to an array of N names.
This is similar to Yuri Finkelstein; but does not required boost. I am using a map so you can assign any value to the enums, any order.
Declaration of enum class as:
DECLARE_ENUM_WITH_TYPE(TestEnumClass, int32_t, ZERO = 0x00, TWO = 0x02, ONE = 0x01, THREE = 0x03, FOUR); The following code will automatically create the enum class and overload:
- '+' '+=' for std::string - '<<' for streams - '~' just to convert to string (Any unary operator will do, but I personally don't like it for clarity) - '*' to get the count of enums
No boost required, all required functions provided.
Code:
#include <algorithm> #include <iostream> #include <map> #include <sstream> #include <string> #include <vector>
#define STRING_REMOVE_CHAR(str, ch) str.erase(std::remove(str.begin(), str.end(), ch), str.end()) std::vector<std::string> splitString(std::string str, char sep = ',') { std::vector<std::string> vecString; std::string item; std::stringstream stringStream(str); while (std::getline(stringStream, item, sep)) { vecString.push_back(item); } return vecString; }
#define DECLARE_ENUM_WITH_TYPE(E, T, ...) \ enum class E : T \ { \ __VA_ARGS__ \ }; \ std::map<T, std::string> E##MapName(generateEnumMap<T>(#__VA_ARGS__)); \ std::ostream &operator<<(std::ostream &os, E enumTmp) \ { \ os << E##MapName[static_cast<T>(enumTmp)]; \ return os; \ } \ size_t operator*(E enumTmp) { (void) enumTmp; return E##MapName.size(); } \ std::string operator~(E enumTmp) { return E##MapName[static_cast<T>(enumTmp)]; } \ std::string operator+(std::string &&str, E enumTmp) { return str + E##MapName[static_cast<T>(enumTmp)]; } \ std::string operator+(E enumTmp, std::string &&str) { return E##MapName[static_cast<T>(enumTmp)] + str; } \ std::string &operator+=(std::string &str, E enumTmp) \ { \ str += E##MapName[static_cast<T>(enumTmp)]; \ return str; \ } \ E operator++(E &enumTmp) \ { \ auto iter = E##MapName.find(static_cast<T>(enumTmp)); \ if (iter == E##MapName.end() || std::next(iter) == E##MapName.end()) \ iter = E##MapName.begin(); \ else \ { \ ++iter; \ } \ enumTmp = static_cast<E>(iter->first); \ return enumTmp; \ } \ bool valid##E(T value) { return (E##MapName.find(value) != E##MapName.end()); } #define DECLARE_ENUM(E, ...) DECLARE_ENUM_WITH_TYPE(E, int32_t, __VA_ARGS__) template <typename T> std::map<T, std::string> generateEnumMap(std::string strMap) { STRING_REMOVE_CHAR(strMap, ' '); STRING_REMOVE_CHAR(strMap, '('); std::vector<std::string> enumTokens(splitString(strMap)); std::map<T, std::string> retMap; T inxMap; inxMap = 0; for (auto iter = enumTokens.begin(); iter != enumTokens.end(); ++iter) { // Token: [EnumName | EnumName=EnumValue] std::string enumName; T enumValue; if (iter->find('=') == std::string::npos) { enumName = *iter; } else { std::vector<std::string> enumNameValue(splitString(*iter, '=')); enumName = enumNameValue[0]; //inxMap = static_cast<T>(enumNameValue[1]); if (std::is_unsigned<T>::value) { inxMap = static_cast<T>(std::stoull(enumNameValue[1], 0, 0)); } else { inxMap = static_cast<T>(std::stoll(enumNameValue[1], 0, 0)); } } retMap[inxMap++] = enumName; } return retMap; }
Example:
DECLARE_ENUM_WITH_TYPE(TestEnumClass, int32_t, ZERO = 0x00, TWO = 0x02, ONE = 0x01, THREE = 0x03, FOUR); int main(void) { TestEnumClass first, second; first = TestEnumClass::FOUR; second = TestEnumClass::TWO; std::cout << first << "(" << static_cast<uint32_t>(first) << ")" << std::endl; // FOUR(4) std::string strOne; strOne = ~first; std::cout << strOne << std::endl; // FOUR std::string strTwo; strTwo = ("Enum-" + second) + (TestEnumClass::THREE + "-test"); std::cout << strTwo << std::endl; // Enum-TWOTHREE-test std::string strThree("TestEnumClass: "); strThree += second; std::cout << strThree << std::endl; // TestEnumClass: TWO std::cout << "Enum count=" << *first << std::endl; }
<kbd>You can run the code [here][1]</kbd>

[1]: http://jdoodle.com/a/w1Q

        
Present in both answers; Present only in the new answer; Present only in the old answer;