编程珠玑第三章习题3.7(5-8)

5.将输入的单词表示成带有后缀连字符的单词

package chapter3;

public class t5 {
    public  static String matchSuffix(String word,String[] suffixs){
        int matchIndex=-1;//存储最后单词word与哪一个后缀匹配,suffixs[matchIndex]
        boolean flag;
        for(int arrIndex=suffixs.length-1;arrIndex>=0;arrIndex--){
            flag=true;
            for(int suffixIndex=suffixs[arrIndex].length()-1,wordIndex=word.length()-1;suffixIndex>=0&&wordIndex>=0;suffixIndex--){
                char s=suffixs[arrIndex].charAt(suffixIndex);
                char w=word.charAt(wordIndex);
                if(s!='-'&&s!=w){
                    flag=false;//如果对应后缀字符不等,则跳出循环
                    break;
                }
                if(s!='-'){
                    wordIndex--;//注意匹配的时候只有不是空格的时候才减
                }
            }
            if(flag){
                matchIndex=arrIndex;
            }
        }
        if(matchIndex>=0){
            String temp[]=suffixs[matchIndex].split("-");
            int matchLength=0;
            for(String s:temp){
                matchLength+=s.length();
            }
            String wordPart=word.substring(0, word.length()-matchLength);
            String result=wordPart+suffixs[matchIndex];
            System.out.print(wordPart);
            System.out.println(suffixs[matchIndex]);
            return result;
        }
        else{
            System.out.println(word);
            return word;
        }


    }
    public static void main(String []args){
        String suffixs[]={"et-ic","al-is-tic","s-tic","p-tic","-lyt-ic",
                "ot-ic","an-tic","n-tic","c-tic","at-ic",
                "h-nic","n-ic","m-ic","l-lic","b-lic",
                "-clic","l-ic","h-ic","f-ic","d-ic",
                "-bic","a-ic","-mac","i-ac"
        };//因为后缀已经按倒序排列,等等匹配的时候就倒着来
        matchSuffix("abasalistic", suffixs);
        matchSuffix("fantastic", suffixs);

    }
}

运行结果
当然第一个单词是随便输的,哈哈哈。
6.格式信函发生器
编写一个格式信函发生器,使之可以通过数据库中的每条记录来生成定制的文档(这常常称为邮件归并特性)。此处用的是本地的文件进行测试。群发邮件时可以用到,替换相应部分即可。

