import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import javax.swing.JScrollPane;
class test {
public static void main(String[] args) throws NumberFormatException,
IOException{
Stack stk = new Stack();
System.out.print("請輸入一個正整數:");
BufferedReader strin=new BufferedReader(new InputStreamReader
(System.in));
int temp=Integer.parseInt(strin.readLine());
do{
//用堆棧 10 % 2 把餘數壓入堆棧後 再彈出
stk.push(temp%2);
temp=temp/2;
}while(temp!=0);
while(!stk.empty()){
System.out.print(stk.pop()+" ");
}
}
}
/*一些對棧的基本操作
* #include <stdio.h>
#define StackSize 100
typedef int ElemType;
typedef struct{
ElemType data[StackSize];
int top;
}SqStack;
/*初始化
void InitStack(SqStack *s)
{
s->top=-1;
}
*/
/*壓棧
int push(SqStack *s,ElemType e)
{
if(s->top<StackSize-1)
{
s->top=s->top+1;
s->data[s->top]=e;
return 1;
}
else
{
printf("overflow!/n");
return 0;
}
}
*/
/*出棧
ElemType pop(SqStack *s)
{
ElemType e;
if(-1==s->top)
{
printf("underflow/n");
return 1;
}
else
{
e=s->data[s->top];
(s->top)--;
return e;
}
}
*/
利用棧將輸入的十進制數及一些基本的棧操作
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.