Advertisement
JumpyLAZERY

decimal to binary using stacks

Oct 1st, 2022 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.Math;;
  3.  
  4. interface Stack<E> {
  5.     E top();
  6.  
  7.     E pop();
  8.  
  9.     void push(E element);
  10.  
  11.     boolean isEmpty();
  12. }
  13.  
  14. class ArrayStack<E> implements Stack<E> {
  15.     public E arr[];
  16.     private int t = -1;
  17.  
  18.     public int size() {
  19.         return t + 1;
  20.     }
  21.  
  22.     public ArrayStack(int capacity) {
  23.         arr = (E[]) new Object[capacity];
  24.     }
  25.  
  26.     public boolean isEmpty() {
  27.         if (size() == 0) {
  28.             return true;
  29.  
  30.         }
  31.         return false;
  32.     }
  33.  
  34.     public void push(E element) throws IllegalStateException {
  35.         if (size() == arr.length) {
  36.             throw new IllegalStateException("Stack is full");
  37.         }
  38.         // size++;
  39.         arr[++t] = element;
  40.     }
  41.  
  42.     public E pop() {
  43.         if (isEmpty()) {
  44.             return null;
  45.         }
  46.         E e = top();
  47.         --t;
  48.         return e;
  49.     }
  50.  
  51.     public E top() {
  52.         if (isEmpty()) {
  53.             return null;
  54.         }
  55.         return arr[size() - 1];
  56.     }
  57. }
  58. class dec_to_bin{
  59.     ArrayStack<Long> a1=new ArrayStack<Long>(32);
  60.    
  61.     public void pushing(double num){  
  62.         while(num>=0){
  63.         long x=(long)num % 2;
  64.         a1.push(x);
  65.         long y=(long)num/2;
  66.         num=y;
  67.         }
  68.     }
  69.    
  70.     public void process(double num){
  71.         double fracpart=num % 1;                //This 2 lines will separate the integer part and fractional part of the number given
  72.         long intpart=(long)num-(long)fracpart;     
  73.         if(fracpart==0){                            //Checking if the given number is a floating point
  74.            
  75.             pushing(num);                   //This method will push the 1/0 onto the stack
  76.             int len=a1.arr.length;
  77.             for(int i=0;i<len;i++){         //This for loop will print the stack i.e. the binary number will get printed
  78.                 long e=a1.pop();
  79.                 System.out.print(e);
  80.             }
  81.             System.out.println();
  82.         }
  83.         else{
  84.             pushing(intpart);       //Since the number contains fractional part so I have implemented stack only for the integer part
  85.             int a[]=new int[23];
  86.             int i=0;
  87.             while(i<23){
  88.                 fracpart=Math.round(fracpart*10000)/10000.0;
  89.                 double y=fracpart*2;
  90.                 int x=(int)y/10000;
  91.                 a[i++]=x;
  92.                 fracpart=y-x;
  93.                 if(fracpart==0)
  94.                 break;
  95.             }
  96.             int len=a1.arr.length;
  97.             for(int j=0;j<len;j++){         //Loop= to print the integer part of binary number
  98.                 long e=a1.pop();
  99.                 System.out.print(e);
  100.             }
  101.             System.out.print(".");
  102.             for(int k=0;k<a.length;k++){        //Loop= to print the fractional part of binary number
  103.                 System.out.print(a[k]);
  104.             }
  105.             System.out.println();
  106.         }
  107.     }
  108. }
  109. public class Main {
  110.     public static void main(String args[]){
  111.         Scanner sc=new Scanner(System.in);
  112.         dec_to_bin d1 =new dec_to_bin();
  113.         System.out.println("Enter decimal number");
  114.         double nu=sc.nextDouble();
  115.         d1.process(nu);
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement