C++ 返回模的解释
我需要一些关于以下代码的解释: 它用于将十进制数转换为二进制数代码: 这是一篇教程,但它让我困惑C++ 返回模的解释,c++,if-statement,modulo,C++,If Statement,Modulo,我需要一些关于以下代码的解释: 它用于将十进制数转换为二进制数代码: 这是一篇教程,但它让我困惑 void binary(int); void main(void) { int number; cout << "Please enter a positive integer: "; cin >> number; if (number < 0) cout << "That is not a positive integer.\n"; else
void binary(int);
void main(void) {
int number;
cout << "Please enter a positive integer: ";
cin >> number;
if (number < 0)
cout << "That is not a positive integer.\n";
else {
cout << number << " converted to binary is: ";
binary(number);
cout << endl;
//cin.get();
}
}
void binary(int number) {
int remainder;
if(number <= 1) {
cout << number;
return;
}
remainder = number%2;
binary(number >> 1);
cout << remainder;
//cin.get();
}
void二进制(int);
真空总管(真空){
整数;
数量;
如果(数字<0)
cout不会覆盖int,因为每当您递归调用函数binary()
时,它[int]会被重新分配为一个自动变量,因此如果您递归调用它n
次,您实际上会为余数分配n
不同的int
s
因此,它被“记住”,因为它们是不同的局部变量。分配通常在调用堆栈上进行
工作原理:让我们看看一个示例的堆栈:binary(5)
:
现在,使用number=5/2=2
|number=2, remainder = 0|
|number=5, remainder = 1|
-------------------------
再次使用number=2/2=1
现在,使用number=5/2=2
重新调用binary()
,并获得:
|number=1, remainder = 1|
|number=2, remainder = 0|
|number=5, remainder = 1|
-------------------------
现在,停止条件为真,因为number任何数字都可以写成sum(ai*2^i)
以及sum(bi*10^i)
。
我将解释小数,因为它更清楚。考虑到12345
要检索数字,您需要
12345 % 10 = 5 (op1)
12345 / 10 = 1234.5 = 1234 in a int (op2)
1234 % 10 = 4 (restart)
1234 / 10 = 123.4 = 123 in a int
等等
在这种情况下
op1 is equivalent to remainder = number%2; (modulo by the base)
op2 is equivalent to number >> 1; (division by the base. bitshifting is division by 2)
restart
意味着用除法的结果重新启动。这就是为什么我们有一个递归函数
调用,使用binary(number>>1);
假设我使用base 2中的abcdef
调用此函数
binary(abcdef)
binary(abcde)
binary(abcd)
binary(abc)
binary(ab)
binary(a)
cout << number;//a
cout << remainer;//b
cout << remainer;//c
cout << remainer;//d
cout << remainer;//e
cout << remainer;//f
二进制(abcdef)
二进制(abcde)
二进制(abcd)
二进制(abc)
二进制(ab)
二进制(a)
cout由于binary
是一个递归函数,因此会发生类似的情况(每个缩进都是一个额外的递归级别):
正如上面所示,递归只用于按与计算相反的顺序打印余数。不要与number
参数混淆——它在每次递归调用中是否被复制是无关紧要的。您也可以使用全局变量,它会起作用(然后,它会被复制)“就地修改”,即您所说的“覆盖”)
重要的是,对于每个递归调用,原始数字向右移动一位,这就是number
参数的全部内容
以下是无递归等价物:
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
string binary(int);
int main(void) {
int number;
cout << "Please enter a positive integer: ";
cin >> number;
if (number < 0)
cout << "That is not a positive integer.\n";
else {
cout << number << " converted to binary is: ";
cout << binary(number) << endl;
}
return 0;
}
string binary(int number) {
stringstream ss;
do {
ss << number % 2;
number >>= 1;
} while (number >= 1);
string s = ss.str();
reverse(s.begin(), s.end());
return s;
}
#包括
#包括
#包括
使用名称空间std;
字符串二进制(int);
内部主(空){
整数;
数量;
如果(数字<0)
这个问题实际上是关于递归的。一旦你理解了递归的概念,代码就会有意义。
op1 is equivalent to remainder = number%2; (modulo by the base)
op2 is equivalent to number >> 1; (division by the base. bitshifting is division by 2)
binary(abcdef)
binary(abcde)
binary(abcd)
binary(abc)
binary(ab)
binary(a)
cout << number;//a
cout << remainer;//b
cout << remainer;//c
cout << remainer;//d
cout << remainer;//e
cout << remainer;//f
binary call #0
calculate remainder0
recursive binary call #1
calculate remainder #1
recursive binary call #2
calculate remainder #2
recursive binary call #3
print number
print remainder #2
print remainder #1
print remainder #0
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
string binary(int);
int main(void) {
int number;
cout << "Please enter a positive integer: ";
cin >> number;
if (number < 0)
cout << "That is not a positive integer.\n";
else {
cout << number << " converted to binary is: ";
cout << binary(number) << endl;
}
return 0;
}
string binary(int number) {
stringstream ss;
do {
ss << number % 2;
number >>= 1;
} while (number >= 1);
string s = ss.str();
reverse(s.begin(), s.end());
return s;
}