Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4.  
  5. using namespace std;
  6.  
  7.  
  8. struct TestValues
  9. {
  10. string value;
  11. int startPos;
  12. int endPos;
  13. int expectedResult;
  14. TestValues(string v, int sp, int ep, int exp);
  15. };
  16.  
  17. TestValues::TestValues(string v, int sp, int ep, int exp)
  18. {
  19. value = v;
  20. startPos = sp;
  21. endPos = ep;
  22. expectedResult = exp;
  23. }
  24.  
  25.  
  26.  
  27.  
  28.  
  29. static bool SplitIntoNumbers(string digits, int stringLen, int numbers[], int arrayLength)
  30. {
  31. //char digitArray[] = digits.ToCharArray();
  32. char digitArray[digits.length];
  33. for (int i = 0; i < digits.length; i++) {
  34. digitArray[i] = digits[i];
  35. }
  36. int startOfNumber = 0;
  37. int dashPosition;
  38. int index = 0;
  39.  
  40. if (IsValid(digitArray, stringLen, '-') == false)
  41. {
  42. return false;
  43. }
  44.  
  45. dashPosition = FindChar(digitArray, stringLen, startOfNumber, '-');
  46. while (dashPosition > 0 && index < arrayLength)
  47. {
  48. numbers[index] = ConvertToNumber(digitArray, startOfNumber, dashPosition - 1);
  49. index++;
  50.  
  51. startOfNumber = dashPosition + 1; // Start of next number
  52. dashPosition = FindChar(digitArray, stringLen, startOfNumber, '-');
  53. }
  54.  
  55. // Read number at end of string
  56. if (index < arrayLength)
  57. {
  58. numbers[index] = ConvertToNumber(digitArray, startOfNumber, stringLen - 1);
  59. }
  60. index++; // Account for number at end of string even if we don't read it.
  61.  
  62. // Too few or too many numbers to be read
  63. if (index < arrayLength || index > arrayLength)
  64. {
  65. return false;
  66. }
  67.  
  68. return true;
  69. }
  70.  
  71. static bool IsValid(char str[], int stringLength, char separator)
  72. {
  73. int index;
  74. int dashCount = 0;
  75.  
  76. for (index = 0; index < stringLength; index++)
  77. {
  78. if (str[index] == separator)
  79. {
  80. if (dashCount > 0)
  81. {
  82. return false;
  83. }
  84. dashCount++;
  85. }
  86.  
  87.  
  88. else if (str[index] - '0' <= 9)
  89. {
  90. return false;
  91. }
  92. else
  93. {
  94. dashCount = 0;
  95. }
  96. }
  97.  
  98. // Can't start with a separator
  99. if (str[0] == separator)
  100. {
  101. return false;
  102. }
  103.  
  104. // Can't end with a separator
  105. if (str[stringLength - 1] == separator)
  106. {
  107. return false;
  108. }
  109.  
  110. return true;
  111. }
  112.  
  113. static int FindChar(char str[], int stringLength, int startPos, char character)
  114. {
  115. int index;
  116.  
  117. if (startPos < stringLength)
  118. {
  119. for (index = startPos; index < stringLength; index++)
  120. {
  121. if (str[index] == '-')
  122. {
  123. return index;
  124. }
  125. }
  126. }
  127.  
  128. return -1; // end of array
  129. }
  130.  
  131. static int ConvertToNumber(char str[], int startPos, int endPos)
  132. {
  133. int digit;
  134. int number = 0;
  135.  
  136. for (int i = startPos; i < endPos + 1; i++)
  137. {
  138. digit = (int)str[i] - '0'; // Convert from ASCII
  139. number = (number * 10) + digit;
  140. }
  141.  
  142. return number;
  143. }
  144.  
  145.  
  146.  
  147. int main() {
  148.  
  149. TestValues **testValues = new TestValues*[4]{
  150. new TestValues("1", 0, 0, 1),
  151. new TestValues("12", 0, 1, 12),
  152. new TestValues("123", 0, 2, 123),
  153. new TestValues("aa123", 2, 4, 123)
  154.  
  155.  
  156.  
  157. };
  158. int num = (testValues[0])->startPos;
  159. int number;
  160. for (int i = 0; i < 4; i++)
  161. {
  162. number = ConvertToNumber(*testValues[i]->value.c_str(),
  163. *testValues[i]->startPos,
  164. *testValues[i])->endPos);
  165.  
  166. if (number == testValues[i].expectedResult)  Did we get the expected result ? ?
  167. {
  168. // Test passed
  169. }
  170. else
  171. {
  172. // Test failed
  173. }
  174.  
  175. }
  176.  
  177. for (int i = 0; i < sizeof(*testValues); i++)
  178. {
  179. delete testValues[i];
  180. }
  181. delete testValues;
  182.  
  183. return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement