froster

博客园 首页 新随笔 联系 订阅 管理
题:输入一个5位的正整数,逐位获取各位的数字,并将之进行逆序输出。

注:这是C++写的程序,主体同学们应该能够看明白。其中,cin 和 cout 是用来进行数据的输入和输出的,相当于C语言中的scanf和printf。

// numberReverse.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
 int number;
 int data[5];
 int i = 0, j = 0;

 //发出提示信息,要求输入一个五位正整数,并将数据存入number变量中:
 cout<<"Please input a positive number under the five power of ten: ";
 cin>>number;
 //判断是否输入的数据合法,并进行不合法的数据的处理:
 if(number >= 100000)
 {
  number = number % 100000;
 }

 //对输入的数据进行取余操作,获得的数值存入数组中,并修改数据,去掉已经获取的数值
 while( number > 0 )
 {
  data[i++] = number % 10;
  number /= 10;
 }

 //将数组中的数值进行输出,注意里面是按从个位开始到高位的顺序存放的,此时i的值从数据的位数个数开始
 while( i-- > 0)
 {
  cout<<data[j++];
 }

 return 0;
}

posted on 2005-11-07 21:48  大牛  阅读(306)  评论(0编辑  收藏  举报