Advertisement
Nakumas

Egzamin PP&JP: 2014, Zad.1 A

Feb 11th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package zad1a2014;
  7.  
  8. import static java.lang.Math.pow;
  9.  
  10. /**
  11.  *
  12.  * @author Szymek
  13.  */
  14. public class Zad1A2014 {
  15.  
  16.     /**
  17.      * @param args the command line arguments
  18.      */
  19.     public static void main(String[] args)
  20.     {
  21.         String xd = "110111010101000011001100101011011"; //10111010 = 186
  22.         System.out.println(liczbaF(xd));
  23.     }
  24.     //0:znak bitowy; cecha: 1, 2, 3, 4, 5, 6, 7, 8; mantysa: 9,10,11...31
  25.     static float liczbaF(String lancuch)
  26.     {
  27.         //System.out.println("Podany łańcuch: " + lancuch);
  28.         int znakLiczby;
  29.         int cecha = 0;
  30.         float mantysa = 0;
  31.         float wartosc;
  32.         znakLiczby = (lancuch.charAt(0)=='1') ? -1 : 1;
  33.  
  34.        
  35.         int wykladnik = 7;
  36.         for (int i = 1; i < 9; i++)
  37.         {
  38.             cecha += Integer.parseInt(String.valueOf(lancuch.charAt(i))) * pow(2, wykladnik--);
  39.         }
  40.         //System.out.print("Cecha: " + cecha);
  41.         cecha -= 127;
  42.         //System.out.println(" Cecha-127: " + cecha);
  43.  
  44.        
  45.         wykladnik = -1;
  46.         for (int i = 9; i < 32; i++)
  47.         {
  48.             mantysa += Integer.parseInt(String.valueOf(lancuch.charAt(i))) * pow(2, wykladnik--);
  49.         }
  50.        
  51.         //System.out.print("Mantysa: " + mantysa);
  52.         mantysa += 1;
  53.         //System.out.println(" Mantysa +1: " + mantysa);
  54.        
  55.         wartosc = Float.parseFloat(String.valueOf(mantysa*pow(2, cecha)));
  56.        
  57.         return znakLiczby * wartosc;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement