编程珠玑第三章习题3.7(5-8)
2016-08-25 21:15
357 查看
5.将输入的单词表示成带有后缀连字符的单词
当然第一个单词是随便输的,哈哈哈。
6.格式信函发生器
编写一个格式信函发生器,使之可以通过数据库中的每条记录来生成定制的文档(这常常称为邮件归并特性)。此处用的是本地的文件进行测试。群发邮件时可以用到,替换相应部分即可。
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
运行结果
7.略过。有兴趣的可以自己写
8.七段显示设备实现显示十进制设备问题
因为2的16次方远小于99999,所以没问题
**思想首先获取这个数的万位,千位等各位的数字;
然后再规定0-9对应的七个灯的情况,这里用boolean表示亮或不亮
最后就是显示了,先显示个完完整整的即8,然后一般地让其不亮的部分进行占位等格式控制**
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 9f0a (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); } }
相关文章推荐
- 编程珠玑第三章3.7习题(1-4)
- 编程珠玑(第三章)中的有趣的一道练习题(习题8):seven-segment devices
- C++primer第三章习题3.7
- 【第三周】第三章习题3.7
- 编程珠玑之第三章习题2
- 编程珠玑 第二版 第三章 习题答案
- 编程珠玑第二版第三章习题(Java)
- 编程珠玑第三章习题
- 《编程珠玑》习题练习In Python——第三章 数据决定程序结构
- 编程珠玑:第三章 数据决定程序结构 习题解答
- 编程珠玑第三章习题1
- 编程珠玑之第三章习题1
- 编程珠玑第三章课后习题
- 编程珠玑第三章习题答案
- 快学scala 第三章 读书笔记及习题答案代码
- 编程珠玑(2)第三章阅读笔记
- uva11809 紫书第三章习题
- 数据结构与算法分析课后习题第三章(4)
- C++Primer第三章习题(1)
- 《算法竞赛入门经典》第三章习题 (2)