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

#include<stdio.h>
#include<string.h>
int main(){
        char cmd[9][10]={"dir","cd","md","rd","cls","date","time","ren","copy"};
        char t[2][5]={"help","quit"};
        char str[10];   //定义数组,将所需命令存入数组内,需要时调用。

        do{
            printf("请输入命令提示符:");
            scanf("%s",str);      //gets(str);
    
            if(strcmp(str,cmd[0])==0)
                printf(" dir 查看当前目录的文件和文件夹\n");    
            else if(strcmp(str,cmd[1])==0)
                printf("cd 目录名: 进入特定的目录\n");
            else if(strcmp(str,cmd[2])==0)
                printf("md 目录名: 建立特定的文件夹\n");
            else if(strcmp(str,cmd[3])==0)
                printf("rd 目录名: 删除特定的目录\n");
            else if(strcmp(str,cmd[4])==0)
                printf("cls 目录名: 表示清屏功能\n");
            else if(strcmp(str,cmd[5])==0)
                printf("date:功能是设置日期\n");
            else if(strcmp(str,cmd[6])==0)
                printf("time: 设置或显示系统时间\n");
            else if(strcmp(str,cmd[7])==0)
                printf("ren: 修改文件名\n");
             else if(strcmp(str,cmd[8])==0)
                printf("copy: 把文件复制到另一个地方\n");
             else if(strcmp(str,t[0])==0)
            {   printf(" dir 查看当前目录的文件和文件夹\n");    
                printf("cd 目录名: 进入特定的目录\n");
                printf("md 目录名: 建立特定的文件夹\n");
                printf("rd 目录名: 删除特定的目录\n");
                printf("cls 目录名: 表示清屏功能\n");
                printf("date:功能是设置日期\n");
                printf("time: 设置或显示系统时间\n");
                printf("ren: 修改文件名\n");
                printf("copy: 把文件复制到另一个地方\n");
            }
            else
                printf("这不是内部命令,请重新输入\n");
        }while(strcmp(str,t[1])!=0);

    
       
}

实验感受:有好长一段时间没有用C语言编写程序了,对C语言的基本语法知识也有些忘记。通过这次命令解释程序的编写,我对字符串数组的定义,初始化,以及字符串数组在字符函数中的应用有了进一步的理解。

算法思想:定义字符串数组,将所需命令存入字符串数组,在需要时调用。用scanf语句gets( )语句实现键盘输入字符串。用字符串比较函数strcmp(str1,str2)比较两字符str1与str2是否相等(返回值为0),来实现键盘输入字符串与字符串数组定义的字符串是否相等,并输出相应命令解释符功能,通过if-else if-else if-else语句进行多次判断,用do while语句实现循环,当用户输入quit退出程序。

原文地址:https://www.cnblogs.com/family6/p/4385955.html