实验一

Posted on 2016-03-25 15:30  18詹耀海  阅读(134)  评论(0编辑  收藏  举报

                            实验一、命令解释程序的编写实验
                        专业:商业软件工程 姓名:詹耀海 学号:201406114118
一、 实验目的
(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. 主要程序段及其解释:

#include<stdio.h>
#include<string.h>//在使用到字符数组时需要使用
#include<stdlib.h>
#define Max 500
struct Cmd{
char cmd[30];
char function[500];
int post;
};
int count=0;
void Init(struct Cmd cm[Max])
{
FILE *fp;
if((fp=fopen("cmd.txt","a+"))==NULL)
{
printf("File open error!\n");
exit(0);
}
while(!feof(fp)&&fgetc(fp)!=EOF)
{
fseek(fp,-1L,SEEK_CUR);
fscanf(fp,"%s%s%d",&cm[count].cmd,&cm[count].function,&cm[count].post);
count++;
}
if(fclose(fp))
{
printf("Can not close the file!\n");
exit(0);
}
}
void display(struct Cmd cm[Max])
{
for(int i=0;i<count;i++)
{
printf("%-10s%s\n",strupr(cm[i].cmd),cm[i].function);
strlwr(cm[i].cmd);
}
}
void process(struct Cmd cm[Max])
{
char str[10];
while(strcmp(str,"quit")!=0){
printf("\nC:\\Document and Settings\\Administrator>");
scanf("%s",str);
strlwr(str);
bool flag=false;
if(strcmp(str,"help")==0)
{
printf("请输入相关命令\n");
display(cm);
flag=true;
}else{
for(int i=0;i<count;i++)
{
if(strcmp(str,cm[i].cmd)==0)
{
if(cm[i].post==1)
printf("'%s' 属于内部,命令输入正确!\n该命令作用是:%s\n",str,cm[i].function);
else
printf("'%s' 属于外部,命令输入正确!\n该命令作用是:%s\n",str,cm[i].function);
flag=true;
break;
}
}
if(!flag){
if(strcmp(str,"quit")!=0)
printf("'%s' 不属于内部或外部命令,也不是可运行的程序\n或批处理文件。\n",str);
}
}
}
printf("\n程序结束!\n\n");
}
int main()
{
printf("Microsoft Windows XP [版本 5.1.2600]\n");
printf("(C) 版权所有 1985-2001 Microsoft Corp.\n");
struct Cmd cm[Max];
Init(cm);
process(cm);
return 0;
}

2. 运行结果及分析

 

 

四、 实验总结

在C语言中插入文件,之前没有学过,接受起来有难度,学到一些新的知识。

Copyright © 2024 18詹耀海
Powered by .NET 8.0 on Kubernetes