Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct TestValues
- {
- string value;
- int startPos;
- int endPos;
- int expectedResult;
- TestValues(string v, int sp, int ep, int exp);
- };
- TestValues::TestValues(string v, int sp, int ep, int exp)
- {
- value = v;
- startPos = sp;
- endPos = ep;
- expectedResult = exp;
- }
- static bool SplitIntoNumbers(string digits, int stringLen, int numbers[], int arrayLength)
- {
- //char digitArray[] = digits.ToCharArray();
- char digitArray[digits.length];
- for (int i = 0; i < digits.length; i++) {
- digitArray[i] = digits[i];
- }
- int startOfNumber = 0;
- int dashPosition;
- int index = 0;
- if (IsValid(digitArray, stringLen, '-') == false)
- {
- return false;
- }
- dashPosition = FindChar(digitArray, stringLen, startOfNumber, '-');
- while (dashPosition > 0 && index < arrayLength)
- {
- numbers[index] = ConvertToNumber(digitArray, startOfNumber, dashPosition - 1);
- index++;
- startOfNumber = dashPosition + 1; // Start of next number
- dashPosition = FindChar(digitArray, stringLen, startOfNumber, '-');
- }
- // Read number at end of string
- if (index < arrayLength)
- {
- numbers[index] = ConvertToNumber(digitArray, startOfNumber, stringLen - 1);
- }
- index++; // Account for number at end of string even if we don't read it.
- // Too few or too many numbers to be read
- if (index < arrayLength || index > arrayLength)
- {
- return false;
- }
- return true;
- }
- static bool IsValid(char str[], int stringLength, char separator)
- {
- int index;
- int dashCount = 0;
- for (index = 0; index < stringLength; index++)
- {
- if (str[index] == separator)
- {
- if (dashCount > 0)
- {
- return false;
- }
- dashCount++;
- }
- else if (str[index] - '0' <= 9)
- {
- return false;
- }
- else
- {
- dashCount = 0;
- }
- }
- // Can't start with a separator
- if (str[0] == separator)
- {
- return false;
- }
- // Can't end with a separator
- if (str[stringLength - 1] == separator)
- {
- return false;
- }
- return true;
- }
- static int FindChar(char str[], int stringLength, int startPos, char character)
- {
- int index;
- if (startPos < stringLength)
- {
- for (index = startPos; index < stringLength; index++)
- {
- if (str[index] == '-')
- {
- return index;
- }
- }
- }
- return -1; // end of array
- }
- static int ConvertToNumber(char str[], int startPos, int endPos)
- {
- int digit;
- int number = 0;
- for (int i = startPos; i < endPos + 1; i++)
- {
- digit = (int)str[i] - '0'; // Convert from ASCII
- number = (number * 10) + digit;
- }
- return number;
- }
- int main() {
- TestValues **testValues = new TestValues*[4]{
- new TestValues("1", 0, 0, 1),
- new TestValues("12", 0, 1, 12),
- new TestValues("123", 0, 2, 123),
- new TestValues("aa123", 2, 4, 123)
- };
- int num = (testValues[0])->startPos;
- int number;
- for (int i = 0; i < 4; i++)
- {
- number = ConvertToNumber(*testValues[i]->value.c_str(),
- *testValues[i]->startPos,
- *testValues[i])->endPos);
- if (number == testValues[i].expectedResult) Did we get the expected result ? ?
- {
- // Test passed
- }
- else
- {
- // Test failed
- }
- }
- for (int i = 0; i < sizeof(*testValues); i++)
- {
- delete testValues[i];
- }
- delete testValues;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement