70,020
社区成员




# include<stdio.h>
# include<string.h>
void main()
{ long n;
int radix;
char b[33];
char *q;
char * trans(long m,int base); // 函数声明
printf("please input the integer:"); //要转换的十进制数
scanf("%ld",&n);
printf("please input the radix:"); // 进制数,可以为2、8、16
scanf("%d",radix);
q=trans(n,radix); //调用函数
for(int i=strlen(b)-1;i>=0;i--)
printf("%c",*(b+i));
}
char *trans(long m,int base)
{ int r;
char a[33];
char *p=a;
while(m>0)
{ r=m%base;
if(r<10) *p=r+48; //小于10的数转换为数字字符输出
else *p=r+55; //大于10的数转为A B C D E F输出
m=m/base;
p++; }
*p='\0';
p=a;
return p;
}
#include <stdio.h>
#include <stdlib.h>
void main() {
long n;
int radix;
char b[33];
while (1) {
printf("Input a integer(>=0):");//要转换的十进制数
fflush(stdout);
rewind(stdin);
if (1==scanf("%ld",&n)) {
if (n>=0) break;
}
}
while (1) {
printf("Input radix(2..36):");//进制,可以为2..36
fflush(stdout);
rewind(stdin);
if (1==scanf("%d",&radix)) {
if (2<=radix && radix<=36) break;
}
}
printf("%d(10)==%s(%d)\n",n,_itoa(n,b,radix),radix);
}
#include <iostream>
using namespace std;
#define ok 1
#define inter int
typedef struct node
{
int data;
struct node *next;
}lnode,*linknode; //结点的定义
inter emptystack(linknode top) //***************************** 栈空与否判断函数
{
if(top==NULL)
return 1;
else
return 0;
}
inter pushstack(linknode &top,int in) //****************** 栈的压入操作函数
{
linknode p;
p=(linknode)malloc(sizeof(lnode));
p->data=in;
p->next=top;
top=p; //栈顶指针往上移动
return in;
}
inter popstack(linknode &top)//3****************** 栈的弹出操作函数
{
linknode p;
int out;
out=top->data;
p=top;
top=top->next; //栈顶指针往下移动
free(p);
return out;
}
int main(void)
{
system("cls");
system("color 2b");
int src=0;
cout<<"请用户输入你要转换的数:"<<endl;
cin>>src;
int n=0;
cout<<"请用户输入你要转换的进制:"<<endl;
cin>>n;
linknode Lnode=NULL;
while (src!=0)
{
pushstack(Lnode,src%n);
src=src/n; //循环检查是否除完
}
cout<<"转换的结果是:"<<endl;
while (!emptystack(Lnode))
{
cout<<popstack(Lnode);
}
cout<<endl;
system("pause");
return 1;
}
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
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; //二进制、八进制(字符0的ASCII码是:48)
else
*p=r+55; //十六进制(字符A的ASCII码是:65)
m=m/base;
p++;
}
*p='\0';
}
int main(void)
{
int i,base;
long n;
char a[33];
printf("请输入一个需要转换的正整数:");
scanf("%ld",&n);
printf("请输入一个进制(2,8,16):");
scanf("%d",&base);
Trans10_2_8_16(a,n,base);
printf("转化为%d进制数为:",base);
for(i=strlen(a)-1;i>=0;i--)
printf("%c",*(a+i));
printf("\n");
system("pause");
return 0;
}