随笔 - 181  文章 - 15  trackbacks - 0
<2008年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

常用链接

留言簿(1)

随笔分类

随笔档案

My Tech blog

搜索

  •  

最新评论

阅读排行榜

评论排行榜

栈在概念方面确实没有提的必要了。看了数据结构的这一部分,就将书中的一小段代码实现了。这里用到了标准库中的stack。下面就是根据输入的十进制正数获取二进制正数的程序(书上是八进制):
 1 #include <stack>
 2 #include <iostream>
 3 using namespace std;
 4 int _tmain(int argc, _TCHAR* argv[])
 5 {
 6     stack<int> binaryStack;
 7     int decimalValue;
 8     cout<<"please input decimal value:";
 9     cin>>decimalValue;
10     while(decimalValue)
11     {
12         binaryStack.push(decimalValue%2);
13         decimalValue=decimalValue/2;
14         
15     }
16     cout<<"\n"<<"the binary result is:";
17     while(!binaryStack.empty())
18     {
19         int result=(int)binaryStack.top();
20         binaryStack.pop();
21         cout<<result;
22     }
23     cout<<"\n";
24     return 0;
25 }
另外顺便看了一下C#和JAVA,当然也支持栈。但是这些东西在我平时使用的时候却很少用到,或者在用到的时候去“重复发明轮子了”。作为一个经典的数据类型,栈理应是被绝大多数语言支持的(不知道PB怎样)。所以在这里要树立一下“重用轮子”的意识:
C#的十进制转二进制实现:
 1 class Class1
 2     {
 3         /// <summary>
 4         /// 应用程序的主入口点。
 5         /// </summary>
 6         [STAThread]
 7         static void Main(string[] args)
 8         {
 9             //
10             // TODO: 在此处添加代码以启动应用程序
11             //
12             Stack binaryStack=new Stack();
13             Console.WriteLine("Please input decimal value:");
14             String sDecimal=Console.ReadLine();
15             int decimalValue=Int32.Parse(sDecimal);
16             while(decimalValue!=0)
17             {
18                 binaryStack.Push(decimalValue%2);
19                 decimalValue=decimalValue/2;
20             }
21             Console.WriteLine("The binary result is:");
22             while(binaryStack.Count!=0)
23             {
24                 Console.Write(binaryStack.Pop());
25             }
26             Console.WriteLine();
27         }
28     }

JAVA的十进制转二进制实现:
 1 import java.io.BufferedReader;
 2 import java.io.InputStreamReader;
 3 import java.util.Stack;
 4 public class hello1 {
 5     public static void main(String args[])
 6     {
 7         try {
 8             Stack<Integer> binaryStack=new Stack<Integer>();
 9             int decimalValue;
10             System.out.println("please input decimal value:");
11             BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
12             decimalValue=Integer.parseInt(bufferedReader.readLine());
13             while(decimalValue!=0)
14             {
15                 binaryStack.push(decimalValue%2);
16                 decimalValue=decimalValue/2;
17             }
18             System.out.println("the binary result is:");
19             while(!binaryStack.empty())
20             {
21                 System.out.print(binaryStack.pop());
22             
23             }
24         } catch (Exception e) {
25             // TODO Auto-generated catch block
26             e.printStackTrace();
27         }
28         
29         
30     }
31     }
通过比较,会发现C#不大有“栈”的感觉,JAVA和C++更贴近于《数据结构》中对于数据类型“栈”的操作的定义。另外支持模板或者范型的特点让人用起来总会感觉十分舒服。在这里提醒自己:以后这三种语言有“栈”可用了!
posted on 2007-06-11 22:11 littlegai 阅读(176) 评论(0)  编辑 收藏 引用 所属分类: 我的读书笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理