文件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}
+”,“*”,“-”分别表示集合并、交、差运算。
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;
/*
*/
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;
}
}
找个开源组件来实现
[code="java"]
import java.util.*;
// ...
Set a = new HashSet();
Set b = new HashSet();
Set c = new HashSet();
// use properties and String to read input file and initilized all sets
// assume you did that
Set result = new HashSet();
result.addAll(a); // make a copy of a, now result = a
result.addAll(b); // result = result + b
result.retainAll(c); // result = result * c
result.removeAll(b); // result = result - b
result.retainAll(a); // result = result * a
[/code]
input.txt
A = {5, 9, 10, 13, 56 }
B = {15, 8, 20 }
C = {78, 19, 10, 65 }