c語言convert函數
㈠ c語言程序:將0—65535之間的整數轉換為16進制,要求用convert函數,急求!
#include "stdio.h"
#include "math.h"
#include "stdlib.h"
rzs(long int *w,long int j);
szr(long int *w,long int d);
main()
{
long int a,b,c,x,y;
char e[50];
while(1)
{
do
{
printf("輸入轉換的原進制數:");
scanf("%ld",&b);
fflush(stdin);
}while(b!=2&&b!=8&&b!=16&&b!=10);
do
{
printf("輸入轉換之後的進制數:");
scanf("%ld",&c);
fflush(stdin);
}while(c!=2&&c!=8&&c!=16&&c!=10);
if(b==10&&c==16)
{
printf("輸入要進行轉換的數字:");
scanf("%ld",&a);
printf("%x\n",a);
}
else if(b==16&&c==10)
{
printf("輸入要進行轉換的數字:");
scanf("%x",&a);
printf("%ld\n",a);
}
else if((b==2||b==8)&&c==10)
{
printf("輸入要進行轉換的數字:");
scanf("%ld",&a);
rzs(&a,b);
printf("%ld\n",a);
}
else if(b==10&&c==8)
{
printf("輸入要進行轉換的數字:");
scanf("%ld",&a);
szr(&a,c);
printf("%ld\n",a);
}
else if(b==2&&c==8||c==2&&b==8)
{
printf("輸入要進行轉換的數字:");
scanf("%ld",&a);
rzs(&a,b);
x=a;
szr(&x,c);
printf("%ld\n",x);
}
else if((b==2||b==8)&&c==16)
{
printf("輸入要進行轉換的數字:");
scanf("%ld",&a);
rzs(&a,b);
printf("%x\n",a);
}
else if(b==16&&(c==8||c==2))
{
printf("輸入要進行轉換的數字:");
scanf("%x",&a);
itoa(a,e,2);
printf("%s\n",e);
}
else if(b==10&&c==2)
{
printf("輸入要進行轉換的數字:");
scanf("%ld",&a);
itoa(a,e,2);
printf("%s\n",e);
}
}
}
rzs(long int *w,long int j)
{
long int k=0,b=0,a[32],f[32],i,c,d=10,e=1;
k=*w;
for(c=0;c<32;c++)
{
if(k/e%d==0)
{
a[c]=0;
e=e*10;
}
else
{
a[c]=k/e%d;
e=e*10;
}
}
for(i=0;i<32;i++)
{
f[i]=pow(j,i);
}
c=0,i=0;
while(c<32||i<32)
{
b=b+a[c++]*f[i++];
}
*w=b;
}
szr(long int *w,long int d)
{
long int a[1000],b=0,i,c=0,e=1,j=0;
b=*w;
for(i=0;;i++)
{
a[i]=b%d;
b=b/d;
c++;
if(b==0)
break;
}
i=c;
while(i>0)
{
a[i]=a[i]*pow(10,i);
e=e/10;
i--;
}
for(i=0;i<c;i++)
{
j+=a[i];
}
*w=j;
}
㈡ C語言 寫一個函數 輸入一個十六進制數 輸出相應的十進制
#包括< stdio, h >
h#包括< math.h >
# include < string。H >
Intmain()
{
Char[10]。
Void轉換(char[]a);
Printf(「輸入十六進制數:」);
(一);輸入一個十六進制數
轉換(a);
返回0;
}
Void轉換(char[]a)
{
Intn,I,num=0;
N=strlen(a);
對於(I = n - 1;I > = 0;我)
{
如果(a < I > > = '0' & a < I > < = '9')
Num + = (a < I > - '0') * pow (16, n - 1 - I);
否則(a < I > > = 'a' && a < I > < = 'Z')
Num + =(10 +(<我> - a)) *戰俘(16 n - 1 - I);
否則(a < I > > = 'a' && a < I > < = 'z')
Num + =(10 +(<我> - a)) *戰俘(16 n - 1 - I);
}
Printf(「將小數轉換為:」);
Printf("%d",num);
}
(2)c語言convert函數擴展閱讀:
函數的作用是:從標准輸入設備(鍵盤)中讀取一個字元串,直到回車結束。但是回車不屬於這個字元串。調用格式為(s);其中s是字元串變數(字元串數組名或字元串指針)。
gets(s)函數與scanf(「%s」,s)類似,但不完全相同。對字元串輸入使用scanf("%s",s)函數的問題是,如果輸入了空格,則認為輸入字元串是完整的。