2015/7/22/异常/重写/字符串加减乘除运算

字符串加减乘除运算

默认方法的重写

异常的处理


随便输入一串带有加减乘除的字符串就可以算出这个字符串的值

package com.baidu.text1;

import java.util.Scanner;

public class Test01 {
public static void main(String[] args) {
    Scanner scanner=new Scanner(System.in);
    String s="5+31*2/3-7*5+12=";//"5+31*2/3-7*5+12=";
//  String s=scanner.next();
    MyStack num=new MyStack();//数字字符的压栈出栈的方法类
    MyStackChar fuhao=new MyStackChar();//符号字符的压栈出栈的方法类
//  int[]array=new int [20];
//  int index=-1;
    String digit="";//定义一个空字符
    for (int i = 0; i < s.length(); i++) {
        char c=s.charAt(i);//一个一个字符进行判断
        if (Character.isDigit(c)) {//判断这个字符是否是数字,如果是则开始下一个循环
            digit+=c;              //将是数字字符的链接在一起
        }
        else{
            System.out.print(digit);//打印一下digit中的字符
            num.push(Integer.parseInt(digit));//将得到的字符串转换为int型压入栈低
            if (c=='=') {//如果遇到‘=’就说明到了式子的结尾开始进行最后的运算
                System.out.print(c);//输出等号
                int after=num.pop();//将数字栈中的数据弹出 ,弹出的先后顺序之间的关系要清楚
                int before=num.pop();
                char top=fuhao.pop();//弹出符号
                int result=yunsuan(before, top, after);
                System.out.println(result);//打印结果
            }else{
                System.out.print(c);
                digit="";//清空digit,方便下个字符的进入
                if (fuhao.getIndex()==-1) {//判断符号栈是否有符号在栈低,如果没有就把符号进行压栈操作
                    fuhao.push(c);
                }else{

                    jisuan(num,fuhao,c);
                }
            }

        }
    }
}

public static void jisuan(MyStack num,MyStackChar fuhao,char c){
    if (fuhao.getIndex()==-1) {//判断符号栈是否有符号在栈低,如果没有就把符号进行压栈操作
        fuhao.push(c);
        return;
    }
    char top=fuhao.getTop();
    if (youxianji(c,top)) {//如果优先级高,就压入栈底
        fuhao.push(c);
    }else{//如果遇到同级的就进行如下计算
        top=fuhao.pop();
        int after=num.pop();
        int before=num.pop();
        int result=yunsuan(before, top, after);//遇到两个优先级一样的符号开始计算
        num.push(result);//将运算结果进行压栈操作
//      fuhao.push(c);  //不能再压入栈操作了
        jisuan(num,fuhao,c);//递归运算
    }
}
//判断优先级方法,如果c是乘法或者是除法说明优先级高,返回true将符号压入栈底等待下一个字符到来
//如果c和top相同,那么返回FALSE,进行计算,
public static boolean youxianji(char c,char top){
    if(c=='*'||c=='/'){//如果c的优先级比top高就返回true如果优先级相等就返回false开始进行计算
        if(top=='*'||top=='/'){
            return false;
        }
        return true;
    }else
        return false;
}
//数据之间的运算
public static int yunsuan(int before,char c,int after){
    int i=0;
    switch(c){
    case'+':
        i=before+after;
        break;
    case'-':
        i=before-after;
        break;
    case'*':
        i=before*after;
        break;
    case'/':
        i=before/after;
        break;
    default:
            break;
    }
    return i;
}
}

默认方法的重写

1.在类中写要重写的方法的名字然后按Ctrl+/就可以重写默认方法

    @Override
    //id一样那么着两个变量就相等
    public boolean equals(Object obj) {
        if(obj!=null&&obj instanceof Student){//如果obj不为空而且属于Student类//instanceof是用来判断obj是否属于Student类的,是返回true
            return ((Student)obj).getId()==this.id;//符合条件就将obj强制转换为Student类
        }                                           //而且将getID函数的输出值作为判断相等的依据
        return false;
    }

2.在调用的时候只需要调用类中的对象即可

        Student zhangsan=new Student();
        zhangsan.setId(1);
        zhangsan.setName("张三");
        Student lisi=new Student();
        lisi.setId(1);
        lisi.setName("李四");
//  修改了equals定义的方法,改为如果Id相等则对象相等    
        System.out.println(zhangsan.equals(lisi));

异常的分类

这里写图片描述

异常的处理规则

1.不过度使用异常
2.不使用庞大的Try块
3.不要忽略捕获到的异常

异常处理的常用语句结构
TryCatch语句结构

多重Catch块
1.一段代码可能引起多种类型的异常
2.当引发异常时,会按照顺序来查看每个Catch语句,并执行第一个与异常类型匹配的Catch语句。一般情况下错误范围大的放下边,错误范围小的放上边
3.执行其中的一条Catch语句之后,其后的Catch语句将自动被忽略。

    public static void main(String[] args) {
        Student zhangsan=null;
    try{
        System.out.println(zhangsan.getName());
    }catch(NullPointerException expection){
        expection.printStackTrace();
        System.out.println("报了空指针异常");
    }
//第二种情况,只执行一句Catch语句,后面就执行不到了   
try {
        FileInputStream is=new FileInputStream("d=//11.txt");
        is.read();
//得到异常
//执行一条catch语句之后后面的就不再执行其他catch语句    
//catch 错误范围大的在下边,范围小的在上边
    } catch (FileNotFoundException e) {
        System.out.println("报错了");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("报错了0");//因为执行了前一条catch语句所以这条语句会自动忽略
        e.printStackTrace();
    }
    System.out.println("继续运行");
    }
    public static void main(String[] args) {
        File f=new File("e://11.txt");//读取文件
        try {
            byte[]arry=new byte[1025];//定义一个数组,数组的长度为1025
//  定义一个类型为Inputstream的对象 FileInputstream是Inputstream的子类
//  他们的作用是文件系统中的某个文件中获得输入字节
            InputStream is=new FileInputStream(f);
            int readindex=is.read(arry);//从is的输入流中读取一个数据字节
            System.out.println(new String (arry));
            while(readindex !=-1){
                readindex=is.read(arry);
                System.out.println(new String (arry));
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
        private String name;
    public void setName(String name) throws MyExpection {
        if(name.equals("张三")){//如果字符内容是”张三“那么就把他扔掉
            throw new MyExpection();
        }
        this.name = name;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值