I need a bit of help with a homework assignment. I\'m just starting to learn C a
ID: 3889232 • Letter: I
Question
I need a bit of help with a homework assignment. I'm just starting to learn C and linux and the assignment requires for the C program to be able to run in Linux. Anyway I need to create a simulation/interpreter that the program will first display a Welcome message followed by a prompt to enter a command. The commands are load filename, execute, debug, dump, help, assemble filename, directory, and exit. If the command requires a parameter and is missing, inform the reader that it's missing a parameter followed by pompting to enter a command again. Same for the conditon if the user gives a parameter but non is needed. If the command is unrecognizable, output to user that the command is unrecognized and suggest the "help" command. Only the help, directory, and exit commands need to function.
Help: will print out a list of avaiable commands.
Directory: will list the files stored in the current directory. By the use of system("ls"); and the library #include <stdlib.h>
Exit: will exit simulator
Also if possible for the interprter/program to recognize the first few letters of some commands (Ex. dir for directory). If you can include comments it would be greatly appreciated, Thank you.
//My idea was to use a while loop with char str[100]; printf() to prompt and scanf() to read the string "%s" and whatever the command was to store it in str and use if and else if for each command comparison.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char *cmd,*arg;
char str[100],s[100];
int i,j=0;
printf("Welcome to My Simulation .... ");
while(1)
{
printf("Enter a command : ");
fflush(stdin);
scanf(" %[^ ]s",str);
cmd=strtok(str," ");
arg=strtok(0," ");
if(strcasecmp(cmd, "load") == 0 || strcasecmp("assemble",cmd) == 0)
{
if(arg==0)
{
printf("Please kindly provide the filename : ");
fflush(stdin);
scanf("%s",s);
cmd=0;
memset(str,0x00,100);
}
}
else if (strcasecmp(cmd,"help") == 0)
{
printf("List of available commands that are supported by this simulation is ... ");
printf("1. load filename 2. execute 3. debug 4. dump 5. help 6. assemble filename 7. directory 8. exit ");
}
else if( strcasecmp(cmd,"directory") == 0)
system("ls");
else if(strcasecmp(cmd, "exit") == 0)
exit(0);
else if(strcasecmp(cmd,"execute") == 0 || strcasecmp(cmd,"dump") == 0 || strcasecmp(cmd,"debug")==0)
printf("command existed in my simulation ... ");
else
printf("Invalid command. Please type help to know the list of commands... ");
fflush(stdin);
}
return 0;
}
/*
Sample output:
==============
lenovo@lenovo-Vbox:~/chegg$ cc simu.c
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Welcome to My Simulation ....
Enter a command : load
Please kindly provide the filename : file
Enter a command : help
List of available commands that are supported by this simulation is ...
1. load filename
2. execute
3. debug
4. dump
5. help
6. assemble filename
7. directory
8. exit
Enter a command : debug
command existed in my simulation ...
Enter a command : ls
Invalid command. Please type help to know the list of commands...
Enter a command : directory
1to100.c 1to100.c~ a.out ccmplx cmplx cmplx.cpp cmplx.cpp~ err.txt fib.c fib.c~ lab5.c lab5.c~ numbers.txt simu.c simu.c~
Enter a command : assemble file
Enter a command : assemble
Please kindly provide the filename : myfile
Enter a command : execute
command existed in my simulation ...
Enter a command : exit
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.