Thông tin tài liệu
Bài tập lớn NLHĐH #include<iostream.h> #include<math.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> #include<string.h> #include<alloc.h> #include<stdio.h> int *tentt,*ten; //tentt: ten cac tt nhap vao, ten: danh sach ten tt xuat ra unsigned int *txh,*tth,*moctg,*daxh; //txh,tth:thoi gian xh va th cau tien trinh. daxh:danh dau tien trinh da xh trong hang doi int n,m; //n:so tien trinh,m:so o duoc ve //HAM NHAP DU LIEU void nhapdl() { cout<<"So tien trinh: "; cin>>n; tentt=(int*)malloc(n*sizeof (int)); txh=(unsigned int*)malloc(n*sizeof (int)); tth=(unsigned int*)malloc(n*sizeof (int)); daxh=(unsigned int*)malloc(n*sizeof (int)); gotoxy(35,4); cout<<"TEN TIEN TRINH"; gotoxy(51,4); cout<<"TG XUAT HIEN"; gotoxy(65,4); cout<<"TG THUC HIEN"; for(int i=0;i<n;i++) { gotoxy(35,5+i); cout<<" P "<<(i+1); tentt[i]=i+1; } for(i=0;i<n;i++) { gotoxy(51,5+i); cin>>txh[i]; gotoxy(65,5+i); cin>>tth[i]; } } //HAM SAP XEP LAI TIEN TRINH THEO THOI GIAN XUAT HIEN void quick_sort(int l,int r) { int t,tg; if(l<r) { t=txh[(l+r)/2]; int i=l,j=r; while(i<=j) { while(txh[i]<t) i++; while(txh[j]>t) j ; if(i<=j) { tg=txh[i]; txh[i]=txh[j]; txh[j]=tg; tg=tentt[i]; tentt[i]=tentt[j];tentt[j]=tg; tg=tth[i]; tth[i]=tth[j]; tth[j]=tg; i++;j ; } } quick_sort(l,j); quick_sort(i,r); } } //HAM THUC HIEN THUAT TOAN FCFS void FCFS(int &m) { //Dinh dang kieu chu cho ham outtextxy settextstyle(1,0,1); settextjustify(1,1); outtextxy(320,80,"Thuat toan FCFS"); int k=-99; m=n; ten=(int*)malloc(n*sizeof(int)); for(int i=0;i<n;i++) ten[i]=tentt[i]; int d1=0,d2=0; moctg=(unsigned int*)malloc(1*sizeof(int)); moctg[0]=0; while(d2<n) //Trong khi cac con tien trinh chua xuat hien { moctg=(unsigned int*)realloc(moctg,(d1+2)*sizeof(int)); if(moctg[d1]<txh[d2]) { m++; ten=(int*)realloc(ten,m*sizeof(int)); moctg[d1+1]=txh[d2]; for(int i=m-1;i>=d1;i ) ten[i+1]=ten[i]; ten[d1]=k; } else { moctg[d1+1]=moctg[d1]+tth[d2]; d2++; } d1++; } } //HAM THUC HIEN CUA THUAT TOAN SJF void thSJF(int *hangdoi, int &xh,int &so,unsigned int *tthhd,unsigned int *tconlai,int &m) { if(so>0 || xh<n) { //Lap hang doi for(int i=0;i<n;i++) if(txh[i]<=moctg[m] && daxh[i]==0) { so++; xh++; daxh[i]=1; hangdoi[so-1]=tentt[i]; tthhd[so-1]=tth[i]; tconlai[so-1]=tth[i]; } //Sap xep lai hang doi theo thoi gian thuc hien int tg; if(so>1) { for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) if(tthhd[j]>=tthhd[i]) { tg=hangdoi[i]; hangdoi[i]=hangdoi[j]; hangdoi[j]=tg; tg=tthhd[i]; tthhd[i]=tthhd[j]; tthhd[j]=tg; tg=tconlai[i]; tconlai[i]=tconlai[j]; tconlai[j]=tg; } } ten=(int*)realloc(ten,(m+1)*sizeof(int)); moctg=(unsigned int*)realloc(moctg,(m+2)*sizeof(int)); if(so==0) //Thuc hien khi so phan tu hang doi bang 0 { ten[m]=-99; moctg[m+1]=txh[xh]; m++; //tao mot o trong } else //Thuc hien khi so pha tu hang doi khac 0 { if(so>0) { ten[m]=hangdoi[so-1]; //Dua phan tu o dau hang doi vao thuc hien moctg[m+1]=moctg[m]+tconlai[so-1]; m++; for(int i=0;i<n;i++) //KT xem co tien trinh nao xuat hien va ngat duoc tien trinh dang thuc hien hay khong if(txh[i]<moctg[m] && daxh[i]==0 && tth[i]<tthhd[so-1]) { moctg[m]=txh[i]; tconlai[so-1]-=moctg[m]-moctg[m-1]; i=n+1; //Neu co tien trinh ngat -> dung tien trinh dang thuc hien } if(i==n) so ; //Neu khong co tien trinh ngat -> thuc hien xong tt dang th -> loai bo khoi hang doi } } if(so>0 || xh<n) //Neu hang doi con phan tu hoac con co pha tu chua xuat hien thSJF(hangdoi,xh,so,tthhd,tconlai,m); //Thuc hien lai voi hang doi theo de quy } } //HAM THUC HIEN THUAT TOAN SJF void SJF(int &m) { settextstyle(1,0,1); settextjustify(1,1); outtextxy(320,80,"Thuat toan SJF"); unsigned int *tthhd,*tconlai; int *hangdoi,so=0,xh=0; ten=(int*)calloc(m+1,sizeof(int)); moctg=(unsigned int*)calloc(m+2,sizeof(int)); hangdoi=(int*)calloc(n,sizeof(int)); tthhd=(unsigned int*)calloc(n,sizeof(int)); tconlai=(unsigned int*)calloc(n,sizeof(int)); m=0; moctg[0]=0; //Gan moc thoi gian dau tien bang 0 for(int i=0;i<n;i++) //Gan moi tien trinh deu o trang thai chua xuat hien trong hang doi daxh[i]=0; //Goi ham thuc hien cua SJF thSJF(hangdoi,xh,so,tthhd,tconlai,m); //outtextxy(640,480,"Xong RR ");; } //Ham thuc hien cua SRT (Chi khac SJF o cho kiem tra tien trinh chen ngang khi co tien trinh dang thuc hien) void thSRT(int *hangdoi, int &xh,int &so,unsigned int *tthhd,unsigned int *tconlai,int &m) { if(so>0 || xh<n) { //Lap hang doi for(int i=0;i<n;i++) if(txh[i]<=moctg[m] && daxh[i]==0) { so++; xh++; daxh[i]=1; hangdoi[so-1]=tentt[i]; tthhd[so-1]=tth[i]; tconlai[so-1]=tth[i]; } //Sap xep lai hang doi int tg; if(so>1) { for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) if(tconlai[j]>=tconlai[i]) { tg=hangdoi[i]; hangdoi[i]=hangdoi[j]; hangdoi[j]=tg; tg=tthhd[i]; tthhd[i]=tthhd[j]; tthhd[j]=tg; tg=tconlai[i]; tconlai[i]=tconlai[j]; tconlai[j]=tg; } } ten=(int*)realloc(ten,(m+1)*sizeof(int)); moctg=(unsigned int*)realloc(moctg,(m+2)*sizeof(int)); if(so==0) { ten[m]=-99; moctg[m+1]=txh[xh]; m++; } else { if(so>0) { ten[m]=hangdoi[so-1]; moctg[m+1]=moctg[m]+tconlai[so-1]; m++; int tam; for(int i=0;i<n;i++) if(txh[i]<moctg[m] && daxh[i]==0) { tam=tconlai[so-1]-(txh[i]-moctg[m-1]); if(tth[i]<tam) { moctg[m]=txh[i]; tconlai[so-1]=tam; i=n+1; } } if(i==n) so ; } } if(so>0 || xh<n) thSRT(hangdoi,xh,so,tthhd,tconlai,m); } } //HAM THUC HIEN THUAT TOAN SRT void SRT(int &m) { settextstyle(1,0,1); settextjustify(1,1); outtextxy(320,80,"Thuat toan SRT"); unsigned int *tthhd,*tconlai; int *hangdoi,so=0,xh=0; ten=(int*)calloc(m+1,sizeof(int)); moctg=(unsigned int*)calloc(m+2,sizeof(int)); hangdoi=(int*)calloc(n,sizeof(int)); tthhd=(unsigned int*)calloc(n,sizeof(int)); tconlai=(unsigned int*)calloc(n,sizeof(int)); m=0; moctg[0]=0; for(int i=0;i<n;i++) daxh[i]=0; thSRT(hangdoi,xh,so,tthhd,tconlai,m); //outtextxy(640,480,"Xong RR ");; } //Ham thuc hien cua RR void thRR(int q,int *hangdoi, int &xh,int &so,unsigned int *tthtr,unsigned int *tconlai,int &m) { if(so>0 || xh<n) //Neu hang doi con phan tu hoac con tien trinh chua xuat hien { if(so==0 && xh<n) //Neu so phan tu hang doi bang 0 -> dua phan tu xuat hien som nhat trong ds cac phan tu chua xh (tuc la daxh=0) vao hang doi { xh++; so++; int vt; for(int i=0;i<n;i++) if(daxh[i]==0) { vt=i; i=n; } hangdoi[so-1]=tentt[vt]; tthtr[so-1]=tconlai[vt]; moctg[m+1]=txh[vt]; ten[m]=-99; m++; ten=(int*)realloc(ten,(m+1)*sizeof(int)); moctg=(unsigned int*)realloc(moctg,(m+2)*sizeof(int)); if(so>0 || xh<n) //Thuc hien voi hang doi vua lap duoc thRR(q,hangdoi,xh,so,tthtr,tconlai,m); } if(so>0) // Neu so phan tu hang doi >0, dua phan tu dau hang doi vao thuc hien { if(tthtr[so-1]<=q) //Neu t thuc hien con lai cua no <=q ->thuc hien xong { moctg[m+1]=moctg[m]+tthtr[so-1]; ten[m]=hangdoi[so-1]; so ; int vt; for(int i=0;i<n;i++) if(tentt[i]==ten[m]) { vt=i;i=n; } tconlai[vt]=0; daxh[vt]=1; } else //Neu t thuc hien con lai >q -> dua no ve cuoi hang doi { moctg[m+1]=moctg[m]+q; ten[m]=hangdoi[so-1]; for(int i=so-1;i>0;i ) { hangdoi[i]=hangdoi[i-1]; tthtr[i]=tthtr[i-1]; daxh[i]=daxh[i-1]; } hangdoi[0]=ten[m]; int vt; for(i=0;i<n;i++) if(tentt[i]==ten[m]) { vt=i;i=n; } tconlai[vt]-=q; tthtr[0]=tconlai[vt]; daxh[vt]=1; } m++; ten=(int*)realloc(ten,(m+1)*sizeof(int)); moctg=(unsigned int*)realloc(moctg,(m+2)*sizeof(int)); } for(int i=0;i<n;i++) // Kiem tra neu co tien trinh xuat hien khi tien trinh cu dang thuc hien thi dua tt moi vao vi tri dau tien cua hang doi if(txh[i]<=moctg[m] && daxh[i]==0) { so++; hangdoi[so-1]=tentt[i]; tthtr[so-1]=tconlai[i]; daxh[i]=1; xh++; } if(so>0 || xh<n) //Thuc hien voi hang doi hien co thRR(q,hangdoi,xh,so,tthtr,tconlai,m); } } //HAM THUC HIEN THUAT TOAN RR void RR(int &m) { int q; cout<<"Luong tu thoi gian q= "; cin>>q; settextstyle(1,0,1); settextjustify(1,1); outtextxy(320,80,"Thuat toan RR"); m=0; int *hangdoi,so=0; unsigned int *tthtr,*tconlai; ten=(int*)calloc(m+1,sizeof(int)); moctg=(unsigned int*)calloc(m+2,sizeof(int)); hangdoi=(int*)calloc(n,sizeof(int)); tthtr=(unsigned int*)calloc(n,sizeof(int)); tconlai=(unsigned int*)calloc(n,sizeof(int)); moctg[0]=0;int xh=0; //truoc khi thuc hien, gan tg con lai cua cactt=tth, cac tt o trang thai chua xuat hien trong hang doi for(int i=0;i<n;i++) { tconlai[i]=tth[i]; daxh[i]=0; } //lap hang doi dau tien tai moc thoi gian bang 0 for(i=0;i<n;i++) if(txh[i]<=moctg[0] && daxh[i]==0) { so++; hangdoi=(int*)realloc(hangdoi,so*sizeof(int)); hangdoi[so-1]=tentt[i]; tthtr=(unsigned int*)realloc(tthtr,so*sizeof(int)); tthtr[so-1]=tconlai[i]; daxh[i]=1; xh++; } thRR(q,hangdoi,xh,so,tthtr,tconlai,m); outtextxy(640,480,"Xong RR ");; } //Ham ve so do Grant voi dau vao la so o can ve m,mang ten,mang moctg void vesodogrant(int ten[],int m,unsigned int moctg[]) { setbkcolor(BLUE); settextjustify(1,2); settextstyle(0,0,1); int ax,ay,bx,by,rong=40; //Toa do cac o vuong can ve int d=0,i=0,j=0; //d:So o can ve; i:Ve o theo hang; j:Xuong dong moi khi het hang char *s; while(d<m) //Lap trong khi chua ve het { bx=30+210+i*rong; if(bx>620) //Neu so o vuong khong the chua het tren mot hang->xuong hang { i=0;j++;} ax=210+i*rong; ay=120+5+j*40; bx=210+rong+i*rong; by=120+5+20+j*40; //Tinh toa do cac o vuong setcolor(WHITE); rectangle(ax,ay,bx,by); //Ve o vuong itoa(ten[d],s,10); setcolor(YELLOW); settextjustify(1,1); outtextxy((ax+bx+textwidth("P"))/2,(ay+by)/2,s); //Xuat cac ten tien trinh settextjustify(2,1); outtextxy((ax+bx+textwidth("P"))/2-textwidth(s)/2,(ay+by)/2,"P"); settextjustify(1,2); setcolor(LIGHTGRAY); itoa(moctg[d],s,10); outtextxy(ax,by+3,s); //Xuat cac moc thoi gian if(i==360/rong) { itoa(moctg[d+1],s,10); outtextxy(bx,by+3,s); } if(ten[d]==-99) //Tao o trong {setfillstyle(0,0); bar(ax+3,ay+3,bx-3,by-3);} d++; i++; //Dem tong so o da ve } itoa(moctg[d],s,10); outtextxy(bx,by+3,s); } //Ham kt de tinh thoi gian cho doi cho cac tien trinh int ktcomat(int a,int ds[],int &d) { int i=0; while(i<=d && a!=ds[i]) i++; if(i<=d) return 1; else return 0; } //HAM TINH THOI GIAN CHO DOI TRUNG BINH float TGCDTB() { int *ds,*tkt,*tcd; //ds:cac tien trinh da duoc tinh, tkt:mang luu tg ket thuc cua cac tien trinh //tcd:mang luu thoi gian cho cua cac tien trinh float ttb; //ttb:thoi gian cho doi trung binh ds=(int*)calloc(1,sizeof(int)); tkt=(int*)calloc(n,sizeof(int)); tcd=(int*)calloc(n,sizeof(int)); int i=m-1,d=0; ds[0]=-99; while(d<=m+1 && i>=0) //Duyet nguoc tu cuoi mang ten,moctg.Neu tt nao chua co mat trong ds cac tien trinh da duoc tinh tg kt -> lay moctg[i+1] la tg ket thuc { if(!ktcomat(ten[i],ds,d)) { tkt[d]=moctg[i+1]; d++; ds=(int*)realloc(ds,(d+1)*sizeof(int)); ds[d]=ten[i]; } i ; } //Sap lai mang thoi gian ket thuc theo dung tentt for(i=0;i<n;i++) for(int j=1;j<n+1;j++) { if(ds[j]==tentt[i]) { d=tkt[i]; tkt[i]=tkt[j-1]; tkt[j-1]=d; d=ds[i+1]; ds[i+1]=ds[j]; ds[j]=d; } } //Tinh thoi gian cho doi=tg ket thuc-(tg xuat hien + tg thuc hien) for(i=0;i<n;i++) tcd[i]=tkt[i]-(txh[i]+tth[i]); int stcho=0; for(i=0;i<n;i++) stcho=stcho+tcd[i]; //Tinh tong tho gian cho ttb=(float)stcho/n; return ttb; } void goithieu() { settextstyle(1,0,3); settextjustify(1,1); outtextxy(320,10,"LAP LICH CHO CPU"); settextjustify(1,2); settextstyle(0,0,1); outtextxy(320,470,"Nhom1 KHMT4_K3 DH Cong Nghiep Ha Noi"); . Bài tập lớn NLHĐH #include<iostream.h> #include<math.h> #include<conio.h> #include<graphics.h> #include<stdlib.h> #include<string.h> #include<alloc.h> #include<stdio.h> int
Ngày đăng: 01/07/2014, 18:20
Xem thêm: Bài tập lớn NLHĐH pptx, Bài tập lớn NLHĐH pptx