菜鸟求救!请耐心!!!

L_jila 2007-05-07 05:25:49
帮我简化这段程序,谢谢!
#include"iostream.h"
main()
{int i=0;
long a,b=0;
cin>>a;
b=a;
while(b>0)
{if(b<0||b>=100000)
break;
else
{b=b/10;i++;}
}
cout<<"该数为"<<i<<"位数"<<endl<<endl;
//显示几位数

if(i==1)
cout<<a;
else if(i==2)
cout<<a/10<<endl<<a-a/10*10;
else if(i==3)
cout<<a/100<<endl<<(a-a/100*100)/10<<endl<<a-a/10*10<<endl;
else if(i==4)
cout<<a/1000<<endl<<(a/100*100-a/1000*1000)/100<<endl<<(a/10*10-a/100*100)/10<<endl<<a-a/10*10;
else if(i==5)
cout<<a/10000<<endl<<(a/1000*1000-a/10000*10000)/1000<<endl<<(a/100*100-a/1000*1000)/100<<endl<<(a/10*10-a/100*100)/10<<endl<<a-a/10*10;
else
cout<<"error!";
cout<<endl<<endl;
//显示每一个数

if(i==1)
cout<<a;
else if(i==2)
cout<<a-a/10*10<<a/10;
else if(i==3)
cout<<a-a/10*10<<(a-a/100*100)/10<<a/100;
else if(i==4)
cout<<a-a/10*10<<(a/10*10-a/100*100)/10<<(a/100*100-a/1000*1000)/100<<a/1000;
else if(i==5)
cout<<a-a/10*10<<(a/10*10-a/100*100)/10<<(a/100*100-a/1000*1000)/100<<(a/1000*1000-a/10000*10000)/1000<<a/10000;
else
cout<<"error!";
//逆序显示这个数
}
...全文
226 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Allen_zhang 2007-05-07
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
long a,b=0;
cout<<"input the long size number:";
cin>>a;

char ch[20] = {0};

_ltoa( a, ch, 10 );
int len = strlen(ch);
cout<<"该数为"<<len<<"位数"<<endl<<endl;

for ( int i=0; i<len; i++ )
{
cout<<"第"<<i+1<<"位数是:"<<ch[i]<<endl;
}
int run = len;
cout<<"逆转的数为:";
while ( run>0 )
{
cout<<ch[run-1];
run--;
}
cout<<endl;

return 0;
}
  • 打赏
  • 举报
回复
手头没编译器,没测试
#include<iostream>
#include<sstream>
using namespace std;
int main()
{
int n;
cin>>n;
stringstream conv;
conv<<n;
string str;
conv>>str;
copy(str.begin(),str.end(),ostream_iterator<char>(cout,"\n"));正序
copy(str.rbegin(),str.rend(),ostream_iterator<char>(cout,"\n"));反序
}
believefym 2007-05-07
  • 打赏
  • 举报
回复
int m = 12345;
char c[20]={0};
itoa(m,c,10);
cout<<"共"<<strlen(c)<<"位"<<endl;
for (int i=0; i<strlen(c); ++i)
{
cout<<"第"<<i+1<<"位:"<<c[i]<<endl;
}
czlyc006 2007-05-07
  • 打赏
  • 举报
回复
可以使用switch+case替代那么多的if和else

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