bai_giai_6333

18 483 5
Tài liệu đã được kiểm tra trùng lặp
bai_giai_6333

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

op[]o

Bài tập thực hành cơ bản- môn Hệ Điều Hành năm 2009 GVHD: Phạm Đức Thành SVTH:Trần Thị Thanh Trâm_Lớp 06CT3 Bài tập thực hành cơ bản – môn Hệ Điều Hành Nhóm I :Các thao tác đọc /ghi nội dung file văn bản /nhị phân, khảo sát hình thức tổ chức dữ liệu trên file ( 2t TH ) Bài 1: Viết chương trình (VCT) nhập 1 số nguyên 2byte, 1 số nguyên 4byte, 1 chuỗi ký tự & lưu chúng vào 1 tập tin nhị phân (theo đúng thứ tự trên). Sau đó dùng 1 công cụ xem nội dung tập tin dưới dạng DUMP (dạng hexa) để khảo sát giá trị từng byte của tập tin & tìm hiểu lý do vì sao giá trị các byte là như vậy. #include <stdio.h> #include <conio.h> #include <string.h> int LuuFileNhiPhan(char *FileName,int n2,long n4,char *S) { FILE *f=fopen(FileName,"wb"); if(f==NULL) return 0; fwrite(&n2,sizeof(n2),1,f); fwrite(&n4,sizeof(n4),1,f); fwrite(S,strlen(S),1,f); fclose(f); return 1; } void main() { clrscr(); int n2; long n4; char S[256]; printf("\n Moi ban nhap n2 : "); scanf("%d",&n2); printf("\n Moi ban nhap n4 : "); scanf("%d",&n4); fflush(stdin); printf("\n Moi ban nhap chuoi S : "); gets(S); if(LuuFileNhiPhan("D:/Bai1.DAT",n2,n4,S)==0) printf("\nLoi"); else printf("\n Thanh cong"); } Bài 2: VCT nhập 1 số nguyên, 1 số thực, 1 chuỗi ký tự & lưu chúng vào 1 tập tin văn bản (trên 3 dòng khác nhau). Sau đó dùng 1 công cụ xem nội dung tập tin dưới dạng DUMP (dạng hexa) để khảo sát giá trị từng byte của tập tin & tìm hiểu lý do vì sao giá trị các byte là như vậy. #include <stdio.h> #include <conio.h> #include <string.h> int LuuFileVB(char *FileName, int n,float x, char *S) { FILE *f=fopen(FileName,"wt"); if(f==NULL) return 0; fprintf(f,"%d\n",n); fprintf(f,"%f\n",x); fprintf(f,"%s\n",S); fclose(f); return 1; } void main() { clrscr(); int n; float x; char S[256]; printf("\n Moi nhap mot so nguyen : "); scanf("%d",&n); printf("\n Moi nhap mot so thuc : "); scanf("%f",&x); fflush(stdin); //Rua bo dem ban phim printf("\n Moi nhap mot chuoi S : "); gets(S); if(LuuFileVB("D:/Bai2.txt",n,x,S)==0) printf("\nLoi"); else printf("\nThanh cong"); getch(); } Trang 1 Bài tập thực hành cơ bản- môn Hệ Điều Hành năm 2009 GVHD: Phạm Đức Thành SVTH:Trần Thị Thanh Trâm_Lớp 06CT3 Bài 3:VCT mã hóa nội dung của 1 tập tin & chương trình giải mã tương ứng. Bài 3 A:(Mã hóa) #include <stdio.h> #include <conio.h> #include <string.h> int DocFileVB(char *FileName,char *S) { FILE *f=fopen(FileName,"rt"); if(f==NULL) return 0; char S1[256]; S[0]=0; while(1) { char *P=fgets(S1,256,f); if(P==NULL) break; strcat(S,S1); } fclose(f); return 1; } int GhiFileVB(char *FileName,char *S) { FILE *f=fopen(FileName,"wt"); if(f==NULL) return 0; fprintf(f,"%s",S); fclose(f); return 1; } void MaHoa(char *P, char *C) { int l=strlen(P); for(int i=0;i<l;i++) C[i]=P[i]^3; C[l]=0;//Ket thuc chuoi } void main() { clrscr(); char S[256],P[256]; if(DocFileVB("D:/bai3.txt",S)==0) printf("\n Loi"); else { printf("\n Chuoi S = %s",S); MaHoa(S,P); if(GhiFileVB("D:/mahoa1.txt",P)==0 printf("\n Loi"); else printf("\n Thanhcong"); } getch(); } Bài 3B:( Giải Mã) #include <stdio.h> #include <conio.h> #include <string.h> int DocFileVB(char *FileName,char *S) { FILE *f=fopen(FileName,"rt"); if(f==NULL) return 0; char S1[256]; S[0]=0; while(1) { char *P=fgets(S1,256,f); if(P==NULL) break; strcat(S,S1); } fclose(f); return 1; } Trang 2 Bài tập thực hành cơ bản- môn Hệ Điều Hành năm 2009 GVHD: Phạm Đức Thành SVTH:Trần Thị Thanh Trâm_Lớp 06CT3 int GhiFileVB(char *FileName,char *S) { FILE *f=fopen(FileName,"wt"); if(f==NULL) return 0; fprintf(f,"%s",S); fclose(f); return 1; } void GiaiMa(char *C, char *P) { int l=strlen(C); for(int i=0;i<l;i++) P[i]=C[i]^3; P[l]=0; } void main() { clrscr(); char S[256],P[256]; if(DocFileVB("D:/mahoa1.txt",S)==0) printf("\n Loi"); else { printf("\n Chuoi S = %s",S); GiaiMa(S,P); if(GhiFileVB("D:/giaima2.txt",P)==0) printf("\n Loi"); else printf("\n Thanhcong"); } getch(); } Nhóm II: Xây dựng CT Shell – thao tác với các hàm ngắt ( 3t TH ) Bài 1: VCT giả lập các lệnh của DOS: MD,CD,RD,DEL,REN,TYPE, . bằng cách dùng các hàm của C. #include <stdio.h> #include <conio.h> #include <string.h> #include <process.h> #include <dir.h> #include <dos.h> char CacLenh[][10]={"MD","CD","RD","DEL","REN","TYPE"}; char Curdir[256]; int TachChuoi(char *S,char M[][50]) { int dem=0; char *p=strtok(S," "); while(p) { strcpy(M[dem++],p); p=strtok(NULL," "); } return dem; } void XuatMangLenh(char M[][50],int n) { for(int i=0;i<n;i++) printf("\n %s",M[i]); } int TimVT_X(char A[][10],int n, char *X) { for(int i=0;i<n;i++) if (strcmp(A[i],X)==0) return i; return -1; } Trang 3 Bài tập thực hành cơ bản- môn Hệ Điều Hành năm 2009 GVHD: Phạm Đức Thành SVTH:Trần Thị Thanh Trâm_Lớp 06CT3 void TaoThuMuc(char *DIRNAME) { int stat; stat = mkdir(DIRNAME); if (!stat) printf("Directory created\n"); else { printf("Unable to create directory\n"); exit(1); } } void ChuyenDoiThuMuc(char *DIRNAME) { if (chdir("\\")) //chuyen ve thu muc goc { perror("chdir()"); exit(1); } if (chdir(DIRNAME)) { perror("chdir()"); exit(1); } } void XoaThuMuc(char *DIRNAME) { int stat; stat = rmdir(DIRNAME); if (!stat) printf("\nDirectory deleted\n"); else { perror("\nUnable to delete directory\n"); exit(1); } } void XoaTapTin(char *file) { if (remove(file) == 0) printf("Removed %s.\n",file); else perror("\nLoi"); } void DoiTenTapTin(char oldname[],char newname[]) { if (rename(oldname, newname) == 0) printf("Renamed %s to %s.\n", oldname, newname); else perror("rename"); } int DocFileVB(char *FileName,char *S) { FILE *f=fopen(FileName,"rt"); if(f==NULL) return 0; char S1[256]; S[0]=0; while(1) { char *P=fgets(S1,256,f); if(P==NULL) break; strcat(S,S1); } fclose(f); return 1; } void XemNoiDungTapTin(char *FileName) { char S[256]; if(DocFileVB(FileName,S)==0) printf("\n Loi"); else printf("\n%s\n",S); } void GiaLapDOS(char M[][50]) { int vt=TimVT_X(CacLenh,6,M[0]); char DIRNAME[256]; Trang 4 Bài tập thực hành cơ bản- môn Hệ Điều Hành năm 2009 GVHD: Phạm Đức Thành SVTH:Trần Thị Thanh Trâm_Lớp 06CT3 switch(vt) { case 0: strcpy(DIRNAME,Curdir); strcat(DIRNAME,M[1]); TaoThuMuc(DIRNAME); break; case 1: strcpy(DIRNAME,M[1]); ChuyenDoiThuMuc(DIRNAME); break; case 2: strcpy(DIRNAME,Curdir); strcat(DIRNAME,M[1]); XoaThuMuc(DIRNAME); break; case 3: strcpy(DIRNAME,M[1]); XoaTapTin(DIRNAME); break; case 4: DoiTenTapTin(M[1],M[2]); break; case 5: XemNoiDungTapTin(M[1]); break; default:printf("\n Chua co lenh nay "); } } char *Current_directory(char *path) { strcpy(path,"D:\\"); path[0]='A' + getdisk(); getcurdir(0,path+3); return (path); } void main() { clrscr(); char MangLenh[6][50]; char ChuoiLenh[256]; while(1) { Curdir[0]=0; Current_directory(Curdir); fflush(stdin);//rua bo dem ban phim printf("%s>",Curdir); gets(ChuoiLenh); strupr(ChuoiLenh); int dem=TachChuoi(ChuoiLenh,MangLenh); if(strcmp(MangLenh[0],"EXIT")==0) break; if(dem >0) GiaLapDOS(MangLenh); } } Bài 2: VCT thực hiện công việc hiển thị mã ASCII & mã SCAN của 1 phím nhập vào, chương trình kết thúc khi nhấn phím 0 bên dãy NumPad. #include<stdio.h> #include<conio.h> #include<dos.h> #include<string.h> void main() { unsigned char result; int port=0x80; getch(); result=inportb(port); printf("Ky tu read from port 0x%X=0x%X la %c\n",port,result,result); getch(); } Bài 2(Sử dụng lập trình ngắt) #include <stdio.h> #include <dos.h> #include <conio.h> #include<string.h> Trang 5 Bài tập thực hành cơ bản- môn Hệ Điều Hành năm 2009 GVHD: Phạm Đức Thành SVTH:Trần Thị Thanh Trâm_Lớp 06CT3 void InPort_Byte() { unsigned char result; int port = 0x80; getch(); result = inportb(port); printf("Ky tu read from port 0x%X = 0x%X la %c\n",port, result,result); } int Get_Ascii_Code() { union REGS r; //b1: khai bao bien thanh ghi r.h.ah=0; //b2: cho biet chuc nang cua ham int86(0x16,&r,&r); //b3: goi ham ngat ban phim return r.h.al; //b4: tra ve ket qua } int Get_Scan_Code() { union REGS r; r.h.ah=0; int86(0x16,&r,&r); return r.h.ah; } unsigned Get_Code() { union REGS r; r.h.ah=0; int86(0x16,&r,&r); return r.x.ax; } void main() { unsigned kq=Get_Code(); printf("\nKet qua ascii=0x%X cua phim %c", kq & 0x00FF , kq & 0x00FF ); printf("\nKet qua scan=0x%X",( kq & 0xFF00 ) >> 8 ); getch(); } Bài 3 : Giả lập các lệnh MD, DEL, TYPE với các phần công việc chính được thực hiện bằng cách gọi ngắt. #include<stdio.h> #include<conio.h> #include<dos.h> unsigned TaoThuMuc(char *DirName) { union REGS inregs,outregs; //b1: khai báo các thanh ghi struct SREGS segregs; inregs.h.ah=0x39; // Cho biet chac nang cua hàm inregs.x.dx=FP_OFF(DirName);//b3: Truy?n vào d?a ch? thu m?c segregs.ds=FP_SEG(DirName); int86x(0x21,&inregs,&outregs,&segregs); // b4: G?i ng?t return outregs.x.ax; // b5: Tr? v? k?t qu? } void main() { clrscr(); unsigned kq=TaoThuMuc("D:/THUMUC1"); if(kq!=0) printf("\n Thanh cong"); else printf("\n That bai"); getch(); } Trang 6 Bài tập thực hành cơ bản- môn Hệ Điều Hành năm 2009 GVHD: Phạm Đức Thành SVTH:Trần Thị Thanh Trâm_Lớp 06CT3 Nhóm III: Thao tác với bộ nhớ chính ( 1t TH ) Bài 1: VCT cho người dùng nhập vào địa chỉ logic của 1 ô nhớ trong bộ nhớ chính và hiển thị ra màn hình (dưới dạng Hex) nội dung của 100h Byte bắt đầu tại ô nhớ đó. #include<stdio.h> #include<conio.h> #include<dos.h> void main() { unsigned char seg,off; printf("\n Moi nhap dia chi seg= "); scanf("%d",&seg); printf("\n Moi nhap dia chi off= "); scanf("%d",&off); unsigned *p=(unsigned *)MK_FP(seg,off); for(int i=0;i<100;i++) printf("%4.2X",p+i); getch(); } Bài 2: Tương tự như bài trên nhưng địa chỉ ô nhớ nhập vào là địa chỉ vật lý. #include<stdio.h> #include<conio.h> #include<dos.h> void main() { unsigned char AddPhysic; unsigned char seg,off; printf("\n\n Moi nhap dia chi vat ly= "); scanf("%d",&AddPhysic); seg=AddPhysic/10; off=AddPhysic%10; unsigned *p=(unsigned *)MK_FP(seg,off); for(int i=0;i<100;i++) printf("%4.2X",p+i); getch(); } Nhóm IV: thao tác với sector ( 2t TH ) Bài 1: VCT nhập vào địa chỉ logic của 1 sector, đọc & hiển thị ra màn hình (dưới dạng Hex) nội dung của sector đó. #include<stdio.h> #include<conio.h> #include<dos.h> #include<process.h> #include <graphics.h> int DocMotSector(int Sector_LG,unsigned char Buf[] ) { if(absread( 0 , 1 ,Sector_LG,Buf)!=0) // 0: đĩa A ; 1: đọc { perror("\n Loi "); return 0; } return 1; } void XuatNoiDungMotSector(unsigned char *Buf) { for(int i=0;i<512;i++) printf("%3.2X",buf[i]); } void main() { int Sector_LG; unsigned char Buf[512]; printf("\n Moi nhap dia chi sector logic= "); scanf("%d",&Sector_LG); if(DocMotSector(sector_LG,Buf)==1) XuatNoiDungMotSector(Buf); else printf("\n That bai"); getch(); } Trang 7 Bài tập thực hành cơ bản- môn Hệ Điều Hành năm 2009 GVHD: Phạm Đức Thành SVTH:Trần Thị Thanh Trâm_Lớp 06CT3 Bài 2: Tương tự như bài trên nhưng địa chỉ sector nhập vào là địa chỉ vật lý #include<stdio.h> #include<conio.h> #include<dos.h> #include<graphics.h> #include<bios.h> int DocMotSectorVL(int sector,int track,int head,unsigned char *buf) { int kq=biosdisk( 2 , 0 ,head,track,sector,1,&buf); //2: doc ; 3:ghi kq=kq&0x00; if(kq==0)return 1; perror("\n Loi");return 0; } void XuatNoiDungMotSector(unsigned char *buf) { for(int i=0;i<512;i++) printf("%3.2X",buf[i]); } void main() { int sector,track,head; unsigned char buf[512]; printf("\n Moi nhap dia chi sector vat ly "); printf("\n\t Sector= "); scanf("%d",&sector); printf("\n\t Track= "); scanf("%d",&track); printf("\n\t Head= "); scanf("%d",&head); if(DocMotSectorVL(sector,track,head,buf)==1) XuatNoiDungMotSector(buf); else printf("\n That bai"); getch(); } Bài 3: VCT nhập vào các thông số của 1 đĩa vật lý: số sector/track, số track/side, số side; sau đó nhập tiếp địa chỉ logic hoặc vật lý của 1 sector, tính & xuất ra địa chỉ tương ứng còn lại của sector đó. #include<stdio.h> #include<conio.h> #include<bios.h> class cSector { int SectorPerTrack,TrackPerSide,Sides; int SecNo,TrackNo,SideNo; int SectorLG; public: void NhapThongSoVL() { printf("\n Moi nhap thong so vat ly cua Disk"); printf("\n\t So Sector tren 1 track= ");scanf("%d",&SectorPerTrack); printf("\n\t So track tren 1 side= "); scanf("%d",&TrackPerSide); printf("\n\t So Sides= "); scanf("%d",&Sides); } void NhapSectorLG() { printf("\n\n Moi nhap Sector logic thu= "); scanf("%d",&SectorLG); } void NhapSectorVL() { printf("\n Moi nhap Sector vat ly thu= "); scanf("%d",&SecNo); printf("\n Moi nhap track thu= "); scanf("%d",&TrackNo); printf("\n Moi nhap Side thu= "); scanf("%d",&SideNo); } void TinhSectorLG()//Ham tinh SectorLG tu dc VL da nhap(SecNo,TrackNo,SideNo) { SectorLG=(TrackNo*SectorPerTrack*Sides)+(SideNo*SectorPerTrack)+(SecNo-1); } void TinhSectorVL() { int tam=SectorLG/SectorPerTrack; TrackNo=tam/Sides; SideNo=tam%Sides; Trang 8 Bài tập thực hành cơ bản- môn Hệ Điều Hành năm 2009 GVHD: Phạm Đức Thành SVTH:Trần Thị Thanh Trâm_Lớp 06CT3 SecNo=SectorLG%SectorPerTrack+1; } void XuatSectorLG() { printf("\n\t Sector logic= %d",SectorLG); } void XuatSectorVL() { printf("\n\t Sector thu= %d",SecNo); printf("\n\t Track thu= %d",TrackNo); printf("\n\t Side thu= %d",SideNo); } }; void main() { clrscr(); cSector objSec; objSec.NhapThongSoVL(); objSec.NhapSectorVL(); objSec.TinhSectorLG(); objSec.XuatSectorLG(); objSec.NhapSectorLG(); objSec.TinhSectorVL(); objSec.XuatSectorVL(); getch(); } BÀI 4: VCT nhập vào địa chỉ logic hoặc vật lý (tùy theo sự lựa chọn của người dùng) của 1 sector, đọc và lưu nội dung sector đó vào 1 file. Và CT thực hiện công việc tương ứng ngược lại (đọc nội dung file & lưu vào sector). BÀI 4A: (Nhập vào địa chỉ của 1 Sector,đọc và ghi nội dung Sector đó vào 1 file) Bài 4A1: (Nhập địa chỉ logic) #include<stdio.h> #include<string.h> #include<conio.h> #include<dos.h> #include<bios.h> class cSector { int SectorLG; unsigned char buf[512]; public: void NhapSectorLG() { do { printf("\n Moi nhap Sector logic thu= "); scanf("%d",&SectorLG); if(SectorLG<0||SectorLG>2879) printf("\n Ban nhap sai "); }while(SectorLG<0||SectorLG>2879) ; } int DocMotSectorLG() { if(absread( 0 ,1,SectorLG,&buf)!=0) // 0:O dia A; { perror("Loi"); return 0; } return 1; } void XuatNoiDung() { for(int i=0;i<512;i++) printf("%3.2X",buf[i]); } int LuuFileVB(char *FileName) { FILE *f=fopen(FileName,"wt"); if(f==NULL) return 0; for(int i=0;i<512;i++) fprintf(f,"%4.2X",buf[i]); fclose(f); return 1; } }; void main() { clrscr(); Trang 9 Bài tập thực hành cơ bản- môn Hệ Điều Hành năm 2009 GVHD: Phạm Đức Thành SVTH:Trần Thị Thanh Trâm_Lớp 06CT3 cSector objSec; objSec.NhapSectorLG(); objSec.DocMotSectorLG(); objSec.XuatNoiDung(); objSec.LuuFileVB("d:/test.txt"); getch(); } Bài 4A2(Nhập địa chỉ vật lý) #include<stdio.h> #include<conio.h> #include<dos.h> #include<bios.h> class cSector { int SecNo,TrackNo,SideNo; unsigned char S1[512]; unsigned char buf[512]; unsigned char S[512]; public: void NhapSectorVL() { printf("\n Moi nhap Sector vat ly thu= "); scanf("%d",&SecNo); printf("\n Moi nhap track thu= "); scanf("%d",&TrackNo); printf("\n Moi nhap Side thu= "); scanf("%d",&SideNo); } int DocMotSectorVL() { int kq=biosdisk( 2 , 0 ,SideNo,TrackNo,SecNo,1,&buf); // 2: Đọc; 0:O dia A kq=kq&0x00; if(kq==0) return 1; perror("Loi");return 0; } int LuuFileVB(char *FileName) { FILE *f=fopen(FileName,"wt"); if(f==NULL) return 0; for(int i=0;i<512;i++) fprintf(f,"%3.2X",buf[i]); fclose(f); return 1; } void XuatNoiDung() { for(int i=0;i<512;i++) printf("%3.2X",buf[i]); } }; void main() { clrscr(); cSector objSec; objSec.NhapSectorVL(); objSec.DocMotSectorVL(); objSec.XuatNoiDung(); objSec.LuuFileVB("e:/test1.txt"); getch(); } BÀI 4B: (Đọc nội dung file và lưu vào sector) Bài 4B1 ( Ghi một Sector vật lý ) #include<stdio.h> #include<conio.h> #include<dos.h> #include<bios.h> #include<string.h> class cSector { int SideNo,TrackNo,SecNo; unsigned char S[512]; public: int DocFileVB(char *FileName) { FILE *f=fopen(FileName,"rt"); if(f==NULL) return 0; for(int i=0;i<512;i++) fscanf(f,"%X",&S[i]); Trang 10 . Loi"); else { printf(" Chuoi S = %s",S); GiaiMa(S,P); if(GhiFileVB("D:/giaima2.txt",P)==0) printf(" Loi"); else. printf(" Moi nhap mot chuoi S : "); gets(S); if(LuuFileVB("D: /Bai2 .txt",n,x,S)==0) printf(" Loi"); else printf(" Thanh

Ngày đăng: 03/03/2013, 20:40

Tài liệu cùng người dùng