文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>第6节 栈的应用举例——数值转换

第6节 栈的应用举例——数值转换

时间:2010-08-31  来源:chinazhangjie

 1 /* 主题: 数制转换(使用栈)
2 * 作者: chinazhangjie(邮箱:[email protected])
3 * 开发语言: C++
4 * 开发环境: Microsoft Visual Studio 2008
5 * 日期: 2010.08.31
6 */     
7 #include "stack.h" // for stack<T>
8 class num_conversion {
9 typedef unsigned int num_type;
10 public:
11 // 构造函数
12 explicit num_conversion(num_type n)
13 :num(n) {
14 __conversion();
15 }
16
17 // 拷贝构造函数
18 num_conversion(const num_conversion& rhs)
19 :num(rhs.num),num_stack(rhs.num_stack) {
20 }
21
22 // 返回要转换的数据
23 num_type get_num() const {
24 return num;
25 }
26
27 // 重新设置(要转换的)数据
28 void reset_num(num_type n) {
29 num = n;
30 num_stack.clear();
31 __conversion();
32 }
33
34 // 打印八进制数据
35 void print() {
36 unsigned int i = 0;
37 unsigned int length = num_stack.size();
38 while (i < length) {
39 cout << num_stack.pop();
40 ++ i;
41 }
42 }
43 // 重载赋值运算符
44 num_conversion& operator = (const num_conversion& rhs) {
45 if (this == &rhs)
46 return *this;
47 num = rhs.get_num();
48 num_stack.clear();
49 num_stack = rhs.num_stack;
50 return *this;
51 }
52
53 private:
54 // 数制转换
55 void __conversion() {
56 num_type tmp = num;
57 if (tmp == 0) {
58 num_stack.push(0);
59 return ;
60 }
61 while (tmp) {
62 num_stack.push(tmp % 8);
63 tmp = tmp / 8;
64 }
65 }
66
67 private:
68 stack<num_type> num_stack; // 转换结果
69 num_type num; // 要转换的数据
70 };

 

相关阅读 更多 +
排行榜 更多 +
方块枪战战场安卓版

方块枪战战场安卓版

飞行射击 下载
战斗火力射击安卓版

战斗火力射击安卓版

飞行射击 下载
空中防御战安卓版

空中防御战安卓版

飞行射击 下载