第二周第三天([输入一个字符串,然后输出字符串的运算结果] [object][异常][IO流,读取D盘某个文件])
2015-07-22 20:00
666 查看
目录
输入一个字符串,然后输出字符串的运算结果object
异常
IO流,读取D盘某个文件
输入一个字符串,然后输出字符串的运算结果
[code]package com.day3_2015_7_22; public class MyStack { //定义了一个数组 private int[] array=new int[20]; private int index=-1; //压栈 public void push(int i){ index++;//每压入一个数字,index+1,所以index++; array[index]=i;//把数字放到数组里 } //得到index public int getIndex(){ return index; } //得到最上面的数字 public int getTop(){ return array[index]; } //出栈 public int pop(){ int j=array[index]; index--; return j; } } package com.day3_2015_7_22; public class MyStackChar { //定义了一个数组 private char[] array=new char[20]; private int index=-1; //压栈 public void push(char i){ index++;//每压入一个运算符,index+1,所以index++; array[index]=i;//把运算符放到数组里 } //得到index public int getIndex(){ return index; } //得到最上面的运算符 public char getTop(){ return array[index]; } //出栈 public char pop(){ char j=array[index]; index--; return j; } } package com.day3_2015_7_22; import java.util.Scanner; public class Test { public static void main(String[] args) { //Scanner scanner=new Scanner(System.in); String s="5+31*2/3-6-7*5+12*2="; MyStack num=new MyStack(); MyStackChar fuhao=new MyStackChar(); String digit=""; for (int i = 0; i < s.length(); i++) { char c=s.charAt(i);//将此字符串转换为一个新的字符数组。 //如果是数字就字符串相加 if(Character.isDigit(c)){ digit+=c; }else{//不是数字就把digit压栈, //因为这时的digit是个字符串 //需要用到Interger.parseInt() //将字符串参数作为有符号的十进制整数进行解析。 num.push(Integer.parseInt(digit)); //如果c是=说明要得到最终结果了,把数字、运算符都出栈进行运算,输出最后结果 if(c=='='){ if(fuhao.getIndex()==0){ int after=num.pop(); int before=num.pop(); char top=fuhao.pop(); int result = yunsuan(before, top, after); System.out.println(result);} else{ char top=fuhao.pop(); int after=num.pop(); int before=num.pop(); int m = yunsuan(before, top, after); int n=num.pop(); char top1=fuhao.pop(); int result =yunsuan(n,top1,m); System.out.println(result); } }else { digit ="";//把digit清空 if (fuhao.getIndex() == -1) { fuhao.push(c); } else { jisuan(num, fuhao, c); } } } } } //这个方法主要是判断得到的这个符号是直接压栈,还是先让最上面的符号出栈计算然后再压栈 private static void jisuan(MyStack num,MyStackChar fuhao,char c){ //如果index=-1,直接把c压栈 if(fuhao.getIndex()==-1){ fuhao.push(c); return; } //把最上面的运算符跟c比较,如果c的优先级大,把c压栈 char top=fuhao.getTop(); if(youxianji(c,top)){ fuhao.push(c); }else{//如果c的优先级小,符号出栈,两个数字出栈进行计算,然后把结果压栈, //再循环之前的算法 top=fuhao.pop(); int after=num.pop(); int before=num.pop(); int result=yunsuan(before,top,after); num.push(result); jisuan(num,fuhao,c); } } //这个方法只要是定义了不同的符号如何计算 public static int yunsuan(int before, char c, int after) { int i = 0; switch (c) { case '+': i = before + after; break;//注意break不要忘写 case '-': i = before - after; break; case '/': i = before / after; break; case '*': i = before * after; break; default://注意最后不要忘写 break; } return i; } //一个判断优先级的方法 //如果c的优先级大,return ture 反之false public static boolean youxianji(char c, char top) { if (c == '*' || c == '/') { if (top == '*' || top == '/') { return false; } return true; } return false; } }
object
[code]package com.day3_2015_7_22; import java.io.FileInputStream; import java.io.FileNotFoundException; public class Test2 { private static final Exception FileNotFoundException = null; public static void main(String[] args) { //*********objec的一些构造方法的改写*****************// // Student zhangsan=new Student(); // zhangsan.setName("张三"); // zhangsan.setId(1); // Student lisi=new Student(); // lisi.setId(2); // lisi.setName("李四"); // Student s=new Student(); // s.setId(1); // System.out.println(zhangsan.equals(s)); //************异常问题************// // Student zhangsan=null; // System.out.println(zhangsan.getName()); //NullPointerException空指针问题 //不需要用try,catch捕捉,不过程序不能继续往下运行 try{ FileInputStream is=new FileInputStream("d://11.txt"); //系统找不到指定文件 }//catch错误范围大的要在下边 catch(FileNotFoundException e){ System.out.println("报错了"); e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } System.out.println("继续运行"); } } //*********objec的一些构造方法的改写toString/equals*****************// package com.day3_2015_7_22; public class Student { int id; String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString(){ return this.name; } public boolean equals(Object obj){ if(obj!=null&&obj instanceof Student){ return((Student)obj).getId()==this.id; } return false; } }
异常
[code]package com.day3_2015_7_22; public class MyException extends Exception{ public void printStackTrace(){ System.out.println("不让张三当老师"); super.printStackTrace(); } } package com.day3_2015_7_22; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Teacher { private String name; public String getName(){ return name; } public void setName(String name) throws MyException{ if(name.equals("张三")){ throw new MyException(); } this.name=name; } public void readBook()throws FileNotFoundException,IOException{ FileInputStream is=new FileInputStream("d://11.txt"); is.read(); } } package com.day3_2015_7_22; public class Test3 { public static void main(String[] args) { Teacher zhangsan=new Teacher(); try{ zhangsan.setName("张三"); }catch(MyException e){ System.out.println("报错了"); e.printStackTrace(); } System.out.println("程序运行完成"); } }
IO流,读取D盘某个文件
[code]package com.day3_2015_7_22; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class Test4 { public static void main(String[] args) { //IO流 File f =new File("d://11.txt"); try {//从输入流中读取数据的下一个字节 byte[]arry = new byte[1024]; InputStream is = new FileInputStream(f); int readindex = is.read(arry); System.out.println(new String(arry)); while(readindex!=-1){ readindex=is.read(arry); System.out.println(new String(arry)); } } catch (FileNotFoundException e) { e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } }
相关文章推荐
- Objective-C学习笔记(三)——用Objective-C编写第一个程序:Hello,World!
- asp:ObjectDataSource+asp:GridView 实现真分页
- 刨根问底Objective-C Runtime(4)- 成员变量与属性
- [学习笔记—Objective-C]《Objective-C-基础教程 第2版》第二章~第七章
- 刨根问底Objective-C Runtime(3)- 消息 和 Category
- 刨根问底Objective-C Runtime(2)- Object & Class & Meta Class
- 刨根问底Objective-C Runtime(1)- Self & Super
- NSObject 的实现分析
- Associative机制使用场景[objective-c有两个扩展机制:category扩展方法和associative扩展属性]
- 关于pch文件
- Objective-C GCC Code Block Evaluation C Extension ({…})语法
- object-c 知识点01
- 【iOS开发系列】NSObject方法介绍
- OC学习笔记之self关键字
- Objective-C学习之旅 第二篇
- OC学习笔记之description
- OC学习笔记之多态
- Objective-C学习笔记(二)——OC基本语法概述
- OC学习之类的进阶
- JSONObject与JSONArray的使用