Advertisement
18126

Day2(ex12)

Jul 7th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int main() {
  6.     char arr[] = {'5', '3', '.', '2', '6', '3'};
  7.     int len = 6;
  8.     float value = 0;
  9.     int foundDot = 0;
  10.     int zeros_counter = 1;
  11.     for(int i = 0; i < len; i++) {
  12.         int temp = arr[i];
  13.         if (temp == 46) {
  14.             foundDot = 1;
  15.             continue;
  16.         }
  17.         if(foundDot == 1) {
  18.             int r = arr[i] - '0';
  19.             float temp2 = 0;
  20.             float temp3 = 0;
  21.             for(int j = 0; j < zeros_counter; j++) {
  22.                 if(j == 0) {
  23.                     temp2 = r*0.1;
  24.                     temp3 = temp2;
  25.                 } else {
  26.                     temp2 = temp3*0.1;
  27.                     temp3 = temp2;
  28.                 }
  29.             }
  30.             value = value + temp2;
  31.             zeros_counter++;
  32.         } else {
  33.             int r = arr[i] - '0';
  34.             value = value*10 + r;
  35.         }
  36.     }
  37.     printf("Converted! Results: %f", value);
  38.     return EXIT_SUCCESS;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement