标题:求助大神!!求一个进制转换的程序。各种进制之间的转化。
只看楼主
神龙赖了
Rank: 10Rank: 10Rank: 10
来 自:萨塔星
等 级:青峰侠
威 望:2
帖 子:711
专家分:1788
注 册:2012-10-13
得分:2 
程序代码:
#include <stdio.h>

int main(void)
{
    int a = 28;
    int b = a;
    int c[180];
    int i = 0;
    int j = 0;

    printf("十进制:\n%d\n\n",a);
    printf("字符类型:\n%c\n\n",a);
    printf("十六进制:\n%x\n\n",a);
    printf("八进制:\n%o\n\n",a);

    /*做二进制并输出*/
    do
    {
        i++;
        a /= 2;
    }while(a >0);
    j = i;

    do
    {
        c[--i] = b%2;
        b /= 2;
    }while(b>0);

    printf("二进制:\n");
    for(i=0;i<j;i++)
    printf("%d",c[i]);

    printf("\n\n");
    return 0;
}

不知是不是你需要的

I have not failed completely
2012-12-11 20:20
幕幕
Rank: 2
等 级:论坛游民
帖 子:19
专家分:10
注 册:2012-12-11
得分:0 
谢拉
2012-12-11 22:06
xuqingxiang
Rank: 1
等 级:新手上路
帖 子:11
专家分:2
注 册:2012-11-8
得分:0 
堆栈解题
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef char DataType;
#define STACK_SIZE 100

typedef struct{                       
     DataType *top;
     DataType *base;
     int stack_size;
}seqstack;

void init(seqstack *s)
{
     s->base=(DataType *)malloc(STACK_SIZE*sizeof(DataType));
     if(!s->base) exit(-1);
     s->top=s->base;
     s->stack_size=STACK_SIZE;
}

int IsEmpty(seqstack *s)
{
     return s->base==s->top;
}

int IsFull(seqstack *s)
{
     return s->top-s->base==STACK_SIZE-1;
}

void Push(seqstack *s,char ch)
{
     if(IsFull(s))
     {
         printf("overflow\n!");
         exit(1);
     }
     else
         *s->top++=ch;
}

char Pop(seqstack *s)
{
     if(IsEmpty(s))
     {
         printf("栈空\n");
         exit(1);
     }
     return *--s->top;
}

char Top(seqstack *s)
{
     if(IsEmpty(s))
     {
         printf("栈空\n");
         exit(1);
     }
     return *(s->top-1);
}

void conversion(int a,int b)
{
     int i;
     seqstack s;
     init(&s);
     while(a)
     {
         Push(&s,a%b);
         a=a/b;
     }
     while(!IsEmpty(&s))
     {
         i=Pop(&s);
         printf("%d",i);
     }
     free(s.base);
}

int main(int argc,char *argv[])
{
     int a,b;
     printf("please input the data you want to conversion\n");
     scanf("%d",&a);
     printf("please input the scale you want to convert to\n");
     scanf("%d",&b);
     conversion(a,b);
     return 0;
}
2012-12-11 22:54
岗地画面
Rank: 2
等 级:论坛游民
帖 子:20
专家分:26
注 册:2011-8-2
得分:0 
支持一下新人
#include<stdio.h>
#include<stdlib.h>
void Change(int number,int bit)
{

    if(number<bit)
    printf("%d",number);
    else
    {    int n=number%bit;
        Change(number/bit,bit);
            printf("%d",n);
    }
}
int main()
{
    int b,n;

    printf("Please input the number you want to change:");
    scanf("%d",&n);
    printf("Please input the bit you want to change:");
    scanf("%d",&b);
    printf("Changed number:");
    Change(n,b);
    putchar('\n');
    system("pause");
    return 0;
}
2012-12-11 23:22
幕幕
Rank: 2
等 级:论坛游民
帖 子:19
专家分:10
注 册:2012-12-11
得分:0 
回复 14楼 岗地画面
太棒了,,,谢谢
2012-12-13 23:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-390712-1-1.html




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

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