“软件工程(C编码实践篇)”实验报告

实验二:命令行菜单小程序V1.0,具体要求参见视频

实验二:命令行菜单小程序V1.0

【孙剑峰 SA16225264 + 《软件工程(C编码实践篇)》MOOC课程作业http://mooc.study.163.com/course/USTC-1000002006

代码地址: http://git.shiyanlou.com/csxiaoyao/shiyanlou_cs122/src/master/lab2

1. 实验要求

1.1 代码风格规范

1.2 实验要求

  • 实现一个命令行的菜单小程序,执行某个命令时调用一个特定的函数作为执行动作,实现的命令个数不少于8个;
  • 类似ftp的help目录或者bash的help目录;
  • 程序循环、接收用户的命令,如help、others等命令;
  • 可以广泛通用的命令行菜单子系统组件,可方便地定制而嵌入到其他系统;

1.3 实验设计

在本次次实验中,我严格遵守代码书写规范,设计了8个简单的菜单命令:help、ls、mkdir、cp、mv、rm、find、logout,每个命令对应一个函数。

2. 实验过程

2.1 创建menu.c文件

命令如下:

cd Code/shiyanlou_cs122
mkdir lab2
cp lab1/hello.c lab2/menu.c

创建menu.c文件

2.2 vim配置

$vim ~/.vimrc

配置如下:

"显示行号"
set nu
"允许鼠标移动光标"
set mouse=a
"自动缩进"   
set autoindent  
"智能选择对齐方式"    
set smartindent  
"使用C语言对齐方式"    
set cindent  
"Tab键宽度为4个空格"    
set tabstop=4  
"换行自动缩进为4个空格"    
set shiftwidth=4  
"用空格代替制表符"    
set expandtab  
"搜索时关键字高亮"    
set hlsearch  
"语法检查、语法高亮"    
syntax on

2.3 menu.c代码

#include<stdio.h>
#include<stdlib.h>

int cmdHelp()
{
    printf( "***********************Help**********************\n"
            "*\t\t\t\t\t\t*\n"
            "*\thelp\tShow help list\t\t\t*\n"
            "*\tls\tList files\t\t\t*\n"
            "*\tpwd\tPrint working directory\t\t*\n"
            "*\tmkdir\tCreate directory\t\t*\n"
            "*\tcp\tCopy file\t\t\t*\n"
            "*\tmv\tMove or rename\t\t\t*\n"
            "*\trm\tDelete file\t\t\t*\n"
            "*\tprint\tPrint Code\t\t\t*\n"
            "*\tquit\tQuit menu program\t\t*\n"
            "*\t\t\t\t\t\t*\n"
            "*************************************************\n\n"
    );
    return 0;
}

int printCode()
{
    char c;
    FILE *fp = NULL;
    fp = fopen("menu.c", "r");
    if (NULL == fp)
    {
        printf("Error: menu.c does not exist!\n");
        return -1;
    }
    while (fscanf(fp, "%c", &c) != EOF)
        printf("%c", c);
    fclose(fp);
    fp = NULL;
    return 0;
}

int main()
{
    char cmd[128];
    cmdHelp();
    while (1)
    {
        printf("Menu->");
        scanf("%s", cmd);
        if (strcmp(cmd, "help") == 0)
        {
            cmdHelp();
        }
        else if (strcmp(cmd, "ls") == 0)
        {
            system(cmd);
        }
        else if (strcmp(cmd, "pwd") == 0)
        {
            system(cmd);
        }
        else if (strcmp(cmd, "mkdir") == 0)
        {
            system(cmd);
        }
        else if (strcmp(cmd, "cp") == 0)
        {
            system(cmd);
        }
        else if (strcmp(cmd, "mv") == 0)
        {
            system(cmd);
        }
        else if (strcmp(cmd, "rm") == 0)
        {
            system(cmd);
        }
        else if (strcmp(cmd, "print") == 0)
        {
            printCode();
        }
        else if (strcmp(cmd, "quit") == 0)
        {
            exit(0);
        }
        else
        {
            printf("Error: unsupported command, you can use 'help' to list the available commands\n");
        }
    }
}

2.4 运行结果

编译,使用./menu运行 图片描述 部分命令运行结果如下: 图片描述

图片描述

图片描述

3. 实验总结

通过本次实验,我主要有以下三个方面的收获:

  1. 详细了解了编程规范与格式
  2. 熟练运用linux系统的编程环境
  3. 熟练运用vim快捷键
最新评论
暂无评论~