Giáo trình Bài tập C và C++
MỤC LỤC
CHƯƠNG 1 CÁC THAO TÁC VÀO RA CƠBẢN VÀ
CÁC CÂU LỆNH CÓ CẤU TRÚC
1.1. CÂU HỎI.
1.2. BÀI TẬP.
Ch−¬ng 2 hµm
2.1. CÂU HỎI.
2.2. BÀI TẬP.
CHƯƠNG 3 MẢNG VÀ CON TRỎ
3.1. CÂU HỎI.
3.3. BÀI TẬP.
CHƯƠNG 4 CHUỖI KÝ TỰ
4.1. CÂU HỎI.
4.2. BÀI TẬP.
CHƯƠNG 5 KIỂU CẤU TRÚC
5.1. CÂU HỎI.
5.2. BÀI TẬP.
CHƯƠNG 6 KIỂU TẬP TIN
6.1. CÂU HỎI.
6.2. BÀI TẬP.
de tro lai dau chuong
trinh");
getch();}
}
}
/*********************************************************
****************/
void suadoi()
{ FILE *f;int ch,stt,n; data tam;
f=fopen("dulieu.dat","r+b");
if( f==NULL){ clrscr();
vedong;
printf("\n\n\n\n CHUA CO DU LIEU -
KHONG THE SUA DOI");
printf("\n\n\n NHAN FIM BAT KI DE TRO
VE DAU CHUONG TRINH\n");
vedong;xuongdong;
getch();}
185
n=n/sizeof(data);
else
{
fseek(f,0,SEEK_END);
n=ftell(f);
clrscr();
printf("Chuong trinh nay se sua doi du lieu\n\n");
printf(" CAC THONG TIN FILE:\n");
printf(" File luu du lieu la : DULIEU.DAT\n");
printf(" File co %d phan tu\n\n\n",n);
printf("\nBan co muon xem lai danh sach sinh vien
khong- C-K:");
printf("\n(GHI CHU :Ban nen xem lai danh sach de biet
thu tu sv muon sua\n sau khi xem xong nhan mot phim bat ki
de tiep tuc sua doi)");
ch=getch();
if( toupper(ch)=='C') {fsuadoi(); in(tfile);}
printf("\n Ban muuon sua doi sinh vien thu may:");
scanf("%d",&stt);
if (stt>n ) {printf("\n\n\n\ Danh sach chi co %d sinh
vien_ cho nen khong tim thay sinh vien nay\n",n);
printf("nhan phim bat ki de tro ve dau chuong
trinh_ de chon chuc nang khac\n");
getch();
}
else
{ clrscr();
printf("Chao mung den chuong trinh sua doi\n\n");
printf("\nDay la thong tin cua sinh vien ban muon sua
doi");
fseek(f,(stt-1)*sizeof(data),SEEK_SET);
fread(&tam,sizeof(data),1,f);
n=ftell(f);
n=n/sizeof(data);
printf(" \n + Sinh vien thu: %d",n);
printf(" \n + Ho lot : %s",tam.holot);
186
printf(" \n + Ten : %s",tam.ten);
printf(" \n + Ngay sinh : %s",tam.nt);
printf(" \n + Diem tin : %d",tam.dtin);
printf(" \n + Diem toan : %d",tam.dtoan);
printf("\nBan co muon sua nhung noi dung tren lai
khong- C-K :");
ch=getch();
if( toupper(ch)=='C')
{ nhapsua(&tam);
tam.stt=n;
fseek(f,(stt-1)*sizeof(data),SEEK_SET);
fwrite(&tam,sizeof(data) ,1,f);
printf("\n BAN DA SUA DOI XONG");
getch();
}
else { printf("\nTam biet !-Nhan phim bat ki de tro ve
dau chuong trinh- va chon chuc nang khac");
getch(); }
}
} fclose(f);
}
Bài 6. Viết chương trình đão ngược nội dung của một file văn bản.
#include
#include
#include
void docfile(int c,FILE *fp)
{
if (c!=EOF) docfile(getc(fp),fp);
putc(c,stdout);
if (wherey()>20) {getch();clrscr();}
}
/*--------------------------------------------------------
-------------------*/
void main()
{
187
FILE *fp;
char filename[80];
int c;
textcolor(10);
textbackground(1);
clrscr();
printf("\n\tCHUONG TRINH DAO NGUOC NOI DUNG FILE DUA RA
STDOUT");
window(1,3,80,25);
printf("\n\tNhap ten file: ");
fflush(stdin);
gets(filename);
if((fp=fopen(filename,"r"))==NULL)
{
cprintf("\n\tFile %s khong mo duoc",filename);
getch();
return 0;
}
clrscr();
printf("\n\t\t\tNOI DUNG FILE %s:\n",filename);
window(3,5,80,25);
c=getc(fp);
docfile(c,fp);
fclose(fp);
getch();
}
Bài 7. Viết chương trình in nội dung file văn bản, chỉ in các ký tự chữ cái và chữ số.
Tên file là đối số của hàm main.
#include
#include
#include
int kt(char *s)
{int i,dem;
for(i=0,dem=0;s[i]&&dem<2;i++)
if((s[i]='a')||(s[i]='A')||(s[i]<=
'9'&&s[i]>='0')||(s[i]==' '))
188
dem++;
return (dem>=2)?1:0;
}
/*--------------------------------------------------------
-------------------*/
main(int agrc,char *agrv[])
{
FILE *fp;
char s[100];
textcolor(10);
textbackground(1);
clrscr();
printf("\n\tCHUONG TRINH XUAT CAC DONG TRONG FILE CO
DANG KY TU IN DUOC");
window(1,3,80,25);
if (agrc==1)
{
printf("\n\tkhong co tham so");
getch();
return 0;
}
while(--agrc>0)
{
if((fp=fopen(*++agrv,"r"))==NULL)
{
cprintf("\n\tFile %s khong mo duoc",*agrv);
continue;
}
clrscr();
printf("\n\t\t\tNOI DUNG FILE %s:\n",*agrv);
window(3,5,80,25);
while(fgets(s,100-1,fp)!=NULL)
{if (kt(s)) fputs(s,stdout);
if (wherey()>20)
{
getch();
clrscr();
189
}
}
fclose(fp);
window(1,3,80,25);
}
getch();
return 0;
}
Bài 8. Viết một chương trình thực hiện các yêu cầu sau:
- Cho phép soạn thảo văn bản trên DOS.
- Các chức năng cơ bản: mở file mới, mở file đã có, lưu file, thoát, trợ giúp.
#include
#include
#include
#include
#include
#include
#include
void starting();
void typing();
void openfile(char *);
void newfile(char *);
void print(char i[],int x,int y);
char ch ;
char s[20];
char xs[20];
char ys[20];
void main()
{
clrscr();
textmode(BW40);
print(" MY NOTEPAD ",3,14);
190
textmode(BW80);
starting();
typing();
getch();
}
void starting()
{ textcolor(1);
textbackground(WHITE);
clrscr();
for(int i=0 ;i<45;i++)
{if(i==0||i==44)
cout<<"||";
else if(i==4)
cout<<" ctrl+O(Open file)";
else if(i==24)
cout<<" ctrl+n(New file)";
else
cout<<"=";}
gotoxy(80,24); cout<<endl;
for( i=0 ;i<37;i++)
{if(i==0||i==47)
cout<<"||";
else if(i==4)
cout<<" ctrl+s(save file)";
else if(i==16)
cout<<" ctrl+k(Help)";
else if(i==26)
cout<<"ctrl+q(Quit)";
else
cout<<"=";}
}
void typing()
{ int i=0;
char *p=new char[2000];
191
step1 : int row=2 ,col=1;
while(ch!=19)
{
gotoxy(col,row);
step2: if(col==79)
{col=1;row++;}
else if (row==24)
goto step1;
int a;
ch=getch();
if(ch == 0)
ch = getch();
a=ch; // Chuyen doi ky tu sang ma ascii
switch(a)
{
case 13 : row=row++;col=1; // Enter de sang dong moi
gotoxy(col,row);
ch='\n';
break;
case 8 :
if(col==1) // backspace
{row--;col=78;}
else
{col--;
cout<<" ";
i--;
}
continue;
case 9 : col=col+8; // tab
if(col>79)
{row++;col=col-79;}
continue;
192
case 72 : row--; //phim mui ten len
continue;
case 77 : col++; continue; //phim mui ten qua phai
case 75 : col--; continue; //phim mui ten qua trai
case 80 : //phim mui ten xuong
row++;
continue;
case 14 : clrscr(); //Tao file moi Ctrl+n
cout<<"Nhap vao ten duong dan: ";
gets(s);
newfile(s);
starting();
break;
case 15 : clrscr(); //Mo file da co Ctrl+o
cout<<"Nhap vao ten duong dan de mo file: " ;
gets(xs);
starting();
openfile(xs);
continue;
case 11 : clrscr();
gotoxy(20,3);
cout<<"~`~`~`~`~ TRO GIUP ~`~`~`~`~";
gotoxy(20,4);
cout<<"CAC PHIM SAU DUOC DUNG TRONG SOAN THAO: ";
int za=10;
for(int i=0;i<46 ;i++ )
{
gotoxy(za,5);
cout<<"=";za++;}
za=6;
for(i=0;i<15;i++)
193
{ gotoxy(10,za);
cout<<"|"<<endl;
za++;}
za=10;
for( i=0;i<46 ;i++ )
{
gotoxy(za,21);
cout<<"=";
za++;}
za=6;
for(i=0;i<15;i++)
{ gotoxy(56,za);
cout<<"|"<<endl;
za++;}
gotoxy(13,6);
cout<<" 1-Mo file da co bam Ctrl + o ";
gotoxy(13,8);
cout<<" 2-Tao file moi bam Ctrl+n " ;
gotoxy(13,10);
cout<<" 3-Di chuyen ca phim mui ten
(up,down,right,left)" ;
gotoxy(13,12);
cout<<" 4-Back Space ";
gotoxy(13,14);
cout<<" 5-Luu file (Ctrl+s)";
gotoxy(13,16);
cout<<" 6-Sang dong moi (Enter) ";
gotoxy(13,18);
cout<<" 7-Thoat Ctrl+q ";
gotoxy(13,20);
cout<<"Bam phim tuy y de quay ve " ;
getch();
clrscr();
starting();
194
continue;
case 17 : clrscr();
gotoxy(24,6);
cout<<"Cam on da su dung Notepad. ";
cout<<endl;
gotoxy(24,8);
cout<<"Hen gap lai!";
getch();
exit(0);
}
cout<<ch;
p[i]=ch;
col++; i++;
p[i]='\0';
}
clrscr();
cout<<"Enter the path where you want to save the file ";
gets(ys); int j=0;
ofstream f1(ys,ios::out);
while(p[j]!='\0')
{f1.write((char*)&p[j],sizeof(p[j]));
j++;}
f1.close();
delete [] p;
exit(0);
}
void newfile(char *s)
{
ofstream file(s,ios::out);
file.close();
}
void openfile(char *xs)
195
{ char ch; int col=3,row=2;
gotoxy(col,row);
ifstream file(xs,ios::in);
while( file.read((char*)&ch,sizeof(ch)))
cout<<ch;
file.close();
}
void print(char i[],int x,int y)
{
char far* ptr=(char far*) 0xB8000000+(160*x)+(2*y) ;
for(int a=0; i[a]!='\0'; a++)
{
*ptr=i[a];
*(ptr+1)=16;
ptr=ptr+2;
delay(250);
}
ptr=ptr+2;
}
196
1
3
16
19
40
54
89
92
118
119
162
163
MỤC LỤC
CHƯƠNG 1 CÁC THAO TÁC VÀO RA CƠ BẢN VÀ
CÁC CÂU LỆNH CÓ CẤU TRÚC
1.1. CÂU HỎI ...............................................................................
1.2. BÀI TẬP ................................................................................
Ch−¬ng 2 hµm
2.1. CÂU HỎI .............................................................................
2.2. BÀI TẬP ..............................................................................
CHƯƠNG 3 MẢNG VÀ CON TRỎ
3.1. CÂU HỎI .............................................................................
3.3. BÀI TẬP ..............................................................................
CHƯƠNG 4 CHUỖI KÝ TỰ
4.1. CÂU HỎI .............................................................................
4.2. BÀI TẬP ..............................................................................
CHƯƠNG 5 KIỂU CẤU TRÚC
5.1. CÂU HỎI ...........................................................................
5.2. BÀI TẬP ............................................................................
CHƯƠNG 6 KIỂU TẬP TIN
6.1. CÂU HỎI ...........................................................................
6.2. BÀI TẬP ............................................................................
File đính kèm:
Giáo trình Bài tập C và C++.pdf

