Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * [Digit.cpp]
- * A program that returns the digit from a specified position in a provided integer.
- *
- * (c) Damion 'Phr0z3n.dev' Tapper, 2016
- * Email: Phr0z3n.Dev@Gmail.com
- */
- #include <iostream>
- #include <sstream>
- /**
- * Function Prototypes:
- */
- int digit(int, int);
- std::string IntToString(int);
- int * StringToIntArray(std::string);
- std::string ReverseString(std::string);
- /**
- *
- */
- using std::cout;
- using std::cin;
- using std::endl;
- /**
- * The main() function:
- *
- * The program entry point.
- */
- int main(void)
- {
- int number;
- int position;
- /**
- * Input:
- */
- cout << "Enter number:" << endl;
- cin >> number;
- cout << endl << "Enter position:" << endl;
- cin >> position;
- cout << endl;
- /**
- * Output:
- */
- cout << digit(number, position) << endl;
- return 0;
- }
- /**
- * The digit function:
- *
- * A function that returns the digit from a specified position in a provided integer.
- */
- int digit(int num, int k)
- {
- /**
- * Error check:
- */
- if(num < 0)
- {
- cout << "Error! Positive integers only!" << endl;
- exit(1);
- }
- /**
- * Convert the number to string:
- */
- std::string StringBuff = IntToString(num);
- /**
- * Error check:
- */
- if(k < 0 || k >= StringBuff.length())
- {
- cout << "Error! Position out of range!" << endl;
- exit(1);
- }
- /**
- * Reverse the string:
- */
- StringBuff = ReverseString(StringBuff);
- /**
- * Convert the string to an integer array:
- */
- int *IntArray = StringToIntArray(StringBuff);
- /**
- * Return the appropriate digit:
- */
- return IntArray[k];
- }
- /**
- * The IntToString() function:
- *
- * A function that converts a given integer value to a string, and returns the conversion.
- */
- std::string IntToString(int Number)
- {
- std::ostringstream converter; // Converts from number to string.
- /**
- * Perform the conversion:
- */
- converter << Number;
- /**
- * Return the result:
- */
- return converter.str();
- }
- /**
- * The StringToIntArray() function:
- *
- * A function that receives a string and returns and integer array representation of it.
- */
- int * StringToIntArray(std::string StringBuff)
- {
- int *IntArray = new int[StringBuff.length()];
- /**
- * Perform string conversion:
- */
- for(int i = 0; i < StringBuff.length(); i++)
- {
- switch(StringBuff.data()[i])
- {
- case '0':
- IntArray[i] = 0;
- break;
- case '1':
- IntArray[i] = 1;
- break;
- case '2':
- IntArray[i] = 2;
- break;
- case '3':
- IntArray[i] = 3;
- break;
- case '4':
- IntArray[i] = 4;
- break;
- case '5':
- IntArray[i] = 5;
- break;
- case '6':
- IntArray[i] = 6;
- break;
- case '7':
- IntArray[i] = 7;
- break;
- case '8':
- IntArray[i] = 8;
- break;
- case '9':
- IntArray[i] = 9;
- break;
- default:
- IntArray[i] = '\0';
- break;
- }
- }
- /**
- * Null-terminate the integer array:
- */
- IntArray[StringBuff.length()] = '\0';
- /**
- * Return the array:
- */
- return IntArray;
- }
- /**
- * The ReverseString() function:
- *
- * A function that returns the reversed version of a provided string.
- */
- std::string ReverseString(std::string NewString)
- {
- char *RevArray = new char[NewString.length()]; // The reversed version of the provided string.
- /**
- * Perform string reversal:
- */
- for(int i = 0, d = NewString.length() - 1; d >= 0; i++, d--)
- RevArray[i] = NewString.data()[d];
- /**
- * Null-terminate the array:
- */
- RevArray[NewString.length()] = '\0';
- /**
- * Return the result:
- */
- return std::string(RevArray);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement