满意答案
sseeeine34
2013.08.25
采纳率:44% 等级:12
已帮助:6721人
//程序的进制转换功能没有问题,你是想把输出的输出做得和和例子里的输出方式一样吧?
//你的程序是输入一个数和要转的进制,就立刻输出转换后的数字,和你的example不一样
//而且count次数的限定无效。
//程序里的好多“{”“}”都是不必要的,我帮你把代码的格式改了过来,你的实在是不“好看”诶
#include
int main()
{
int count,i,k,top;
int n;
long n_[1000][2];//存放输入的count个数字以及相应的进制,嫌大的话可以用malloc来申请一个count大小的二维数组,这样可以合理利用内存空间
char num[100];//存放相应数字转换后的数字,放在一个栈里
while(1)
{
scanf("%d",&count);//count在循环外面输入
if(count<1||count>1000)
printf("the number is too big or too small! please input again--->");
else break;
}
printf("n=%d\n",count);
for(i=0;i
scanf("%ld%ld",&n_[i][0],&n_[i][1]);//数据是否合规范我就不判断了(n_[i][0]放的是要转换的数)
printf("\n");
top=-1;//栈空标志为-1
for(i=count-1;i>=0;i--)
{
/*
scanf("%ld %d",&n,&k);
if(k<2||k>36||n<0||n>32767) //这个我就不用了
{return 0;}
*/
n=n_[i][0];k=n_[i][1];//取出数来转换
while(n)
{
top++;
if(n%k<10)
num[top]=n%k+'0';
else
num[top]=n%k-10+'A';//num[top++]中,top++很容易出错,不建议你这样用。有一个“先自加再使用”还是“先使用再自加”的问题存在。
n/=k;
}
num[++top]='\n';//转换完一个用'\n'表示结束
}
while(top!=-1)
{
printf("%c",num[top]);
top--;
}
return 0;
}
00分享举报