/******************************************* Author: Hualin Li Name: The header file for set cpp Descrition: This is the header file for simple set class *******************************************/ #include<iostream> #include<set> class myset { public: // std::set<int> mySet; myset() { totalNum=0; numOfElem=0; arraySet=new int[100]; } std::set<int> makeSet() { std::set<int>mySet(arraySet,arraySet+totalNum); return mySet; } bool isEmpty() { if(numOfElem==0) return true; else return false; } int size() { // makeSet(); std::set<int>mySet(arraySet,arraySet+totalNum); numOfElem=mySet.size(); return numOfElem; } void clear() { arraySet=new int[100]; totalNum=0; numOfElem=0; } void add(int m) { arraySet[totalNum]=m; totalNum++; // makeSet(); } bool contains(int n) { bool doesContain=false; for(int i=0;i<totalNum;i++) { //arraySet[i] is the same as *(arraySet+i) if(arraySet[i]==n) doesContain=true; } return doesContain; } int totalNum; int numOfElem; int *arraySet; }; /******************************************* Author: Hualin Li Name: The main fucntion for set cpp Descrition: This is the main function for simple set class *******************************************/ #include"myset.h" #include<iostream> #include<fstream> using namespace std; ofstream outfile; void menu() { cout<<"***********************"<<endl; cout<<"I - Add element"<<endl; cout<<"S - Search"<<endl; cout<<"B - Size of the set"<<endl; cout<<"P - Print the set"<<endl; cout<<"E - Empty set or not"<<endl; cout<<"D - Total number of the array"<<endl; cout<<"A - Print the array"<<endl; cout<<"C - Clear the set"<<endl; cout<<"Q - Quit"<<endl; cout<<"**********************"<<endl<<endl; cout<<"Please input your option:"<<endl; outfile<<"***********************"<<endl; outfile<<"I - Add element"<<endl; outfile<<"S - Search"<<endl; outfile<<"B - Size of the set"<<endl; outfile<<"P - Print the set"<<endl; outfile<<"E - Empty set or not"<<endl; outfile<<"D - Total number of the array"<<endl; outfile<<"A - Print the array"<<endl; outfile<<"C - Clear the set"<<endl; outfile<<"Q - Quit"<<endl; outfile<<"**********************"<<endl<<endl; outfile<<"Please input your option:"<<endl; } int main() { char choice; outfile.open("myset.output"); myset mySet; menu(); cin>>choice; while(choice!='Q' && choice!='q') { switch(choice){ case 'I': case 'i': { outfile<<"I"<<endl; int temp; cout<<"please input the number to add"<<endl; outfile<<"please input the number to add"<<endl; cin>>temp; outfile<<temp<<endl; mySet.add(temp); break; } case 'S': case 's': { outfile<<"S"<<endl; int temp2; cout<<"Please input the number to search"<<endl; outfile<<"Please input the number to search"<<endl; cin>>temp2; outfile<<temp2; if(mySet.contains(temp2)) { cout<<"The number "<<temp2<<" exists!"<<endl; outfile<<"The number "<<temp2<<" exists!"<<endl; } else { cout<<"The number "<<temp2<<"doesn't exist in the set!"<<endl; outfile<<"The number "<<temp2<<"doesn't exist in the set!"<<endl; } break; } case 'B': case 'b': outfile<<"B"; cout<<"The size of the set is "<<mySet.size()<<endl; outfile<<"The size of the set is "<<mySet.size()<<endl; break; case 'P': case 'p': { outfile<<"P"<<endl; for(std::set::iterator it=mySet.makeSet().begin();it!=mySet.makeSet().end();++it) {cout<<" "<<*it; outfile<<" "<<*it;} cout<<""<<endl; outfile<<""<<endl; break; } case 'E': case 'e': { outfile<<"E"<<endl; if(mySet.isEmpty()) { cout<<"The set is empty."<<endl; outfile<<"The set is empty."<<endl; } else { cout<<"The set is not empty."<<endl; outfile<<"The set is not empty."<<endl; } break; } case 'D': case 'd': { outfile<<"D"<<endl; cout<<"The size of array is "<<mySet.totalNum<<endl; outfile<<"The size of array is "<<mySet.totalNum<<endl; break; } case 'A': case 'a': { outfile<<"A"<<endl; for(int i=0;i<mySet.totalNum;i++) { cout<<mySet.arraySet[i]<<","; outfile<<mySet.arraySet[i]<<","; } cout<<endl; outfile<<endl; break; } case 'C': case 'c': { outfile<<"C"<<endl; mySet.clear(); cout<<"The set is cleared already."<<endl; outfile<<"The set is cleared already."<<endl; } case 'Q': case 'q': outfile<<"Q"<<endl; cout<<"Thank you for using simple set."<<endl; break; default: outfile<<choice<<endl; cout<<"Input is not valid. Please input *valid* option."<<endl; outfile<<"Input is not valid. Please input *valid* option."<<endl; } menu(); cin>>choice; } return 0; }
/******************************************* Author: Hualin Li Name: The main fucntion for set in JAVA Descrition: This is the JAVA function for simple set class *******************************************/ import java.util.HashSet; import java.util.Set; import java.util.Scanner; import java.io.PrintWriter; import java.io.*; import java.io.BufferedWriter; public class MySet { public int numOfElem; public int totalNum; public int[] arraySet=new int[100]; Set<Integer> set=new HashSet<Integer>(); public MySet() { numOfElem=0; totalNum=0; //int[] arraySet=new int[100]; } public void makeSet() { this.set=new HashSet<Integer>(); for(int i=0;i<totalNum;i++) set.add(arraySet[i]); } public int size() { makeSet(); numOfElem=set.size(); return numOfElem; } public boolean isEmpty() { if(numOfElem==0) return true; else return false; } public void clear() { this.arraySet=new int[100]; numOfElem=0; totalNum=0; makeSet(); } public boolean contains(int n) { boolean isContains=false; for(int i=0;i<totalNum;i++) { if(arraySet[i]==n) isContains=true; } return isContains; } public void add(int n) { arraySet[totalNum]=n; makeSet(); totalNum++; } public String toString() { String str=""; for(int i=0;i<totalNum;i++) str= str+arraySet[i]+" "; return str; } public static void menu() { try{PrintWriter printWriter=new PrintWriter(new BufferedWriter(new FileWriter("MySet.output", true))); System.out.println("***********************"); System.out.println("I - Add element"); System.out.println("S - Search"); System.out.println("B - Size of the set"); System.out.println("P - Print the set"); System.out.println("E - Empty set or not"); System.out.println("D - Total number of the array"); System.out.println("A - Print the array"); System.out.println("C - Clear the set"); System.out.println("Q - Quit"); System.out.println("**********************"); System.out.println(); System.out.println("Please input your option:"); printWriter.println("***********************"); printWriter.println("I - Add element"); printWriter.println("S - Search"); printWriter.println("B - Size of the set"); printWriter.println("P - Print the set"); printWriter.println("E - Empty set or not"); printWriter.println("D - Total number of the array"); printWriter.println("A - Print the array"); printWriter.println("C - Clear the set"); printWriter.println("Q - Quit"); printWriter.println("**********************"); printWriter.println(); printWriter.println("Please input your option:"); printWriter.close(); }catch(IOException e){} } public static void main(String[] args) { try{ PrintWriter printWriter=new PrintWriter(new BufferedWriter(new FileWriter("MySet.output", true))); MySet simpleSet =new MySet(); char input; menu(); Scanner keyboard=new Scanner(System.in); input=keyboard.next().charAt(0); while(input!='Q' && input!='q') { switch(input){ case 'I': case 'i': { System.out.println("please input the number to add"); printWriter.println("i"); printWriter.println("please input the number to add"); int addNum=keyboard.nextInt(); printWriter.println(addNum); simpleSet.add(addNum); break; } case 'B': case 'b': { printWriter.println("B"); System.out.println("The number of element in the set is "+simpleSet.size()); printWriter.println("The number of element in the set is "+simpleSet.size()); break; } case 'P': case 'p': { printWriter.println("P"); System.out.println("The set now is "+simpleSet.set); printWriter.println("The set now is "+simpleSet.set); break; } case 'E': case 'e': { printWriter.println("E"); if(simpleSet.isEmpty()) {System.out.println("The set is empty"); printWriter.println("The set is empty");} else { System.out.println("The set is not empty"); printWriter.println("The set is not empty");} break; } case 'S': case 's': { printWriter.println("S"); System.out.println("Please input the number to search"); printWriter.println("Please input the number to search"); int searchNum=keyboard.nextInt(); printWriter.println(searchNum); if(simpleSet.contains(searchNum)) {System.out.println("The number "+searchNum +" exits!"); printWriter.println("The number "+searchNum +" exits!"); } else { System.out.println("The number "+searchNum +" doesn't exist."); printWriter.println("The number "+searchNum +" doesn't exist.");} break; } case 'D': case 'd': { printWriter.println("D"); System.out.println("The size of array is "+simpleSet.totalNum); printWriter.println("The size of array is "+simpleSet.totalNum); break; } case 'A': case 'a': { printWriter.println("A"); System.out.println(simpleSet.toString()); printWriter.println(simpleSet.toString()); break; } case 'C': case 'c': { printWriter.println("C"); simpleSet.clear(); break; } case 'q': case 'Q': { printWriter.println("Q"); System.out.println("Thank you for using simple set"); printWriter.println("Thank you for using simple set"); break; } default: printWriter.println(input); System.out.println("Input is not valid. Please re-input"); printWriter.println("Input is not valid. Please re-input"); } System.out.println("Please input your option:"); printWriter.println("Please input your option:"); // menu(); input=keyboard.next().charAt(0); // printWriter.close(); } printWriter.close(); }catch(IOException e){ } } }回到主页 回到目录