Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- void readInput(std::string& numeralSystemStr, std::string& numberOne, std::string& numberTwo) {
- std::cin >> numeralSystemStr >> numberOne >> numberTwo;
- }
- std::vector<char> createNumeralSystem(const std::string& numeralSystemStr) {
- std::vector<char> vec;
- for (const char letter : numeralSystemStr) {
- vec.push_back(letter);
- }
- return vec;
- }
- int createIntFromDigits(const int& digit, int& number) {
- if (digit == 0) {
- number *= 10;
- } else {
- number *= 10;
- number += digit;
- }
- return number;
- }
- int convertStrToInt(std::vector<char> numeralSystem, const std::string& strNumber) {
- int number = 0;
- size_t size = numeralSystem.size();
- for (const char letter : strNumber) {
- for (size_t i = 0; i < size; ++i) {
- if (letter == numeralSystem[i]) {
- number = createIntFromDigits(i, number);
- break;
- }
- }
- }
- return number;
- }
- std::string convertIntToStr(const std::vector<char>& numeralSystem, const int& sum) {
- std::string sumToString = std::to_string(sum);
- std::string finalStr;
- for (const char letter : sumToString) {
- int i = int(letter) - '0';
- finalStr.push_back(numeralSystem[i]);
- }
- return finalStr;
- }
- void printResult(const std::string& stringSum) {
- std::cout << stringSum;
- }
- int main()
- {
- std::string numeralSystemStr;
- std::string numberOne;
- std::string numberTwo;
- readInput(numeralSystemStr, numberOne, numberTwo);
- std::vector<char> numeralSystem = createNumeralSystem(numeralSystemStr);
- int numberOneInt = convertStrToInt(numeralSystem, numberOne);
- int numberTwoInt = convertStrToInt(numeralSystem, numberTwo);
- int intSum = numberOneInt + numberTwoInt;
- std::string stringSum = convertIntToStr(numeralSystem, intSum);
- printResult(stringSum);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement