weixin_42298593 2008-11-18 21:07
浏览 230
已采纳

读取TXT文件实现集合运算

从事先写好的Input.txt文件中读取数,
Input.txt 内容
A{3,4,10,20,30,50}
B{1,2,34,5,6}
C{2,3,12,23,14,11}

用户在键盘随意输入,例如((A*B))+B-C,期中+,*,-,分别代表集合的并交差运算,控制台打印输出。
[b]问题补充:[/b]
从事先写好的Input.txt文件中读取数,
Input.txt 内容
A{3,4,10,20,30,50}
B{1,2,34,5,6}
C{2,3,12,23,14,11}

用户在键盘随意输入,例如((A*B))+B-C,期中+,*,-,分别代表集合的并交差运算,控制台打印输出。
我要全部代码,用JAVA实现,谢谢。
[b]问题补充:[/b]
从事先写好的Input.txt文件中读取数,
Input.txt 内容
A{3,4,10,20,30,50}
B{1,2,34,5,6}
C{2,3,12,23,14,11}

用户在键盘随意输入,例如((A*B))+B-C,期中+,*,-,分别代表集合的并交差运算,控制台打印输出。
问题补充:
从事先写好的Input.txt文件中读取数,
Input.txt 内容
A{3,4,10,20,30,50}
B{1,2,34,5,6}
C{2,3,12,23,14,11}

用户在键盘随意输入,例如((A*B))+B-C,期中+,*,-,分别代表集合的并交差运算,控制台打印输出。
最后解析式表达上有点困难
我只是学习中,不是工作。
请教大家了
谢谢。
[b]问题补充:[/b]
从事先写好的Input.txt文件中读取数,
Input.txt 内容
A{3,4,10,20,30,50}
B{1,2,34,5,6}
C{2,3,12,23,14,11}

用户在键盘随意输入,例如((A*B))+B-C,期中+,*,-,分别代表集合的并交差运算,控制台打印输出。
问题补充:
从事先写好的Input.txt文件中读取数,
Input.txt 内容
A{3,4,10,20,30,50}
B{1,2,34,5,6}
C{2,3,12,23,14,11}

用户在键盘随意输入,例如((A*B))+B-C,期中+,*,-,分别代表集合的并交差运算,控制台打印输出。
我要全部代码,用JAVA实现,谢谢。
问题补充:
从事先写好的Input.txt文件中读取数,
Input.txt 内容
A{3,4,10,20,30,50}
B{1,2,34,5,6}
C{2,3,12,23,14,11}

用户在键盘随意输入,例如((A*B))+B-C,期中+,*,-,分别代表集合的并交差运算,控制台打印输出。
<strong>问题补充:</strong>
从事先写好的Input.txt文件中读取数,
Input.txt 内容
A{3,4,10,20,30,50}
B{1,2,34,5,6}
C{2,3,12,23,14,11}

用户在键盘随意输入,例如((A*B))+B-C,期中+,*,-,分别代表集合的并交差运算,控制台打印输出。
最后解析式表达上有点困难
我只是学习中,不是工作。
请教大家了
谢谢。
对于核心部分肯本没思路
[b]问题补充:[/b]
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 Math {

/**
 * @param args
 */
public static void main(String[] args) {
    int j = 0;
    int A[] = null, B[] = null, C[] = null;
    BufferedReader br = null;
    File file = new File("D:\\input.txt");
    try {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(
                file)));
        String str = null;
        while ((str = br.readLine()) != null) {
            String str2 = str.charAt(0) + "";
            String str1 = str.replace("{", "").replace("}", "").replace(
                    str2, "");
            String[] s = str1.split(",");
            int[] n = new int[s.length];
            for (int i = 0; i < s.length; i++) {
                n = Integer.parseInt(s);
            }
            if (j == 0) {
                A = n;
            } else if (j == 1) {
                B = n;
            } else {
                C = n;
            }
            ++j;
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    int[] mer = subtraction(A, B);
    for (int i = 0; i < mer.length; i++) {
        System.out.print(mer + " ");
    }
}

// 求并集
public static int[] merger(int[] a, int[] b) {
    int n = a.length;
    int[] c = new int[a.length + b.length];
    for (int i = 0; i < a.length; i++) {
        c = a;
    }
    for (int i = 0; i < b.length; i++) {
        boolean isEqual = true;
        for (int j = 0; j < a.length; j++) {
            if (b == a) {
                isEqual = false;
                break;
            }
        }
        if (isEqual) {
            c[n++] = b;
        }
    }
    int d[] = new int[n];
    for (int i = 0; i < d.length; i++) {
        d = c;
    }

    return d;
}

// 求交集
public static int[] intersection(int[] a, int[] b) {
    int[] c = new int[a.length];
    int n = 0;
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < b.length; j++) {
            if (a == b[j]) {
                c[n++] = a;
                break;
            }
        }
    }
    int[] d = new int[n];
    for (int i = 0; i < d.length; i++) {
        d = c;
    }
    return d;
}

// 求差
public static int[] subtraction(int[] a,int[]b) {
    int[] c = new int[a.length];
    int n = 0;
    for (int i = 0; i < a.length; i++) {
        boolean isDifference=true;
        for (int j = 0; j < b.length; j++) {
            if(a==b[j]){
                isDifference=false;
                break;
            }
        }
        if(isDifference){
            c[n++]=a;
        }
    }
    int d[]=new int[n];
    for(int i=0;i<d.length;i++){
        d=c;
    }
    return d;
}

}

谁能帮忙改下
[b]问题补充:[/b]
现在解析式表达很头疼啊
待人帮忙了

[b]问题补充:[/b]
那位出来帮忙能下啊

  • 写回答

8条回答 默认 最新

  • chengxing6666 2008-11-19 16:38
    关注

    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 Math {

    /** 
     * @param args 
     */ 
    public static void main(String[] args) { 
        int j = 0; 
        int A[] = null, B[] = null, C[] = null; 
        BufferedReader br = null; 
        File file = new File("D:\\input.txt"); 
        try { 
            br = new BufferedReader(new InputStreamReader(new FileInputStream( 
                    file))); 
            String str = null; 
            while ((str = br.readLine()) != null) { 
                System.out.println("str:"+str);
                String str2 = str.charAt(0) + ""; 
                System.out.println("str2:"+str2);
                String str1 = ((str.replace('{', ' ')).replace('}', ' ')).replace(str2.charAt(0), ' '); 
                String[] s = str1.split(","); 
                int[] n = new int[s.length]; 
                for (int i = 0; i < s.length; i++) { 
                    n[i] = Integer.parseInt(s[i].trim()); 
                } 
                if (j == 0) { 
                    A = n; 
                } else if (j == 1) { 
                    B = n; 
                } else { 
                    C = n; 
                } 
                ++j; 
            } 
        } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } finally { 
            try { 
                br.close(); 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
        } 
        int[] mer = subtraction(A, B); 
        for (int i = 0; i < mer.length; i++) { 
            System.out.print(mer[i] + " "); 
        } 
    } 
    
    // 求并集 
    public static int[] merger(int[] a, int[] b) { 
        int n = a.length; 
        int[] c = new int[a.length + b.length]; 
        for (int i = 0; i < a.length; i++) { 
            c[i] = a[i]; 
        } 
        for (int i = 0; i < b.length; i++) { 
            boolean isEqual = true; 
            for (int j = 0; j < a.length; j++) { 
                if (b[i] == a[j]) { 
                    isEqual = false; 
                    break; 
                } 
            } 
            if (isEqual) { 
                c[n++] = b[i]; 
            } 
        } 
        int d[] = new int[n]; 
        for (int i = 0; i < d.length; i++) { 
            d[i] = c[i]; 
        } 
    
        return d; 
    } 
    
    // 求交集 
    public static int[] intersection(int[] a, int[] b) { 
        int[] c = new int[a.length]; 
        int n = 0; 
        for (int i = 0; i < a.length; i++) { 
            for (int j = 0; j < b.length; j++) { 
                if (a[i] == b[j]) { 
                    c[n++] = a[i]; 
                    break; 
                } 
            } 
        } 
        int[] d = new int[n]; 
        for (int i = 0; i < d.length; i++) { 
            d[i] = c[i]; 
        } 
        return d; 
    } 
    
    // 求差 
    public static int[] subtraction(int[] a,int[]b) { 
        int[] c = new int[a.length]; 
        int n = 0; 
        for (int i = 0; i < a.length; i++) { 
            boolean isDifference=true; 
            for (int j = 0; j < b.length; j++) { 
                if(a[i]==b[j]){ 
                    isDifference=false; 
                    break; 
                } 
            } 
            if(isDifference){ 
                c[n++]=a[i]; 
            } 
        } 
        int d[]=new int[n]; 
        for(int i=0;i<d.length;i++){ 
            d[i]=c[i]; 
        } 
        return d; 
    } 
    

    }

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

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试