#include#include#includeint top = -1;
void push(char *a,char element){
a[++top] = element;
}
void pop(char *a){
//空栈
if (top == -1)
{
return;
}
if (a[top]>=10)
{
printf("%c",a[top]+55);
}else{
printf("%d",a[top]);
}
top--;
}
//转十进制
int scaleFunction(char *data,int system){
int k = (int)strlen(data)-1;
int system_10_data = 0;
int i;
for (i = k;i >= 0;i--)
{
int temp;
if (data[i]>=48 && data[i]<=57)
{
temp = data[i] -48;
}else{
temp = data[i]-55;
}
system_10_data += temp * pow((double)system,k-i);
}
return system_10_data;
}
int main(){
char data[100];
printf("进制转换器,请输入原数据的进制类型(2-36)");
int system;
scanf("%d",&system);
getchar();
printf("请输入你要转换的数据:");
scanf("%s",data);
getchar();
int system_10_data = scaleFunction(data,system);
printf("请输入转换后的数据进制:");
int newSystem;
scanf("%d",&newSystem);
getchar();
while (system_10_data / newSystem)
{
push(data,system_10_data % newSystem);
system_10_data /= newSystem;
}
push(data,system_10_data % newSystem);
printf("转换后的结果为; \n");
while (top != -1)
{
pop(data);
}
return 0;
}
网友评论