C++学习练习___关于整型取出各个数字问题
记得以前学C的时候就有这个问题,从一个整数中取出各个数字到底怎样才可以通用。不过这个问题确实很困扰,今天发现一种方法,在表面上是可行的,但是性能怎么样不懂。
下面是代码:
1 //get each character of a number 2 #include <iostream> 3 using std::cin; 4 using std::cout; 5 using std::endl; 6 7 #include <cstdlib> 8 9 void getCharacter( int ); //definition of function getCharacter 10 11 int main() 12 { //Test the function 13 int x; 14 cout << "Enter an integer :"; 15 cin >> x; 16 getCharacter( x ); 17 18 system("pause"); 19 return 0; 20 }//end main 21 22 void getCharacter( int x) 23 { 24 int y = 0; 25 long bigTen = 10000000; //is there a mistake? is it big enough? 26 while( x > 0 ){ 27 y = x / bigTen; //get character 28 if( y != 0 ){ 29 cout << y << " "; //whether the quotient is true 30 x = x % bigTen; //change x to the arithmetical compliment 31 } 32 bigTen /= 10; 33 } 34 }
浙公网安备 33010602011771号