#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;
}
本文地址:http://com.8s8s.com/it/it21691.htm