BÀI TẬP CHƯƠNG 7
Bài 1:
Bài 2:
#include <iostream.h>
#include <conio.h>
char ASCII(char A[])
{
for(int i=0; A[i]!='\0'; i++)
{
if ( A[i] != ' ' )
{
cout<<"Ma ASCII cua ki tu "<<A[i]<<" la : "<<int(A[i])<<endl;
}
}
return 0;
}
int main()
{
char s[1000];
cout<<"Nhap vao mot chuoi: \t";
cin>>(s);
cout<<"\n \n";
ASCII(s);
cout<<endl;
}
#include <iostream.h>
#include <conio.h>
int main()
{
char *s;
cout<<"Nhap vao mot chuoi : \t";
int doixung(char *st)
{
int i,j;
for(i=0,j=strlen(st)-1;i<strlen(st)/2;j ,i++)
if(st[i]!=st[j])
return 0;
return 1;
}
Bài 4:
#include <iostream.h>
#include <conio.h>
int main()
{
char s[1000],kt;
int D=0;
cout<<"Nhap vao mot chuoi : ";
gets(s);
cout<<"\n \n";
cout<<"Nhap ki tu can dem : ";
cin>>kt;
for(int i=0; i<strlen(s); i++)
if(s[i]==kt)
D++;
cout<<"\n \n";
cout<<"Ki tu "<<kt<<" xuat hien "<<D<<" lan ";
cout<<"\n \n";
cout<<endl;
}
Bài 5:
#include<iostream.h>
}
for(int j=tam+1;j<l;j++)
cout<<s[j];
cout<<"\n";
for(int j=0;j<tam;j++)
cout<<s[j];
cout<<"\n";
}
Bài 6:
#include <iostream.h>
#include <conio.h>
int main()
{
char s[100];
cout<<" Nhap vao mot chuoi : ";
cin.getline(s,100);
int l=strlen(s);
cout<<"Xuat chuoi ra man hinh moi tu mot dong \n";
cout<<"\n \n";
for(int i=0;i<l;i++)
{
if (s[i]!=' ')
cout<<s[i];
else
cout<<"\n";
}
cout<<"\n";
}
Bài 7:
#include <iostream.h>
cout<<"\n";
}
Bài 9:
#include <iostream.h>
#include <conio.h>
int ktd(char s1[], int n);
int ktd(char s1[], int n)
{
int d=0;
for(int i=0;i<n;i++)
if(s1[i]==' ')
d++;
else
break;
return d;
}
int ktc(char s1[], int n);
int ktc(char s1[], int n)
{
int c=0;
for(int i=n-1; i>=0; i )
if(s1[i]==' ')
c++;
else
break;
return c;
}
int main()
{
char *s1,*s2;
for(int l=s2[dem-1]; l<n-c; l++)
if(s1[l]!=' ')
cout<<s1[l];
cout<<endl;
}
BÀI TẬP CHƯƠNG 8
Bài 1:
#include<iostream.h>
#include<conio.h>
struct nhanvien
{
unsigned int maNV;
char hoten[100];
char diachi[100];
int CBQL;
};
typedef nhanvien dsNV[100];
int nhap(dsNV nv, int n)
{
for (int i=0; i< n; i++)
{
cout<<"Nhan vien thu "<<i+1<<":\n";
cout<<"Nhap ma Nhan vien:\t";
cin>>nv[i].maNV;
cout<<"Nhap Ho va ten:\t";
cin.ignore();
cin.getline(nv[i].hoten,100);
cout<<"Nhap Dia chi:\t";
cin.getline(nv[i].diachi,100);
cout<<"Nhap 1 neu la Can bo quan ly,nguoc lai nhap 0 :\t";
xuatCBQL(nv,n);
getche();
system("cls");
return main();
}
Bài 2:
#include<iostream.h>
#include<conio.h>
struct nhanvien
{
unsigned int maNV;
char hoten[100];
char diachi[100];
int CBQL;
};
typedef nhanvien dsNV[100];
int nhap(dsNV nv, int n)
{
for (int i=0; i< n; i++)
{
cout<<"Nhan vien thu "<<i+1<<":\n";
cout<<"Nhap ma Nhan vien:\t";
cin>>nv[i].maNV;
cout<<"Nhap Ho va ten:\t";
cin.ignore();
cin.getline(nv[i].hoten,100);
cout<<"Nhap Dia chi:\t";
cin.getline(nv[i].diachi,100);
cout<<"Nhap 1 neu la Can bo quan ly,nguoc lai nhap 0 :\t";
cin>>nv[i].CBQL;
cin.getline(nv[n].hoten,100);
cout<<"Nhap Dia chi:\t";
cin.getline(nv[n].diachi,100);
cout<<"Nhap 1 neu la CQLY Nguoc lai Nhap 0 :\n";
cin>>nv[n].CBQL;
for (int i=n; i!=k-1; i )
{
tam[0]=nv[i];
nv[i]=nv[i-1];
nv[i-1]=tam[0];
}
}
int main()
{
int n,k;
dsNV nv;
cout<<"Nhap n:\t";
cin>>n;
nhap(nv,n);
cout<<"Nhap vi tri can them k:\t";
cin>>k;
chen(nv,n,k);
xuatdsNV(nv,n+1);
return main();
}
Bài 3:
#include<iostream.h>
#include<conio.h>
struct nhanvien
{
}
int xuatdsNV(dsNV nv, int n)
{
for(int i=0; i<n; i++)
xuat(nv,i);
}
int xoaCBQL(dsNV nv, int n)
{
for(int i=0; i<n; i++)
if(1 != nv[i].CBQL)
for(int j=i; j<n; j++)
nv[j]=nv[j+1];
}
int main()
{
int n,k;
dsNV nv;
cout<<"Nhap n:\t";
cin>>n;
nhap(nv,n);
xoaCBQL(nv,n);
xuatdsNV(nv,n);
return main();
}
BÀI TẬP TRƯỚC
Bài 1/33:
Bài 6/33:
//chu vi va dien tich hinh tron
#include <iostream>
int main()
//tinh chu vi va dien tich tam giac
#include <iostream>
#include <math.h>
#include <iomanip.h>
int main()
{
float a , b , c , p , s , e;
cout << "Nhap canh a= ";cin>>a;
cout << "Nhap canh b= ";cin>>b;
cout << "Nhap canh c= ";cin>>c;
p=a+b+c;
e=p/2;
s=sqrt(e*(e-a)*(e-b)*(e-c));
cout << "Chu vi: P= "<<p;
cout<<endl;
cout <<setprecision(2);
cout << "Dien tich: S= "<<s;
return 0;
}
//tinh tong hieu tich thuong 2 chu so
#include <iostream>
#include <iomanip.h>
int main()
{
float a , b , c , d , e , f;
cout << "Nhap a= ";cin>>a;
cout << "Nhap b= ";cin>>b;
c=a+b;
d=a-b;
e=a*b;
#include <math.h>
int main()
{
int n , a;
cout<<"nhap a= ";
cin>>a;
a=abs(n);
cout << "Gia tri tuyet doi cua n= "<<a;
cout<<endl;
return 0;
}
//tinh can bac hai
#include <iostream>
#include <math.h>
#include <iomanip.h>
int main()
{
float n , a;
cout << "Nhap so n= ";cin>>n;
a=sqrt(n);
cout<<setprecision(2);
cout << "Can bac hai cua n= "<<a;
cout<<endl;
return 0;
}
Bài 11/33:
Bài 12/33:
//tinh sin cos tan cot
#include <iostream>
#include <math.h>
cout << "Nhap toa do diem B:X2= ";cin>>x2;
cout << "Nhap toa do diem B:Y2= ";cin>>y2;
x=x2-x1;
y=y2-y1;
AB=sqrt(pow(x,2)+pow(y,2));
cout<<setprecision(2);
cout << "Do dai doan AB = "<<AB;
cout<<endl;
return 0;
}
Bài 13/33:
Bài 14/34:
//tim he so goc
#include <iostream>
#include <iomanip.h>
int main()
{
float x1 , y1 , x2 , y2 , k , x , y;
cout << "Nhap toa do diem A: X1= ";cin>>x1;
cout << "Nhap toa do diem A: Y1= ";cin>>y1;
cout << "Nhap toa do diem B: X2= ";cin>>x2;
cout << "Nhap toa do diem B: Y2= ";cin>>y2;
x=x2-x1;
y=y2-y1;
k=y/x;
cout<<setprecision(2);
cout << "He so goc cua duong thang di qua A va B la k= "<<k;
cout<<endl;
return 0;
}
pm";
cout<<endl;
return 0;
}
#include <iostream.h>
//#include <conio.h>
int main()
{
int d,m,y,yy;
cout<<"Nhap ngay ";cin>>d;cout<<"\n";
cout<<"Nhap thang ";cin>>m;cout<<"\n";
cout<<"Nhap nam ";cin>>y;cout<<"\n";
(d<10) ? cout<<"ngay 0"<<d : cout<<"ngay "<<d;cout<<"\n";
(m<10) ? cout<<"thang 0"<<d : cout<<"thang "<<m;cout<<"\n";
yy=y%100;
(yy<10) ? cout<<"nam 0"<<yy : cout<<"nam "<<yy;cout<<"\n";
return 0;
}
Bài 19/34:
Bài 3/55:
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
int main()
{
float t,l,h,tb;
cout<<"Nhap diem toan ";cin>>t;cout<<"\n";
cout<<"Nhap diem ly ";cin>>l;cout<<"\n";
cout<<"Nhap diem hoa ";cin>>h;cout<<"\n";
tb=(t+l+h)/3;
cout<<"Nhap so nguyen n= ";cin>>n;
c=n%2;
if (c==0)
cout<<n<<" la so chan";
else
cout<<n<<" la so le";
cout<<endl;
return 0;
}
#include <iostream.h>
int main()
{
int a , b;
cout<<"Nhap so nguyen a = ";cin>>a;
cout<<"Nhap so nguyen b = ";cin>>b;
if (a>b)
cout<<"a>b";
else
{
if ( a=b )
cout<<"a=b";
else
cout<<"a<b";
cout<<endl;
return 0;
}
}
BT chuỗi:
#include<iostream.h>
#include<iomanip.h>
xuatdongchan(A,n);
cout<<endl;
}
void nhapm2c(int A[][10], int n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
A[i][j]=rand()%100;
}
void xuatm2c(int A[][10],int n)
{
// cout<<"\n ma tran cua ban la \n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<setw(5)<<A[i][j]<<"\t";
cout<<endl;
}
}
void DCC(int A[][10], int n)
{
cout<<"\n duong cheo chinh la: \n";
for(int i=0;i<n;i++)
cout<<A[i][i]<<"\t";
}
void tongDCC(int A[][10], int n)
{
int s=0;
for(int i=0;i<n;i++)
s=s+A[i][i];
for(int j=0;j<n;j++)
if(j<i)
// s=s+A[i][j];
cout<<setw(5)<<A[i][j];
}
cout<<endl;
}
void xuatdongle(int A[][10],int n)
{
cout<<"cac phan tu trong dong le la \n";
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i%2!=0)
cout<<setw(5)<<A[i][j]<<"\t";
cout<<endl;
}
void xuatdongchan(int A[][10],int n)
{
cout<<"cac phan tu trong dong chan la \n";
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i%2==0)
cout<<setw(5)<<A[i][j]<<"\t";
cout<<endl;
}