一、目的和要求
1. 实验目的
(1)掌握命令解释程序的原理;
(2)*掌握简单的DOS调用方法;
(3)掌握C语言编程初步。
2.实验要求
编写类似于DOS,UNIX的命令行解释程序
(1)自行定义系统提示符
(2)自定义命令集(8-10个)
(3)用户输入HELP以查找命令的帮助
(4)列出命令的功能,区分内部还是外部命令
(5)用户输入QUIT退出
(6)内部命令有dir, cd, md, rd, cls, date, time, ren, copy等。
二、实验内容
根据教师指定的实验课题,完成设计、编码、测试工作。
三、实验环境
1.PC微机
2.Windows 操作系统
3.C/C++程序开发集成环境
四、实验原理及核心算法参考程序段
- 命令解释程序的原理:
命令解释程序的主要功能是:
1) 接受和执行一条用户从键盘输入的命令,它通常保存一张命令名字(动词)表,其中记录着所有操作命令及其处理程序的入口地址或有关信息。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 #define MAXLINE 1000 6 #define N 100 7 #define Head1 "Microsoft Windows XP [版本 5.1.2600]" 8 #define Head2 "<C> 版权所有 1985-2001 Microsoft Corp." 9 #define Head3 "C:\Documents and Settings\Administrator>" 10 11 12 void FakeTopTitle() 13 { 14 printf(Head1" "); 15 printf(Head2" "); 16 printf(" "); 17 //printf(Head3); 18 } 19 20 /* 21 void ReadFromDocument() 22 { 23 FILE *fp; 24 char arr[MAXLINE+1]; 25 printf(" "); 26 if ((fp = fopen ("CommandLine.txt", "r")) == NULL) 27 { 28 perror ("File open error! "); 29 exit (1); 30 } 31 while ((fgets (arr, MAXLINE, fp)) != NULL) 32 { 33 fputs (arr, stderr); 34 } 35 printf(" "); 36 printf(Head3); 37 } 38 */ 39 /* 40 void Test() 41 { 42 char s[200][200] = {0}; 43 int i=0; 44 FILE *fp; 45 fp = fopen("CommandLine.txt","r"); 46 printf(" "); 47 for(i=0;i<200;i++) 48 { 49 fscanf(fp,"%s",s[i]); 50 printf("%s ",s[i]); 51 } 52 } 53 */ 54 55 int main() 56 { 57 int i; 58 char a[100][100]={"assoc","attrib","break","bootcfg","cacls","call","cd","chcp","chdir", 59 "chkdsk","chkntfs","cls","cmd","color","comp","compact","convert", 60 "copy","date","del","dir","diskcomp","diskcopy","diskpart","doskey", 61 "driverquery","echo","endlocal","erase","eventquery","fc", 62 "find","for","format","fsutil","ftype","goto","gpresult","help","if", 63 "label","md","mkdir","mode","more","move","openfiles","pagefileconfig", 64 "path","pause","popd","print","prompt","pushd","rd","recover","rem", 65 "ren","rename","replace","rmdir","set","setlocal","sc","schtasks", 66 "shift","shutdown","sort","start","subst","systeminfo","tasklist", 67 "taskkill","time","title","tree","type","ver","verify","vol","xcopy", 68 "wmic"}; 69 char b[100]; 70 FakeTopTitle(); 71 loop1:printf(Head3); 72 gets(b); 73 for(i=0;i<N;i++) 74 { 75 if(strcmp(a[i],b)==0) 76 { 77 system(b); 78 printf(" "); 79 printf(Head3); 80 getchar(); 81 } 82 else 83 { 84 break; 85 } 86 if(strcmp(b,"exit")==0) 87 { 88 exit(0); 89 getchar(); 90 } 91 } 92 system(b); 93 goto loop1; 94 getchar(); 95 return 0; 96 }