输入一个不多于五位的正整数,判断它是几位数,分别打印出各位数字,并按逆序打印各位数字。
#include<iostream>
using namespace std;
int main()
{ void function(int a);
int input,flag;
cout<<"pls input the number! no more than 5 bits"<<endl;
cin>>input;
if(input>=0 && input<=9) flag=1; //根据输入数据大小,判定位数
if(input>=10 && input<=99) flag=2;
if(input>=100 && input<=999) flag=3;
if(input>=1000 && input<=9999) flag=4;
if(input>=10000 && input<=99999) flag=5;
cout<<"this number is "<<flag<<" bits"<<endl;
function(input); //调用函数function()
cout<<endl;
return 0; }
void function(int a)
{ int tmp[5]; //将输入数的各位分别存贮
tmp[4]=a/10000;
tmp[3]=(a%10000)/1000;
tmp[2]=(a%1000)/100;
tmp[1]=(a%100)/10;
tmp[0]=a%10;
int i,j,icount;
cout<<"the number of each bits is"<<endl;
for(i=4;i>=0;i--)
{ if(tmp[i]!=0) //如果数据不足5位,判断多余的几位,使其不输出
{ icount=i; //记录空位的个数
break; }
}
for(i;i>=0;i--) //按位输出
cout<<tmp[i]<<'/t';
cout<<endl<<"the reverse order of the number is "<<endl;
for(j=0;j<=icount;j++) //反向输出各位,到空位时停止输出
cout<<tmp[j];
}