运营商路由器:C++作业 进制转换

来源:百度文库 编辑:高考问答 时间:2024/05/03 04:16:16
//进制转换
#include<iostream.h>
int *jinzhi(int x,int y)
file://x是十进制,y是要转换去的进制.
{
int a[1000],i=0,*ptr;
while(x>0)
{
a[i]=x%y;
*(ptr+i)=a[i];
i++;
x=x/y;
}
return ptr;
}
void main()
{
int x,y;
cout<<"请输入你要转换的十进制整数;"<<endl;
cin>>x;
cout<<"请输入进制数"<<endl;
cin>>y;
cout<<"转换前的十进制是"<<x<<endl;
cout<<"转换成"<<y<<"进制后是:"<<jinzhi(x,y)<<endl;
}

请指出错误,那方面的错误,
当然还有正确答案.
今天晚上QQ:254173927
ao2ao2008@yahoo.com.cn
f:\娱乐\c++系列\作业\0409\23.cpp(45) : fatal error C1010: unexpected end of file while looking for precompiled header directive

错误有:1.ptr没有申请内存;2.转换进制后没有倒叙输出!3.转换后,如果目标进制比10大,就会看不明转换后的数。
下面是答案:
#include<iostream.h>
int *jinzhi(int x,int y)
{
int a[1000],i=0;
for(int j=0;j<1000;j++)
{
a[j] = 0;
}
while(x>0)
{
a[i]=x%y;
i++;
x=x/y;
}
return a;
}
void main()
{
int x,y;
cout<<"请输入你要转换的十进制整数;"<<endl;
cin>>x;
cout<<"请输入进制数"<<endl;
cin>>y;
if (y<=0) cout<<"ERROR!";
else{
cout<<"转换前的十进制是"<<x<<endl;
cout<<"转换成"<<y<<"进制后是:";
int *ptr = new int;
ptr = jinzhi(x,y);
int j=1000;
bool flag = true;
while(j>0)
{j--;
if ((*(ptr+j) == 0)&&flag)
{
continue;
}else{
flag = false;
cout<<*(ptr+j)<<',';
}
}cout<<"\b"<<endl;}
}