添加二进制数:java [英] adding binary numbers:java

查看:82
本文介绍了添加二进制数:java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Node{
	
		int num;
		Node next;
	    
	    Node(int n)
	    {
	    	num=n;
	    	next = null;
	    }
	    
}   //end node

  
 class Q88
 {
 
 	
 	public static void main (String[] args)
 	{
 		
 		
 		Node head1 = toBinary(4);
		//print(head1);
		Node head2 = toBinary(2);
		//print(head2);
		
		
		double x = addBinaryNumbers(head1, head2);
		System.out.println(x);
 				
 	}//main
 	public static void print(Node top)
	    {
		while(top!=null)
		{
			System.out.printf("%d",top.num);
			top =  top.next;
		}
	    }//end print
  		public static Node toBinary(int n){
		Node np;
		Node top = null;
		Node last = null;
		
		Node xp= new Node (n);
		if (xp.num==0)return xp;
		while(n>0){
			int x = n%2;
			np = new Node(x);
			if(top==null)
				top = np;
			else 
				last.next = np;
				last = np;
				n = n/2;
		}
		return top;
		}//end function
	public static double toDecimal(Node top){
	int c = 0;
	double result = 0;
	int ans = 0;
	
	double y = Math.pow(2,2);
	
	while(top!=null)
	{
		
		result = result + (Math.pow(2,c)*top.num);	
		
		c++;
		top = top.next;
	}
	   
	return result;
}//toDecimal

	public static double addBinaryNumbers(Node top1, Node top2)
 	{
 		//declaration of variables
 		int sum = 0;
 		int carry = 0;
 		int result = 0;
 		double res = 0.0;
 		Node tail = null;
 		Node last = null;
 		Node ans  = null;
 		boolean ape = true ;
 	
 	 		while(top1!=null || top2!=null)
 		{
 			sum = top1.num + top2.num + carry;
 			System.out.println("sum" + sum);
 			carry = 0;//clear carry before next calculation
 			
 			if (sum==1 || sum == 0) 
 			{
 				
 					//System.out.println("ssssuur"+sum);
 				Node ncar = new Node (sum);
				
				if (tail==null)tail = ncar;
				else
				  last.next = ncar;
				  last = ncar;
		
				  
 			}else{
 				ans = toBinary(sum);
 				result = ans.num;
 				Node x = ans.next;
 				carry  = x.num;
 				
  				Node ncar = new Node (result);
				
				if (tail==null)tail = ncar;
				else
				  last.next = ncar;
				  last = ncar;
 				
 			}//end if;
 			
 				if ( top1.next !=null && top2.next==null) 
 			{
 				top1   = top1.next;
 				
 				System.out.println("ddddddddddddd" + top1.num);
 			}
 			
 			//get to end and both numbers are the same
 			
 			if (top1.next==null && top2.next==null && carry==1)
 			{
 				System.out.println("carrrrrrrrrrrrrr"+carry);
 				
 				Node car = new Node (carry);
 				
 				if (tail==null)tail = car;
 				else 
 					last.next = car;
 					last = car;
 				
 			}//end if
 			
 			if ( top2.next !=null && top1.next ==null) 
 		
 			{
 					top2 = top2.next;
 					System.out.println("vvvvvvvvvvvv" + top2.num);
 			}
 				
 			
 			if ( top1!=null && top2 != null)
 			{
 				System.out.println("brian");
 				top1 = top1.next;
 				top2 = top2.next;
 				
 			}
 					    
 			
 		}//while
 	/*	
 		while(tail!=null)
 		{
 			System.out.println("tail   "+tail.num);
 			tail = tail.next;
 		}
 	*/	
 		 res = toDecimal(tail); //have to send head/top
		
		return res;
 	}//addBinaryNumbers
 	
 }//class



我将十进制数字转换为二进制并将它们加在一起.例如,我将4和2相加.将值转换为二进制时,数字将反转,例如4 = 001和2 =01.如果我输入两个相同的数字(6,6),则程序可以运行给出正确的答案,但是如果我输入两个不同的数字(4、2),则会出现错误.



该程序可以解决这一点
top1 = 001
top2 = 01
0+ 0 = 0(正确)
0 + 1 = 1(正确)
除了最后1个



I am converting decimal numbers to binary and adding them together. For example, I am adding 4 and 2. When the values are converted to binary, the digits are reversed, for example, 4 =001 and 2= 01. The program works if I enter two numbers the same (6, 6)it gives the correct answer, but if I enter two different numbers (4, 2) I am getting an error.



The program works up this point
top1=001
top2 =01
0+ 0 = 0 (correct)
0+ 1= 1 (correct)
Except for the last 1

if ( top1.next !=null && top2.next==null) 
 	{
 		top1   = top1.next;
 	}


上面的结构检查,top1具有1,并且top2为空(没有更多值).我可以将top1.num(1)中的数字取回sum = top1.num + top2.num +进位,以便无法添加最后的1来完成答案.

返回的答案是2(不正确)

请帮忙!


The structure above checks , top1 has 1 and top2 is null (no more values). I can get the number in top1.num (1) back to sum = top1.num+ top2.num+ carry, so that I cannot add the final 1 to complete the answer.

Answer returned is 2(incorrect)

Please help!

推荐答案

如果可以将十进制数字转换"为二进制,则表示它们不是数字.所有数字都是二进制,你知道吗?
"4 = 001"表示您创建一个字符串"001",该字符串表示"="左侧数字的二进制文字(这不是赋值,只是示例的一些自由书写形式).输入数据可以是数字类型,但是如果不能是十进制"或十六进制"或与基数有关的任何值,则它也可以是表示数字的字符串.这不是很重要;您首先需要获取一个数字(数字类型,而不是字符串),然后从中构建一个字符串.

现在,这个问题真的可以一口气解决了.或两个.你写的东西毫无意义.您需要使用按位运算符.参见以下内容: http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3 .html [^ ].

我将把编码留给您做功课.如果您想至少了解一些内容,应该做一些作业,不是吗?

—SA
If you can "convert" decimal number to binary, it means they are not numbers. All numbers a binary, did you know that?
"4 = 001" means that you create a string "001" which represent a binary literal of the number on the left side of ''='' (this is not assignment, just some free written form of the example). The input data can be of numeric type, but then if cannot be "decimal" or "hexadecimal" or anything related to the base, but it could be also a string representing the number; it is not very important; you first need to get a number (of numeric type, not string) and then build a string out of it.

Now, the problem is really solved in one line. Or two. What you have written has little sense. You need to use bitwise operators. See this: http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html[^].

I will leave the coding for your homework. You are supposed to do some homework if you want to understand at least something, aren''t you?

—SA


这篇关于添加二进制数:java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