输入一个十进制正整数,将其转换成二进制、八进制、十六进制数输出
1、形参采用指针
2、形参采用数组
#include "stdio.h"
#include "conio.h"
#include "string.h"
void main()
{
int i,radix;
long n;
char a[33];
void trans10_2_8_16(char *p,long m,int base);
printf("\nInput radix(2,8,16):");
scanf("%d",&radix);
printf("\nInput a positive integer:");
scanf("%ld",&n);
trans10_2_8_16(a,n,radix);
for(i=strlen(a)-1;i>=0;i--)
printf("%c",*(a+i));
puts("\n");
getch();
}
void trans10_2_8_16(char *p,long m,int base)
{
int r;
while(m>0)
{
r=m%base;
if(r<10) *p=r+48;
else *p=r+55;
m=m/base;
p++;
}
*p='\0';
}
#include "conio.h"
#include "string.h"
void main()
{
int i,radix;
long n;
char a[33];
void trans10_2_8_16(char *p,long m,int base);
printf("\nInput radix(2,8,16):");
scanf("%d",&radix);
printf("\nInput a positive integer:");
scanf("%ld",&n);
trans10_2_8_16(a,n,radix);
for(i=strlen(a)-1;i>=0;i--)
printf("%c",*(a+i));
puts("\n");
getch();
}
void trans10_2_8_16(char *p,long m,int base)
{
int r;
while(m>0)
{
r=m%base;
if(r<10) *p=r+48;
else *p=r+55;
m=m/base;
p++;
}
*p='\0';
}
2、形参采用数组
#include "stdio.h"
#include "conio.h"
#include "string.h"
void main()
{
int i,radix;
long n;
char a[33];
void trans10_2_8_16(char a[],long m,int base);
printf("\nInput radix(2,8,16):");
scanf("%d",&radix);
printf("\nInput a positive integer:");
scanf("%ld",&n);
trans10_2_8_16(a,n,radix);
for(i=strlen(a)-1;i>=0;i--)
printf("%c",*(a+i));
puts("\n");
getch();
}
void trans10_2_8_16(char a[],long m,int base)
{
int r;
while(m>0)
{
r=m%base;
if(r<10) *a=r+48;
else *a=r+55;
m=m/base;
a++;
}
*a='\0';
}
#include "conio.h"
#include "string.h"
void main()
{
int i,radix;
long n;
char a[33];
void trans10_2_8_16(char a[],long m,int base);
printf("\nInput radix(2,8,16):");
scanf("%d",&radix);
printf("\nInput a positive integer:");
scanf("%ld",&n);
trans10_2_8_16(a,n,radix);
for(i=strlen(a)-1;i>=0;i--)
printf("%c",*(a+i));
puts("\n");
getch();
}
void trans10_2_8_16(char a[],long m,int base)
{
int r;
while(m>0)
{
r=m%base;
if(r<10) *a=r+48;
else *a=r+55;
m=m/base;
a++;
}
*a='\0';
}