递归函数是先调用自己,再执行下一个语句吗 #号这一行一直在调用,那么*号这一行是以什么顺序执行的
问题描述:
#include <iostream>
using namespace std;
int main()
{
void convert(int n);
int number;
cout << "input an integer:";
cin >> number;
cout << "output:" << endl;
if (number < 0)
{
cout << "-";
number = -number;
}
convert(number);
cout << endl;
return 0;
}
void convert(int n)
{
int i;
char c;
if ((i = n / 10) != 0)
convert(i); //#######
c = n % 10 + '0';
cout << " " << c; //*********
}
答
#号这一行一直调用,直到你的i = 0为止,当你的i=0时,才不会产生调用了,然后就能看到输出。所以是最先看到i = 0时的输出,然后输出倒数第二个调用的i,然后输出倒数第三个调用的i,一直输出,直到你最先开始传入convert()函数的i经过i = n/10之后的值。