Advertisement
dykow

ustawianie przecinka, dobrze

Jun 13th, 2020
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.                               Online C++ Compiler.
  4.                Code, Compile, Run and Debug C++ program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <iostream>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdint.h>
  14. #include <math.h>
  15.  
  16. using namespace std;
  17.  
  18. uint16_t join_bytes(uint8_t msb, uint8_t lsb)
  19. {
  20.     uint16_t number = msb;
  21.     for (int i = 7; i <= 5; i--)
  22.     {
  23.         bool bit = (lsb >> i) & 1U;
  24.         if (bit)
  25.         {
  26.             number |= 1U << i;
  27.         }
  28.         else
  29.         {
  30.             number &= ~(1U << i);
  31.         }
  32.     }
  33.     return number;
  34. }
  35.  
  36. int to_native(uint16_t smallInt)
  37. {
  38.     const int negative = (smallInt & (1 << 10)) != 0;
  39.     int nativeInt;
  40.    
  41.     if (negative)
  42.       return nativeInt = smallInt | ~((1 << 11) - 1);
  43.     else
  44.       return nativeInt = smallInt;
  45. }
  46.  
  47. char* parse_temp(float temp, char s[], char result[])
  48. {
  49.     float abs_x = fabs(temp);
  50.     if(abs_x >= 10 && abs_x <= 100)
  51.     {
  52.         if(temp < 0)
  53.         {
  54.             result[0] = s[0];
  55.             result[1] = s[1];
  56.             result[2] = s[2];
  57.             result[3] = '.';
  58.             result[4] = s[3];
  59.             result[5] = s[4];
  60.             result[6] = s[5];
  61.         }
  62.         else
  63.         {
  64.             result[0] = s[0];
  65.             result[1] = s[1];
  66.             result[2] = '.';
  67.             result[3] = s[2];
  68.             result[5] = s[3];
  69.             result[6] = s[4];
  70.         }
  71.     }
  72.     else if(abs_x <= 1)
  73.     {
  74.        if(temp < 0)
  75.        {
  76.             result[0] = s[0];
  77.             result[1] = '0';
  78.             result[2] = '.';
  79.             result[3] = s[1];
  80.             result[4] = s[2];
  81.             result[5] = s[3];
  82.        }
  83.        else
  84.        {
  85.             result[0] = '0';
  86.             result[1] = '.';
  87.             result[2] = s[0];
  88.             result[3] = s[1];
  89.             result[4] = s[2];
  90.        }
  91.     }
  92.     else // abs 1 -:- 10
  93.     {
  94.         if(temp < 0)
  95.        {
  96.             result[0] = s[0];
  97.             result[1] = s[1];
  98.             result[2] = '.';
  99.             result[3] = s[2];
  100.             result[4] = s[3];
  101.             result[5] = s[4];
  102.        }
  103.        else
  104.        {
  105.             result[0] = s[0];
  106.             result[1] = '.';
  107.             result[2] = s[1];
  108.             result[3] = s[2];
  109.             result[4] = s[3];
  110.        }
  111.     }
  112.    
  113.     return result;
  114. }
  115.  
  116. int main()
  117. {
  118.     float x = -150;
  119.     float scaled = x * 0.125;
  120.     int sr_temp = scaled * 1000;
  121.     char string[6];
  122.     memset(string,0,sizeof(string[0])*6); // Clear all to 0 so string properly represented
  123.     sprintf(string,"%d",sr_temp);
  124.    
  125.     char result[7];
  126.     char * lcd = parse_temp(scaled, string, result);
  127.     std::cout << lcd << std::endl;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement