| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1586 人关注过本帖
标题:十进制转二进制的程序...(限制溢出...呵呵..)
只看楼主 加入收藏
peanut_acer
Rank: 1
等 级:新手上路
帖 子:16
专家分:7
注 册:2009-8-12
结帖率:100%
收藏
 问题点数:0 回复次数:9 
十进制转二进制的程序...(限制溢出...呵呵..)
#include <stdio.h>
#include <string.h>

void outint(int);
void outfloat(float ,int );


int main()
{
    float num;
    int i,count;
    float f;
    printf("Please enter a decimal count:");
    scanf("%f",&num);
    if(num>10240.0||num<-10240)
    {
        printf(" You must use a high-level calculator,I can't help you!!!!\n");
        exit(1);
    }
    i=num;
    f=num-i;
    if(f!=0)
    {
        printf("How many decimal do you want to retain:");
        scanf("%d",&count);
        printf("The count entered by you has been changed,look!!!-->:");
        if(i!=0)
            outint(i);
        else
            printf("0");
        outfloat(f,count);
    }
    else if(i!=0)
        outint(i);
    else
        printf("0");
    printf("B\n");
   

}


void outint(int i)
{
    int a[32],m=0,n=0,mod;
    int next=0;
    if(i>=0)
    {
        while(i/2>=1)
        {
            a[next]=i%2;
            //printf("%d",a[next]);
            ++next;
            i/=2;
        }
        a[next]=1;
        next++;
        a[next]=0;
        //printf("%d",a[next]);
        //printf("%d\n",a[0]);
        //printf("%d\n",next);
    }
    else
    {   
        i*=-1;
        while(i/2>=1)
        {
            a[next]=i%2;
            //printf("%d",a[next]);
            ++next;
            i/=2;
        }
        a[next]=1;
        next++;
        a[next]=1;
    }        
   
    for(m=0;m<=next-m;m++)
    {
        mod=a[m];
        a[m]=a[next-m];
        a[next-m]=mod;
    }
    for(m=0;m<=next;m++)
        printf("%d",a[m]);
}



void outfloat(float f,int count)
{
    int a[32],next=0,i;
    printf(".");
    while(f*2.0!=1.0)
    {
        if(f*2.0>1.0)
        {
            a[next]=1;
            next++;
            f=f*2.0-1.0;
        }
        else
        {
            a[next]=0;
            next++;
            f=f*2.0;
        }
    }
    for(i=0;i<=next&&i<=count-1;i++)
        printf("%d",a[i]);
}
搜索更多相关主题的帖子: 十进制 二进制 
2009-09-14 19:35
peanut_acer
Rank: 1
等 级:新手上路
帖 子:16
专家分:7
注 册:2009-8-12
收藏
得分:0 
添加的一些 //注释  是为了 方便调试.... 呵呵
2009-09-14 19:38
peanut_acer
Rank: 1
等 级:新手上路
帖 子:16
专家分:7
注 册:2009-8-12
收藏
得分:0 
发现个错误... 修改了下..


#include <stdio.h>
#include <string.h>
 
void outint(int);
void outfloat(float ,int );
 
 
int main()
{
    float num;  //需要转换的数
    int i,count; //i为用来存放num的整数部分  count是输出结果保留的小数位数
    float f;//f用来存放num的小数部分
    printf("Please enter a decimal count:");
    scanf("%f",&num);
    if(num>10240.0||num<-10240)  //懒得解决溢出问题 就加个限制
    {
        printf(" You must use a high-level calculator,I can't help you!!!!\n");
        exit(1);
    }
    i=num;
    f=num-i;
    printf("How many decimal do you want to retain:");
    scanf("%d",&count);
    printf("The count entered by you has been changed,look!!!-->   ");
    outint(i);
    outfloat(f,count);
    printf("B\n");
     
 
}
 
 
void outint(int i)
{
    int a[32],m=0,n=0,mod;
    int next=0;
    if(i==0)
    {
        printf("0");
        exit(0);
    }
     
    if(i>=0)
    {
        while(i/2>=1)
        {
            a[next]=i%2;
            ++next;
            i/=2;
        }
        a[next]=1;
        next++;
        a[next]=0;
    }
    else
    {     
        i*=-1;
        while(i/2>=1)
        {
            a[next]=i%2;
            ++next;
            i/=2;
        }
        a[next]=1;
        next++;
        a[next]=1;
    }         
     
    for(m=0;m<=next-m;m++)
    {
        mod=a[m];
        a[m]=a[next-m];
        a[next-m]=mod;
    }
    for(m=0;m<=next;m++)
        printf("%d",a[m]);
}
 
 
 
void outfloat(float f,int count)
{
    int a[32],next=0,i;
    printf(".");
     
    if(f==0.0)
    {
        printf("0");
        goto out;
    }
    if(f<0)
        f*=-1;
    while(f*2.0!=1.0)
    {
        if(f*2.0>1.0)
        {
            a[next]=1;
            next++;
            f=f*2.0-1.0;
        }
        else
        {
            a[next]=0;
            next++;
            f=f*2.0;
        }
    }
     
 
    for(i=0;i<next&&i<=count-1;i++)
        printf("%d",a[i]);
out:
    ;
}
2009-09-14 20:07
rs369007
Rank: 2
等 级:论坛游民
帖 子:30
专家分:11
注 册:2009-2-25
收藏
得分:0 
itoa函数不是可以转换吗?
2009-09-15 20:45
云雨谣
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-8-4
收藏
得分:0 
你还不如干脆用一个数组存放所有位。
2009-09-15 20:52
peanut_acer
Rank: 1
等 级:新手上路
帖 子:16
专家分:7
注 册:2009-8-12
收藏
得分:0 
只是写的玩玩 我知道有函数可以转换...  请各位放上自己写的源码...  
放上源码大家才能一起研究...  
小白嘴下留情
2009-09-17 18:26
shuijiashui8
Rank: 2
等 级:论坛游民
帖 子:21
专家分:44
注 册:2009-9-15
收藏
得分:0 
一个整数的
#include "stdafx.h"
void zhhuan(int data,int a[]);
int main(int argc, char* argv[])
{
    int a[32]={0},data;
     
    scanf("%d",&data);
    zhhuan(data,a);
    for(int i=0;i<32;i++)
        printf(" %d ",a[i]);
     
    return 0;
}
void zhhuan(int data,int a[])
{
    int wz=31;
    while(data!=0)
    {
        a[wz]=data%2;
        data=data/2;
        wz--;
    }
}
2009-09-17 19:49
暗留香
Rank: 2
等 级:论坛游民
帖 子:49
专家分:75
注 册:2009-9-4
收藏
得分:0 
二进制转10进制= =! 嘎嘎~咱两真配
#include "stdio.h"
main()
{
   FILE *tp_in,*tq_out;
   char a[2000];
   void zhuan_huan(char *,FILE *);
   tp_in=fopen("1.txt","r");
   tq_out=fopen("2.txt","w");
   while(!feof(tp_in))
      {
            fscanf(tp_in,"%s",a);
            zhuan_huan(a,tq_out);
      }
fclose(tp_in);
fclose(tq_out);
getch();
}
 
void zhuan_huan(char *a,FILE *tq)
{
        int sum=0,i=0;
        while(i<strlen(a))
        {
                sum*=2;
                sum+=*(a+i++)-'0';
        }
        printf("%d",sum);
    fprintf(tq,"%d",sum);     
}
2009-09-18 19:04
peanut_acer
Rank: 1
等 级:新手上路
帖 子:16
专家分:7
注 册:2009-8-12
收藏
得分:0 
整数的没有挑战性 要带小数的才带劲 哈哈....
两位的程序收下了  哈哈..
2009-09-23 23:59
pgy
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:C
等 级:小飞侠
威 望:8
帖 子:1248
专家分:2329
注 册:2009-9-23
收藏
得分:0 
各位大虾如果楼主不加注释且不加标题,一般能需要多久看出程序的作用啊?
我都要一句一句的看,是不是很慢?。我学了1个月了,感觉看程序看的好慢。

我可好玩啦...不信你玩玩^_^
2009-09-24 00:20
快速回复:十进制转二进制的程序...(限制溢出...呵呵..)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013931 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved