操作系统课程设计:如何用c或c++或java模拟dos文件的建立与使用?
发布网友
发布时间:2022-05-14 07:06
我来回答
共4个回答
热心网友
时间:2023-10-09 11:57
#include<string.h>
#include<iostream>
#include <iomanip>
#include "Graphics.h"
#include<conio.h>
using namespace std;
const int FDF=-2;
const int FFF=-1;
const int N=100;//存储空间
int fnum;//文件数量
struct FILEINFO{
char name[10];
int length;
int index[10];
};
FILEINFO file[10];
char DISK[N]; int blankspace;
void Graphindex(int last);
void printDISK()
{
int i;
cout<<"空闲块数:"<<blankspace<<endl;
for(i=0;i<N/5;i++){
cout<<" No."<<setw(3)<<i<<setw(4)<<DISK[i];
cout<<" No."<<setw(3)<<i+1*N/5<<setw(4)<<DISK[i+1*N/5];
cout<<" No."<<setw(3)<<i+2*N/5<<setw(4)<<DISK[i+2*N/5];
cout<<" No."<<setw(3)<<i+3*N/5<<setw(4)<<DISK[i+3*N/5];
cout<<" No."<<setw(3)<<i+4*N/5<<setw(4)<<DISK[i+4*N/5]<<endl;
}
}
void write(char *tmpname,int tmplength)
{
int i,j;
//复制文件名和文件块个数
strcpy(file[fnum].name,tmpname);
file[fnum].length=tmplength;
//存文件
for(i=0;i<tmplength;i++){
for(j=0;j<N;j++){
if(DISK[j]=='N'){
file[fnum].index[i]=j;//首个空闲块为文件开始块
DISK[j]='Y';
break;
}
}
}
blankspace-=tmplength;//改变空闲块个数
fnum++;
cout<<"name and size :"<<tmpname<<" "<<tmplength<<endl;
}
void insert(char *tmpname,int insertpoint)
{
int i;
int last;
//寻找要执行插入操作的文件,将其数组下标存入last
for(i=0;i<fnum;i++){
if(strcmp(file[i].name,tmpname)==0){
last=i;
break;
}
}
for(i=0;i<file[last].length-insertpoint;i++){
file[last].index[file[last].length-i*1]=file[last].index[file[last].length-(i+1)*1];
}
for(i=0;i<N;i++){
if(DISK[i]=='N'){
file[last].index[insertpoint]=i;
DISK[i]='Y';
break;
}
}
//改变空闲块个数与文件长度
file[last].length++;
blankspace--;
cout<<"name and size :"<<tmpname<<" "<<file[last].length<<endl;
}
void printindex(char *tmpname)
{
int last,i;
for(i=0;i<fnum;i++){
if(strcmp(file[i].name,tmpname)==0){
last=i;
break;
}
}
cout<<" "<<file[last].name<<" 索引表"<<endl;
cout<<" 逻辑记录号 物理块号"<<endl;
for(i=0;i<file[last].length;i++){
cout<<setw(7)<<i<<setw(10)<<file[last].index[i]<<endl;
}
Graphindex(last);
}
void itol(int i)
{
LPCTSTR yy;
char zz[10];
sprintf(zz, "%d", i);
//itoa(i,zz,10);
yy = LPCTSTR( zz );
//moveto(x+i*2,y+20);
moverel(25,9);
outtext(yy);
//return yy;
}
void ctol(char *c)
{
LPCTSTR yy;
yy = LPCTSTR( c );
moverel(0,0);
outtext(yy);
}
void Graphindex(int last)
{
int i,xx=100,yy=100,x=400,y=50;
initgraph(640, 480);
setfillstyle(SOLID_FILL,WHITE);
floodfill(5,5,WHITE);
setcolor(BLACK);
moveto(110,50); ctol(file[last].name);
outtextxy(150,50,"索引表");
outtextxy(115,80,"逻辑"); outtextxy(175,80,"物理");
rectangle(xx,yy-30,xx+60,yy); rectangle(xx+60,yy-30,xx+120,yy);
for(i=0;i<file[last].length;i++){
rectangle(xx,yy+i*30,xx+60,yy+(i+1)*30);
moveto(xx,yy+i*30); itol(i);
rectangle(xx+60,yy+i*30,xx+120,yy+(i+1)*30);
moveto(xx+60,yy+i*30); itol(file[last].index[i]);
moveto(xx+130,yy+i*30+15); lineto(x+(file[last].index[i]/20)*20+5,y+(file[last].index[i]%20)*20+5);
}
ellipse(x+45,25,0,360,60,15);
ellipse(x+45,440,180,360,60,15);
line(x-15,25,x-15,440);
line(x+105,25,x+105,440);
for(i=0;i<N;i++){
if(DISK[i]=='Y'){
setcolor(RED);
rectangle(x+(i/20)*20,y+(i%20)*20,x+(i/20)*20+10,y+(i%20)*20+10);
}else setcolor(GREEN);
rectangle(x+(i/20)*20,y+(i%20)*20,x+(i/20)*20+10,y+(i%20)*20+10);
}
getch();
closegraph();
}
void Graph()
{
int i,x=300,y=50;
initgraph(640, 480);
setfillstyle(SOLID_FILL,WHITE);
floodfill(5,5,WHITE);
setcolor(BLACK);
ellipse(x+45,25,0,360,60,15);
ellipse(x+45,440,180,360,60,15);
line(x-15,25,x-15,440);
line(x+105,25,x+105,440);
for(i=0;i<N;i++){
if(DISK[i]=='Y'){
setcolor(RED);
rectangle(x+(i/20)*20,y+(i%20)*20,x+(i/20)*20+10,y+(i%20)*20+10);
}else setcolor(GREEN);
rectangle(x+(i/20)*20,y+(i%20)*20,x+(i/20)*20+10,y+(i%20)*20+10);
}
getch();
closegraph();
}
void main()
{
int i;
char tmpname[10];
int tmplength;//要写入文件长度
int o;//命令
fnum=0;
for(i=0;i<N;i++){ //初始化FAT表
DISK[i]='N';
}
DISK[1]='Y';
blankspace=N;
while(1){
cout<<"请选择: 1.写入 2.插入 3.显示文件目录 4.显示DISK表"<<endl;
cin>>o;
switch(o){
case 1: cout<<"输入文件名:";
cin>>tmpname;
cout<<"输入文件长度:";
cin>>tmplength;
write(tmpname,tmplength);
break;
case 2: cout<<"输入文件名:"<<endl;
cin>>tmpname;
int insertpoint;
cout<<"输入插入点"<<endl;
cin>>insertpoint;
insert(tmpname,insertpoint);
break;
case 3: cout<<"输入文件名:"<<endl;
cin>>tmpname;
printindex(tmpname);break;
case 4: printDISK();Graph();break;
}
}
cin.get();
}
热心网友
时间:2023-10-09 11:58
可以,C++/CLI中的CLR项目就是用来做这个的,但是.NET界面要求对方电脑中安装了Framework,在中国通用性不好
另,你得单独学一下C++/CLI...其实和C#很像……
热心网友
时间:2023-10-09 11:58
你还是问问刘老师吧,他是最知道这个问题的答案,估计还会放你过的!:)
热心网友
时间:2023-10-09 11:59
话说,你是谁,小心我给你不及格。