实验一 命令解释程序的编写

一、实验目的

(1)掌握命令解释程序的原理;

(2)掌握简单的DOS调用方法;

(3)掌握C语言编程初步。

二、实验内容和要求

根据教师指定的实验课题,完成设计、编码、测试工作。

编写类似于DOS,UNIX的命令行解释程序

(1)自行定义系统提示符

(2)自定义命令集(8-10个)

(3)用户输入HELP以查找命令的帮助

(4)列出命令的功能,区分内部还是外部命令

(5)用户输入QUIT退出

(6)内部命令有dir, cd, md, rd, cls, date, time, ren, copy等。

三、实验方法、步骤及结果测试

1,源程序名:cpp1.cpp

可执行程序名:cpp1.cpp 

2.原理分析及流程图

运用数组合字符串,函数strcmp

3.主要程序段及其解释

  1 #include<stdio.h>
  2 #include<string.h>
  3 void main()
  4 {
  5     char cmd[30][30]={"dir","cd","md","rd","cls","date","time","ren","copy","help","quit"};
  6     char str[30];
  7     
  8     while(1)
  9     {
 10         printf("hi,please input the order:");
 11         gets(str);
 12         if(strcmp(str,cmd[0])==0)
 13         {
 14             printf("type:order of inside\n");
 15             printf("check the catalogue");
 16             printf("\n");
 17             printf("\n");
 18             //break;
 19         }
 20 
 21         else if(strcmp(str,cmd[1])==0)
 22         {
 23             printf("type:order of inside\n");
 24             printf("enter the catalogue");
 25             printf("\n");
 26             printf("\n");
 27             //break;
 28         }
 29 
 30         else if(strcmp(str,cmd[2])==0)
 31         {
 32             printf("type:order of inside\n");
 33             printf("make the new catalogue");
 34             printf("\n");
 35             printf("\n");
 36             //break;
 37         } 
 38 
 39 
 40         else if(strcmp(str,cmd[3])==0)
 41         {
 42             printf("type:order of inside\n");
 43             printf("delete the document");
 44             printf("\n");
 45             printf("\n");
 46             //break;
 47         }
 48 
 49         else if(strcmp(str,cmd[4])==0)
 50         {
 51             printf("type:order of inside\n");
 52             printf("clean the screen");
 53             printf("\n");
 54             printf("\n");
 55             //break;
 56         }
 57 
 58         else if(strcmp(str,cmd[5])==0)
 59         {
 60             printf("type:order of inside\n");
 61             printf("show or reset the date");
 62             printf("\n");
 63             printf("\n");
 64             //break;
 65         }
 66 
 67         else if(strcmp(str,cmd[6])==0)
 68         {
 69             printf("type:order of inside\n");
 70             printf("show or reset the time");
 71             printf("\n");
 72             printf("\n");
 73             //break;
 74         }
 75 
 76         else if(strcmp(str,cmd[7])==0)
 77         {
 78             printf("type:order of inside\n");
 79             printf("modify the name");
 80             printf("\n");
 81             printf("\n");
 82             //break;
 83         }
 84 
 85         else if(strcmp(str,cmd[8])==0)
 86         {
 87             printf("type:order of inside\n");
 88             printf("copy the document");
 89             printf("\n");
 90             printf("\n");
 91             //break;
 92         }
 93 
 94         else if(strcmp(str,cmd[9])==0)
 95         {
 96             printf("order\n");     
 97             printf("dir\t check the catalogue\n");              
 98             printf("cd\t enter the catalogue\n");                
 99             printf("md\t make the new catalogue\n");              
100             printf("rd\t delete the document\n");              
101             printf("cls\t lean the screen\n");             
102             printf("date\t show or reset the date\n");          
103             printf("time\t show or reset the time\n");          
104             printf("ren\t modify the name\n");        
105             printf("copy\t copy the document\n");        
106             printf("quit\t quit\n");         
107             printf("help\t the imformation u need\n");      
108             printf("\n");
109         }
110 
111         else if(strcmp(str,cmd[10])==0) 
112         {    
113             return; 
114         }
115    
116         else  
117         {   
118             printf("the order is wrong\n");    
119             printf("\n");
120         }
121 }
122 } 

4.运行结果及分析

四、实验总结

第一次上机课是编写命令指示,一看到这些命令指示符,还有很多指示符是不知道它是什么意思,所以有一些蒙。

原以为我们编写的还包括把系统的内容也导出显示出来,这让我想了好久,一点头绪都没有,然后就请教了度娘。原来只是编写命令指示符的基本输入和输出显示,这让我明朗了很多。

我还查了那些指示符的意思和参考了代码。代码中是用一个字符数组来输入命令指示符和字符串的运用,然后大框架就是输入的命令对应是数组显示出来,如果第一个命令指示符成功意味其它命令基本也能成功运行。原本不熟悉的东西又再运用了一遍。

我先是看了代码,然后理解一遍,再一边编写一边思考,过程有时候会有些混乱,不知道下一步怎么走,但静下心一步一步思考,就会有新内容新想法。

我的程序语言基础不好,所以常常会参考别的代码,希望通过练习可以有新想法新思路。

原文地址:https://www.cnblogs.com/zhengyh/p/4394711.html