c语言编程 从键盘输入一个十进制正整数,将其转换成二至十六任意进制的数字字符串 c语言编程题:从键盘输入一个十进制数,将其转换为二进制、八进...

C\u8bed\u8a00\u7a0b\u5e8f\u5c06\u5341\u8fdb\u5236\u6570\u8f6c\u6362\u4e3a2\uff5e16\u4efb\u610f\u8fdb\u5236\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u8f93\u3002\u8bf7\u8f93\u5165\u7a0b\u5e8f\u5e76\u586b

\u7ed9\u4f60\u4e00\u4e2a\u901a\u7528\u7684\u51fd\u6570
int sort(int n)
{
if(n == 0)
return 0;
sort(n/2);
printf("%d",n%2);


}
\u8fd9\u4e2a\u662f\u5341\u8fdb\u5236\u8f6c\u62102\u8fdb\u5236\u7684 \u4f60\u628a2\u66ff\u6362\u6210\u4f60\u60f3\u8f6c\u7684\u6570\u5b57 \u6bd4\u59828\u6216\u800516\u7b49
\u4f46\u662f printf("%d",n%2);\u8fd9\u91cc\u5f97\u6539\u4e0b
8\u8fdb\u5236 printf("%o",n%2);
16\u8fdb\u5236 printf("%x",n%2);

/*\u8fd9\u9898\u975e\u5e38\u7b80\u5355\uff0c\u4f60\u8981\u5b66\u8fc7\u6570\u636e\u7ed3\u6784\u5c31\u975e\u5e38\u5bb9\u6613*/
/*\u6211\u7528\u4e2a\u6808\u6765\u5e2e\u4f60\u505a*/
/*\u6b64\u6808\u6570\u636e\u9879\u4e3a\u6574\u578b\uff0c\u53ea\u5b9a\u4e49\u521d\u59cb\u5316\u6808\uff0c\u5165\u6808\uff0c\u51fa\u6808\u64cd\u4f5c,\u8f93\u51fa\u6808\u4e2d\u5143\u7d20\u64cd\u4f5c*/

#include
#define MaxSize 100
typedef struct {
int data[MaxSize];
int top;
}Stack;//\u987a\u5e8f\u6808\uff0c\u4e5f\u53ef\u7528\u94fe\u6808

void InitStack(Stack &S)//\u521d\u59cb\u5316\u6808
{
S.top=-1;
}

int push(Stack &S,int x)//\u5165\u6808
{
if(S.top==MaxSize)
return 0; //\u6808\u6ee1
else{
S.top++;
S.data[S.top]=x;
return 1;
}
}

int pop(Stack &S,int &x)//\u51fa\u6808
{
if(S.top==-1)
return 0; //\u6808\u7a7a
else{
x=S.data[S.top];
S.top--;
return 1;
}
}

void display(Stack &S)
{
int x;
while(S.top!=-1)
{
pop(S,x);
cout<<x<<' ';
}
cout<<endl;
}


//\u5e94\u7528\u7a0b\u5e8f
void func(int n,int m)//n\u4e3a\u5341\u8fdb\u5236\u6570\uff0cm\u4e3a2\uff0c8\uff0c\u621616
{
Stack S;
InitStack(S);
while(n!=0)
{
push(S,n%m);
n=n/m;
}
display(S);
}

void main()//\u8fd9\u7528\u6765\u6d4b\u8bd5func
{
int n=12,m=2;
func(n,m);
m=8;
func(n,m);
m=16;
func(n,m);
}

//\u5df2\u5728vc2005\u4e2d\u6d4b\u8bd5\u8fc7\uff0c\u53ef\u7528\uff0c\u81f3\u4e8e16\u8fdb\u5236\u65f6\u600e\u4e48\u8f93\u51faA,B,..,F,\u5403\u8fc7\u996d\u6211\u518d\u6765\u8d34

#include<stdio.h>
void convert(unsigned int in, unsigned int type, char *out) {
int i=0, n;
char temp;
while (in > 0) {
n = in % type;
if(n<10){
out[i] = n + '0';
}
else {
out[i] = n-10 + 'A';
}
i++;
in = in / type;
}
out[i] = 0;
if (i == 0) {
out[0] = '0';
out[1] = 0;
}
else {
for (n = 0; n < i/2; n++) {
temp = out[n];
out[n]=out[i - n - 1];
out[i - n - 1] = temp;
}
}
}
int main()
{
unsigned int d;
char t[33];
scanf_s("%u", &d);
convert(d, 2, t);
printf("二进制:%s\n", t);
convert(d, 8, t);
printf("八进制:%s\n", t);
convert(d, 16, t);
printf("十六进制:%s\n", t);
return 0;
}

// asdf.cpp : Defines the entry point for the console application.
//
#include "stdio.h"
void _10ton(int x, char *des, int n) //将十进制数x转化为n进制, 结果存于des
{ int i=0, j=0;
char t;

if(n<10)
{ while( x>0 )
{ des[i] = x%n + '0';
i++;
x = x/n;
}
}
else
{ while( x>0 )
{ int tx=x%n;
if(tx<10)
des[i] = tx + '0';
else
des[i] = tx-10+'A';
i++;
x = x/n;
}
}

i--;
for(j=0; j<i; j++, i--)
{ t = des[i];
des[i] = des[j];
des[j] = t;
}
}
int main(int argc, char* argv[])
{
int x = 28; //十进制数
int n = 14; //转为16进制
char str[81]={'\0'};
_10ton(x, str, n);
printf("%s", str);
return 0;
}

扩展阅读:女生学计算机有多可怕 ... c语言编程题经典100例 ... 女生学计算机的弊端 ... c++入门程序代码 ... c语言在线编译器 ... c++编程适合几岁学 ... 编程必背100个代码 ... 为什么不建议女生学计算机 ... 初学编程100个代码自学 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网