xt3-12.cpp
上传用户:liubin
上传日期:2022-06-13
资源大小:85k
文件大小:1k
源码类别:

书籍源码

开发平台:

Visual C++

  1. #include <iostream>
  2. using namespace std;
  3. int main ()
  4. {long int num;
  5.   int indiv,ten,hundred,thousand,ten_thousand,place; 
  6.                                    /*分别代表个位,十位,百位,千位,万位和位数*/
  7.   cout<<"enter an integer(0~99999):";
  8.   cin>>num;
  9.   if (num>9999)
  10.        place=5;
  11.   else  if (num>999)
  12.        place=4;
  13.   else  if (num>99)
  14.        place=3;
  15.   else  if (num>9)
  16.        place=2;
  17.   else place=1;
  18.   cout<<"place="<<place<<endl;
  19.   //计算各位数字
  20.   ten_thousand=num/10000;
  21.   thousand=(int)(num-ten_thousand*10000)/1000;
  22.   hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;
  23.   ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
  24.   indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
  25.   cout<<"original order:";
  26.   switch(place)
  27.     {case 5:cout<<ten_thousand<<","<<thousand<<","<<hundred<<","<<ten<<","<<indiv<<endl;
  28.     cout<<"reverse order:";
  29.     cout<<indiv<<ten<<hundred<<thousand<<ten_thousand<<endl;
  30.     break;
  31.      case 4:cout<<thousand<<","<<hundred<<","<<ten<<","<<indiv<<endl;
  32.     cout<<"reverse order:";
  33.     cout<<indiv<<ten<<hundred<<thousand<<endl;
  34.     break;
  35.      case 3:cout<<hundred<<","<<ten<<","<<indiv<<endl;
  36.     cout<<"reverse order:";
  37.     cout<<indiv<<ten<<hundred<<endl;
  38.     break;
  39.      case 2:cout<<ten<<","<<indiv<<endl;
  40.     cout<<"reverse order:";
  41.     cout<<indiv<<ten<<endl;
  42.     break;
  43.      case 1:cout<<indiv<<endl;
  44.     cout<<"reverse order:";
  45.     cout<<indiv<<endl;
  46.     break;
  47.   }
  48.   return 0;
  49. }