package chapter3;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class t6 {
    public static void explainLetterScheme(String [] record){
        String letterScheme="Welcome back,$1!\n" +
        "We hope that you and all the members\n" +
        "of the $0 family are constantly \n" +
        "reminding your neighbours there\n" +
        "on $5 to shop with us.\n" +
        "As usual,we will ship your order to\n" +
        "$3 $1 $2. $0\n" +
        "$4 $5\n" +
        "$6,$7 $8 \n";
        for(int index=0;index<letterScheme.length();index++){
            char c=letterScheme.charAt(index);
            if(c!='$'){
                System.out.print(c);
            }
            else {
                char next=letterScheme.charAt(++index);
                switch (next) {
                case '$':
                    System.out.print('$');
                    break;
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                    int i=Integer.valueOf(next)-48;//因为’0’-'9'转换为十进制为48-57
                    System.out.print(record[i]);
                    break;
                default:
                    break;
                }
            }
        }
    }
    public static void main(String[]args){
        try {
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader
                    (new FileInputStream(new File("E:/Study/Pearls/C3/t6.txt"))));
            String line=bufferedReader.readLine();
            while (line!=null) {
                String record[]=line.split(";");
                explainLetterScheme(record);
                line=bufferedReader.readLine();
                System.out.println();
                System.out.println();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}

E:/Study/Pearls/C3/t6.txt文件内容,以分号分隔为
Finch;Harold;Q;Mr.;600;Maple Street;Your Town;lowa;12345
Reese;Jhon;Q;Mrs.;600;Maple Street;Your Town;lowa;12345
运行结果

Welcome back,Harold!
We hope that you and all the members
of the Finch family are constantly 
reminding your neighbours there
on Maple Street to shop with us.
As usual,we will ship your order to
Mr. Harold Q. Finch
600 Maple Street
Your Town,lowa 12345 


Welcome back,Jhon!
We hope that you and all the members
of the Reese family are constantly 
reminding your neighbours there
on Maple Street to shop with us.
As usual,we will ship your order to
Mrs. Jhon Q. Reese
600 Maple Street
Your Town,lowa 12345 

7.略过。有兴趣的可以自己写
8.七段显示设备实现显示十进制设备问题
因为2的16次方远小于99999,所以没问题
**思想首先获取这个数的万位,千位等各位的数字;
然后再规定0-9对应的七个灯的情况,这里用boolean表示亮或不亮
最后就是显示了,先显示个完完整整的即8,然后一般地让其不亮的部分进行占位等格式控制**

package chapter3;

import java.util.Arrays;

public class t8 {

    public boolean[] getTheLightOnes(int digit){
        boolean lights[]=new boolean[7];
        switch (digit) {
        case 0:
            lights[2]=lights[3]=lights[5]=lights[0]=lights[6]=lights[4]=true;
            return lights;
        case 1:
            lights[4]=lights[6]=true;
            return lights;
        case 2:
            lights[2]=lights[4]=lights[1]=lights[5]=lights[0]=true;
            return lights;
        case 3:
            lights[2]=lights[4]=lights[1]=lights[6]=lights[0]=true;
            return lights;
        case 4:
            lights[3]=lights[1]=lights[4]=lights[6]=true;
            return lights;
        case 5:
            lights[2]=lights[3]=lights[1]=lights[6]=lights[0]=true;
            return lights;
        case 6:
            lights[2]=lights[3]=lights[5]=lights[0]=lights[6]=lights[1]=true;
            return lights;
        case 7:
            lights[2]=lights[4]=lights[6]=true;
            return lights;
        case 8:
            lights[0]=lights[1]=lights[2]=lights[3]=lights[4]=lights[5]=lights[6]=true;
            return lights;
        case 9:
            lights[2]=lights[3]=lights[1]=lights[4]=lights[6]=true;
            return lights;
        default:
            return lights;
        }
    }
    public int[] getTheFive(int number){//将最高位到最低位依次存在数组的1-5下标元素中
        int theFive[]=new int[6];
        for(int i=1;i<=5;i++){
            number-=theFive[i-1]*(int)Math.pow(10, 6-i);
            theFive[i]=(number)/(int)Math.pow(10, 5-i);
        }
        return theFive;
    }
    public void showDigit(int digit){
        boolean lights[]=getTheLightOnes(digit);
        //获取数字digit(0-9)要亮的的部分,然后由上到下,由左到右让对应部分亮,当然不亮的时候得进行必要的格式控制
        if(lights[2])
            System.out.println(" _ ");
        if(lights[3])
            System.out.print('|');
        else {
            System.out.print(' ');
        }
        if(lights[1])
            System.out.print('_');
        else {
            System.out.print(" ");
        }
        if(lights[4])
            System.out.println("|");
        else {
            System.out.println();
        }
        if(lights[5])
            System.out.print('|');
        else {
            System.out.print(' ');
        }
        if(lights[0])
            System.out.print('_');
        else {
            System.out.print(" ");
        }
        if(lights[6])
            System.out.println("|");
        else {
            System.out.println();
        }
    }
    public void showNumber(int number){
        int theFive[]=getTheFive(number);
        System.out.println(Arrays.toString(theFive));
        for(int i=1;i<theFive.length;i++){
            showDigit(theFive[i]);
        }
    }
    public static void main(String args[]){
        //      System.out.println(Math.pow(2, 16));//因为五段可以表示的最大数为99999远远大于2的16次方
        /*      System.out.println(" _ ");
        System.out.print('|');
        System.out.print('_');
        System.out.println("|");
        System.out.print('|');
        System.out.print('_');
        System.out.println("|");尝试凑出8的形状*/
        t8 testT8=new t8();//名称不规范,此处为小练习请见谅
        testT8.showNumber(58923);
        testT8.showNumber(4320);
    }
}

这里是运行结果

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值