MinDos--操作系统

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!
");
        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!
");
        exit(0);
    }
}
void display(struct Cmd cm[Max])
{
    for(int i=0;i<count;i++)
    {
        printf("%-10s%s
",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]
");
    printf("(C) 版权所有 1985-2001 Microsoft Corp.
");
    while(strcmp(str,"quit")!=0){
        printf("
C:\Documents and Settings\hskd>");
        scanf("%s",str);
        strlwr(str);
        bool flag=false;
        if(strcmp(str,"help")==0)
        {
            printf("有关某个命令的详细信息,请键入 HELP 命令名
");
            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' 内部命令输入正确!
该命令作用是:%s
",str,cm[i].function);
                    else
                        printf("'%s' 外部命令输入正确!
该命令作用是:%s
",str,cm[i].function);
                    //实现windwins 命令
                    system(str);
                    //判断是否带参数
                    if(cm[i].can=='t')
                        printf("带参数命令
");
                    else if(cm[i].can=='f')
                        printf("不带参数命令");
                    else
                        printf("可带参数命令");
                    flag=true;
                    break;
                }
            }
            if(!flag){
                if(strcmp(str,"quit")!=0)
                    printf("'%s' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
",str);
            }
        }
    }
    printf("
程序结束!

");
}
int main()
{
    struct Cmd cm[Max];
    Init(cm);
    process(cm);
    return 0;
}
原文地址:https://www.cnblogs.com/caishun/p/5316460.html