//test full public boolean isFull(){ if(size==elements.length) return true; return false; }
//Return the number at the top of the stack public int peak(){ if(isEmpty()) return -99999; return elements[size]; }
//Push a number into the stack public void push(int value){ if(isFull()){ System.out.println("The stack is FULL!"); return; } elements[++size]=value; }
//Pop and return the number at the top of the stack public int pop(){ if(isEmpty()){ System.out.println("The stack is EMPTY"); return -99999; } size--; return elements[size+1]; }
//Get the size of the stack public int getSize(){ return size; } }
import java.util.Scanner; public class TestBai2 { public static boolean isNT(int number){ if(number<2) return false; if(number==2) return true; for(int i=2;i<=Math.sqrt(number);i++) if(number%i==0) return false;
return true; }
public static void main (String [] args){ //Nhap so n System.out.print("Nhap n = "); Scanner input = new Scanner(System.in); int n = input.nextInt();
//Dua cac so nguyen to <n vao stack theo thu tu tang dan StackOfIntegers stack= new StackOfIntegers(1000); for(int i=2;i<n;i++) if(isNT(i)) stack.push(i); while(!stack.isEmpty()) System.out.println(stack.pop());
} }
import java.util.Scanner;
/** * * @author NGOCCHIEN2 */ public class TestBai3 { public static boolean isNT(int number){ if(number<2) return false; if(number==2) return true; for(int i=2;i<=Math.sqrt(number);i++) if(number%i==0) return false;
return true; }
public static void main (String [] args){ //Nhap so n System.out.print("Nhap n = "); Scanner input = new Scanner(System.in); int n = input.nextInt();
//Dua cac so nguyen to <n vao stack theo thu tu tang dan StackOfIntegers stack= new StackOfIntegers(1000); for(int i=2;i<=n;i++) while(isNT(i)&& n%i==0){ stack.push(i); n/=i; } while(!stack.isEmpty()) System.out.println(stack.pop());
} }
Bai45 public class StackOfChars { private int size; private char[] elements;
//constructor 0-arg public StackOfChars(){
elements = new char[16]; }
//constructor 1-arg public StackOfChars(int capacity){ elements= new char[capacity]; }
//test full public boolean isFull(){ if(size==elements.length) return true; return false; }
//Return the number at the top of the stack public char peak(){ if(isEmpty()) return '@'; return elements[size]; }
//Push a number into the stack public void push(char ch){ if(isFull()){ System.out.println("The stack is FULL!"); return; } elements[++size]=ch; }
//Pop and return the number at the top of the stack public char pop(){ if(isEmpty()){ System.out.println("The stack is EMPTY"); return '@'; } size--; return elements[size+1]; }
//Get the size of the stack public int getSize(){ return size; } }
import BaiLam.Bai23.StackOfIntegers; import java.util.Scanner; public class TestBai45 { public static void inFixToPostFix( char [] sourceChars, char [] targetChars){ int count=0; StackOfChars stack = new StackOfChars(1000); for(int i=0;i<=sourceChars.length-1;i++){ //Stack chi chua toan tu +-*/
switch(sourceChars[i]){ //Neu la dau '(' thi bo qua case '(':{ break; } //Neu la cac toan tu + - * / thi dua vao stack case '+':{ stack.push(sourceChars[i]); break; } case '-':{ stack.push(sourceChars[i]); break; } case 'x':{ stack.push(sourceChars[i]); break; } case '/':{ stack.push(sourceChars[i]); break; } //Neu la dau ')' thi lay (pop) toan tu + - * / ra khoi Stack va // dua vao bieu thuc moi case ')':{
targetChars[count++]=stack.pop(); break; } //Neu la toan hang thi dua vao bieu thuc moi' default:{ targetChars[count++]=sourceChars[i]; } } /** * CACH 2 : DUNG IF-ELSE //Neu la dau '(' thi bo qua if(sourceChars[i]=='('){ continue; }else if(sourceChars[i]=='+' ||sourceChars[i]=='-' || sourceChars[i]=='x' ||sourceChars[i]=='/'){ stack.push(sourceChars[i]);//Neu la cac toan tu + - * / thi dua vao stack }else if(sourceChars[i]==')'){ //Neu la dau ')' thi lay (pop) toan tu + - * / ra khoi Stack va // dua vao bieu thuc moi targetChars[count++]=stack.pop(); }else{ //Neu la toan hang thi dua vao bieu thuc moi' targetChars[count++]=sourceChars[i]; } */
} }
public static int calculate(char [] postFixChars){ StackOfIntegers stack = new StackOfIntegers(1000);
for(int i=0;i<=postFixChars.length-1;i++){ switch (postFixChars[i]){ case '+':{ int temp= stack.pop()+stack.pop(); stack.push(temp); break; } case '-':{ int temp= -stack.pop()+stack.pop(); // So dung sau se tru truoc stack.push(temp); break;
} case 'x':{ int temp= stack.pop()*stack.pop(); stack.push(temp); break; } case '/':{ int temp= stack.pop()/stack.pop(); stack.push(temp); break; }
default:{ String str= Character.toString(postFixChars[i]); stack.push(Integer.parseInt(str)); } } } return stack.peak(); } public static void main (String [] args){
//Nhap bieu thuc dang trung to (infix) //System.out.print("Nhap bieu thuc dang trung to = "); //Scanner input = new Scanner(System.in); //String inputString =input.nextLine(); String inputString ="((3+7)x(9-(6-2)))"; String inputString1 ="(3x(((5-2)x(7+1))-6))"; char [] source = inputString1.toCharArray(); int count=0; for(int i=0;i<source.length;i++){ if(source[i]=='(' || source[i]==')') count++; }
char [] target = new char[source.length-count]; inFixToPostFix(source, target); System.out.print("Bieu thuc hau to la: "); for(char index : target){ System.out.print(index); } System.out.println("\nGia tri = "+calculate(target)); } }
Bai 6: public class MyPoint { private double x; private double y;
//constructor 0-arg public MyPoint(){
}
//constructor 2-args public MyPoint(double x, double y){ this.x=x; this.y=y; }
//copying constructor public MyPoint(MyPoint p){ this(p.x,p.y); }
//Getters public double getX(){ return x; }
public double getY(){ return y; }
public double distance (MyPoint secondPoint){ return Math.sqrt(Math.pow(secondPoint.x-this.x, 2)+Math.pow(secondPoint.y-this.y, 2)); }
/** * * @author NGOCCHIEN2 */ public class TestBai6 { public static int n; public static Scanner input; public static double maxDistance=0; public static int vt1,vt2; public static void nhapN(){ input = new Scanner(System.in); System.out.println("Nhap n= "); n= input.nextInt();
}
public static MyPoint nhapDiem( int count){
System.out.println("Nhap diem thu "+count+":"); System.out.print("Nhap x= "); double tempX=input.nextDouble(); System.out.print("Nhap y= "); double tempY=input.nextDouble(); return new MyPoint(tempX,tempY); }
public static void main (String [] args){ nhapN();
//In ra ket qua System.out.println("Toa do hai diem co khoang cach lon nhat la:"); System.out.printf("Diem thu nhat :(%4.2f %4.2f) \n",arrayPoint[vt1].getX(),arrayPoint[vt1].getY()); System.out.printf("Diem thu hai :(%4.2f %4.2f) \n",arrayPoint[vt2].getX(),arrayPoint[vt2].getY()); System.out.printf("Khoang cach = %4.2f \n", maxDistance); } }
Bai 7:
import java.util.Scanner;
/** * * @author NGOCCHIEN2 */ public class Matrix { private float [][]a;
//constructor 0-arg public Matrix(){
}
//constructor 2-args public Matrix(int rows, int cols){ a= new float [rows][cols]; }
public Matrix transpose(){ Matrix res = new Matrix (a[0].length,a.length); for(int i=0;i<a[0].length;i++){ for(int j=0;j<a.length;j++){ res.a[i][j]=this.a[j][i]; } } return res; }
public Matrix mul(Matrix m){ Matrix res = new Matrix (this.a.length,m.a[0].length); // Hang cua ma tran dau va cot ma tran thu 2 for(int i=0;i<this.a.length;i++){ for(int j=0;j<m.a[0].length;j++){ res.a[i][j]=0; for(int k=0;k<this.a[0].length;k++){
/** * * @author NGOCCHIEN2 */ public class TestBai7 { public static void main (String [] args){ Matrix a= new Matrix (2,3); Matrix b= new Matrix (3,2); Scanner in = new Scanner(System.in); a.input(in); b.input(in);
if(a.add(b) != null){ // Kiem tra 2 ma tran hop le hay khong a.add(b).print(); a.sub(b).print(); } else{ System.out.println("Hai ma tran khong hop le de cong /tru"); }
a.mul(b).print();
} }
Bai 8: public class PhanSo { private int ts; private int ms;
//constructor 0-arg public PhanSo(){
}
//constructor 2-args public PhanSo(int ts, int ms){ this.ts=ts; this.ms=ms; }
//Cong 2 phan so public PhanSo cong(PhanSo sp2){ return new PhanSo(this.ts*sp2.ms+sp2.ts*this.ms,this.ms*sp2.ms).toiGian(); }
//Tru 2 phan so public PhanSo tru(PhanSo ps2){ return new PhanSo(this.ts*ps2.ms-ps2.ts*this.ms,this.ms*ps2.ms).toiGian(); }
//Nhan 2 phan so public PhanSo nhan(PhanSo ps2){ return new PhanSo(this.ts*ps2.ts,this.ms*ps2.ms).toiGian(); }
//Chia 2 phan so public PhanSo chia(PhanSo ps2){ return new PhanSo(this.ts*ps2.ms,this.ms*ps2.ts).toiGian(); }
//Nghich dao public PhanSo nghichDao(){ return new PhanSo(this.ms, this.ts); }
//Doi dau public PhanSo doiDau(){ return new PhanSo(-this.ts,this.ms); }
//Toi gian phan so public PhanSo toiGian(){ int max=1; for(int i=2;i<=ts;i++){ if(ts%i==0 && ms%i==0) max=i; } if(ms>0){ return new PhanSo(ts/max,ms/max); }else{ // Mau so <0 suy ra ca ts va ms se doi dau de trinh bay // vi du -a/-b = a/b // vi du a/-b = -a/b return new PhanSo(-this.ts/max,-this.ms/max); }
}
//So sanh bang public boolean bangNhau(PhanSo ps2){ PhanSo kq= this.chia(ps2).toiGian(); if(kq.ts==1 && kq.ms==1){ return true; } return false; }
//So sanh lon hon public boolean lonHon(PhanSo ps2){ if(this.bangNhau(ps2)==false && this.nhoHon(ps2)==false) return true; return false; }
//So sanh nho hon public boolean nhoHon(PhanSo ps2){ int tsThis=this.toiGian().ts* ps2.toiGian().ms; // Quy dong phan so This int tsPs2 =ps2.toiGian().ts*this.toiGian().ms; //Quy dong phan so Ps2 if(tsThis<tsPs2) return true; return false;
} public void hien(){ this.toiGian(); //Do ham toiGian da loai bo cac dau (-) du thua` //nen ham hien chi con` lai :
//So sanh if(ps1.bangNhau(ps2)) System.out.println("\nHai phan so bang nhau."); else if(ps1.nhoHon(ps2)) System.out.println("\nPhan so 1 nho hon ."); else System.out.println("\nPhan so 1 lon hon .");
} }
Bai 9: public class SoPhuc { private float a; private float b;