Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define MAX_LINE_LENGTH 100
  5.  
  6. enum positionState {
  7. POSITION_STATE_WHOLE,
  8. POSITION_STATE_DECIMAL,
  9. POSITION_STATE_NOTATION
  10. };
  11.  
  12. int mygetline(char line[], int max) {
  13. int c, i;
  14.  
  15. for (i = 0; i < max && (c = getchar()) != EOF && c != '\n'; i++) {
  16. line[i] = c;
  17. }
  18.  
  19. return i;
  20. }
  21.  
  22. int isDigit(char input) {
  23. return (input >= '0' && input <= '9') ? 1 : 0;
  24. }
  25.  
  26. long power(int base, int exponent) {
  27. long result = (exponent > 0) ? base : 0;
  28.  
  29. for (int i = 0; i < exponent - 1; i++) {
  30. result *= base;
  31. }
  32.  
  33. return result;
  34. }
  35.  
  36. float atof(char input[]) {
  37. int sign = 1;
  38. int whole = 0;
  39. float decimal = 0;
  40. int decimalCount = 0;
  41. int notationExponents = 0;
  42. int notationSign = 1;
  43.  
  44. int i = 0;
  45. while ((input[i] < '0' || input[i] > '9') && input[i] != '+' && input[i] != '-') {
  46. i++;
  47. }
  48.  
  49. if (input[i] == '+' || input[i] == '-') {
  50. sign = (input[i++] == '-') ? -1 : sign;
  51. }
  52.  
  53. enum positionState position = POSITION_STATE_WHOLE;
  54.  
  55. while (input[i] != '\n' && input[i] != ' ' && input[i] != '\t' && input[i] != '\0') {
  56. // Return -1 for invalid characters.
  57. if ((input[i] < '0' || input[i] > '9') && input[i] != 'e' && input[i] != 'E' && input[i] != '-' && input[i] != '+' && input[i] != '.') {
  58. return -1;
  59. }
  60.  
  61. // Handle whole numbers
  62. if (position == POSITION_STATE_WHOLE) {
  63. if (isDigit(input[i])) {
  64. whole = whole * 10 + (input[i] - '0');
  65. }
  66. }
  67.  
  68. // Handle decimals
  69. if (position == POSITION_STATE_DECIMAL) {
  70. if (isDigit(input[i])) {
  71. decimalCount++;
  72. decimal = decimal * 10 + (input[i] - '0');
  73. }
  74. }
  75. position = (input[i] == '.') ? POSITION_STATE_DECIMAL : position;
  76.  
  77. // Handle notation
  78. if (position == POSITION_STATE_NOTATION) {
  79. if (input[i] == '-') {
  80. notationSign = -1;
  81. i++;
  82. }
  83.  
  84. if (isDigit(input[i])) {
  85. notationExponents = notationExponents * 10 + (input[i] - '0');
  86. }
  87. }
  88. position = (input[i] == 'e' || input[i] == 'E') ? POSITION_STATE_NOTATION : position;
  89.  
  90. // Next char in input
  91. i++;
  92. }
  93.  
  94. // Calculate result
  95. double result = whole;
  96.  
  97. if (decimalCount > 0) {
  98. result += decimal / power(10, decimalCount);
  99. }
  100.  
  101. if (notationExponents > 0) {
  102. if (notationSign > 0) {
  103. result *= power(10, notationExponents);
  104. } else {
  105. result /= power(10, notationExponents);
  106. }
  107. }
  108.  
  109. return sign * result;
  110. };
  111.  
  112. int main() {
  113. int c;
  114. char line[MAX_LINE_LENGTH];
  115.  
  116. while (mygetline(line, MAX_LINE_LENGTH)) {
  117. printf("%f", atof(line));
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement