Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with the creation of the pre and post conditions for each function o

ID: 3773040 • Letter: I

Question

I need help with the creation of the pre and post conditions for each function of my program. Please see below.

#include <iostream>
#include <string.h>
using namespace std;
#define TOTAL_COMMANDS 7
struct command {
char name[1024];
// Store the name of the command
int *paramsCount;
// Stores the valid no. of params
char error[4096];
// Stores the valid errors
int differentParams;
// Store no. of different ways to specify params
int differentErrors; // Store no. of different ways to error can occur
char windows[1024]; // Store actual command for windows system
};
struct command* populateCommands(void);


void processCommands(char**, int, struct command*);

/* This function reads a line of words from the keyboard and puts them into array a
The size of the array is written into num
*/
void parse(char** a, int &num)
{ char c;
int i = 0, j = 0;
bool space = false; // Used to get rid of spaces in the end
bool nonempty = false; // Used to check whether the string is empty or not
cout << "dysh> " ; // Skip leading spaces
do{ c = cin.get(); }
while(c == ' ' && c != ' '); // Now treat the main part of the line
while(c != ' ')
{
if(c == ' ')
{ // Space means new word
space = true;
a[i][j] = '';
i++;
j = 0;
while(c == ' ')
{
c = cin.get(); // Skip a bunch of consequent spaces
}
}
else
{ nonempty = true;
space = false;
a[i][j] = c;
j++;
c = cin.get();
}
}
a[i][j] = ''; // Terminate the very last word
if(!space)
num = i + 1; // User hits enter right after typing a word
else num = i; // There is a space in the end
if(!nonempty)
num = 0; // The line is empty or made of spaces only
}
  
int main()
{
int n;
// Create a dynamic two-dimensional array of characters a[30][1024]
char** a = new char*[30];
if(!a)
{
cerr << "Memory Allocation error. ";
exit(1);
}
for(int i = 0; i < 30; i++)
{
a[i] = new char[1024];
if(!a[i])
{
cerr << "Memory allocation error. ";
exit(1);
}
}
// Create a list of commands
command *commands = populateCommands();
do{ // Sentinel-controlled loop
parse(a, n);
processCommands(a, n, commands);
for(int i = 0; i < n; i++)
{ // Print the array
cout << "a[" << i << "] = '" << a[i] << "'" << endl;
}
cout << "# of items: " << n << endl; // Print the size of the array
} while(strcmp(a[0], "QUIT"));
// Delete the array
for(int i = 0; i < 30; i++)
{
delete [] a[i];
}
delete [] a;
return 0;
}
//Implementation to populate commands RUN, LIST, COPY, CD, SHOW, HELP, QUIT
struct command* populateCommands()
{
struct command* commands = new struct command[7];
strcpy(commands[0].name, "RUN");
strcpy(commands[1].name, "LIST");
strcpy(commands[2].name, "COPY");
strcpy(commands[3].name, "CD");
strcpy(commands[4].name, "SHOW");
strcpy(commands[5].name, "HELP");
strcpy(commands[6].name, "QUIT");
commands[0].paramsCount = new int[1];
commands[0].paramsCount[0] = 1;
strcpy(commands[0].error, "Please specify the command to execute. ");
commands[0].differentParams = 1;
commands[0].differentErrors = 1;
strcpy(commands[0].windows, "cmd");
commands[1].paramsCount = new int[2];
commands[1].paramsCount[0] = 0;
commands[1].paramsCount[1] = 1;
strcpy(commands[1].error, "No Error. ");
commands[1].differentParams = 2;
commands[1].differentErrors = 0;
strcpy(commands[1].windows, "dir");
commands[2].paramsCount = new int[1];
commands[2].paramsCount[0] = 1024;
strcpy(commands[2].error, "Please specify the Destination Address. ");
commands[2].differentParams = 1024;
commands[2].differentErrors = 1;
strcpy(commands[2].windows, "copy");
commands[3].paramsCount = new int[1];
commands[3].paramsCount[0] = 1;
strcpy(commands[3].error, "Please specify the directory to change into. ");
commands[3].differentParams = 1;
commands[3].differentErrors = 1;
strcpy(commands[3].windows, "cd");
commands[4].paramsCount = new int[1];
commands[4].paramsCount[0] = 1;
strcpy(commands[4].error, "Please specify filename to view. ");
commands[4].differentParams = 1;
commands[4].differentErrors = 1;
strcpy(commands[4].windows, "more");
commands[5].paramsCount = new int[1];
commands[5].paramsCount[0] = 1;
strcpy(commands[5].error, "Please specify command to view help. ");
commands[5].differentParams = 1;
commands[5].differentErrors = 1;
strcpy(commands[5].windows, "help");
commands[6].paramsCount = new int[1];
commands[6].paramsCount[0] = 1;
strcpy(commands[6].error, "No Error. ");
commands[6].differentParams = 1;
commands[6].differentErrors = 0;
strcpy(commands[6].windows, "exit");
}
  
//
void processCommands(char** a, int num, struct command* commands)
{
if(num == 0)
{
cout << "Please enter the command. ";
return;
}
for(int i = 0; i < TOTAL_COMMANDS; i++)
{
cout << i << commands[i].name << a[0] << "sdsd";
if(strcmp(commands[i].name, a[0]) == 0)
{
if( (num - 1) != commands[i].differentParams)
cout << commands[i].error;
else
{
char actualCommand[4096];
}
break;
}
}
return;
}

Explanation / Answer

Pre and post conditions for each function are in bold

#include <iostream>
#include <string.h>
using namespace std;
#define TOTAL_COMMANDS 7
struct command
{
   char name[1024];
   // Store the name of the command
   int *paramsCount;
   // Stores the valid no. of params
   char error[4096];
   // Stores the valid errors
   int differentParams;
   // Store no. of different ways to specify params
   int differentErrors; // Store no. of different ways to error can occur
   char windows[1024]; // Store actual command for windows system
};
struct command* populateCommands(void);

void processCommands(char**, int, struct command*);

/* This function reads a line of words from the keyboard and puts them into array a
The size of the array is written into num
*/
/*
Pre-Condition : Two dimensional array of characters, a, should be allocated enoug memory
Post-Condition : a is updated with a command and its arguments from keyboard, num is updated with number of parameters of that comman
*/

void parse(char** a, int &num)
{
   char c;
   int i = 0, j = 0;
   bool space = false; // Used to get rid of spaces in the end
   bool nonempty = false; // Used to check whether the string is empty or not
   cout << "dysh> " ; // Skip leading spaces
   do
   {
       c = cin.get();
   } while(c == ' ' && c != ' '); // Now treat the main part of the line
   while(c != ' ')
   {
       if(c == ' ')
       { // Space means new word
           space = true;
           a[i][j] = '';
           i++;
           j = 0;
           while(c == ' ')
           {
               c = cin.get(); // Skip a bunch of consequent spaces
           }
       }
       else
       {
           nonempty = true;
           space = false;
           a[i][j] = c;
           j++;
           c = cin.get();
       }
   }
   a[i][j] = ''; // Terminate the very last word
   if(!space)
       num = i + 1; // User hits enter right after typing a word
   else
       num = i; // There is a space in the end
   if(!nonempty)
       num = 0; // The line is empty or made of spaces only
}

/*
This is the main method which is executed first, calss other functions and ends the program.
Pre-Condition : Nothing, main starts everything
Post-Condition : Stores all the commands in a 2-dimensional character array and prints them
*/

int main()
{
   int n;
   // Create a dynamic two-dimensional array of characters a[30][1024]
   char** a = new char*[30];
   if(!a)
   {
       cerr << "Memory Allocation error. ";
       exit(1);
   }
   for(int i = 0; i < 30; i++)
   {
       a[i] = new char[1024];
       if(!a[i])
       {
           cerr << "Memory allocation error. ";
           exit(1);
       }
   }
   // Create a list of commands
   command *commands = populateCommands();
   do
   { // Sentinel-controlled loop
       parse(a, n);
       processCommands(a, n, commands);
       for(int i = 0; i < n; i++)
       { // Print the array
       cout << "a[" << i << "] = '" << a[i] << "'" << endl;
       }
       cout << "# of items: " << n << endl; // Print the size of the array
   } while(strcmp(a[0], "QUIT"));
   // Delete the array
   for(int i = 0; i < 30; i++)
   {
       delete [] a[i];
   }
   delete [] a;
   return 0;
}
//Implementation to populate commands RUN, LIST, COPY, CD, SHOW, HELP, QUIT
/*
Pre-Condition : A structure of name command needs to be defined
Post-Condition : Creates an array of struct of size 7, populates the data in the array and return the pointer of struct array
*/

struct command* populateCommands()
{
   struct command* commands = new struct command[7];
   strcpy(commands[0].name, "RUN");
   strcpy(commands[1].name, "LIST");
   strcpy(commands[2].name, "COPY");
   strcpy(commands[3].name, "CD");
   strcpy(commands[4].name, "SHOW");
   strcpy(commands[5].name, "HELP");
   strcpy(commands[6].name, "QUIT");
   commands[0].paramsCount = new int[1];
   commands[0].paramsCount[0] = 1;
   strcpy(commands[0].error, "Please specify the command to execute. ");
   commands[0].differentParams = 1;
   commands[0].differentErrors = 1;
   strcpy(commands[0].windows, "cmd");
   commands[1].paramsCount = new int[2];
   commands[1].paramsCount[0] = 0;
   commands[1].paramsCount[1] = 1;
   strcpy(commands[1].error, "No Error. ");
   commands[1].differentParams = 2;
   commands[1].differentErrors = 0;
   strcpy(commands[1].windows, "dir");
   commands[2].paramsCount = new int[1];
   commands[2].paramsCount[0] = 1024;
   strcpy(commands[2].error, "Please specify the Destination Address. ");
   commands[2].differentParams = 1024;
   commands[2].differentErrors = 1;
   strcpy(commands[2].windows, "copy");
   commands[3].paramsCount = new int[1];
   commands[3].paramsCount[0] = 1;
   strcpy(commands[3].error, "Please specify the directory to change into. ");
   commands[3].differentParams = 1;
   commands[3].differentErrors = 1;
   strcpy(commands[3].windows, "cd");
   commands[4].paramsCount = new int[1];
   commands[4].paramsCount[0] = 1;
   strcpy(commands[4].error, "Please specify filename to view. ");
   commands[4].differentParams = 1;
   commands[4].differentErrors = 1;
   strcpy(commands[4].windows, "more");
   commands[5].paramsCount = new int[1];
   commands[5].paramsCount[0] = 1;
   strcpy(commands[5].error, "Please specify command to view help. ");
   commands[5].differentParams = 1;
   commands[5].differentErrors = 1;
   strcpy(commands[5].windows, "help");
   commands[6].paramsCount = new int[1];
   commands[6].paramsCount[0] = 1;
   strcpy(commands[6].error, "No Error. ");
   commands[6].differentParams = 1;
   commands[6].differentErrors = 0;
   strcpy(commands[6].windows, "exit");
}

/*
Pre-Condition : a should be populated with commands from keyboard, n should be updated with number of arguments of command in a, array of struct, commands, should be populated
Post-Condition : compares the command in a to list of commands and checks the number of parameters for that command
*/

void processCommands(char** a, int num, struct command* commands)
{
   if(num == 0)
   {
       cout << "Please enter the command. ";
       return;
   }
   for(int i = 0; i < TOTAL_COMMANDS; i++)
   {
       cout << i << commands[i].name << a[0] << "sdsd";
       if(strcmp(commands[i].name, a[0]) == 0)
       {
           if( (num - 1) != commands[i].differentParams)
               cout << commands[i].error;
           else
               char actualCommand[4096];
           break;
       }
   }
   return;
}