Experiment one, the programming of the command interpreter
Major: Business Software Engineering class name: Zhang Xin Phase No.: 201406114109
First, Experimental Purpose
(1) Master the Principle of command interpretation procedure;
(2) Master the simple Dos call method;
(3) Master C language programming preliminary.
Second, experimental content and requirements
Write a command-line interpreter similar to Dos,unix
(1) Self-defined system prompt
(2) Custom set of commands (8-10)
(3) User input Help to find commands
(4) List the function of the command, distinguish internal or external commands
(5) User input quit quit
(6) Internal commands are dir, CD, MD, RD, CLS, date, time, Ren, copy, etc.
Third, test methods, procedures and results
1. Source program name: The source program name MiniOS.cpp in the archive package file
executable program Name: MiniOS.exe
2. principle Analysis and Flowchart
This experiment is mainly to put the command in Notepad, and finally through the program to read the document, this approach is mainly convenient to change the data.
Build a command structure in the program that contains the command name, command function, the number of internal or external commands, parameters, such as:
struct cmd{
Char cmd[30]; Command name
Char function[500]; Command function
int post; Whether it is an internal command, or 1 otherwise 0
Char parameter[1000]; Parameters
};
Finally, by the user input string, the comparison is the same as the command name inside the Notepad, the same output all the information of the command,
If different, the output of this command is not an internal command or an external command. If the user enters quit, the program ends.
3.       Main program segment and its interpretation:
struct Cmd{char cmd[30]; command character Char function[500]; function int post; Internal or external};int count=0;void Init (struct CMD Cm[max])//Initialize {FILE *fp; Function prototype The first parameter is the file path and filename to open the file, the second parameter indicates how to open the file if ((Fp=fopen ("Cmd.txt", "A +") ==null)//Read the text document, A + opens the writable file {printf ( "File open error!\n"); exit (0);} while (!feof (FP) &&fgetc (FP)!=eof) {fseek (fp,-1l,seek_cur); The first is the file pointer, the second is the offset of the pointer, and the third is the start position of the pointer offset fscanf (FP, "%s%s%d", &cm[count].cmd,&cm[count].function,&cm[count] . Post);//Use fscanf to read text files count++;} if (fclose (FP))//close a file stream, use Fclose to output the last remaining data in the buffer to the disk file and release the file pointer and the associated buffer {printf ("Can not close the file!\n"); exit (0) ;}} void display (struct CMD Cm[max])//output {for (int i=0;i<count;i++) {printf ("%-10s%s\n", Strupr (Cm[i].cmd), Cm[i]. function);//strupr converts the string to uppercase strlwr (CM[I].CMD); STRLWR function--converts uppercase letters to lowercase}}void process (struct CMD Cm[max])//processing {char str[10];p rintf ("Microsoft Windows XP [version 5.1.2600] \ n ");p rintf (" (C) Copyright 1985-2001 Microsoft corp.\n "), while (strcmp (str," quit ")!=0) {//ComparisonString (case-sensitive) printf ("\nc:\\documents and Settings\\hskd>"), scanf ("%s", str); strlwr (str); bool Flag=false;if (strcmp ( STR, "help") ==0)//strcmp is a comparison of the size of two strings, two strings return 0, a positive value is returned when the first string is greater than the second string, or a negative value is returned. {printf ("For more information on a command, type the help command name \ n");d isplay (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 ' internal command entered correctly!) \ n the command function is:%s\n ", str,cm[i].function); elseprintf ("'%s ' external command entered correctly!) \ n the command function is:%s\n ", str,cm[i].function); system (str); flag=true;break;}} if (!flag) {if (strcmp (str, "quit")!=0) printf ("'%s ' is not an internal or external command, nor is it a running program \ n or a batch file.) \ n ", str);}}} printf ("\ nthe program ends! \ n ");} int main () {struct CMD cm[max];init (cm);p rocess (cm); return 0;}
4.operational results and analysis
Four, Experimental Summary
From the writing of the command interpreter more in-depth understanding of C + + Chinese document reading, experience the magic of command, suddenly found that after entering the new date, the date of their own computer changed. A little excitement in my heart.
0320-Experiment one command interpreter programming