新微赢技术网

标题: 关于The C++ Programming Language中的一个习题 [打印本页]

作者: !今生无缘!    时间: 2009-11-4 00:47
标题: 关于The C++ Programming Language中的一个习题
这是The C++ Programming Language中的一道习题,就是把一个整数按照给定进制输出.
#include <assert.h>
#include <limits>
#include <stdexcept>
#include <string>
#include <iostream>
using namespace std;
namespace { char const digit[] = "0123456789ABCDEF"; }
void print(int value, int base = 10) {
int const MXS = 128; // Maximum number of digits
assert((numeric_limits<int>::radix==2)
& (numeric_limits<int>::digits<=MXS));
if ((base<2) | (base>16))
throw std::domain_error(std::string("Print error"));
char rep[MXS+2];
rep[MXS+1] = '\0'; // Place an end-of-string marker
if (value<0) { // Add in the sign if needed
rep[0] = '-'; // This may have to move later
value = -value;
} else
rep[0] = '+';
char *p = rep+MXS; // Start at the end
do { // Move back, inserting digits as you go
*p-- = digit[value%base];
value = value/base;
} while (value);
*p = rep[0]; // Place the sign just before the most
// significant digit.
std::cout << p;
}

int main() {
for (int base = 10; base<11; ++base) {
std::cout << "1000 in base " << base << ": ";
print(1000, base);
std::cout << '\n';
}
return 0;
}
我有两个问题:
1. char *p = rep+MXS; 这有什么作用.
2.这是分步执行的结果:
  1000 in base 10: ?烫1烫0烫0烫0
 这是直接运行的结果:
    1000 in base 10: +1000
    Press any key to continue
 为什么?




欢迎光临 新微赢技术网 (http://bbs.weiying.cn/) Powered by Discuz! X3.2