【孙剑峰 SA16225264 + 《软件工程(C编码实践篇)》MOOC课程作业http://mooc.study.163.com/course/USTC-1000002006】
代码地址: http://git.shiyanlou.com/csxiaoyao/shiyanlou_cs122/src/master/lab2
在本次次实验中,我严格遵守代码书写规范,设计了8个简单的菜单命令:help、ls、mkdir、cp、mv、rm、find、logout,每个命令对应一个函数。
命令如下:
cd Code/shiyanlou_cs122
mkdir lab2
cp lab1/hello.c lab2/menu.c
$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
#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");
}
}
}
编译,使用./menu运行
部分命令运行结果如下:
通过本次实验,我主要有以下三个方面的收获:
学习时间 244分钟
操作时间 38分钟
按键次数 944次
实验次数 3次
报告字数 3874字
是否完成 完成