c++模拟DOS的comand一个小程序

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

#include<fstream.h>

#include<string.h>

#include<dir.h>

#define Max 7

char *cmd[Max] ;

char *str;

char init_path[80];

int find;

char *path="C:\\WINDOWS"; // 设定路径

int dir(char *p)

{

system(p); //调用系统命令

return 0;

}

int cd(char *p)

{

int i ;

for (i=0;i<=strlen(p);i++)

p[i]=p[i+3];

p[i]='\0';

chdir(p);

return 0;

}

int del(char *p)

{

system(p);

return 0;

}

int copy(char *p)

{

system(p);

return 0;

}

int mkdir(char *p)

{

system(p);

return 0;

}

int cls(char *p)

{

system(p);

return 0;

}

int help(char *p)

{

system(p);

return 0;

}

int search(char *p)

{

ifstream file;

char filename[80];

int circlenum;

circlenum=0;

do

{

strcpy(filename,p);

strcat(filename,".exe");

file.open(filename);

if (file)

{

cout<<"this is a "<<filename<<" file"<<endl;

system(filename);

find=1;

file.close();

}

else

{

strcpy(filename,p);

strcat(filename,".com");

file.open(filename,ios::binary);

if (file)

{

cout<<"this is a "<<filename<<" file"<<endl;

system(filename);

find=1;

file.close();

}

else

{

strcpy(filename,p);

strcat(filename,".bat");

file.open(filename);

if (file)

{

cout<<"this is a "<<filename<<" file"<<endl;

system(filename);

find=1;

file.close();

}

}

}

if (circlenum==1)

{

chdir(init_path);

break;

}

if (find)

break;

else

{

++circlenum;

chdir(path);

}

}

while(circlenum<=1);

if (!find)

cout<<"bad cmd or file name"<<endl;

return find;

}

int main()

{

cmd[0]="dir";

cmd[1]="cd";

cmd[2]="del";

cmd[3]="mkdir";

cmd[4]="copy";

cmd[5]="cls";

cmd[6]="help";

char a[80];

char b[80];

int i,length;

do

{

for (i=0;i<80;i++)

b[i]=' ';

getcwd(init_path,80); //保存当前路径

cout<<init_path<<'>';

gets(a); //读入命令

i=0;

while ((a[i]!=' ')&&(a[i]!='\n'))

{

b[i]=a[i];

i++;

} //把字符命令存放在数组b中

if (a[i]==' ')

{

b[i]='\0';

}

str=b;

find=0;

if (strcmp(str,"exit")==0)

break;

for (i=0;i<=Max;i++)

{

if (strcmp(str,cmd[i])==0)

switch(i) // 判断是哪个内部命令

{

case 0: dir(a); find=1; break;

case 1: cd(a); find=1; break;

case 2: del(a); find=1; break;

case 3: mkdir(a); find=1; break;

case 4: copy(a); find=1; break;

case 5: cls(a); find=1; break;

case 6: help(a); find=1; break;

}

if (find) break;

}

if (!find)

search(str);

}

while(strcmp(str,"exit")!=0);

return 0;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航