MinDos--作業系統
總結 : 本次實作的是功能是為
(1)自行定義系統提示符
(2)自定義指令集(8-10個)
(3)使用者輸入HELP以查找指令的幫助
(4)列出指令的功能,區分内部還是外部指令
(5)使用者輸入QUIT退出
(6)内部指令有dir, cd, md, rd, cls, date, time, ren, copy等。
添加了system();實作window 指令操作
添加了 參數can 用于判斷指令是否帶參數
不足 :帶參數的指令 還不能實作 system 操作
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#define Max 500
struct Cmd{
char cmd[30];
char function[500];
cahr can;
int post;
};
int count=0;
void Init(struct Cmd cm[Max])
{
FILE *fp;
if((fp=fopen("cmd.txt","a+"))==NULL)
{
printf("File open error!\n");
exit(0);
}
while(!feof(fp)&&fgetc(fp)!=EOF)
{
fseek(fp,-1L,SEEK_CUR);
fscanf(fp,"%s%s%c%d",&cm[count].cmd,&cm[count].function,&cm[count].can,&cm[count].post);
count++;
}
if(fclose(fp))
{
printf("Can not close the file!\n");
exit(0);
}
}
void display(struct Cmd cm[Max])
{
for(int i=0;i<count;i++)
{
printf("%-10s%s\n",strupr(cm[i].cmd),cm[i].function);
strlwr(cm[i].cmd);
}
}
void process(struct Cmd cm[Max])
{
char str[10];
printf("Microsoft Windows XP [版本 5.1.2600]\n");
printf("(C) 版權所有 1985-2001 Microsoft Corp.\n");
while(strcmp(str,"quit")!=0){
printf("\nC:\\Documents and Settings\\hskd>");
scanf("%s",str);
strlwr(str);
bool flag=false;
if(strcmp(str,"help")==0)
{
printf("有關某個指令的詳細資訊,請鍵入 HELP 指令名\n");
display(cm);
flag=true;
}else{
for(int i=0;i<count;i++)
{
if(strcmp(str,cm[i].cmd)==0)
{
if(cm[i].post==1)
printf("'%s' 内部指令輸入正确!\n該指令作用是:%s\n",str,cm[i].function);
else
printf("'%s' 外部指令輸入正确!\n該指令作用是:%s\n",str,cm[i].function);
//實作windwins 指令
system(str);
//判斷是否帶參數
if(cm[i].can=='t')
printf("帶參數指令\n");
else if(cm[i].can=='f')
printf("不帶參數指令");
else
printf("可帶參數指令");
flag=true;
break;
}
}
if(!flag){
if(strcmp(str,"quit")!=0)
printf("'%s' 不是内部或外部指令,也不是可運作的程式\n或批處理檔案。\n",str);
}
}
}
printf("\n程式結束!\n\n");
}
int main()
{
struct Cmd cm[Max];
Init(cm);
process(cm);
return 0;
}