Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.42 KB | None | 0 0
  1.  
  2. #include <iostream> //do not include anything other than this
  3.  
  4. using namespace std;
  5.  
  6. const int ASCII_ZERO = 48;
  7. const int ASCII_NINE = 57;
  8.  
  9. void testCharacteristicAndMantissa();
  10. void shouldConvert(char number[], int expectedCharacteristic, int expectedNumerator, int expectedDenominator);
  11. void shouldNotConvert(char number[]);
  12.  
  13. //void testMath();
  14. //void testAdd();
  15. //void testSubtract();
  16. //void testMultiply();
  17. //void testDivide();
  18.  
  19. bool characteristic(char numString[], int&c);
  20. int TenToThePower(int exponent);
  21. int LastWholeNumIndex(char numString[], bool& decimal_found);
  22.  
  23. bool ValidateAndGetMantissaLength(char numString[], int& startOfMantissaPosition, int& length);
  24. bool mantissa(char numString[], int& numerator, int& denominator);
  25.  
  26. int main()
  27. {
  28.     //characteristic and mantissa test
  29.     testCharacteristicAndMantissa();
  30.    
  31.     //math function tests
  32.     //testMath();
  33.    
  34.     return 0;
  35. }
  36. //--
  37. void testCharacteristicAndMantissa()
  38. {
  39.     shouldConvert("123.456", 123, 456, 1000);
  40.     shouldConvert("    123.456", 123, 456, 1000);
  41.     shouldConvert("123.456    ", 123, 456, 1000);
  42.     shouldConvert("    123.456    ", 123, 456, 1000);
  43.    
  44.     shouldConvert("+123.456", 123, 456, 1000);
  45.     shouldConvert("   +123.456", 123, 456, 1000);
  46.     shouldConvert("+123.456   ", 123, 456, 1000);
  47.     shouldConvert("   +123.456   ", 123, 456, 1000);
  48.    
  49.     shouldConvert("-123.456", -123, 456, 1000);
  50.     shouldConvert("   -123.456", -123, 456, 1000);
  51.     shouldConvert("-123.456   ", -123, 456, 1000);
  52.     shouldConvert("    -123.456   ", -123, 456, 1000);
  53.    
  54.     shouldConvert("0.456", 0, 456, 1000);
  55.     shouldConvert("   0.456", 0, 456, 1000);
  56.     shouldConvert("0.456   ", 0, 456, 1000);
  57.     shouldConvert("   0.456   ", 0, 456, 1000);
  58.    
  59.     shouldConvert("-0.456", 0, -456, 1000);
  60.     shouldConvert("   -0.456", 0, -456, 1000);
  61.     shouldConvert("-0.456   ", 0, -456, 1000);
  62.     shouldConvert("   -0.456   ", 0, -456, 1000);
  63.    
  64.     shouldConvert(".456", 0, 456, 1000);
  65.     shouldConvert("    .456", 0, 456, 1000);
  66.     shouldConvert(".456   ", 0, 456, 1000);
  67.     shouldConvert("   .456   ", 0, 456, 1000);
  68.    
  69.     shouldConvert("-.456", 0, -456, 1000);
  70.     shouldConvert("    -.456", 0, -456, 1000);
  71.     shouldConvert("-.456   ", 0, -456, 1000);
  72.     shouldConvert("   -.456   ", 0, -456, 1000);
  73.    
  74.     shouldConvert("123456", 123456, 0, 10);
  75.     shouldConvert("   123456", 123456, 0, 10);
  76.     shouldConvert("123456   ", 123456, 0, 10);
  77.     shouldConvert("   123456   ", 123456, 0, 10);
  78.    
  79.     shouldConvert("-123456", -123456, 0, 10);
  80.     shouldConvert("   -123456", -123456, 0, 10);
  81.     shouldConvert("-123456   ", -123456, 0, 10);
  82.     shouldConvert("   -123456   ", -123456, 0, 10);
  83.    
  84.     shouldConvert("000123.456", 123, 456, 1000);
  85.     shouldConvert("123.45600000", 123, 456, 1000);
  86.     shouldConvert("00000123.45600000", 123, 456, 1000);
  87.    
  88.     shouldConvert("-000123.456", -123, 456, 1000);
  89.     shouldConvert("-123.45600000", -123, 456, 1000);
  90.     shouldConvert("-00000123.45600000", -123, 456, 1000);
  91.    
  92.     shouldConvert("123.00000456", 123, 456, 100000000);
  93.     shouldConvert("-123.00000456", -123, 456, 100000000);
  94. }
  95. //--
  96. void shouldConvert(char number[], int expectedCharacteristic, int expectedNumerator, int expectedDenominator)
  97. {
  98.     int c, n, d;
  99.    
  100.     //if the conversion from C string to integers can take place
  101.     if (characteristic(number, c) && mantissa(number, n, d))
  102.     {
  103.         if (c == expectedCharacteristic && n == expectedNumerator && d == expectedDenominator)
  104.         {
  105.             //test passes, do not print anything on a successful test
  106.         }
  107.         else
  108.         {
  109.             cout << "Test failed: '" << number << "' "
  110.             << "was parsed but did not produce the expected results" << endl;
  111.            
  112.             if (expectedCharacteristic != c)
  113.             {
  114.                 cout << "expected characteristic: " << expectedCharacteristic << " "
  115.                 << "actual characteristic: " << c << endl;
  116.             }
  117.            
  118.             if (expectedNumerator != n)
  119.             {
  120.                 cout << "expected numerator: " << expectedNumerator << " "
  121.                 << "actual numerator: " << n << endl;
  122.                
  123.             }
  124.            
  125.             if (expectedDenominator != d)
  126.             {
  127.                 cout << "expected denominator: " << expectedDenominator << " "
  128.                 << "actual denominator: " << d << endl;
  129.             }
  130.         }
  131.     }
  132.     else
  133.     {
  134.         cout << "Test failed: '" << number << "' "
  135.         << "was NOT parsed when it should have been." << endl;
  136.     }
  137. }
  138. //--
  139. void shouldNotConvert(char number[])
  140. {
  141.     int c, n, d;
  142.    
  143.     //if the conversion from C string to integers can take place
  144.     if (characteristic(number, c) && mantissa(number, n, d))
  145.     {
  146.         cout << "Test failed: '" << number << "' "
  147.         << "was parsed when it should NOT have been." << endl;
  148.     }
  149. }
  150. //--
  151. //void testMath()
  152. //{
  153. //    //add
  154. //    testAdd();
  155. //    testSubtract();
  156. //    testMultiply();
  157. //    testDivide();
  158. //}
  159. //--
  160. //void testAdd()
  161. //{
  162. //    const int SHORT_ARRAY_LENGTH = 5;
  163. //    char shortArray[SHORT_ARRAY_LENGTH];
  164. //
  165. //    const int MEDIUM_ARRAY_LENGTH = 10;
  166. //    char mediumArray[MEDIUM_ARRAY_LENGTH];
  167. //
  168. //    const int LARGE_ARRAY_LENGTH = 20;
  169. //    char largeArray[LARGE_ARRAY_LENGTH];
  170. //
  171. //    //should not be enough space in the array for the result
  172. //    if (add(INT_MAX, 0, 10, INT_MAX, 0, 10, shortArray, SHORT_ARRAY_LENGTH))
  173. //    {
  174. //        cout << "Error: not enough space in array" << endl;
  175. //    }
  176. //
  177. //    //0 + 0 = "0"
  178. //    add(0, 0, 10, 0, 0, 10, shortArray, SHORT_ARRAY_LENGTH);
  179. //    shouldConvert(shortArray, 0, 0, 10);
  180. //    add(0, 0, 10, 0, 0, 10, mediumArray, MEDIUM_ARRAY_LENGTH);
  181. //    shouldConvert(mediumArray, 0, 0, 10);
  182. //    add(0, 0, 10, 0, 0, 10, largeArray, LARGE_ARRAY_LENGTH);
  183. //    shouldConvert(largeArray, 0, 0, 10);
  184. //
  185. //    //1 + 1 = "2"
  186. //    add(1, 0, 10, 1, 0, 10, shortArray, SHORT_ARRAY_LENGTH);
  187. //    shouldConvert(shortArray, 2, 0, 10);
  188. //    add(1, 0, 10, 1, 0, 10, mediumArray, MEDIUM_ARRAY_LENGTH);
  189. //    shouldConvert(mediumArray, 2, 0, 10);
  190. //    add(1, 0, 10, 1, 0, 10, largeArray, LARGE_ARRAY_LENGTH);
  191. //    shouldConvert(largeArray, 2, 0, 10);
  192. //
  193. //    //1 + -1.5 = "-.5"
  194. //    add(1, 0, 10, -1, 1, 2, shortArray, SHORT_ARRAY_LENGTH);
  195. //    shouldConvert(shortArray, 0, -5, 10);
  196. //    add(1, 0, 10, -1, 1, 2, mediumArray, MEDIUM_ARRAY_LENGTH);
  197. //    shouldConvert(mediumArray, 0, -5, 10);
  198. //    add(1, 0, 10, -1, 1, 2, largeArray, LARGE_ARRAY_LENGTH);
  199. //    shouldConvert(largeArray, 0, -5, 10);
  200. //
  201. //    //1.125 + 1.6R = "2.79"
  202. //    add(1, 1, 8, 1, 2, 3, shortArray, SHORT_ARRAY_LENGTH);
  203. //    shouldConvert(shortArray, 2, 79, 100);
  204. //
  205. //    //1.125 + 1.6R = "2.7916666"
  206. //    add(1, 1, 8, 1, 2, 3, mediumArray, MEDIUM_ARRAY_LENGTH);
  207. //    shouldConvert(mediumArray, 2, 7916666, 10000000);
  208. //
  209. //    //1.125 + 1.6R = "2.79166666666666666"
  210. //    add(1, 1, 8, 1, 2, 3, largeArray, LARGE_ARRAY_LENGTH);
  211. //    //can't use the convert function because the num/denom would overflow
  212. //    char expectedResult[] = "2.79166666666666666";
  213. //    for (int i = 0; i < LARGE_ARRAY_LENGTH; i++)
  214. //    {
  215. //        if (expectedResult[i] != largeArray[i])
  216. //        {
  217. //            cout << "Error: mismatch in C strings in add()." << endl
  218. //            << "Expected: " << expectedResult << " "
  219. //            << "Actual: " << largeArray
  220. //            << endl;
  221. //        }
  222. //    }
  223. //}
  224. ////--
  225. //void testSubtract()
  226. //{
  227. //    const int SHORT_ARRAY_LENGTH = 5;
  228. //    char shortArray[SHORT_ARRAY_LENGTH];
  229. //
  230. //    const int MEDIUM_ARRAY_LENGTH = 10;
  231. //    char mediumArray[MEDIUM_ARRAY_LENGTH];
  232. //
  233. //    const int LARGE_ARRAY_LENGTH = 20;
  234. //    char largeArray[LARGE_ARRAY_LENGTH];
  235. //
  236. //    //should not be enough space in the array for the result
  237. //    if (subtract(INT_MIN, 0, 10, INT_MAX, 0, 10, shortArray, SHORT_ARRAY_LENGTH))
  238. //    {
  239. //        cout << "Error: not enough space in array" << endl;
  240. //    }
  241. //
  242. //    //0 - 0 = "0"
  243. //    subtract(0, 0, 10, 0, 0, 10, shortArray, SHORT_ARRAY_LENGTH);
  244. //    shouldConvert(shortArray, 0, 0, 10);
  245. //    subtract(0, 0, 10, 0, 0, 10, mediumArray, MEDIUM_ARRAY_LENGTH);
  246. //    shouldConvert(mediumArray, 0, 0, 10);
  247. //    subtract(0, 0, 10, 0, 0, 10, largeArray, LARGE_ARRAY_LENGTH);
  248. //    shouldConvert(largeArray, 0, 0, 10);
  249. //
  250. //    //2 - 1 = "1"
  251. //    subtract(2, 0, 10, 1, 0, 10, shortArray, SHORT_ARRAY_LENGTH);
  252. //    shouldConvert(shortArray, 1, 0, 10);
  253. //    subtract(2, 0, 10, 1, 0, 10, mediumArray, MEDIUM_ARRAY_LENGTH);
  254. //    shouldConvert(mediumArray, 1, 0, 10);
  255. //    subtract(2, 0, 10, 1, 0, 10, largeArray, LARGE_ARRAY_LENGTH);
  256. //    shouldConvert(largeArray, 1, 0, 10);
  257. //
  258. //    //1 - -1.5 = "2.5"
  259. //    subtract(1, 0, 10, -1, 1, 2, shortArray, SHORT_ARRAY_LENGTH);
  260. //    shouldConvert(shortArray, 2, 5, 10);
  261. //    subtract(1, 0, 10, -1, 1, 2, mediumArray, MEDIUM_ARRAY_LENGTH);
  262. //    shouldConvert(mediumArray, 2, 5, 10);
  263. //    subtract(1, 0, 10, -1, 1, 2, largeArray, LARGE_ARRAY_LENGTH);
  264. //    shouldConvert(largeArray, 2, 5, 10);
  265. //
  266. //    //1.125 - 1.6R = "-.54"
  267. //    subtract(1, 1, 8, 1, 2, 3, shortArray, SHORT_ARRAY_LENGTH);
  268. //    shouldConvert(shortArray, 0, -54, 100);
  269. //
  270. //    //1.125 - 1.6R = "-.5416666"
  271. //    subtract(1, 1, 8, 1, 2, 3, mediumArray, MEDIUM_ARRAY_LENGTH);
  272. //    shouldConvert(mediumArray, 0, -5416666, 10000000);
  273. //
  274. //    //1.125 - 1.6R = "-.54166666666666666"
  275. //    subtract(1, 1, 8, 1, 2, 3, largeArray, LARGE_ARRAY_LENGTH);
  276. //    //can't use the convert function because the num/denom would overflow
  277. //    char expectedResult[] = "-.54166666666666666";
  278. //    for (int i = 0; i < LARGE_ARRAY_LENGTH; i++)
  279. //    {
  280. //        if (expectedResult[i] != largeArray[i])
  281. //        {
  282. //            cout << "Error: mismatch in C strings in subtract()." << endl
  283. //            << "Expected: " << expectedResult << " "
  284. //            << "Actual: " << largeArray
  285. //            << endl;
  286. //        }
  287. //    }
  288. //}
  289. ////--
  290. //void testMultiply()
  291. //{
  292. //    const int SHORT_ARRAY_LENGTH = 5;
  293. //    char shortArray[SHORT_ARRAY_LENGTH];
  294. //
  295. //    const int MEDIUM_ARRAY_LENGTH = 10;
  296. //    char mediumArray[MEDIUM_ARRAY_LENGTH];
  297. //
  298. //    const int LARGE_ARRAY_LENGTH = 20;
  299. //    char largeArray[LARGE_ARRAY_LENGTH];
  300. //
  301. //    //should not be enough space in the array for the result
  302. //    if (multiply(INT_MAX, 0, 10, INT_MAX, 0, 10, shortArray, SHORT_ARRAY_LENGTH))
  303. //    {
  304. //        cout << "Error: not enough space in array" << endl;
  305. //    }
  306. //
  307. //    //0 * 0 = "0"
  308. //    multiply(0, 0, 10, 0, 0, 10, shortArray, SHORT_ARRAY_LENGTH);
  309. //    shouldConvert(shortArray, 0, 0, 10);
  310. //    multiply(0, 0, 10, 0, 0, 10, mediumArray, MEDIUM_ARRAY_LENGTH);
  311. //    shouldConvert(mediumArray, 0, 0, 10);
  312. //    multiply(0, 0, 10, 0, 0, 10, largeArray, LARGE_ARRAY_LENGTH);
  313. //    shouldConvert(largeArray, 0, 0, 10);
  314. //
  315. //    //3 * 2 = "6"
  316. //    multiply(3, 0, 10, 2, 0, 10, shortArray, SHORT_ARRAY_LENGTH);
  317. //    shouldConvert(shortArray, 6, 0, 10);
  318. //    multiply(3, 0, 10, 2, 0, 10, mediumArray, MEDIUM_ARRAY_LENGTH);
  319. //    shouldConvert(mediumArray, 6, 0, 10);
  320. //    multiply(3, 0, 10, 2, 0, 10, largeArray, LARGE_ARRAY_LENGTH);
  321. //    shouldConvert(largeArray, 6, 0, 10);
  322. //
  323. //    //3 * -1.5 = "-4.5"
  324. //    multiply(3, 0, 10, -1, 1, 2, shortArray, SHORT_ARRAY_LENGTH);
  325. //    shouldConvert(shortArray, -4, 5, 10);
  326. //    multiply(3, 0, 10, -1, 1, 2, mediumArray, MEDIUM_ARRAY_LENGTH);
  327. //    shouldConvert(mediumArray, -4, 5, 10);
  328. //    multiply(3, 0, 10, -1, 1, 2, largeArray, LARGE_ARRAY_LENGTH);
  329. //    shouldConvert(largeArray, -4, 5, 10);
  330. //
  331. //    //1.125 * 1.6R = "1.87"
  332. //    multiply(1, 1, 8, 1, 2, 3, shortArray, SHORT_ARRAY_LENGTH);
  333. //    shouldConvert(shortArray, 1, 87, 100);
  334. //
  335. //    //1.125 * 1.6R = "1.875"
  336. //    multiply(1, 1, 8, 1, 2, 3, mediumArray, MEDIUM_ARRAY_LENGTH);
  337. //    shouldConvert(mediumArray, 1, 875, 1000);
  338. //
  339. //    //1.125 * 1.6R = "1.875"
  340. //    multiply(1, 1, 8, 1, 2, 3, largeArray, LARGE_ARRAY_LENGTH);
  341. //    shouldConvert(largeArray, 1, 875, 1000);
  342. //}
  343. ////--
  344. //void testDivide()
  345. //{
  346. //    const int SHORT_ARRAY_LENGTH = 5;
  347. //    char shortArray[SHORT_ARRAY_LENGTH];
  348. //
  349. //    const int MEDIUM_ARRAY_LENGTH = 10;
  350. //    char mediumArray[MEDIUM_ARRAY_LENGTH];
  351. //
  352. //    const int LARGE_ARRAY_LENGTH = 20;
  353. //    char largeArray[LARGE_ARRAY_LENGTH];
  354. //
  355. //    //should not be enough space in the array for the result
  356. //    if (divide(INT_MAX, 0, 10, 1, 0, 10, shortArray, SHORT_ARRAY_LENGTH))
  357. //    {
  358. //        cout << "Error: not enough space in array" << endl;
  359. //    }
  360. //
  361. //    //cannot divide by zero
  362. //    if (divide(10, 0, 10, 0, 0, 10, shortArray, SHORT_ARRAY_LENGTH))
  363. //    {
  364. //        cout << "Error: cannot divide by zero" << endl;
  365. //    }
  366. //
  367. //    //0 / 1 = "0"
  368. //    divide(0, 0, 10, 1, 0, 10, shortArray, SHORT_ARRAY_LENGTH);
  369. //    shouldConvert(shortArray, 0, 0, 10);
  370. //    divide(0, 0, 10, 1, 0, 10, mediumArray, MEDIUM_ARRAY_LENGTH);
  371. //    shouldConvert(mediumArray, 0, 0, 10);
  372. //    divide(0, 0, 10, 1, 0, 10, largeArray, LARGE_ARRAY_LENGTH);
  373. //    shouldConvert(largeArray, 0, 0, 10);
  374. //
  375. //    //6 / 3 = "2"
  376. //    divide(6, 0, 10, 3, 0, 10, shortArray, SHORT_ARRAY_LENGTH);
  377. //    shouldConvert(shortArray, 2, 0, 10);
  378. //    divide(6, 0, 10, 3, 0, 10, mediumArray, MEDIUM_ARRAY_LENGTH);
  379. //    shouldConvert(mediumArray, 2, 0, 10);
  380. //    divide(6, 0, 10, 3, 0, 10, largeArray, LARGE_ARRAY_LENGTH);
  381. //    shouldConvert(largeArray, 2, 0, 10);
  382. //
  383. //    //1 / -1.5 = "-.66"
  384. //    divide(1, 0, 10, -1, 1, 2, shortArray, SHORT_ARRAY_LENGTH);
  385. //    shouldConvert(shortArray, 0, -66, 100);
  386. //
  387. //    //1 / -1.5 = "-.6666666"
  388. //    divide(1, 0, 10, -1, 1, 2, mediumArray, MEDIUM_ARRAY_LENGTH);
  389. //    shouldConvert(mediumArray, 0, -6666666, 10000000);
  390. //
  391. //    //1 / -1.5 = "-.66666666666666666"
  392. //    divide(1, 0, 10, -1, 1, 2, largeArray, LARGE_ARRAY_LENGTH);
  393. //    char expectedResult1[] = "-.66666666666666666";
  394. //    for (int i = 0; i < LARGE_ARRAY_LENGTH; i++)
  395. //    {
  396. //        if (expectedResult1[i] != largeArray[i])
  397. //        {
  398. //            cout << "Error: mismatch in C strings in divide()." << endl
  399. //            << "Expected: " << expectedResult1 << " "
  400. //            << "Actual: " << largeArray
  401. //            << endl;
  402. //        }
  403. //    }
  404. //
  405. //    //1.125 / 1.6R = "0.67"
  406. //    divide(1, 1, 8, 1, 2, 3, shortArray, SHORT_ARRAY_LENGTH);
  407. //    shouldConvert(shortArray, 0, 67, 100);
  408. //
  409. //    //1.125 / 1.6R = "0.675"
  410. //    divide(1, 1, 8, 1, 2, 3, mediumArray, MEDIUM_ARRAY_LENGTH);
  411. //    shouldConvert(mediumArray, 0, 675, 1000);
  412. //
  413. //    //1.125 / 1.6R = "0.675"
  414. //    divide(1, 1, 8, 1, 2, 3, largeArray, LARGE_ARRAY_LENGTH);
  415. //    shouldConvert(largeArray, 0, 675, 1000);
  416. //}
  417.  
  418. bool characteristic(char numString[], int& c)
  419. {
  420.     bool still_true = true;
  421.     int whole_num = 0;
  422.    
  423.     //used to make sure that if there are only mantissa values and a decimal place, the characteristic doesn't return false, but instead returns 0
  424.     bool decimal_found = false;
  425.    
  426.     int last_digit_pos = LastWholeNumIndex(numString, decimal_found);
  427.     bool positive_value = true;
  428.     bool before_digits = true;
  429.    
  430.     for (int i = 0; i <= last_digit_pos; i++)
  431.     {
  432.         if (isdigit(numString[i])) //If the character value is a digit, the integer will no longer accept spaces, +, or -, as well as other characters that are not digits as well
  433.         {
  434.             before_digits = false;
  435.             int place_value = TenToThePower(last_digit_pos - i); //Decimal place value for specific char array value
  436.            
  437.             //char array value being added to whole_num cannot exceed the maximum integer value - also checks if the place value exceeds the INT_MAX in TenToThePower, extra case checked for billions place since 1 & 2 billion can be used
  438.             if (place_value == -1 || (place_value == 1000000000 && (numString[i] - '0') >= 3) || ((numString[i] - '0') * place_value > INT_MAX - whole_num) )
  439.             {
  440.                 still_true = false;
  441.                 break;
  442.             }
  443.            
  444.             //numString[i] - '0' subtracts the ASCII value of 0 from the numString[i] digit to get the int value
  445.             whole_num += (numString[i] - '0') * place_value; //adds to the whole number and adds the character array value at its correct place value, ex: 1234 would add 1000, 200, 30, and then 4.
  446.         }
  447.         else if ((numString[i] == '+' || numString[i] == ' ') && before_digits) //accepts + or leading spaces before the integer begins
  448.         {
  449.             continue;
  450.         }
  451.         else if (numString[i] == '-' && before_digits) //accepts '-' before integer begins, set integer to negative after loop
  452.         {
  453.             positive_value = false;
  454.         }
  455.         else
  456.         {
  457.             still_true = false;
  458.             break;
  459.         }
  460.     }
  461.    
  462.     if (!decimal_found && before_digits) //checks if only spaces, +, -, or nothing was added to the char array
  463.         still_true = false;
  464.    
  465.     if (!positive_value && still_true) //Changes integer to a negative if there was a minus symbol, made outside of loop to avoid changing whole number value when creating the whole number
  466.         whole_num = whole_num *(-1);
  467.    
  468.     if(still_true)
  469.         c = whole_num;
  470.    
  471.     return still_true;
  472. }
  473.  
  474. //Finds the decimal place in the character array if there is one and returns the position right before the decimal. Used to only find the whole number integer values
  475. int LastWholeNumIndex(char numString[], bool& decimal_found)
  476. {
  477.     int pos = 0;
  478.    
  479.     for (pos; numString[pos] != '\0'; pos++)
  480.     {
  481.         if (numString[pos] == '.')
  482.         {
  483.             decimal_found = true;
  484.             break;
  485.         }
  486.     }
  487.    
  488.     return pos - 1;
  489. }
  490.  
  491. //Works similar to the pow function, but only returns 10 to the power of the parameter (used for place values)
  492. int TenToThePower(int exponent)
  493. {
  494.     if (exponent < 0)
  495.         return -1;
  496.    
  497.     int result = 1;
  498.     for (int i = 0; i < exponent; i++)
  499.     {
  500.         if (INT_MAX / 10 < result)
  501.             return -1;
  502.         result *= 10;
  503.     }
  504.    
  505.     return result;
  506. }
  507.  
  508. //This function will go through the numString and check to see if there are any
  509. //invalid chracters, it will store where the mantissa starts in the numString, and
  510. //it will store the length of the mantissa
  511. bool ValidateAndGetMantissaLength(char numString[], int& startOfMantissaPosition, int& mantissaLength)
  512. {
  513.     const int decimalVal = '.';
  514.    
  515.     bool retval = true;
  516.    
  517.     //We will use current element to keep track of where we are in the numString
  518.     int currentElement = 0;
  519.    
  520.     //When looking for the mantissa, we only want to store numbers that come after the decimal
  521.     //therefore we will ignore everyting until we find a decimal value
  522.     while(numString[currentElement] != decimalVal)
  523.     {
  524.         //If we get to \0 we know there is no matissa in numString so we will set retval to false
  525.         if(numString[currentElement] == '\0')
  526.         {
  527.             retval = false;
  528.             break;
  529.         }
  530.        
  531.         currentElement++;
  532.     }
  533.    
  534.     //Only enter this if statement if we did find a decimal
  535.     if(!numString[currentElement] == '\0')
  536.     {
  537.         //Need to add one to the index or we will start with the decimal
  538.         currentElement = currentElement + 1;
  539.         startOfMantissaPosition = currentElement;
  540.        
  541.         //Counter to see how many extra zeros numString may have
  542.         int numOfZeros = 0;
  543.        
  544.         //This while loop will go through the rest of numString and make sure
  545.         //all the elements are in fact numbers and it will keep track of extra zeros
  546.         while(numString[currentElement] != '\0')
  547.         {
  548.             //Checks to see if the element is 0-9 and if it's not set retval to false for invalid numString
  549.             if(numString[currentElement] < ASCII_ZERO || numString[currentElement] > ASCII_NINE)
  550.             {
  551.                 retval = false;
  552.                 break;
  553.             }
  554.             //If we find a zero we need to keep track of how many zeros we have found to make sure
  555.             //that if it's only zeros at the end we need to cut them off the mantissa
  556.             else if(numString[currentElement] == ASCII_ZERO)
  557.             {
  558.                 numOfZeros++;
  559.             }
  560.             //If we found a number 1-9 reset the zero count
  561.             else
  562.             {
  563.                 numOfZeros = 0;
  564.             }
  565.            
  566.             //continue to next element
  567.             currentElement++;
  568.         }
  569.        
  570.         //The overal mantissa length will be how long numString is minus the startOfMantissaPosition
  571.         //Also note we add numOfZeros due to the fact we do not want extra zeros to be part of our mantissaLength
  572.         mantissaLength = currentElement - (startOfMantissaPosition + numOfZeros);
  573.        
  574.         if (mantissaLength >= 13)
  575.         {
  576.             retval = false;
  577.         }
  578.     }
  579.    
  580.     return retval;
  581. }
  582.  
  583. bool mantissa(char numString[], int& numerator, int& denominator)
  584. {
  585.     bool retval = false;
  586.    
  587.     //This variable will be passed to numStringCheck to store the initial index of the mantissa
  588.     int startOfMantissaPosition = 0;
  589.    
  590.     //This variable will be passed to numStringCheck to store the length of the mantissa
  591.     int mantissaLength = 0;
  592.    
  593.     //ValidateAndGetMantissaLength returns whether or not we have a valid numString so we will only
  594.     //build the mantissa if it is valid
  595.     if(ValidateAndGetMantissaLength(numString, startOfMantissaPosition, mantissaLength))
  596.     {
  597.         retval = true;
  598.        
  599.         numerator = 0;
  600.        
  601.         //Denominator is base 10 so we can multiple 1 by 10 however many times to get the correct denominator
  602.         denominator = 1;
  603.        
  604.         //We will start from the back due to the fact if the mantissa was 321 we build 321 with ints by adding
  605.         //1 + 20 + 300 and we can do that by multiple the numerator by the current denominator
  606.         for(int i = startOfMantissaPosition + mantissaLength - 1; i >= startOfMantissaPosition; i--)
  607.         {
  608.             numerator += (numString[i] - ASCII_ZERO) * denominator;
  609.            
  610.             //Go up by a base of ten for every element
  611.             denominator = denominator * 10;
  612.         }
  613.        
  614.         //In case the mantissa is empty, we want the denominator to be 10
  615.         if(denominator == 1)
  616.         {
  617.             denominator = 10;
  618.         }
  619.     }
  620.    
  621.     return retval;
  622. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement