Professional Documents
Culture Documents
Đề Cương Bài GiangLTCB
Đề Cương Bài GiangLTCB
- H iu hnh MSDOS,
7. Cu hi n tp
- Hy thc hnh thnh tho vic chuyn i cc h m.
- Ti sao phi s dng bng m ASCII m rng?
CNG BI GING
1. Thng tin chung
Mn hc: Lp trnh c bn
B mn:
CNG BI GING
1. Thng tin chung
Mn hc: Lp trnh c bn
B mn:
CNG BI GING
1. Thng tin chung
Mn hc: Lp trnh c bn
B mn:
unsigned,
while,
void,
asm,
1.3. Hng,bin.
- Hng s l thng tin c gi tr khng i trong qu
trnh thc hin chng trnh:
+ Hng s hc: S nguyn v s thc
+ Hng s nguyn c th biu din vi cc c s khc
nhau 8,10,16
+ -14, +14, 0114(c s 8 ng vi 76 trong c s 10)
+ Hng s thc vit di dng s thp phn thng thng
hoc di dng khoa hc.
+ Hng k t: Gm cc k t vit trong du nhy n.
a,b,
+ Hng kiu chui: Gm mt dy cc k t vit trong du
nhy n. abcxd,dbhk,
- Khai bo hng. const <kiu d liu> <tn hng>= <gi
tr>;
1.4.Bin.
- Thng tin c gi tr thay i trong qu trnh thc hin
chng trnh gi l bin.
- Bin c nhiu kiu, tn bin bt u bng ch ci, vit
lin nhau nu vit cch phi c du ni di.
- Tn bin khng c trng t kho.
nhiu
rng
cho
ngun:
char(-
2.2E308)
long
double(-3.4E-4932 n 3.4E4932);
+ V d: float x;
x=333.45; hoc
float x=333.45;
c/ Kiu k t: char
+ Khai bo: char <tn bin>;
+ Hng k t c c biu din nm gia 2 du nhy
n:a,b,
+ Trong bng m ASCII cc k t t 0-31 l cc k t
iu khin khng in ra c, bng sau cho ta s tng ng
gia k t v dy m
K t
BEL
Nhy cch
Dy m
\a
\t
K t
Xo tri
Nhy cch
Dy m
\b
\v
ngang
Xung dng
\n
ng
Xung di FF
\f
miLF
V u dng
\r
Du
\*
\
\\
Du ?
M nul
\?
\0
CR
Du
Du\
V
1
U&&V
1
U||V
1
!U
0
10
1
0
0
0
0
1
0
0
0
1
0
1
0
1
1
CNG BI GING
1. Thng tin chung
Mn hc: Lp trnh c bn
B mn:
11
b/ Logic (BOOLEAN)
- Gm : Hoc(||),v (&&),ph nh(!)
- Kt qu l gi tr cc bng chn l
U
1
1
0
0
V
1
0
0
1
||(OR)
1
1
0
1
&&(AND)
1
0
0
0
(NOT)
0
0
1
1
d/ Tng gim mt n v.
- ++ tng ln 1, (++a) tng ng a+=1
- -- Gim i 1
e/ p kiu:
- C/C++ cho php trn ln cc kiu d liu vi nhau,
nhng ng thi mi kiu d liu phn ng khac nhau i
vi mi php ton. Do di khi ta p cho my i kiu
d liu t kt qu mong mun
- V d:
13
int i,m,n;
float x;
x=m/n /* Kt qu l phn nguyn*/
x=m/ float(n) /* n b p thnh s thc nn Kt qu l
s thc*/
ng chuyn v int
2
hng t sau thnh kiu double.
y=7;
z=123.44;
cout<<(int)(z/3+10*y);
cout<<z/3+10*y;
}
Kiu
int
double
Kt qu
Cho tr tuyt i ca i
Cho tr tuyt i ca
double
s thc d
Cho tr gia tr
exp,log,sqrt,floor,ceil
sin,cos,cosh,tan,e
m,loga,cn,ct trn
pow(d1,d2),fmod(d1,d2)
double
s,hm lm trn s
d1 m d2, ly d1/d2
d/ Cc v d minh ha
V d 3: Minh ha cc hm
# include <iostream.h>
#include <conio.h>
void main()
{
int i,m,n;
float x;
m=13; n=4;
cout<< ket qua cua cac phep chia m/n<<endl;
x=m/n;
cout<<x<<
x=m/float(n);
cout<< ket qua chinh xac phep chia
m/n<<x<<endl;
}
15
V d 4:
# include <iostream.h>
#include <conio.h>
void main()
{
float x;
x=2.3+1.7;/* x=4*/
cout<<Gia tri x=%d\n<<x;
x=int(2.3)+int(1.7);/* x=3*/
cout<<Gia tri moi cua x=%d\n<<x;
}
void main()
{
int x1,x2
x2=x1=100;(php gn kp);
cout<< gia tri cua x1 la <<x1<<x2;
}
- S hiu chnh s c thc hin theo chiu:
int
long
float
double
long double
#include <stdio.h>
V d 6:
#include <stdio.h>
#include <iostream.h>
main()
{
cout(
phan
nguyen
trong
phep
chia
1770/25
la
%d\n,1770/25);
}
V d 7: Chuyn n v thi gian c a vo bng giy
thnh gi pht giy
#include <stdio.h>
main()
{
long int giay,phut,gio;
cout<< Nhap thoi gian bang giay:\n;
cin>>giay;
gio=giay/3600; /* chuyn giy thnh gi*/
phut= (giay-3600*gio)/60;/* chuyn giy thnh pht */
giay= giay-3600*gio-60*phut/* phn giy cn li */
cout<< Doi giay ra gio phut giay la\n;
cout<<giay<< giay <<phut<< phut <<gio<< gio
getch();
return 0;
}
6. Ti liu tham kho
- Quch Tun Ngc, Ngn ng lp trnh C++ trang 82-99
- Bi ging ca gio vin
- Dng T Cng, Ngn ng lp trnh C, trang 15-25
7. Cu hi n tp
18
CNG BI GING
1. Thng tin chung
Mn hc: Lp trnh c bn
B mn:
19
}.
20
{
float a,b,c,chuvi,dientich;
printf("nhap gia tri a,b,c:\n");
cin>>a>>b>>c;
if ((a>=b+c)||(b>=a+c)||(c>=b+a))
printf("a,b,c khong la canh cua tam giac\n");
else
{
chuvi=a+b+c;
dientich=chuvi/2*(chuvi/2-a)*(chuvi/2-b)*(chuvi/2-c);
dientich=sqrt(dientich);
printf("Chu vi %f \n",chuvi);
printf("Dien tich %f \n",dientich);
}
getch();
}
V d 10: Tnh tin in phi tr hng thng theo quy
nh sau:
+ n 50 kw phi tr 50 ng
+ Ln hn 50 v <=100 tr 70 ng
+ Ln hn 100 v <=200 tr 100 ng
+ Ln hn 200 tr 150.5 ng
#include <stdio.h>
#include <math.h>
#include <conio.h>
main()
{
int tieuthu;
float tiendien;
printf(nhap luong dien tieu thu\n);
22
cin>>tieuthu;
if(tieuthu<=50)
tiendien=tieuthu*50;
else
if (tieuthu<=100)
tiendien=50*50+(tieuthu-50)*70;
else
if (tieuthu<=200)
tiendien=50*50+50*70+(tieuthu-100)*100;
else
tiendien=50*50+50*70+100*100+(tieuthu-200)*150.5;
printf(So tien phai tra la:%f\n,tiendien);
}
V d 11: Gii phng trnh bc 2 vi a, b, c c nhp
vo t bn phm.
//#include <stdio.h>
# include <iostream.h>
#include <math.h>
#include <conio.h>
main()
{
float a,b,c,delta,x1,x2;
cout<<" Nhap vao gia tri a b c:
";
cin>>a>>b>>c;
delta=b*b-4*a*c;
if (delta > 0)
{
cout<<"Nghiem x1 ="<<(-b+sqrt(delta))/(2*a)<<endl;
cout<<"Nghiem x2 ="<<(-b-sqrt(delta))/(2*a)<<endl;
}
23
else
if (delta==0)
cout<<"Nghiem x1=x2 ="<<-b/(2*a)<<endl;
else
{
cout<<"Nghiem
phuc
x1
="<<-b/(2*a)<<"
x2
="<<-b/(2*a)<<"
*"<<sqrt(-delta)/(2*a)<<endl;
cout<<"Nghiem
phuc
*"<<sqrt(-delta)/(2*a)<<endl;
}
getch();
}
}
- Gii thch: Thc hin mt dy lnh ng vi mt gi tr
ca biu thc.
V d 12: Nhp vo mt thng ca nm cho bit s ngy ca
thng .
# include <iostream.h>
#include <math.h>
#include <conio.h>
24
main()
{
int ngay,thang,nam;
cout<<" Cho thang: \n";
cin>>thang;
switch (thang)
{
case 1,3,,5,7,8,10,12:
cout<<thang<<" co 31 ngay"<<endl;
break;
case 4,6,9,11:
cout<<thang<<" co 30 ngay"<<endl;
break;
case 2:
{
cout<<" Cho nam: \n";
cin>>nam;
if (nam%4==0)
cout<<thang<<" co 29 ngay"<<endl;
else
cout<<thang<<" co 28 ngay"<<endl;
break;
}
}
getch();
return 0;
}
- Ch : Sau mi case phi c lnh break thot ra khi tho mn.
6. Ti liu tham kho
25
B mn:
27
int i,a,b,tam;
long tong;
printf(nhap so thu nhat\n);
cin>>a;
printf(nhap so thu hai\n);
cin>>b;
if (a>b)
{
tam=a;
a=b;
b=tam;
}
tong=0;
for(i=a;i<=b;i++)
tong+=i;
printf(Tong la: %d,tong);
}
V d 14: Nhp n vo t bn phm, tnh 1!+2!++n!
#include <stdio.h>
#include <math.h>
*/#define n 5;*/
void main()
{
int i,n,tam;
long int tonggiaithua;
printf(nhap n= \n);
cin>>n;
tonggiaithua=0;tam=1;
for(i=1;i<=n;i++)
{
28
tam=tam*i;
tonggiaithua=tonggiaithua+tam
}
printf(Tong giai thua la: %d,tonggiaithua);
}
6.2. Cu lnh lp khng xc inh: while...
a/C php while(<biu thc>)
{
< Cc lnh>
};
- Gii thch: Vng lp s c thc hin nu biu thc
tho
mn(Nhn
gi
tr
true)
kt
thc
khi
<biu
thc>nhn gi tr false.
b/ Cc v d
V d 16: Tnh tng s=1-1/(1*2)++(-1)n *1/(n*(n+1))+
vi chnh xc epsilon=0.0001;
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int i;
float tam,tg,tong;
const float epsilon=0.0001;
i=1;tam=1;tong=1;tg=1;
while (fabs(tg)>=epsilon)
{
tam=-tam;
tg=tam/[i*(i+1)];
29
i++;
tong=tong+tg;
}
cout<<" tong la:<<tong<<endl;
getch();
}
V d 17: Tnh gi tr ca e m x vi x c nhp t bn
phm vi chnh xc tu .
# include <iostream.h>
# include <math.h>
# include <conio.h>
main()
{
int i;
float emu,epsilon,x,tam,tong;
cin>>x;
cin>>epsilon;
emu=1;
i=1;
tam=1;
while (tam>=epsilon)
{ tam=tam*x/i;
i++;
emu+=tam;
}
cout<< Gia tri ham e mu x : <<emu;
getch();
return 0;
}
30
nu ng chy tip.
- Ch : Cu lnh do while th phi c du kt thc;
sau
a/ Cc v d
V d 18: Tnh cosx=1-x2/2!+..+(-1)n*x2n/(2n)!+.. vi x
c nhp t bn phm
1. Dng vng lp while:
#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
void main()
{
int i;
float tam,x,epsilon,tong;
epsilon=0.0001;
printf("nhap gia tri x \n");
cin>>x;
i=1;tam=1;tong=1;
while(fabs(tam)>=epsilon)
{
if (i==1)
31
tam=x;
tam=-x*x*tam*1/((2*i-1)*(2*i));
tong+=tam;
i++;
}
cout<<"tong la:"<<tong;
getch();
}
2. Dng vng lp dowhile:
#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
void main()
{
int i;
float tam,x,epsilon,tong;
epsilon=0.00001;
printf("nhap gia tri x \n");
cin>>x;
i=1;tam=1;tong=1;
do
{
tam=-x*x*tam*1/((2*i-1)*(2*i));
tong+=tam;
i++;
}
while (fabs(tam)>=epsilon);
cout<<"tong la:"<<tong;
getch();
32
}
V d 19: (tham kho)S pi c tnh thng qua cng thc
sau: Pi/4=1-1/3++(-1)n/(2n+1)+..Vit chng trnh tnh
s pi
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <iomanip.h>
#include <math.h>
void main()
{
int i;
float tam,epsilon,pi;
epsilon=0.00001;
pi=1;i=1;
tam=1;
while (fabs(tam/(2*i+1))>=epsilon)
{
tam=-tam;
pi+=tam/(2*i+1);
i++;
}
cout<<" so pi=
"<<setw(10)<<4*pi;
}
- V d 20: (tham kho) Va g va ch, b li cho trn,
36 con 100 chn chn. Tnh s ch, g
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
void main()
33
{
int x,y;
x=0;
do
{ x++;
y=36-x;
}
while(2*x+4*y!=100);
cout<<"So ga:
"<<x<<endl;
{
cout<< Gia tri I = <<i<<endl;
if (i<4) continue; /* nu tho th tip tuc */
cout<<Sang lenh moi khi khong thoa va thuc hien tiep\n;
}
getch();
}
- V d 22: Minh ho lnh continue,bng cch tnh bnh
phng ca s dng nhp t bn phm.
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
void main()
{
int i;
do
{
cout<< Nhap vao so i \n<<endl;
cin>>i;
if (i<0)
{ cout<< nhap lai so >0 \n;
continue; /* nu tho th tip tuc */
}
cout<<Binh phuong cua so vua nhap la : <<i*i<<endl;
}
while(i);
getch();
}
c/ Lnh: goto
35
36
B mn:
37
6.2. Con tr
3. Thi lng: 4 tit
4. Mc ch
- hc vin nm c c php ca cc mng 1-2 chiu
- ng dng khc khai bo mng gii cc bi ton thc t
- Nm c tnh tin dng ca khai bo v s dng con tr trong lp trnh
5. Ni dung
7.1.Mng
a/ Mng 1 chiu
-
php:<Kiu
phn
t>
<Tn
mng>[<S
ph.
ca
mng];
- V d: int a[100]; char text[80]
- Lu : Ch s u tin ca mng l 0
a[0]
a[1]
a[2]
)x+)
-2
float p,x;
printf("nhap bac cao nhat cua da thuc\ n");
cin>>k;
printf("nhap cac he so \n");
for (i=0;i<=k;i++)
{
cout<<"a["<<i<<"]=");
cin>>a[i];
}
printf("nhap gia tri x \n");
cin>>x;
p=a[k];
for(i=k-1;i>=0;i--);
p=p*x+a[i];
printf("Gia tri da thuc=%f",p);
}
- Bi ton trn c gii bi thut ton Horner.
V d 28: Nhp vo t bn phm dy s nguyn hy sp xp
theo th t tng dn.
#include <iostream.h>
#define n 5
main()
{
int i,j,tam;
int a[n];
for (i=0;i<=n;i++)
cin>>a[i]<<endl;
for (i=0;i<=n;i++)
for (j=i+1;j<=n;j++)
if(a[i]>a[j])
39
{ tam=a[i];a[i]=a[j]; a[j]=tam;}
for (i=0;i<=n;i++)
cout<<a[i]<<
b/ Khi to gi tr mng.
- Mng c th khi to gi tr ngay sau khi khai bo:
int a[3]={3,4,5}. iu ny ngha l a[0]=3,a[1]=4,a[2]=5,
- Mng c truy nhp trc tip.
a[0][2]
a[1][2]
a[2][2]
a[0][3]
a[1][3]
a[2][3]
ct];
a[0][4]
a[1][4]
a[2][4]
int a[3,2]={{1,2},{3,4},{5,6}};
-V d 29: Hy tnh tch ca 2 ma trn vung c nhp t
bn phm.
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <iostream.h>
# define m 4
# define n 4
# define l 4
void main()
{
int i,j,k;
float a[m][n],b[n][l],c[m][l];
40
for (i=0;i<m;i++)
for (j=0;j<n;j++)
cin>>a[i][j];
for (i=0;i<n;i++)
for (j=0;j<l;j++)
cin>>b[i][j];
for (i=0;i<m;i++)
for (j=0;j<l;j++)
{
c[i][j]=0;
for (k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
cout<< Ma tran tich la: \n;
for (i=0;i<m;i++)
{
for (j=0;j<m;j++)
cout<<c[i][j]<<
cout<<<<endl;
}
getch();
}
- V d trn d trn ch m t tch ca 2 ma trn ch
cha tnh n vic vit cha p mt iu ny s thc
hin sau ny.
7.2. Con tr
a/ Nguyn tc hot ng
- B nh ca my tnh gm nhiu nh xp lin nhau. Mi
nh c 2 phn: Phn a ch v Phn ni dung.
- K hiu ton t con tr v a ch nh sau:
41
l
a ch
Ni dung nh
a ch
Ni dung nh
56F0
56F4
1234
FOF
F0F0
FOF
F6
15
&p
P=&x
&x
*p=x
b/ M t qua v d
#include <iostream.h>
main()
{
int so;
int *contro;
so=10;
cout<<\n Dia chi cua so<<&so;
cout<<\n Gia tri cua so<<so;
42
B mn:
4. Mc ch
- hc vin nm c thut ton i ch trc tip v chn trc tip
- ng dng cc thut ton vo mt s v d c th
- Nm c c php, cc thao tc vo-ra i vi xu k t
5. Ni dung
8.1. Sp xp
a/ i ch trc tip:
- S dng mng a[n] khai bo v lu gi dy cho trc.
-Thc hin lp t i=1,2,,n
- So snh a[i], ln lt vi a[i+1], a[i+2], a[n], nu ln hn i ch
- Kt thc lp ta c dy c sp xp tng dn
V d 28: Nhp vo t bn phm dy s nguyn hy sp xp
theo th t tng dn.
#include <iostream.h>
#define n 5
main()
{
int i,j,tam;
int a[n];
for (i=0;i<=n;i++)
cin>>a[i]<<endl;
for (i=0;i<=n;i++)
for (j=i+1;j<=n;j++)
if(a[i]>a[j])
{ tam=a[i];a[i]=a[j]; a[j]=tam;}
for (i=0;i<=n;i++)
cout<<a[i]<<
- Gi s a[1],,a[i-1] c sp
- Thc hin lp chn a[i] vo v tr thch hp dy a[1],...,a[i] tng
8.2. Xu k t.
a/ Khi nim xu.
- C/C++ s dng mng cc xu k t cha xu k t.
- Xu k t c lu tr trong mng k t song n c
nh du s kt thc xu bng mt phn t cui cng mang
gi tr c bit: K t null l k t u tin c m k
t 0 trong ASCII c k hiu trong C/C++ l \0
b/ Khai bo: char <ten xu>[< di];
<tn xu> s cha ti a l < di> k t.
- V d: char a[50];
- Xu ch cha ti a l < di>-1, cui cng cha k
hiu kt thc.
- C th khai bo: char str[]=day la xau can nhap;
(Khng ch ra s cc i my s t ng b tr mng
nh cha dy k t trn + 1 cha \0
c/ Lnh nhp xu thng thng
gets(<tn xu): Cho php nhp <tn xu> vo t bn phm
- V d: gets(a) v nhp xu nguyen van a khi trong
b nh c dng sau:
n
\0
45
Nh vy text[0]=D,text[1]=C,text[2]=f,text[3]=S,
text[4]=P.
- Hai kiu u thiu 1 k t cho \0.
e/ In mt xu ra mn hnh.
- C php: cout<< <tn xu>;
- V d: cout<< Thi du; cout<<st;
f/ Nhp mt xu t bn phm
- n gin ta k hiu str l mt xu k t
- C php: + cin>> str;
(1) ly t tp iostream.h
+ cin.get(str,max); (2)ly t tp
iostream.h
+ gets(str);
(3) ly t tp stdio.h
46
#include <conio.h>
#include <string.h>
main()
{
char str[30],str1[30];
int i;
int max=15;
cout<<" Minh hoa cac cach nhap boi cin, cin.get, gets
\n";
cin>>str;
cout<<" Xau nhan duoc:
"<<str<<endl;
getch();
cin.get(str1,max);
cout<<" Xau nhan duoc1:
"<<str1<<endl;
getch();
gets(str);
cout<<" Xau nhan duoc:
"<<str<<endl;
getch();
return 0;
}
g/ Ghp xu k t.
-
th
dng
hm
strcat(str1,str2)
trong
th
vin
{
char str[40]=" Hoc C++ can kien tri";
char str1[40]="Khong chan nan";
int dem1=0,dem2=0;
while (str[dem1] !='\0') dem1++;
while(str1[dem2] !='\0') dem2++;
if ((sizeof(str)/sizeof(str[0]))<(dem1+dem2+1))
{
cout<<"
}
else
{
dem2=0;
while (str1[dem2] != '\0')
str[dem1++]=str1[dem2++];
str[dem1]='\0';
cout<<str;
}
getch();
return 0;
}
-V d 31b: (tham kho) Ghp cc xu k t
#include <iostream.h>
/*#include <stdio.h>*/
#include <conio.h>
#include <string.h>
main()
{
char str[40]=" Hoc C++ can kien tri";
char str1[40]="Khong chan nan";
48
int dem1=0,dem2=0;
while (str[dem1] !='\0') dem1++;
while(str1[dem2] !='\0') dem2++;
if ((sizeof(str)/sizeof(str[0]))<(dem1+dem2+1))
{
cout<<"
}
else
{
dem2=0;
while (dem2<strlen(str1) )
str[dem1++]=str1[dem2++];
str[dem1]='\0';
cout<<str;
}
getch();
return 0;
}
h/ Copy xu k t.
- C th dng hm strcpy(str1,str2) trong th vin chun
<string.h>.
- Th d sau minh ho cch to ra hm strcpy.
-V d 32: Copy cc xu k t
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
49
strcpy(destination,turbo);
strcat(destination,turbo);
strcat(destination,c);
cout<<" xau dich :"<<destination;
getch();
return 0;
}
i/ So snh xu k t.
- Hai xu c di khc nhau ging nhau n di xu
ngn hn c coi l xu b hn.
50
dFILENAME<FILENAME
A,FILENAME==FILENAME
gi tr true.
- Hm so snh: strcmp(str1,str2): Cho kt qu 0 nu bng,
<0 nu str1<str2,>0 nu str1>str2.
-V d 34: Chng trnh so snh 2 xu.
/* Copy xau ky tu */
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
main()
{
char str1[40], str2[40];
cout<<" Nhap vao xau 1:"<<endl;
gets(str1);
cout<<" Nhap vao xau 2:"<<endl;
gets(str2);
if (strcmp(str1, str2)==0)
cout<<" giong nhau hoan toan";
else
if(strcmp(str1,str2)<0)
cout<<str1;
else(cout<<str2);
cout<<" dung truoc ";
(strcmp(str1,str2)<0) ? (cout<<str2): (cout<<str1);
getch();
return 0;
51
}
-V d 35:In ra m k t tng ng vi cc k t trong
xu
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char *str1;
char st[80];
do
{
str1=st;
cin.get(st,80);
while(*str1)
cout<<int(*str1++)<<"
";
}
while(strcmp(st,"done")==0);
getch();
}
-V d 36: (tham kho)i ch thnh ch hoa v m c bao
nhiu ln xut hin ca cc k t xut trong xu.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define dodai 80
52
void main()
{
char str[dodai];
int i, demchu['Z' - 'A'+1];
clrscr();
for(i=0;i<=('Z'-'A'+1);i++)
demchu[i]=0;
cin.get(str,dodai);
for(i=0;i<strlen(str);i++)
str[i]=toupper(str[i]);
cout<<str;
for(i=0;i<=strlen(str);i++)
demchu[str[i]-'A']++;
for(i='A';i<='Z';i++)
if (demchu[i-'A'])
cout<<"\n so chu"<<char(i)<<"--"<<demchu[i-'A'];
getch();
}
6. Ti liu tham kho
- Quch Tun Ngc, Ngn ng lp trnh C++ trang 1-59
- Bi ging ca gio vin (tp bi ging km theo)
- Dng T Cng, Ngn ng lp trnh C
7. Cu hi n tp
- Vit chng trnh sp xp tng dn theo thut ton chn trc tip
- Phn tch s khc nhau v ging nhau ca cc phng php nhp xu
- Tm, m s t trong xu, s ln xut hin ca cc loi k t trong xu
53
B mn:
55
c/ Lnh return.
- Hm return c th tr gi tr ca c mt biu thc v
d 2 cch vit sau l tng ng:
float vidu(float x, int a, int b,int c)
{
return a*x*x+b*x+c;
}
v
float vidu(float x, int a, int b,int c)
{ float giatri;
giatri=a*x*x+b*x+c;
return giatri;
- Lnh return c th xut hin nhiu ln trong hm
- V d: double trituyetdoi(double u,double v)
{ double s;
s=u+v;
if s>0 return s
else return s;
}
d/ Hm khng c gi tri.
- Hm khng c gi tr phi c khai bo theo dng (2)
v khng cha lnh return trong.
- void vidu(float a, int b,int c);
56
e/ V d minh ho
V d 24: Tnh gi tr a*x2+b*x+c;
#include <iostream.h>
#include <conio.h>
#include <math.h>
float ham_vi_du(float,int,int,int);
void main()
{
%float ham_vi_du(float,int,int,int);
float x,y,z;
int a,b,c;
cout<<" nhap he so a, b, c : \n";
cin>>a>>b>>c;
cout<<" nhap gia tri x lan 1: \n";
cin>>x;
y=ham_vi_du(x,a,b,c);
cout<<" Gia tri cua ham ham_vi_d = "<<y<<endl;
cout<<" nhap gia tri x lan 2: \n";
cin>>x;
y=ham_vi_du(x,a,b,c);
cout<<" Gia tri moi cua ham ham_vi_du = "<<y<<endl;
getch();
}
float ham_vi_du(float x,int a,int b,int c)
{
float gia_tri;
gia_tri=a*x*x+b*x+c;
return gia_tri;
}
V d 25: Tnh n giai tha
57
#include <iostream.h>
#include <conio.h>
void main()
{
long int giai_thua(int);
int n;
long int y;
cout<<" Nhap gia tri n : \n";
cin>>n;
y=giai_thua(n);
cout<<" gia tri giai thua = "<<y;
getch();
}
long int giai_thua(int n)
{
int i;
long int gia_tri;
gia_tri=1;
for (i=1;i<=n;i++)
gia_tri*=i;
return gia_tri;
}
e/ Truyn tham s gi tr cho hm
- Truyn tham s cho hm trong C/C++ c thc hin duy
nht l truyn tham tr iu c ngha l tham s thc
s trc v sau khi gi hm l khng i v l kiu
duy nht.
V d 27: V d m t cch truyn tham tr
#include <iostream.h>
main()
58
{
void hoan_vi(int a, int b);
int n=10,m=20;
cout<<Truoc khi goi ham
:<<n<<
<<m<<endl;
hoan_vi(n,m);
cout<< Sau khi goi ham
:<<n<<
<<m<<endl;
}
void hoan_vi(int a, int b)
{
int t;
cout<< Truoc khi hoan vi :<<n<<
<<m<<endl;
t=a;
a=b;
b=t;
cout<< Sau khi hoan vi :<<a<<
<<b<<endl;
void thidu(void)
{
int m=3; /* m l bin a phng*/
m++;
count<<i<<
<<m<<endl;
g/ Tm hot ng ca bin
- Bin ton cc: Hot ng trn ton b chng trnh ngay
sau khi khai bo n
- Bin a phng: Ch hot ng trong hm cha n. Cch
cp pht cho bin a phng l cp pht t ng, mi ln
gi hm l mt ln cp pht.
- Bin a phng tnh (static):L
bin do yu cu c
<<m<<endl;
\n;
cin>>n;
dichchuyen(n,1,2,3);
}
62
63
64
{
<kiu d liu 1> <trng 1>;
. . . . . . . . . .
<kiu d liu k> <trng k>;
} <bin cu trc>;
Hoc c th khai bo trc tip nh sau:
struct
{
<kiu d liu 1> <trng 1>;
. . . . . . . . . .
<kiu d liu k> <trng k>;
} <bin cu trc>;
c/ Kt hp vi mng
65
{
int i;
for(i=0;i<max;i++)
{
cout<<" Nhap ho so cho nguoi thu : "<<i+1<<endl;
cout<<" Ho ten : ";
gets(nguoi[i].hoten);
cout<<endl;
cout<<" Ngay sinh : ";
cin>>nguoi[i].ngaysinh.ngay;
cout<<"Thang sinh : ";
cin>>nguoi[i].ngaysinh.thang;
cout<<"Nam sinh : ";
cin>>nguoi[i].ngaysinh.nam;
cout<<endl;
}
for(i=0;i<max;i++)
{
cout<<nguoi[i].hoten<<"
"<<"
Ngay
sinh
"<<nguoi[i].ngaysinh.ngay<<
"Thang sinh : "<<nguoi[i].ngaysinh.thang<<"Nam sinh :
"<<nguoi[i].ngaysinh.nam;
cout<<endl;
}
getch();
return 0;
}
- V d 38:
#include <iostream.h>
#include <stdlib.h>
67
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <math.h>
#define max 2
struct nhansu
{
char hoten[30];
struct dat
{
int ngay;
int thang;
int nam;
} ngaysinh;
float luong;
}
nguoi[5];
void nhapdl(void);
void danhsach(void);
main()
{
nhapdl();
danhsach();
getch();
return 0;
}
void nhapdl(void)
{
68
int i;
for(i=0;i<max;i++)
{
cout<<" Nhap ho so cho nguoi thu : "<<i+1<<endl;
cout<<" Ho ten : ";
gets(nguoi[i].hoten);
cout<<endl;
cout<<" Ngay sinh : ";
cin>>nguoi[i].ngaysinh.ngay;
cout<<"Thang sinh : ";
cin>>nguoi[i].ngaysinh.thang;
cout<<"Nam sinh : ";
cin>>nguoi[i].ngaysinh.nam;
cout<<endl;
}
}
void danhsach(void)
{
int i;
for(i=0;i<max;i++)
{
cout<<nguoi[i].hoten<<"
"<<"
Ngay
sinh
"<<nguoi[i].ngaysinh.ngay<<
"Thang sinh : "<<nguoi[i].ngaysinh.thang<<"Nam sinh :
"<<nguoi[i].ngaysinh.nam;
cout<<endl;
}
}
- V d 39: (tham kho)
#include <iostream.h>
69
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <math.h>
#define max 4
struct nhansu
{
char hoten[30];
struct dat
{
int ngay;
int thang;
int nam;
} ngaysinh;
float luong;
}
nguoi[max];
void input(void);
void list(void);
void delet(void);
main()
{
int i;
int choice;
do
{
cout<<"1: Nhap du lieu \n";
cout<<"2: Hien thi danh sach \n";
cout<<"3: Xoa mot nguoi \n ";
70
cin>>nguoi[i].ngaysinh.ngay;
cout<<"Thang sinh : ";
cin>>nguoi[i].ngaysinh.thang;
cout<<"Nam sinh : ";
cin>>nguoi[i].ngaysinh.nam;
cout<<endl;
}
}
void list(void)
{
int i;
for(i=0;i<max;i++)
{
cout<<" Hien thi danh sach :"<<endl;
cout<<nguoi[i].hoten<<"
"<<"
Ngay
sinh
"<<nguoi[i].ngaysinh.ngay<<
"Thang sinh : "<<nguoi[i].ngaysinh.thang<<"Nam sinh :
"<<nguoi[i].ngaysinh.nam;
cout<<endl;
}
}
void delet(void)
{
int i,k;
cout<<"Ban ghi can xoa: "<<endl;
cin>>k;
for(i=k-1;i<max;i++)
nguoi[i]=nguoi[i+1];
cout<<" So nguoi con lai :"<<endl;
for(i=0;i<max;i++)
72
{
cout<<" Hien thi danh sach :"<<endl;
cout<<nguoi[i].hoten<<"
"<<"
Ngay
sinh
"<<nguoi[i].ngaysinh.ngay<<
"Thang sinh : "<<nguoi[i].ngaysinh.thang<<"Nam sinh :
"<<nguoi[i].ngaysinh.nam;
cout<<endl;
}
}
6. Ti liu tham kho
- Quch Tun Ngc, Ngn ng lp trnh C++ trang 1-59
- Bi ging ca gio vin (km theo)
- Dng T Cng, Ngn ng lp trnh C, trang 222-264
7. Cu hi n tp
- Hy xy dng cu trc qun l cn b l quan qun i trong mt s on
- Vit chng trnh qun l cn b cho s on gm cc chc nng: nhp, thm,
xa, sp xp, tm kim, tnh lng, ph cp,
a/ Khai bo tp.
- C php: FILE *<tn con tr tp>;
- Gii thch: <Tn con tr tp> do ngi s dng t theo
mun min l hp l. Ta hay dng fprt (file pointer)
b/ M tp ghi hoc c.
74
c/ Cc quy c x l tp
- w : M tp vn bn mi ghi.
- r : M tp tn ti c.
- a : M tp tn ti v ghi thm d liu vo cui
tp. Nu tp cha c th s c t ng to ra.
- r+ : M tp c cho php c ghi v c.
- w+ : M tp vn bn mi cho php c ghi v c.Nu
c tp th my s hu v thay d liu c.
- a+ : M tp c cho php c c v ghi thm d liu
vo cui tp hoc c tp.
- i vi tp nh phn th ta thm k t b hoc +b
vo sau cc k t trn.
d/ ng tp
- C php: fclose(<bien tep>);
- Cu trc chung ca chng trnh x l tp c dng:
#include <stdio.h>
main()
{
FILE *fprt;
fprt=fopen(vidu.dat,w);
if (fprt==null)
cout<< Khong mo duoc tep)<<endl;
else
75
{
. . .
fclose(fprt);
}
}
f/ c d liu t tp nh phn
- Hm c d liu t tp nh phn c:
fread(&<bin>,sizeof(<bin>),<s khi>,<bien tep>)
76
fclose(f);
77
getch();
return 0;
}
11.2 Cc v d minh ha lin quan n cu trc tp
a/ Ghi vo tp nh phn
- V d 42: Ghi vo tp mi 50 s cui ca tp v I ra
mn hnh bnh phng ca chng
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
main()
{
char filename[20];
int i;
FILE *f, *f1;
cout<<" ten tep can doc:"<<endl;
cin>>filename;
f=fopen(filename,"rb");
f1=fopen("sn1.dat","wb");
while (fread(&i,sizeof(int),1,f), !feof(f))
{
if(i>50)
fwrite(&i,sizeof(int),1,f1);
cout<<(i*i)<<"
";
}
/* do
{
fread(&i,sizeof(int),1,f)
if(!feof(f))
cout<<(i*i)<<"
";
78
}
while ( !feof(f)) */
fclose(f);
fclose(f1);
getch();
return 0; }
b/ Sao chp sang tp khc
- V d 43: Sao chep mot tep sang tep khac
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
main()
{
char filename[20];
char filename1[20];
int i;
FILE *f, *f1;
cout<<" ten tep nguon: "<<endl;
cin>>filename;
/*cout<<" ten tep dich:
"<<endl;
cin>>filename1;*/
f=fopen(filename,"rb");
f1=fopen("sn1.dat","wb");
{
while (fread(&i,sizeof(int),1,f), !feof(f))
fwrite(&i,sizeof(int),1,f1);
}
fclose(f);
fclose(f1);
79
getch();
return 0; }
{
int flag=TRUE;
mau_nhan_su Nguoi;
fpt=fopen("nhansu.dat","wb");
Nguoi.luong=0;
while(flag)
{
cout<<" Ten :(AN enter de ket thuc
" <<endl;
gets(Nguoi.ten);
if(Nguoi.ten[0]=='\0')
break;
Nguoi=read_screen(Nguoi);
fwrite(&Nguoi,sizeof(mau_nhan_su),1,fpt);
}
fclose(fpt);
return 0;
}
mau_nhan_su read_screen(mau_nhan_su Nguoi)
{
printf("Cho ngay thang nam sinh: ");
scanf("%d/%d/
%d",Nguoi.ngaysinh.ngay,Nguoi.ngaysinh.thang,Nguoi.ngaysi
nh.nam);
printf("Dia chi hien tai: ");
gets(Nguoi.diachi);
/*gets(ss);*/
printf("Luong : ");
scanf("%f",&Nguoi.luong);
/*gets(ss);*/
return(Nguoi);
81
}
d/ Truy cp trc tip tp d liu nh phn.
- C/C++ ch nh ngha mt kiu tp: Tp truy cp tun t
- Truy nhn do di ca cc phn t l nh nhau nn c
th tnh ton to ca chng v iu ny cho php ta
truy cp trc tip n tng phn t ca tp.
- Hm truy cp trc tip l:fseek
-Cphp:fseek(fptr,No*<kich_thuoc-phan_tu>,<vi tri>)
- Gii thch:
+ fptr: Con tr tp
+ No: V tr s th t ca phn t trong tp
+ <kich_thuoc-phan_tu>: thng c thc hin bng
sizeof(<ten kpt>) v cho ta to ca phn t.
+ Tham s cui cng:<vi tri>=
SEEK_SET: Di chuyn v tr truy cp bt u t u
tp
SEEK_CUR: Di chuyn tnh t v tr hin ti ca ca
s hin ti ca tp
SEEK_END: Di chuyn tnh t to cui tp.
-V d 45: Truy cp trc tip tp c:
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
FILE * f;
main()
{
char tentep[20];
char ch;
82
int n,No;
printf(" Cho ten tep:" );
scanf("%20s",tentep);
f=fopen(tentep,"r");
while(printf("Phantucantruycapthu: ")
,scanf("%1d",&No,No))
{
fseek(f,sizeof(int)*(No-1 ),SEEK_SET);
fread(&n,sizeof(int),1,f);
printf("Gia tri tim thay : %d \n",n);
}
fclose(f);
getch();
}
- Vi du 46: Truy cp trc tip tp va ghi va c
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
FILE * stream;
main(void)
{
char msg[]="Day la chuong trinh chay thu";
char buf[20];
if ((stream=fopen("ghidoc.dat","w+"))==NULL)
{
fprintf(stderr," Khong mo duoc tep\n");
return 1;
}
83
fwrite(msg,strlen(msg)+1,1,stream);
printf("%s\n",buf);
fclose(stream);
getch();
return 0;
}
- Vi du 47: (tham kho) Truy cp trc tip cp nht d
liu
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
main()
{
FILE * f;
char tentep[20];
int n,k;
long m;
printf(" Ten tep can xu ly: ");scanf("%20s", tentep);
f=fopen(tentep,"w");
k=1;
while(printf("Phan
tu
thu
may
can
cap
nhat:
"),scanf("%1d",&m,m))
{
printf("Gia tri cap nhat:\n");
scanf("%1d",&n);
fseek(f,sizeof(int)*(m-1),SEEK_SET);
fwrite(&n,sizeof(int),1,f);
84
int No;
long kich_thuoc,pos;
mau_nhan_su Nguoi;
fpt=fopen("nhansu.dat","rb");
fseek(fpt,0,SEEK_END); /* Xac dinh kich thuoc tep*/
kich_thuoc=ftell(fpt);
cout<<"Can cap
cin>>No;
pos=No*sizeof(mau_nhan_su);
if(No<0 || pos>kich_thuoc)
cout<<"Khong co phan tu trong tep"<<endl;
else
{
fseek(fpt,pos-1,0);
fread(&Nguoi,sizeof(mau_nhan_su),1,fpt);
write_screen(Nguoi);
}
return 0;
}
void write_screen(mau_nhan_su Nguoi)
{
printf("Ten : %s\n",Nguoi.ten);
printf("Ngaysinh:
%2d/%2d/
%2d\n",Nguoi.ngaysinh.ngay,Nguoi.ngaysinh.thang,Nguoi.nga
ysinh.nam);
printf("Dia chi hien tai: ",Nguoi.diachi);
printf("Luong : %8.2f\n",Nguoi.luong);
return;
}
11.3 Tp vn bn
86
- Cc phn t ca tp vn bn l cc k t v c t
chc thnh tng dng.
- Mi dng c k hiu kt thc dng(End of line)
- Cc cp k t iu khin: CR(nhy v u dng m s
ASCII=13,\r);
LF(nhy
xung
dng
tip
theo
ASCII=10,\n)
a/ M tp vn bn mi ct d liu
- Gm cc hm sau:
1. putc(ch,fprt): Ghi mt k t vo tp vn bn
2. fputs(str,fprt): Ghi xu k t vo tp vn bn
3. fprintf(fprt,): kt qu ra tp vn bn c con tr
fprt
- Mi ln ghi xong my s t ng chuyn ca s n v
tr tip theo.
b/ Cc v d
- V d 49: Ghi mt dng cc ch hoa vo tp
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
main()
{
FILE * f;
char c;
/* Cach nay doc doc ten tep vao tu ban phim
char tentep[11];
cout<<"Ten tep :"<<endl;
gets(tentep);
f=fopen(tentep,"w");
*/
87
f=fopen("d:\\tc\\bin\\vidu.txt","w");
do
tep*/
putc(toupper(c=getchar()),f);
while(c!='\n');
fclose(f);
return 0;
}
- Vi du 50: Ghi c d liu cu trc
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define TRUE 1
typedef struct
{ int ngay;
int thang;
int nam;
} date;
typedef struct
{ char ten[80];
char diachi[80];
date ngaysinh;
float luong;
} mau_nhan_su;
mau_nhan_su read_screen(mau_nhan_su Nguoi);
void write_file(mau_nhan_su Nguoi);
FILE * f;
char ss[80];
88
main()
{
int flag=TRUE;
mau_nhan_su Nguoi;
f=fopen("Nhan_su.dat","w");
Nguoi.luong=0;
while (flag)
{
cout<<" Ten : (An ENTER de ket thuc";
gets(Nguoi.ten);
if (Nguoi.ten[0]=='\0')
break;
Nguoi=read_screen(Nguoi);
write_file(Nguoi);
}
fclose(f);
return 0;
}
mau_nhan_su read_screen(mau_nhan_su Nguoi)
{
printf(" Hay cho ngay/thang/nam
:");
scanf("%d/%d/
%d",&Nguoi.ngaysinh.ngay,&Nguoi.ngaysinh.thang,&Nguoi.nga
ysinh.nam);
gets(ss);
cout<<"Dia chi :";
gets(Nguoi.diachi);
cout<<"Luong :";
scanf("%f",&Nguoi.luong);
gets(ss);
89
return(Nguoi);
}
void write_screen(mau_nhan_su Nguoi)
{
fprintf(f,Nguoi.ten);
fprintf(f,"Ngaysinh:
%2d/%2d/
%2d\n",Nguoi.ngaysinh.ngay,Nguoi.ngaysinh.thang,
Nguoi.ngaysinh.nam);
fprintf(f,"Dia chi hien tai: ",Nguoi.diachi);
fprintf(f,"Luong : %8.2f\n",Nguoi.luong);
return;
}
- Vi du 51: c mt dng cc ch t tp vn bn
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
main()
{
FILE *f;
char c;
/* Cach nay doc doc ten tep vao tu ban phim
char tentep[11];
cout<<"Ten tep :"<<endl;
gets(tentep);
if((f=fopen(tentep,"r"))==NULL);
printf(" Tep co loi, hoac chua ton tai \n");
else */
if ((f=fopen("d:\\tc\\vidu.txt","r"))==NULL)
90
91
DETECT
CGA
MCGA
EGA
EGA64
EGAMONO
IBM8514
HERCMONO
ATT400
VGA
PC3270
CurrentDriver
GraphDriver
CGA
GI TR NGHA
0
Yu cu t ng pht hin
1
Vi mch CGA
2
Vi mch MCGA
3
Ch EGA
4
Ch EGA64
5
Ch EGAMono
6
Ch IBM8514
7
Ch HercMono
8
Ch ATT400
9
Ch VGA
10
Ch PC 3270
-128
Chuyn ti GetModeRange
Bng 1: Cc hng iu khin ha
GraphMode
phn gii
CGAC0, 1, 2, 3, 4 320x200 CGA phn gii thp
CGAHI
640x200 CGA phn gii cao
92
MCGA
94
#include "conio.h"
int main()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if(errorcode != grOK)
{
printf("Graphics error: %s\n",
grapherrormsg(errorcode));
printf("Pess any key to halt: ");
getch();
exit(1);
}
line(0, 0, getmaxx(), getmaxy());
getch();
closegraph();
return 0;
}
Gi tr
en
Xanh
Xanh l cy
Xanh cm thch
Ta
Nu
Xm nh
Xm m
Xanh nht
Xanh l cy nht
Xanh cm thch nht
Hng
Ta nht
Vng
Trng
95
<graphics.h>
<stdlib.h>
<stdio.h>
<conio.h>
int main()
{
/* T ng pht hin ha, request auto detection */
int gdriver = DETECT , gmode, errorcode;
int midx, midy;
int radius = 100;
// Khi to initialize graphics and local variables
96
.
5.6.
V hnh ch nht
5.7.
Hin th vn bn trong ch ha
SANS_SERIF_FONT
3
Strocked sans-serif font
GOTHIC_FONT
4
Strocked gothic font
Chiu vit:
HORIZ_DIR = 0 l ng nm ngang, t tri qua phi
VERT_DIR = 1 l thng ng, t di ln trn.
- Kch thc ch charsize nhn gi tr t 1 n 10, l h s phng to ch
- Hm xc nh v tr dng vn bn vit ra theo hai hm outtext:
settextjustification(int Horz, int Vert);
+ Tham s Horz c th c cc gi tr sau:
LEFT_TEXT = 0 vn bn xut hin bn phi con tr ha
CENTER_TEXT = 1 vn bn xut hin vi tm l v tr con tr
ha
RIGHT_TEXT = 2 vn bn xut hin bn phi con tr
+ Tham s Vert c th c cc gi tr sau:
BOTTOM_TEXT = 0 vn bn xut hin pha trn con tr
CENTER_TEXT = 1 vn bn xut hin quanh con tr
TOP_TEXT = 2 vn bn xut hin pha di con tr.
0
1
2
3
4
5
6
7
8
9
10
11
12
Mu nn Background color
Vng t vi mu t Solid fill
--///
///, thick lines
\\\, thick lines
\\\
Light hatch
Heavy crosshatch
Interleaving lines
Widely spaced dots
Closely spaced dots
T theo mu ca ngi dng
User defined fill pattern
2.
3.
4.
5.
6.
Dng phuong php hnh thang tnh (sin 2 x cos x)dx vi a, b nhp vo t bn
a
phm
b
7.
Dng phuong php Simson tnh (sin 2 x cos x)dx vi a, b nhp vo t bn phm
a
99
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
101
102