新华医疗销售怎么样:请C++高手来帮忙?急用:

来源:百度文库 编辑:神马品牌网 时间:2024/05/09 07:22:07
请帮我编个程序:
给出一个不多于5位的正整数,要求:(1)求出它是几位数(2)分别打印出每一位数字(3)按逆序打印个位数字,例如原数字123,应输出321。
用C++语言编写,请高手帮忙,先谢谢了。
可以帮忙写一下吗?急用多谢了。好吗?

#include <iostream.h>

void main()
{
int x,xx;
int x1,x2,x3,x4,x5;

cout<< "enter x"<<endl;
Lab1:;
cin >>x;
if (x > 99999) {
cout << "xis too big, please re-enter x"<<endl;
goto Lab1;
}
xx = x;
if (x > 9999) {
x1 = x % 10;
x = (x - x1) / 10;
x2 = x % 10;
x = (x - x2) / 10;
x3 = x % 10;
x = (x - x3) / 10;
x4 = x % 10;
x = (x - x4) / 10;
x5 = x % 10;
cout<< "5 digits" << xx <<endl;
cout<< x1 << x2 << x3 << x4 << x5 <<endl;
} else if (x >999){
x1 = x % 10;
x = (x - x1) / 10;
x2 = x % 10;
x = (x - x2) / 10;
x3 = x % 10;
x = (x - x3) / 10;
x4 = x % 10;
cout<< "4 digits" << xx <<endl;
cout<< x1 << x2 << x3 << x4 <<endl;
} else if (x > 99) {
x1 = x % 10;
x = (x - x1) / 10;
x2 = x % 10;
x = (x - x2) / 10;
x3 = x % 10;
cout<< "3 digits" << xx <<endl;
cout<< x1 << x2 << x3 <<endl;
} else if (x > 9) {
x1 = x % 10;
x = (x - x1) / 10;
x2 = x % 10;
cout<< "2 digits" << xx <<endl;
cout<< x1 << x2 <<endl;
} else {
cout<< "1 digit" << xx <<endl;
cout << x << endl;
}
}

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
int x;
int n_digits=0;
cout<<"input a number"<<endl;
cin>>x;
ostringstream ss;
ss<<x;
string str(ss.str());
n_digits=str.size();
cout<<"This number contains "<<n_digits<<" digits"<<endl;

cout<<"Now printing each digit of this number:"<<endl;
for(int i=0;i<str.size();i++)
{
cout<<str[i]<<endl;
}

cout<<"Now printing each digit of this number reversely"<<endl;
for(int i=str.size()-1;i>=0;i--)
{
cout<<str[i]<<endl;
}

}
//今天心情不错,最讨厌的就是直接post作业

把整数转化成字符串,然后用字符串的操作就可以实现的。

楼上那个不错,不过如果修改为for循环做会更简洁,而且复用更好