| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 556 人关注过本帖
标题:关于The C++ Programming Language中的一个习题
只看楼主 加入收藏
haolihui9527
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-10-28
收藏
 问题点数:0 回复次数:0 
关于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
 为什么?

搜索更多相关主题的帖子: Programming The 习题 Language 
2006-10-28 11:10
快速回复:关于The C++ Programming Language中的一个习题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014657 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved