You are on page 1of 17

BI TP NGN NG LP TRNH

H tn:L Vn Nhm
MSSV:20101955













II.Cc thnh phn c bn
Bi 1.To project PhuongTrinhBac2 trn Visual Studio .NET v kim tra vi cc h s:
a = 1, b = 2, c = 1
a = 1, b = 2, c = 3
a = 1, b = 3, c = 2
Ti u on m gii phng trnh bc 2

Code:
#include<iostream>
#include<math.h>
using namespace std;
void main()
{
double a,b,c;
cout<<"Nhap a=0 de ket thuc.";
while(1)
{

cout<<"\n\nNhap cac he so:\na="; cin>>a;
cout<<"b="; cin>>b;
cout<<"c="; cin>>c;
if(a==0)
{
cout<<"Nhap sai";
break;
}
double d=b*b-4*a*c;
a*=2;
b=-b;
if(d<0) cout<<"Phuong trinh vo nghiem.";
if(d==0) cout<<"Phuong trinh co nghiem kep:x1=x2="<<b/a;
if(d>0)
{
d=sqrt(d);
cout<<"Phuong trinh co 2 nghiem thuc phan
biet:\nx1="<<(b-d)/a
<<"\tx2="<<(b+d)/a;
}
}
cout<<endl;
system("pause");
}
III.Biu thc v ton t
Bi 1.Vit chng trnh in ra mn hnh cc gi tr t 1 n k di dng ma trn mxn vi k = mxn,
m v n c khi to trong hm main().
Code:
#include<iostream>
using namespace std;
void main()
{
int m=3,n=4;
int k=m*n;
for(int i=1;i<=k;i++)
{
cout<<i<<"\t";
if(i%n==0) cout<<"\n\n";
}
system("pause");
}

Bi 2.Vit chng trnh xc nh dng tam gic c cho bi 3 cnh a, b, c lcc s thc. Yu
cu:
Nhp 3 cnh ca tam gic
In ra mn hnh kiu ca tam gic:
Tam giac thuong
Tam giac vuong
Tam giac can
Tam giac vuong can
Tam giac deu
Chng trnh ch kt thc khi a, b, c khng to thnh tam gic
Code:
#include<iostream>
using namespace std;
void main()
{
float a,b,c;
while(1)
{
cout<<"Nhap 3 canh a,b,c cua tam giac:\n";
cin>>a>>b>>c;
int m=(a<(b+c))&(b<(c+a))&(c<(a+b));
if(m==0)
{
cout<<"a,b,c khong la 3 canh cua 1 tam giac"<<endl;
break;
}
//cout<<m;
int canh=(b==c)|((a==c)<<1)|((a==b)<<2);
a*=a; b*=b; c*=c;
int goc=(a==b+c)|((b==a+c)<<1)|((c==a+b)<<2);
cout<<"Tam giac ";
switch((goc<<4)|canh)
{
case 0: cout<<"thuong";break;
case 7: cout<<"deu";break;
default:
if(goc!=0) cout<<"vuong";
if(canh!=0)cout<<"can";
}
cout<<endl;
}
system("pause");
}

Bi 3.Vit chng trnh in ra mn lch ca nm nay theo mu ca nm 2000
Code:
#include<iostream>
using namespace std;
void main()
{
int ngay,thang,nam;
while(1)
{
system("cls");
cout<<"Nam "; cin>>nam;
if(nam<2000) continue;
cout<<"\n";
int t2,t;
if((nam&3)==0) t2=29;
else t2=28;
//(nam%4)==0?t2=29:28;*/
int i=0;
/*for(int j=2001;j<=nam;j++)
{
if(((j-1)&3)==0) i++;
}*/
//if((nam-1)%4==0)
if(nam>2000) i=(int)(nam-2001)/4+1;
t=((nam-2000)*365+i+6)%7;
for(thang=1;thang<=12;thang++)
{
cout<<"Thang "<<thang
<<"\nCN\tT2\tT3\tT4\tT5\tT6\tT7\n";
int day;
switch(thang)
{
case 2: day=t2; break;
case 4: case 6: case 9: case 11:day=30; break;
default: day=31;break;
}
for(ngay=1;ngay<=(day+t);ngay++)
{
if(ngay<=t) cout<<"\t";
else
cout<<ngay-t<<"\t";
if((ngay%7)==0) cout<<"\n";
}
t=(day+t)%7;
cout<<"\n\n";
}
cout<<"Nhan 0->enter de ket thuc \nNhan cac phim bat ky khac ->enter de tiep
tuc\n";
char m; cin>>m;
if(m=='0') break;
else continue;
//system("pause");
}
}
IV.Hm,mng v con tr
Bi 1.Thit k v ci t chng trnh thc hin cc thao tc sau:
Khai bo mt mng cha 1000 s nguyn
t gi tr ngu nhin t 1 .. 1000 cho cc phn t ca mng (dng hm rand())
m s lng s chn, s l v s chia ht cho 8 ca mn

Code:
#include<iostream>
using namespace std;
void main()
{
int a[1000],d2=0,d8=0,i;
for(i=0;i<1000;i++)
a[i]=rand()%1000+1;
for(i=0;i<1000;i++)
{
if((a[i]&1)==0) d2++;
if((a[i]&7)==0) d8++;
}
int m=6&3;
cout<<m<<endl;
cout<<"So so chan la: "<<d2<<endl
<<"So so le la: "<<1000-d2<<endl
<<"So so chia het cho 8 la: "<<d8<<endl;
//getch();
system("pause");
}
b.Cp pht b nh ng
code:
#include<iostream>
using namespace std;
void main()
{
int n;
int d2=0,d8=0;
cout<<"Nhap so phan tu: "; cin>>n;
int *a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=rand()%1000+1;
//cout<<"a["<<i<<"]="<<a[i]<<endl;
if((a[i]&1)==0) d2++;
if((a[i]&7)==0) d8++;
}
cout<<"so so chan la: "<<d2
<<"\nso so le la: "<<n-d2
<<"\nso so chia het cho 8 la: "<<d8<<endl;
delete[]a;
system("pause");
}

Bi 2.Vit chng trnh thc hin cc thao tc sau:
Nhp gi tr cho xu k t s (cha c nhiu nht 49 k t khc NULL)
In s ra mn hnh vi cc ch ci thng (VD. s = 123ABC 123abc)
i s thnh s nguyn v gn cho bin a (VD. s = 123abc a = 123)

Code:
a.Duyt cc k t bng mng
#include<iostream>
#include<string.h>
using namespace std;
void main()
{
char a[50];
cout<<"Nhap xau: ";
cin.getline(a,50);
int b=0;
for(int i=0;a[i]!=0;i++)
{
char c=a[i];
if((c>='A')&&(c<='Z')) c+=32;
cout<<c;
if(c>='0'&&c<='9')
b=b*10+c-48;

}
cout<<"\n";
cout<<"a="<<b<<endl;
system("pause");
}
b.Duyt cc k t bng con tr
#include<iostream>
#include<string.h>
using namespace std;
void main()
{
char a[50],*p=a;
cout<<"Nhap xau: ";
cin.getline(a,50);
int b=0;
for(int i=0;*(a+i)!=0;i++)
{
char c=*(a+i);
if((c>='A')&&(c<='Z')) c+=32;
cout<<c;
if(c>='0'&&c<='9')
b=b*10+c-48;
}
cout<<"\n";
cout<<"a="<<b<<endl;
system("pause");
}


Bi 3. Debug on biu thc sau:
char s[100] = "1234567890"; short *p = (short *)s;
*(p += 2) = 0x41; cout << s;
Kt qu Debug l: 1234APress any key to continue . . .

