免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1282 | 回复: 0
打印 上一主题 下一主题

将十进制转换为R进制 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-18 18:07 |只看该作者 |倒序浏览
帮朋友做的一个作业,要求用栈实现,快两年没写过Java啦!

import java.util.*;
import java.io.*;
public class NToR {
    public static void main(String[] args) {
        
        int n=0;
        int m=0;
        try
        {
            InputStreamReader isr=new InputStreamReader(System.in);
            BufferedReader result=new BufferedReader(isr);
            System.out.print("请输入你要转化的十进制数:");
            String s1 = result.readLine();
            n=Integer.parseInt(s1);
            System.out.print("请问你需要把它转化为多少进制(2~32):");
            String s2 = result.readLine();
            m=Integer.parseInt(s2);
        }
        catch (Exception e) {
            System.out.println("读取数据失败!");
        }
        cover_to (n,m);
    }
   
    public static void cover_to(int num,int base){
        
        int t = num;
        int r = 0;
        int n = 0;
        Stack re=new Stack();
        if (base  2 || base > 32)
        {
            System.out.println("所输入的进制数非法!");
            return;
        }
               
        while((n = t/base) != t) {
         
                r = t%base;
                t = n;
               
                if(r > 9)
                    re.push((char)('A'+(r-10)));
                else
                    re.push((char)('0'+r));
        }
        System.out.print("转化结果为:");
        while(!re.isEmpty()){
            System.out .print(re.pop());
        }
    }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/66656/showart_1422399.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP