[输入一个字符串,然后输出字符串的运算结果] [object][异常]

目录

输入一个字符串,然后输出字符串的运算结果
object
异常
IO流,读取D盘某个文件

输入一个字符串,然后输出字符串的运算结果

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

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;
    }
}

异常

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盘某个文件

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();
    }
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
因各个项目中需要使用根据字符串计算数值,这里写出一个算法,专门计算字符串。配有大量常用公式。只有一个人方法,直接调用即可。 类名:CustomMath 函数名:Calculations(string value) 说明:求解算式表达式字符串的值 表达式中包含的符号或函数: truncate, ceiling,floor,round,log10, sign,sinh,sqrt, asin,atan,cosh, tanh, sin,cos,tan ,abs,acos, exp,log,max,min,pow,mod,+,-,*,/,',',(,) 函数说明:(不区分大小写) truncate(num) 计算指定数的整数部分 truncate(1.23)=1 ceiling (num) 返回大于或等于指定的双精度浮点数的最小整数值 ceiling(1.23)=2 floor(num) 返回小于或等于指定双精度浮点数的最大整数 floor(1.23)=1 round(num) 将双精度浮点值舍入为最接近的整数值 round(1.23)=1 round(num,num1) 将小数值按指定的小数位数舍入 round(1.23,1)=1.2 log10(num) 返回指定数字以 10 为底的对数 log10(10)=1 sign(num) 返回表示数字符号的值 sign(1.23)=1 sinh(num) 返回指定角度的双曲正弦值 sinh(1.23)=1.5644 sqrt(num) 返回指定数字的平方根 sqrt(9)=3 sqrt(num,num1) 返回指定数字的num1根 sqrt(27,3)=3 asin(num) 返回正弦值为指定数字的角度 asin(0.5)=PI/6 atan(num) 返回正切值为指定数字的角度 atan(1)=45 cosh(num) 返回指定角度的双曲余弦值 cosh(1.23)=1.8567 tanh(num) 返回指定角度的双曲正切值 tanh(1.23)=0.8425 sin(num) 返回指定角度的正弦值 sin(PI/6)=0.5 cos(num) 返回指定角度的余弦值 sin(PI/3)=0.5 tan(num) 返回指定角度的余切值 sin(PI/4)=1 abs(num) 返回数字的绝对值 abs(-12)=12 acos(num) 返回余弦值为指定数字的角度 acos(0.5)=PI/3 exp(num) 返回 e 的指定次幂 exp(1)=2.718 log(num) 返回指定数字的自然对数(底为 e) log(e)=1 log(num,num1) 返回指定数字在使用指定底时的对数 log(e,e)=1 max(num,um1) 返回最大值 max(1,2)=2 min(num,num1) 返回最小值 min(1,2)=1 pow(num,num1) 返回指定数字的指定次幂 pow(2,2)=4 mod(num,num1) 返回余数 mod(3,2)=1 常量: PI 值:3.14159265358979323846 E 值:2.7182818284590452354 YEAR 值:当前年份 MONTH 值:当前月份 DAY 值: 当前日 HOUR 值:当前时 MINUTE 值:当前分 SECOND 值:当前秒 RANDOM 值:一个随机数(0-1 之间) 实例 系统计算:1+2*3/4-0.5=2 函数计算:1+2*3/4-0.5=2 调用方式:CustomMath.Calculations("1+2*3/4-0.5") 系统计算:(1+2)*3/4-0.5=1.75 函数计算:(1+2)*3/4-0.5=1.75 调用方式:CustomMath.Calculations("(1+2)*3/4-0.5") 系统计算:(sin(pi)+sqrt(3+5*7+(2+8/4*5+2)))/6=1.20185042515466 公式计算:(sin(pi)+sqrt(3+5*7+(2+8/4*5+2)))/6=1.20185042515466 调用方式:CustomMath.Calculations("(sin(pi)+sqrt(3+5*7+(2+8/4*5+2)))/6") 系统计算:sin(pow(3,2)/4)+3.5-9*sqrt(81)=-76.7219268031121 函数计算:sin(pow(3,2)/4)+3.5-9*sqrt(81)=-76.7219268031121 调用方式:CustomMath.Calculations("sin(pow(3,2)/4)+3.5-9*sqrt(81)")

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值