Professional Documents
Culture Documents
int songto(int n)
{
if(n<=1)
return 0;
for(int i=2;i<n;i++)
if(n%i==0)
return 0;
return 1;
}
bool KTSNT(int x)
{
if(x<2)
return false;
for(int i=2; i<=x/2; i++)
if(x%i==0)
return false;
return true;
}
S hon thin
int sohoanthien(int n)
{
int S=0;
for(int i=1;i<n;i++)
if(n%i==0)
S+=i;
if(n==S)
return 1;
return 0;
}
S hon ho
bool SOHOANHAO(int n)
{
int tong=0;
if(n<1)
return false;
else
{
for(int i=1;i<(n/2)+1;i++)
{
if(n%i==0)
tong+=i;
}
if(tong==n)
return true;
else
return false;
}
S chnh phng
int sochinhphuong(int n)
{
int a=sqrt(n);
if(a*a==n)
{
return 1;
}
return 0;
}
i ch
void doicho(int &a, int &b)
{
int tam;
tam=a;
a=b;
b=tam;
}
Xp dng
void xepdong(int a[][100], int row, int colum)
{
for(int i=0;i<row;i++)
for(int j=0;j<colum;j++)
{
for(int k=j+1;k<colum;k++)
if(a[i][j]>a[i][k])
{
doicho(a[i][j],a[i][k]);
}
}
xuat(a,row,colum);
}
Xp ct
void xepcot(int a[][100], int row, int colum)
{
for(int j=0;j<ct;j++)
for(int i=0;i<hng;i++)
{
for(int k=i+1;k<hng;k++)
if(a[i][j]>a[i][k])
{
doicho(a[i][j],a[i][k]);
}
}
xuat(a,row,colum);
}
Tng ng cho chnh
void tongduongcheochinh(int a[][100], int hang, int cot)
{
int tdc=0;
for (int i=0;i<hang;i++)
for (int j=0;j<cot;j++)
if (i==j)
{
tdc=tdc+a[i][j];
}
cout<<"\nTong Cac So Tren Duong Cheo Chinh La :="<<tdc;
cout<<"\n";
}
Tng ng cho ph.
void duongcheophu(int a[][100],int hang,int cot)
{
int s=0;
for(int i=0;i<hang;i++)
for(int j=0;j<cot;j++)
if(j==hang-1-i)
s+=a[i][j];
if(cot==hang)
cout<<"\nTong duong cheo phu la: "<<s;
else cout<<"Khong phai mtvuong";
}
Bi xp dng, ct
#include<iostream.h>
#include<conio.h>
int a[3][3], b[3][3];
int i,j,t,k;
void xepdong()
{
cout<<"mang da sap xep theo dong la:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
for(k=j+1;k<3;k++)
if(a[i][j]>a[i][k])
{
t=a[i][j];
a[i][j]=a[i][k];
a[i][k]=t;
}
for(j=0;j<3;j++)
cout<<a[i][j]<<"\t";
cout<<endl<<endl;
}
}
void xepcot()
{
cout<<"ma tran da dc xep theo cot la :\n";
for(j=0;j<3;j++)
for(i=0;i<2;i++)
for(k=i+1;k<3;k++)
if(b[i][j]>b[k][j])
{
t=b[i][j];
b[i][j]=b[k][j];
b[k][j]=t;
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<b[i][j]<<"\t";
cout<<endl<<endl;
}
}
void main()
{
nhapmang();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=a[i][j];
xuat();
xepdong();
xepcot();
}
Tm v tr x trong mng
void timxtrongmang(int a[][100],int dong, int cot, int x) //Tim so lon nhat
{
int k,l,f;
f=0;
for(int i=0;i<d;i++)
for(int j=0;j<c;j++)
if(a[i][j]==x)
{
k=i+1;
l=j+1;
f=1;
}
if(f==1)
cout<<"X co trong mang; Vi tri cua X la: Dong "<<k<<" Cot "<<l;
else
Chui
1 m khong trng
int demkhoangtrang(char *s)
{
int d=0;
while(strstr(s," ")!=NULL)
{
d++;
s=strstr(s," ")+1;
}
return d;
}
//lam theo ham de quy
// s=strstr(s," ");
//if(s==NULL)
//return 0;
//else return 1+demkhoangtrang(s+1);
2 Xa khong trng
void xoakhoangtrang(char *s)
{
char *c=strstr(s," ");
while(c!=NULL)
{
int t=strlen(s)-strlen(c);
for(int i=t;i<strlen(s);i++)
s[i]=s[i+1];
c=strstr(s," ");
}
if(s[0]==' ')
s=s+1; //xoa khang trang dau chuoi
int n=strlen(s);
if(s[n-1]==' ')
s[n-1]='\0';//xoa khoang trang cuoi chuoi
cout<<s; //for(int i=0;i<n;i++)
//cout<<s[i];
}
3 Ni chui
void noichuoi(char *a,char *b)
{
strcat(a,b);
puts(a);
}
4 Ch thng
void chuthuong(char *a)
{
for(int i=0;i<strlen(a);i++)
if(a[i]>=65 && a[i]<=90)
a[i]=a[i]+32;
puts(a);
}
5 Ch hoa
void chuhoa(char *a)
{
for(int i=0;i<strlen(a);i++)
a[i]=toupper(a[i]); //if(a[i]>=97 && a[i]<=122)
puts(a);
//a[i]=a[i]-32;
}
6 Ch hoa u t.
void chuhoadau(char *s)
{
s[0]=toupper(s[0]);
while(strstr(s," ")!=NULL)
{
s=strstr(s," ")+1;
s[0]=toupper(s[0]);
}
}
7. Ch hoa, ch thng xem k
void chuxenke(char *a)
{
int n=strlen(a);
for(int i=0;i<n;i++)
{
if(i%2==0)
if((int)a[i]>=97 && (int)a[i]<=122)
a[i]=char((int)a[i]-32);
if(i%2==1)
if((int)a[i]>=65 && (int)a[i]<=90)
a[i]=char((int)a[i]+32);
}
puts(a);
}
8 o chui
void daochuoi(char *s)
{
puts(strrev(s));
}
if(strcmpi(s,strrev(c))==0)
return true;
else return false;
}
17 Tch s
void tachso(char *s)
{
int a[100],j=0;
for(int i=0;i<strlen(s);i++)
if(s[i]>='0' && s[i]<='9')
{
a[j]=s[i];//ki tu kieu char chuyen thanh kieu int(vd:1=>49)
j++;
for(int t=i;t<strlen(s);t++)
s[t]=s[t+1];
i--;
}
puts(s);
for(int i=0;i<j;i++)
cout<<char(a[i])<<" ";
}
18 Xa k t
void xoakitu(char *a,char b)
{
int n=strlen(a);
for(int i=0;i<n;i++)
if(a[i]==b)
{
for(int j=i;j<n;j++)
a[j]=a[j+1];
i--;
n--;
}
cout<<a;
}
19 ch hoa
void chuhoa(char *s)
{
s[0]=toupper(s[0]);
for(int i=1;i<strlen(s);i++) //chua tim dc cach nao toi uu hon
if(s[i]==' ')
{
s[i+1]=toupper(s[i+1]);
i++;
}
else
{
if(s[i]>='A' && s[i]<='Z')
s[i]=s[i]+32;
}
puts(s);
}
20 o th t
void daothutu(char *s)
{
char c[255];
c[0]='\0';//ham strcat noi chuoi tai vi tri NULL
for(int i=strlen(s)-1;i>=0;i--)
if(s[i]==' ')
{
strcat(strcat(c,s+i+1)," ");
s[i]='\0';
}
strcat(c,s);
puts(c);
}
21 Xa k t ti v tr n.
void xoa(char *s,int vt,int n)
{
strcpy(s+vt,s+vt+n);
puts(s);
}
22 chn k t.
void chen(char *s,char *d,int vt)
{
char c[255];
strcpy(c,s+vt);
strcpy(s+vt,d);
strcat(s,c);
puts(s);
}
23 Xa t.
void xoatu(char *s,char *c)
{
while(strstr(s,c)!=NULL)
{
int t=strlen(s)-strlen(strstr(s,c)),d=strlen(c);
if((s[t-1]==' '&& s[t+d]==' ')||(s[t-1]==' '&& t+d==strlen(s)))//truong
hop tu giua va tu cuoi
}
for(int i=0;i<(int)strlen(s);i++)
cout<<"ki tu "<<s[i]<<" xuat hien "<<a[i]<<" lan\n";
}
Bi chui
#include<iostream.h>
#include<string.h>
#include<iomanip.h>
struct hocsinh
{
char ten[20];
char masinhvien[10];
char namsinh[10];
int toan,ly,hoa;
float tb;
}
hs['n'];
void nhap(hocsinh hs['n'], int &n)
{
cout<<"nhap vao so hs :";
cin>>n;
cin.get();
for(int i=1;i<=n;i++)
{
cout<<"Hoc sinh thu "<<i<<" :\n";
cout<<"nhap ho va teen :";
cin.getline(hs[i].ten,20);
cout<<"Nhap ma so sinh vien :";
cin.getline(hs[i].masinhvien,10);
cout<<"Nhap nam sinh :";
cin.getline(hs[i].namsinh,10);
cout<<"Nhap diem toan:";
cin>>hs[i].toan;
cin.get();
cout<<"Nhap diem Ly :";
cin>>hs[i].ly;
cin.get();
cout<<"Nhap diem hoa :";
cin>>hs[i].hoa;
cin.get();
}
}
void diemtb( hocsinh hs['n'], int n)
{
cout<<"Diem trung binh cua cac hs la :\n";
for(int i =1; i<=n;i++)
{
hs[i].tb = float(hs[i].toan+hs[i].hoa+hs[i].ly)/3;
cout<<"hoc sinh "<<hs[i].ten<<" co diem trung binh la:"<<hs[i].tb<<endl;
}
}
void xuat(hocsinh hs['n'], int n)
{
for(int i=1;i<=n;i++)
cout<<"\nHo Va Ten: "<<hs[i].ten<<"\nDiem Toan: "<<hs[i].toan<<"\nDiem ly:
"<<hs[i].ly<<"\nDiem hoa: "<<hs[i].hoa<<"\nDiem TB: "<<hs[i].tb;
}
void main()
{
int n;
nhap(hs,n);
diemtb(hs,n);
xuat(hs,n);
}
Con tr
Bi 1
#include<iostream.h>
#include<conio.h>
void nhap( int *a, int n)
{
for(int i=0;i<n;i++)
{
cout<<"nhap vao phan tu thu "<<i<<":";
cin>>a[i];
}
}
void xuat (int *a, int n)
{
cout<<"mang da nhap la: ";
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
}
void tong(int *a, int n)
{
int dem1=0;
int dem2=0;
for(int i=0;i<n;i++)
{
if(i%2==0)
{
dem1=dem1+a[i];
}
else
dem2=dem2+a[i];
}
cout<<"\n tong chan la:"<<dem1;
cout<<"\n tong le la:"<<dem2<<"\n";
}
int ktsnt(int x)
{
if(x<=1)
return 0;
for(int i=2;i<x;i++)
{
if(x%i==0)
return 0;
}
return 1;
}
void tongngto(int *a, int n)
{
int tong=0;
for(int i=0;i<n;i++)
{
if(ktsnt(a[i])==1)
{
tong=tong+a[i];
}
}
cout<<"\n tong so ngto la: "<<tong<<"\n";
}
void sxchan( int *a, int n)
{
for(int i=0;i<n;i++)
{
if(a[i]%2==0)
{
for(int j=i+1;j<=n;j++)
{
if(a[j]%2==0)
{
if(a[j]<a[j])
{
int t =a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
}
cout<< a[i]<<"\t";
}
void main()
{
int *a;
int n;
a= new int[n];
cout<<"nhap n: ";
cin>>n;
nhap(a,n);
xuat(a,n);
tong(a,n);
tongngto(a,n);
sxchan(a,n);
getch();
Bi 2.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void nhap(int **A, int row, int colum)
{
for(int i=0;i<row;i++)
{
A[i]=new int [colum];
for(int j=0;j<colum;j++)
{
cout<<"Nhap ["<<i<<","<<j<<"]= ";
cin>>A[i][j];
}
}
}
void xuat(int **A, int row, int colum)
{
for(int i=0;i<row;i++)
{
for(int j=0;j<colum;j++)
cout<<A[i][j]<<"\t";
cout<<"\n"<<"\n";
}
}
void congmang(int **A, int **B, int row, int colum)
{
for(int i=0;i<row;i++)
for(int j=0;j<colum;j++)
{
A[i][j]= A[i][j]+B[i][j];
}
xuat(A,row,colum);
}
void doicho(int &a,int &b)
{
int tam;
tam=a;
a=b;
b=tam;
}
void xepdong(int **A, int row, int colum)
{
for(int i=0;i<row;i++)
for(int j=0;j<colum; j++)
{
for(int k=j+1;k<colum;k++)
if(A[i][j]<A[i][k])
{
doicho(A[i][j],A[i][k]);
}
}
xuat(A,row,colum);
}
void tongbien(int **A, int row, int colum)
{
cout<<"Tong bien se l:\n";
int T=0;
for(int i=0;i<row;i++)
for(int j=0; j<colum; j++)
{
if(i==0||i==row-1||j==0||j==colum-1)
{
T+=A[i][j];
}
}
cout<<T;
}
int sohoanthien(int &n)
{
int tong=0;
for(int i=1;i<n; i++)
if(n%i==0)
tong=tong+i;
if( tong==n)
return 1;
return 0;
}
void tongsohoanthien(int **A, int row, int colum)
{
int HT=0;
for( int i=0;i<row; i++)
for( int j=0;j<colum;j++)
{
if(sohoanthien(A[i][j])==1)
{
HT += A[i][j];
}
}
cout<<HT;
}
int songto(int n)
{
if(n<=1)
return 0;
for(int i=2;i<n;i++)
if(n%i==0)
return 0;
return 1;
}
void tongsongto(int **A, int row, int colum)
{
int tong=0;
for(int i=0;i<row;i++)
for( int j=0;j<colum; j++)
{
if (songto(A[i][j])==1)
{
tong+=A[i][j];
}
}
cout<<tong;
}
int amdautien(int **A, int row, int colum)
{
for(int i=0;i<row;i++)
{
for(int j= 0;j<colum;j++)
if(A[i][j]<0)
{
return A[i][j];
}
}
return 0;
}
void amlonnhat(int **A, int row,int colum)
{
cout<<"\ngia tri am max la :";
int ammax= amdautien(A,1,colum);
for(int i=0;i<row;i++)
for(int j=0;j<colum;j++)
if(A[i][j]<0)
{
if(A[i][j]>ammax)
{
ammax=A[i][j];
}
}
cout<<ammax;
}
void main()
{
int **A, **B, row, colum;