zoukankan      html  css  js  c++  java
  • 《算法》第五章部分程序 part 7

    ▶ 书中第五章部分程序,包括在加上自己补充的代码,字符串的二进制表示、十六进制表示、图形表示

    ● 二进制表示

     1 package package01;
     2 
     3 import edu.princeton.cs.algs4.StdOut;
     4 import edu.princeton.cs.algs4.BinaryStdIn;
     5 
     6 public class class01
     7 {
     8     private class01() {}
     9 
    10     public static void main(String[] args)
    11     {
    12         int bitPerLine = 16;                                // 默认每行显示 16 Bit
    13         if (args.length == 1)
    14             bitPerLine = Integer.parseInt(args[0]);
    15         int count;                                          // 统计输入的字符数
    16         for (count = 0; !BinaryStdIn.isEmpty(); count++)
    17         {
    18             if (bitPerLine == 0)
    19             {
    20                 BinaryStdIn.readBoolean();
    21                 continue;
    22             }
    23             if (count % bitPerLine == 0) // 显示够 bitPerLine 个 bit,换行
    24                 StdOut.println();
    25             StdOut.print(BinaryStdIn.readBoolean() ? 1 : 0);// 读取一个 bit,分别显示 1 和 0
    26         }
    27         if (bitPerLine != 0)
    28             StdOut.println();
    29         StdOut.println(count + " Bits");                    // 总字符数
    30     }
    31 }

    ● 十六进制表示

     1 package package01;
     2 
     3 import edu.princeton.cs.algs4.StdOut;
     4 import edu.princeton.cs.algs4.BinaryStdIn;
     5 
     6 public class class01
     7 {
     8     private class01() {}
     9 
    10     public static void main(String[] args)
    11     {
    12         int bytePerLine = 16;         // 默认每行显示 16 Byte
    13         if (args.length == 1)
    14             bytePerLine = Integer.parseInt(args[0]);
    15         int count;
    16         for (count = 0; !BinaryStdIn.isEmpty(); count++)
    17         {
    18             if (bytePerLine == 0)
    19             {
    20                 BinaryStdIn.readChar();
    21                 continue;
    22             }
    23             if (count % bytePerLine == 0)
    24                 StdOut.println();
    25             else
    26                 StdOut.print(" ");
    27             StdOut.printf("%02x", BinaryStdIn.readChar() & 0xff);
    28         }
    29         if (bytePerLine != 0)
    30             StdOut.println();
    31         StdOut.println(count + " Bytes");
    32     }
    33 }

    ● 图形表示

     1 package package01;
     2 
     3 import java.awt.Color;
     4 import edu.princeton.cs.algs4.BinaryStdIn;
     5 import edu.princeton.cs.algs4.Picture;
     6 
     7 public class class01
     8 {
     9     private class01() {}
    10 
    11     public static void main(String[] args)
    12     {
    13         int width = Integer.parseInt(args[0]), height = Integer.parseInt(args[1]);  // 列数(Bit)和行数
    14         Picture picture = new Picture(width, height);                               // 新建一张图
    15         for (int row = 0; row < height; row++)
    16         {
    17             for (int col = 0; col < width; col++)
    18             {
    19                 if (!BinaryStdIn.isEmpty())                                         // 按照输入流填充依次图形
    20                     picture.set(col, row, BinaryStdIn.readBoolean() ? Color.BLACK : Color.WHITE);
    21                 else
    22                     picture.set(col, row, Color.RED);                               // 红色补齐没有元素的部分
    23             }
    24         }
    25         picture.show();
    26     }
    27 }

    ■ 用图形显示《白鲸记》前 18 万字符

  • 相关阅读:
    虚拟机中Linux 下安装tools
    各个复位标志解析,让我们对MCU的程序的健康更有把控
    用过的两种鼠标右键添加快捷指令方法
    一个ftp协议传输文件之后执行脚本无法工作的情况
    一文入门Linux下gdb调试(二)
    一文入门Linux下gdb调试(一)
    VS CODE远程办公篇一
    Kwp2000协议的应用(程序后续篇)
    Kwp2000协议的应用(程序原理篇)
    Kwp2000协议的应用(硬件原理使用篇)
  • 原文地址:https://www.cnblogs.com/cuancuancuanhao/p/9869403.html
Copyright © 2011-2022 走看看