Advertisement
Phr0zen_Penguin

Digit.cpp - Get a Digit from a Specific Position

Aug 23rd, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. /**
  2.  * [Digit.cpp]
  3.  * A program that returns the digit from a specified position in a provided integer.
  4.  *
  5.  * (c) Damion 'Phr0z3n.dev' Tapper, 2016
  6.  * Email: Phr0z3n.Dev@Gmail.com
  7.  */
  8. #include <iostream>
  9. #include <sstream>
  10.  
  11.  
  12. /**
  13.  * Function Prototypes:
  14.  */
  15. int digit(int, int);
  16. std::string IntToString(int);
  17. int * StringToIntArray(std::string);
  18. std::string ReverseString(std::string);
  19.  
  20.  
  21. /**
  22.  *
  23.  */
  24. using std::cout;
  25. using std::cin;
  26. using std::endl;
  27.  
  28.  
  29. /**
  30.  * The main() function:
  31.  *
  32.  * The program entry point.
  33.  */
  34. int main(void)
  35. {
  36.     int number;
  37.     int position;
  38.  
  39.     /**
  40.      * Input:
  41.      */
  42.     cout << "Enter number:" << endl;
  43.     cin >> number;
  44.  
  45.     cout << endl << "Enter position:" << endl;
  46.     cin >> position;
  47.  
  48.     cout << endl;
  49.  
  50.     /**
  51.      * Output:
  52.      */
  53.     cout << digit(number, position) << endl;
  54.  
  55.     return 0;
  56. }
  57.  
  58.  
  59. /**
  60.  * The digit function:
  61.  *
  62.  * A function that returns the digit from a specified position in a provided integer.
  63.  */
  64. int digit(int num, int k)
  65. {
  66.         /**
  67.          * Error check:
  68.          */
  69.         if(num < 0)
  70.         {
  71.             cout << "Error!  Positive integers only!" << endl;
  72.  
  73.             exit(1);
  74.         }
  75.  
  76.  
  77.     /**
  78.      * Convert the number to string:
  79.      */
  80.     std::string StringBuff = IntToString(num);
  81.  
  82.         /**
  83.          * Error check:
  84.          */
  85.         if(k < 0 || k >= StringBuff.length())
  86.         {
  87.             cout << "Error!  Position out of range!" << endl;
  88.  
  89.             exit(1);
  90.         }
  91.  
  92.  
  93.     /**
  94.      * Reverse the string:
  95.      */
  96.     StringBuff = ReverseString(StringBuff);
  97.  
  98.  
  99.     /**
  100.      * Convert the string to an integer array:
  101.      */
  102.     int *IntArray = StringToIntArray(StringBuff);
  103.  
  104.  
  105.     /**
  106.      * Return the appropriate digit:
  107.      */
  108.     return IntArray[k];
  109. }
  110.  
  111.  
  112. /**
  113.  * The IntToString() function:
  114.  *
  115.  * A function that converts a given integer value to a string, and returns the conversion.
  116.  */
  117. std::string IntToString(int Number)
  118. {
  119.     std::ostringstream  converter;                                      // Converts from number to string.
  120.  
  121.  
  122.     /**
  123.      * Perform the conversion:
  124.      */
  125.     converter << Number;
  126.  
  127.  
  128.     /**
  129.      * Return the result:
  130.      */
  131.     return converter.str();
  132. }
  133.  
  134.  
  135. /**
  136.  * The StringToIntArray() function:
  137.  *
  138.  * A function that receives a string and returns and integer array representation of it.
  139.  */
  140. int * StringToIntArray(std::string StringBuff)
  141. {
  142.     int *IntArray = new int[StringBuff.length()];
  143.  
  144.  
  145.         /**
  146.          * Perform string conversion:
  147.          */
  148.         for(int i = 0; i < StringBuff.length(); i++)
  149.         {
  150.             switch(StringBuff.data()[i])
  151.             {
  152.                 case '0':
  153.                     IntArray[i] = 0;
  154.                     break;
  155.                 case '1':
  156.                     IntArray[i] = 1;
  157.                     break;
  158.                 case '2':
  159.                     IntArray[i] = 2;
  160.                     break;
  161.                 case '3':
  162.                     IntArray[i] = 3;
  163.                     break;
  164.                 case '4':
  165.                     IntArray[i] = 4;
  166.                     break;
  167.                 case '5':
  168.                     IntArray[i] = 5;
  169.                     break;
  170.                 case '6':
  171.                     IntArray[i] = 6;
  172.                     break;
  173.                 case '7':
  174.                     IntArray[i] = 7;
  175.                     break;
  176.                 case '8':
  177.                     IntArray[i] = 8;
  178.                     break;
  179.                 case '9':
  180.                     IntArray[i] = 9;
  181.                     break;
  182.                 default:
  183.                     IntArray[i] = '\0';
  184.                     break;
  185.             }
  186.         }
  187.  
  188.  
  189.     /**
  190.      * Null-terminate the integer array:
  191.      */
  192.     IntArray[StringBuff.length()] = '\0';
  193.  
  194.  
  195.     /**
  196.      * Return the array:
  197.      */
  198.     return IntArray;
  199. }
  200.  
  201.  
  202. /**
  203.  * The ReverseString() function:
  204.  *
  205.  * A function that returns the reversed version of a provided string.
  206.  */
  207. std::string ReverseString(std::string NewString)
  208. {
  209.     char    *RevArray = new char[NewString.length()];                               // The reversed version of the provided string.
  210.  
  211.  
  212.         /**
  213.          * Perform string reversal:
  214.          */
  215.         for(int i = 0, d = NewString.length() - 1; d >= 0; i++, d--)
  216.             RevArray[i] = NewString.data()[d];
  217.  
  218.  
  219.     /**
  220.      * Null-terminate the array:
  221.      */
  222.     RevArray[NewString.length()] = '\0';
  223.  
  224.  
  225.     /**
  226.      * Return the result:
  227.      */
  228.     return std::string(RevArray);
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement