运用栈设计一个简单的二进制计算器

import java.util.Scanner;
import java.util.Stack;

public class erJinZhiJiSuanQi {
public static void main(String[] args) {
	int sum = 0;
	Stack stack = new Stack();
	Scanner console = new Scanner(System.in);
		int num = console.nextInt();
		while(num != 0){
			int x = num % 10;
			num  /= 10;
			stack.push(x);
		}
		while(stack.size() > 0){
			int y = (int) stack.pop();
			sum +=y * Math.pow(2, stack.size());
		}
		System.out.println(sum);
	}
}

 

你可能感兴趣的:(数据结构)