中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > 综合其它
c++模拟DOS的comand一个小程序
作者:未知 时间:2005-07-27 23:27 出处:CSDN 责编:chinaitpower
              摘要:c++模拟DOS的comand一个小程序

#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;
}


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有