#include <iostream.h> #include <conio.h> #include <stdlib.h> const MAX = 100; struct MANG { int data[MAX]; int n; }; void Khoitao(MANG &); int Is_Empty(MANG &); void Nhap(MANG &); void Xuat(MANG); int Tong_Duong(MANG); int Tong_Am(MANG); int Dem_Duong(MANG); int Dem_Am(MANG); float TBC_Am(MANG); float TBC_Duong(MANG); void TBC(MANG); int Is_SNT(int); void SNT(MANG); void swap(int &,int &); int KT_Chan(int); int Sosanh(int,int); void SapXepChan(MANG &); void SapXepTang(MANG &); void ChenX(MANG &); int Search(MANG, int, int); void Delete_Trung(MANG &); int Is_DoiXung(MANG ); void menu(); void main () { menu(); } void Khoitao(MANG &A) { A.n=0; } int Is_Full(MANG A) { if(A.n == MAX) return 1; return 0; } int Is_Empty(MANG &A) { if(0 == A.n) return 1; return 0; } void Nhap(MANG &A) { cout<<"\nNhap So Phan Tu: "; cin>>A.n; if(A.n > MAX) { A.n = MAX; cout<<"\nBan Chi Duoc Nhap "<<MAX<<" Phan Tu"; } for(int i = 0;i < A.n; i++) { cout<<"\nNhap Phan Tu Thu "<<i + 1<<": "; cin>>A.data[i]; } } void Xuat(MANG A) { cout<<"\nDu Lieu Trong Mang: "; for(int i = 0;i < A.n; i++) cout<<A.data[i]<<" "; } int Sign(int n)//Kiem Tra 1 So La Am Hay Duong { if(n > 0) return 1; if(n < 0) return -1; return 0; } int Tong_Duong(MANG A) { int tong=0; for(int i=0;i<A.n;i++) { if(Sign(A.data[i]) == 1) tong+=A.data[i]; } return tong; } int Tong_Am(MANG A) { int tong=0; for(int i=0;i<A.n;i++) { if(Sign(A.data[i]) == -1) tong+=A.data[i]; } return tong; } int Dem_Duong(MANG A) { int dem=0; for(int i=0;i<A.n;i++) { if(Sign(A.data[i]) == 1) dem++; } return dem; } int Dem_Am(MANG A) { int dem=0; for(int i=0;i<A.n;i++) { if(Sign(A.data[i]) == -1) dem++; } return dem; } float TBC_Am(MANG A) { float TBC=0; if(Dem_Am(A)!=0) TBC=float(Tong_Am(A))/Dem_Am(A); else TBC=0; return TBC; } float TBC_Duong(MANG A) { float TBC=0; if(Dem_Duong(A)!=0) TBC=float(Tong_Duong(A))/Dem_Duong(A); else TBC=0; return TBC; } void TBC(MANG A) { if(TBC_Duong(A)==0) cout<<"\nTrong Mang Khong Co So Duong"; else cout<<"\nTrung Binh Cong Cac So Duong La: "<<TBC_Duong(A); if(TBC_Am(A)==0) cout<<"\nTrong Mang Khong Co So Am"; else cout<<"\nTrung Binh Cong Cac So Am La: "<<TBC_Am(A); } int ChiaHet(int n, int i) { if(n%i == 0) return 1; return 0; } //Ham kiem tra 1 so co phai la SNT int Is_SNT(int n) { if(n<2) return 0; for(int i = 2;i < n; i++) if(ChiaHet(n,i) == 1) return 0; return 1; } //Ham xuat ra cac SNT trong MANG void SNT(MANG A) { cout<<"\nCac SNT: "; for(int i=0;i<A.n;i++) { if(Is_SNT(A.data[i]) == 1) cout<<A.data[i]<<" "; } } //Ham hoan vi 2 so a va b void swap(int &a,int &b) { int t; t = a; a = b; b = t; } //Ham kiem tra 1 so co la so chan int Is_Chan(int x) { if(x%2 == 0) return 1; return 0; } int Sosanh(int a,int b) { if(a>b) return 1; if(a==b) return 0; return -1; } void SapXepChan(MANG &A) { for(int i=0;i<A.n-1;i++) { if(Is_Chan(A.data[i]) == 1) { for(int j=i+1;j<A.n;j++) { if(Is_Chan(A.data[j]) == 1) { if(Sosanh(A.data[i],A.data[j]) == 1) { swap(A.data[i],A.data[j]); } } } } } } void Selection_Sort(MANG &A) { int i,j,pos; int min; for(i=0;i<A.n-1;i++) { min = A.data[i]; pos = i; for(j = i + 1; j < A.n; j++) { if(Sosanh(min,A.data[j]) == 1) { min = A.data[j]; pos = j; } } swap(A.data[i],A.data[pos]); } } //Ham Nay Cua Ham Xoa int Search_Pos_x(MANG &A, int x) { for(int i = 0; i < A.n; i++) { if(Sosanh(A.data[i],x) == 0) { return i; } } return A.n; } //Ham Nay Cua Ham Chen int ISearch_Pos_x(MANG &A, int x) { for(int i = 0; i < A.n; i++) { if(Sosanh(A.data[i],x) == 1) { return i; } } return A.n; } //Chen 1 So x vao Mang A o Vi Tri k void Insert_Pos_k(MANG &A, int x, int k) { if(k < 0 || k > A.n) return; if(Sosanh(k,A.n) == 0) { A.n++; A.data[A.n - 1] = x; } else { A.n++; for(int i = A.n - 1; i > k; i--) A.data[i] = A.data[i - 1]; A.data[k] = x; } } //Ham Chen x Vao Mang A, van Dam Bao Thu Tu void ChenX(MANG &A, int x) { if(Is_Full(A) == 1) { cout<<"\nMang Da Day, Khong Chen Duoc"; return; } int k = ISearch_Pos_x(A,x); Insert_Pos_k(A,x,k); } //Ham Dem x Trong Mang int Count_x(MANG A, int x) { int count = 0; for(int i = 0; i < A.n; i++) { if(Sosanh(A.data[i],x) == 0) count++; } return count; } //Ham Search x Trong Mang, tim thay return ve 1 int Search_x(MANG A, int x) { for(int i = 0; i < A.n; i++) { if(Sosanh(A.data[i],x) == 0) return 1; } return 0; } //Ham Xoa 1 So X Trong Mang A Tai Vi Tri k void Xoa_Pos_x(MANG &A, int x, int k) { //Neu Ko Co x Trong Day if(Search_x(A,x) == 0) { cout<<"\nKhong Co "<<x<<" Trong Mang"; return; } //Neu x Nam Cuoi Cung if(k == A.n - 1) { A.n--; return; } //Dich Cac Phan Tu De Xoa x for(int i = k; i < A.n - 1; i++) A.data[i] = A.data[i + 1]; A.n--; } //Xoa Het Cac So Trung Voi x Trong Mang A void Xoa_Trung_x(MANG &A, int x) { while(Count_x(A,x) > 1) { int k = Search_Pos_x(A,x); Xoa_Pos_x(A,x,k); } } //Xoa Het Cac So Trung Nhau Trong Mang A void Xoa_Trung_Mang(MANG &A) { for(int i = 0; i < A.n - 1; i++) { Xoa_Trung_x(A,A.data[i]); } } int Is_DoiXung(MANG A) { int flag=1; int i=0,j=A.n-1; while(i<j && i!=j) { if(Sosanh(A.data[i],A.data[j])!=0) { flag=0; return flag; } i++;j--; } return flag; } void DeBai() { cout<<"-----------------------------------------------"; cout<<"\n| BAITAP VAN DAP CO SO LAP TRINH - BAITAP 2 |"; cout<<"\n-----------------------------------------------\n\n"; } void menu() { MANG A; Khoitao(A); int x; char chon; do { clrscr(); DeBai(); cout<<"\nBAI TAP VAN DAP CO SO LAP TRINH - BAI SO 2"; cout<<"\n0. THOAT"; cout<<"\n1. Nhap Mang"; cout<<"\n2. Xuat Mang"; cout<<"\n3. Trung Binh Cong Cac So Am/So Duong"; cout<<"\n4. Cac So Nguyen To Trong Mang"; cout<<"\n5. Sap Xep Cac So Chan Tang Dan"; cout<<"\n6. Chen 1 So Vao Day Da Tang Va Van Giu Nguyen Tinh Chat Tang"; cout<<"\n7. Xoa Cac Phan Tu Trung Nhau"; cout<<"\n8. Kiem Tra Tinh Doi Xung Cua Day"; cout<<"\nCHON: "; chon = getch(); switch(chon) { case '0': return; case '1': Nhap(A); break; case '2': Xuat(A); getch(); break; case '3': TBC(A); getch(); break; case '4': SNT(A); getch(); break; case '5': SapXepChan(A); cout<<"\nCac So Chan Da Duoc Sap Xep Tang Dan"; Xuat(A); getch(); break; case '6': Selection_Sort(A); cout<<"\nNhap So Can Chen Vao: "; cin>>x; ChenX(A,x); cout<<"\nMang Van Dam Bao Thu Tu: "; Xuat(A); getch(); break; case '7': Xoa_Trung_Mang(A); cout<<"\nDa Xoa Cac Phan Tu Trung Nhau"; Xuat(A); getch(); break; case '8': if(Is_DoiXung(A)) cout<<"\nDay Doi Xung"; else cout<<"\nDay Khong Doi Xung"; getch(); break; default: cout<<"\nKhong Co Phim Chuc Nang Nay"; getch(); break; } }while(chon); }