Guest User

2. Numeral System

a guest
May 7th, 2021
63
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. void readInput(std::string& numeralSystemStr, std::string& numberOne, std::string& numberTwo) {
  6.     std::cin >> numeralSystemStr >> numberOne >> numberTwo;
  7. }
  8.  
  9. std::vector<char> createNumeralSystem(const std::string& numeralSystemStr) {
  10.     std::vector<char> vec;
  11.     for (const char letter : numeralSystemStr) {
  12.         vec.push_back(letter);
  13.     }
  14.     return vec;
  15. }
  16.  
  17. int createIntFromDigits(const int& digit, int& number) {
  18.     if (digit == 0) {
  19.         number *= 10;
  20.     } else {
  21.         number *= 10;
  22.         number += digit;
  23.     }
  24.     return number;
  25. }
  26.  
  27. int convertStrToInt(std::vector<char> numeralSystem, const std::string& strNumber) {
  28.     int number = 0;
  29.     size_t size = numeralSystem.size();
  30.     for (const char letter : strNumber) {
  31.         for (size_t i = 0; i < size; ++i) {
  32.             if (letter == numeralSystem[i]) {
  33.                 number = createIntFromDigits(i, number);
  34.                 break;
  35.             }
  36.         }
  37.     }
  38.     return number;
  39. }
  40.  
  41. std::string convertIntToStr(const std::vector<char>& numeralSystem, const int& sum) {
  42.     std::string sumToString = std::to_string(sum);
  43.     std::string finalStr;
  44.     for (const char letter : sumToString) {
  45.         int i = int(letter) - '0';
  46.         finalStr.push_back(numeralSystem[i]);
  47.     }
  48.     return finalStr;
  49. }
  50.  
  51. void printResult(const std::string& stringSum) {
  52.     std::cout << stringSum;
  53. }
  54.  
  55. int main()
  56. {
  57.     std::string numeralSystemStr;
  58.     std::string numberOne;
  59.     std::string numberTwo;
  60.     readInput(numeralSystemStr, numberOne, numberTwo);
  61.     std::vector<char> numeralSystem = createNumeralSystem(numeralSystemStr);
  62.     int numberOneInt = convertStrToInt(numeralSystem, numberOne);
  63.     int numberTwoInt = convertStrToInt(numeralSystem, numberTwo);
  64.     int intSum = numberOneInt + numberTwoInt;
  65.     std::string stringSum = convertIntToStr(numeralSystem, intSum);
  66.     printResult(stringSum);
  67. }
  68.  
RAW Paste Data