Bi 4.Ci t hm gii phng trnh bc 2 sau:
Iunttun FindRoot o, b, c ; var x1, x2
J b
2
4oc
tI J < 0 then return 0
tI J = 0 then x1 x2
b
2u
; return 1
x1 (b J)/2o; x2 (b + J)/2o
return 2

Code:
#include<iostream>
#include<math.h>
using namespace std;
int FindRoot(double a,double b,double c,double* x1,double* x2)
{
double d=b*b-4*a*c;
int f;
a=2*a;
if(d<0) f=0;
if(d==0){
*x1=*x2=-b/a;
f=1;
}
if(d>0){
*x1=(-b-sqrt(d))/a;
*x2=(-b+sqrt(d))/a;
f=2;
}
return f;
}
void main()
{
double a,b,c,x1,x2;
cout<<"a="; cin>>a;
cout<<"b="; cin>>b;
cout<<"c="; cin>>c;
switch(FindRoot(a,b,c,&x1,&x2))
{
case 0:cout<<"Phuong trinh vo nghiem!"; break;
case 1:cout<<"Phuong trinh co nghiem kep: x1=x2="<<x1;break;
default:cout<<"Phuong trinh co 2 nghiem thuc phan biet:\nx1="<<x1
<<"\nx2="<<x2;break;
}
cout<<endl;
system("pause");
}





Bi 5.Thit k v ci t hm tr v xu k t theo chun h tn t xu u vo.


Code:
#include<iostream>
using namespace std;
char *Correct(char *s)
{
int i=0,j=0;
char last=32;
while(char c=s[i++])
{
if(last==32)
{
if(c==32) continue;
if(c>='a'&&c<='z') c-=32;
}
else
{
if(c>='A'&&c<='Z') c+=32;
}
s[j++]=last=c;
}
j=j-1;
while(s[j++]) s[j]=0;
return s;
}
void main()
{
char s[100];
cout<<"Nhap ten: ";
cin.getline(s,100);
cout<<"Ten dung: "<<Correct(s)<<endl;
system("pause");
}
Bi 6. Ci t hm sau:
Iunttun IsSorted o Array 1. . I ; t *0, 1+ 0, 1
Iur i 1 tu n 1 du
tI t = 1 o i > o i + 1 hen return 0
tI t = 0 o i < o i + 1 hen return 0
return
Coue:
#include<iostream>
using namespace std;
int IsSorted(int n,int a[],int t)
{
int f=0;
for(int i=1;i<=n-1;i++)
{
if((t==1)&&(a[i]>a[i+1])) continue;
if((t==0)&&(a[i]<a[i+1])) continue;
f=1;
}
return f;
}
void main()
{
int a[100],t,n;
cout<<"so phan tu: "; cin>>n;
cout<<"t="; cin>>t;
for(int i=1;i<=n;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
if(IsSorted(n,a,t)==0)
{
cout<<"Mang da duoc sap xep ";
if(t==1) cout<<"giam dan.\n";
if(t==0) cout<<"tang dan.\n";
}
else cout<<"mang chua duoc xap xep.\n";
system("pause");
}
Bi 7. Ci t cc hm sp xp v tim kim
a.sp xp chen:
void InsertionSort(int a[], int n)
{
int i,j,last;
for (i=1; i<=n; i++)
{
last = a[i];
j = i-1;
while (j>=0 && last<a[j])
{
a[j+1] = a[j];
j--;
}
a[j+1] = last;
}
}

b.sp xp la chn
void SelectionSort(int a[], int n)
{
int i,min,j,temp;
for (i=0; i<=n-2; i++)
{
min = i;
for (j=i+1; j<=n; j++)
if (a[j] < a[min])
{
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}

c.Sp xp ni bt
void BubbleSort(int a[], int n)
{
int i,j,k,temp;
for (i=0; i<n-1; i++)
for (j=i+1; j<n; j++)
if (a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
} }

You might also like