weixin_42350207 2009-11-27 22:42
浏览 301
已采纳

集合表达式运算

文件input.txt,集合表达式((A+B)*C-B)*A
文件input.txt的内容如下:
A = {5, 9, 10, 13, 56 }
B = {15, 8, 20 }
C = {78, 19, 10, 65 }
计算((A+B)*C-B)*A
输出结果:((A+B)*C-B)*A = {10}
+”,“*”,“-”分别表示集合并、交、差运算。

  • 写回答

4条回答 默认 最新

  • shawn.bug 2009-11-28 12:47
    关注

    package math;

    import java.io.BufferedReader;
    import java.io.InputStreamReader;

    public class Main {//((A+B)*C-B)*A
    public static void main(String args[]){

        try {
            while(true){
                BufferedReader br=new BufferedReader(
                        new InputStreamReader(System.in));
                System.out.println("Enter 'exit' withdraw from this program.");
                System.out.print("Enter Expressions >");
                String exp=br.readLine().replace(" ","");
                if(!exp.equals("exit")){
                    new MathCollection(exp);
                    System.out.println();
    
                } else { 
                    System.exit(0);
                }
            }
        } catch (Exception e) {
            System.out.println("invalid expressions");
        } 
    
    
    }
    

    }

    package math;

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;

    /*

    • 离散数学,集合运算,读取三个集合 实现并、交、差运算
    • @author david
    • */
      public class MathCollection {
      private static int[] type;
      private static int[] A,B,C;
      public MathCollection(){}
      public MathCollection(String exp) throws Exception {
      System.out.println("init ollection.");
      initArray();
      operation(exp);
      System.out.print(exp+" = { ");
      for(int a:type){

          System.out.print(a+" ");
      }
      System.out.print("}");
      

      }
      public static String operation(String s) throws Exception{
      if(s.equals("T")){
      return null;
      } else {
      if(ifchar(s)){
      String str=priority(s);
      String a=str.replace("(","").replace(")","");
      System.out.println(" "+s);
      operate(a);
      String strnew=strReplace(s,str,"T");
      return operation(strnew);
      } else {
      return operate(s);
      }
      }
      }
      public static String priority(String s){
      int l,r;
      l = s.lastIndexOf("(");
      String newStr;
      r = s.substring(l).indexOf(")")+1+s.substring(0, l).length();
      newStr = s.substring(l, r);
      return newStr;
      }
      public static boolean ifchar(String s){
      char[] f=s.toCharArray();
      for(char ff:f){
      if(ff=='(') return true;
      }
      return false;
      }

      public static String operate(String exp){
      if(exp.equals("T")){
      return null;
      } else {
      String s1=exp.substring(0,3);
      String a1=s1.substring(0,1);
      String a2=s1.substring(2,3);
      String oper=s1.substring(1,2);
      int[] A1=getArray(a1);
      int[] A2=getArray(a2);
      type=operateCollection(A1,A2,oper);
      String str_new=strReplace(exp,s1,"T");
      System.out.println("->"+exp);
      System.out.println("->"+str_new);
      return operate(str_new);
      }
      }
      public static String strReplace(String rStr,String rFix,String rRep) {

      int l=0;

      String gRtnStr = rStr;

      do {
      l = rStr.indexOf(rFix,l);

      if(l == -1) break;
      gRtnStr = rStr.substring(0,l)+rRep+rStr.substring(l+rFix.length());

      l+=rRep.length();
      rStr=gRtnStr;
      }while(true);
      return gRtnStr.substring(0,gRtnStr.length());
      }
      public static int[] operateCollection(int[] A1,int[] A2,String ope){
      Mylist conn = new Mylist();
      if(ope.equals("+")){
      for(int num:A1){
      conn.add(num);
      }
      for(int num:A2){
      if(!conn.find(num)){
      conn.add(num);
      }
      }
      return getType(conn);
      }
      if(ope.equals("*")){
      for(int num_a:A1)
      for(int num_b:A2)
      if(num_a==num_b){
      if(!conn.find(num_a))
      conn.add(num_a);
      }
      return getType(conn);
      }

      if(ope.equals("-")){
          for(int num:A1){
             conn.add(num);
          }
          for(int num:A2){
              if(conn.find(num)){
                  conn.remove(conn.findByvalue(num));
              }else{
                  conn.add(num);
              }
          }
          return getType(conn);
      }
      else return null;
      

      }

      public static int[] getType(Mylist list){
      type=new int[list.size()];
      for(int i=0;i<list.size();i++){
      type[i]=list.get(i);
      }
      return type;
      }
      public static int[] getArray(String name){
      if(name.equals("A")||name.equals("a")){
      return A;
      }
      if(name.equals("B")||name.equals("b")){
      return B;
      }
      if(name.equals("C")||name.equals("c")){
      return C;
      }
      if(name.equals("T")||name.equals("t")){
      return type;
      }
      return null;
      }

      public static void initArray(){
      try{
      BufferedReader br = new BufferedReader(
      new FileReader("d:\input.txt"));
      String line="";
      int i=0;
      while((line=br.readLine()) !=null){
      i++;
      parse(line,i);
      }
      } catch(FileNotFoundException e){
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      public static int[] parse(String line,int i){
      System.out.println(" "+line);
      String str=line.replace(" ", "").replace("{", "").replace("}", "");
      String[] num=(str.substring(2,str.length())).split(",");
      if(i==1){
      A = new int[num.length];
      for(int ni=0; ni<num.length;ni++){
      A[ni]=Integer.parseInt(num[ni]);
      }
      return A;
      }
      if(i==2){
      B = new int[num.length];
      for(int ni=0; ni<num.length;ni++){
      B[ni]=Integer.parseInt(num[ni]);
      }
      return B;
      }
      if(i==3){
      C = new int[num.length];
      for(int ni=0; ni<num.length;ni++){
      C[ni]=Integer.parseInt(num[ni]);
      }
      return C;
      }
      return null;
      }
      }

    package math;

    public class Mylist {
    private int[] data=new int[3];
    private int index=0;
    private void expand(){
    int[] data2=new int[data.length*2];
    System.arraycopy(data,0,data2,0,data.length);
    data=data2;
    }
    public void add(int o){
    if (data.length==index) expand();
    data[index]=o;
    index++;
    }
    public void add(int pos,int o){
    if (data.length==index) expand();
    for(int i=index;i>pos;i--){
    data[i]=data[i-1];
    }
    data[pos]=o;
    index++;
    }
    public int remove(int pos){
    int o=data[pos];
    index--;
    for(int i=pos;i<index;i++){
    data[i]=data[i+1];
    }
    return o;
    }
    public int size(){
    return index;
    }
    public int get(int pos){
    return data[pos];
    }

    public boolean find(int i){
        for(int num:data){
            if(num==i) return true;
        }
        return false; 
    }
    
    public int findByvalue(int i){
        for(int i1=0;i1<data.length;i1++){
            if(data[i1]==i) 
                return i1;
        }
        return -1; 
    }
    
    public void clear(){
        index=0;
    }
    public boolean isEmpty(){
        return index==0;
    }
    public int[] toArray(){
        return data;
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog