Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ * * * CSC A 4 8 - Intro to Computer Science II * * This is the program file where you will implement

/**
* CSC A48- Intro to Computer Science II
*
* This is the program file where you will implement your solution for
* assignment 1. Please make sure you read through this file carefully
* and that you understand what is provided and what you need to complete.
*
* You will need to have read the handout carefully. Parts where you have to
* implement functionality are clearly labeled TODO.
*
* Be sure to test your work thoroughly, our testing will be extensive and will
* check that your solution is *correct*, not only that it provides
* functionality.
*
* Developed by Mustafa Quraish for CSCA48
*(c) Mustafa Quraish
*/
#include "imgUtils.c"
// This lets the driver code override the image size if it needs to. Make sure
// you don't hard-code these values anywhere!
#ifndef SIZEX
#define SIZEX 512
#define SIZEY 512
#endif
/*---------------------------------------------------------------------------*/
/**
* This struct contains one node of the linked list, which represents a single
* command to the Turtle. It's field should include:
*
*- cmd : A char array of size 10 holding the command name
*
*- val : An integer that stores a parameter for the command (like forward,
* backward and colour).
*
*- next : A pointer to a struct of the same type, this is used for the
* linked list
*
* TODO: Complete this struct definition
****/
typedef struct cmdnode{
char cmd;
int val;
struct cmdnode* next;
} CmdNode;
/*---------------------------------------------------------------------------*/
CmdNode *newCmdNode(char cmd[10], int val){
/**
* This function allocates a new CmdNode struct and initializes it's values
* based on the input paramaters given. The next pointer is always
* initialized to NULL.
*
* If the 'cmd' parameter is not a correct command, then print
* "Invalid command.
" and return NULL.
*
* Note that we will always pass in a value here, even if the
* command doesn't need one. In this case, we can just ignore
* it. It saves us from having to make separate functions to
* deal with this.
*/
if(strcmp(cmd, "backward")==0||strcmp(cmd, "forward")==0||strcmp(cmd, "pendown")==0||
strcmp(cmd, "colour")==0||strcmp(cmd, "penup")==0||strcmp(cmd, "right")==0||strcmp(cmd, "left")==0){
CmdNode* new =(CmdNode*)calloc(1, sizeof(CmdNode*));
strcpy(new->cmd, cmd);
new-> val = val;
new-> next = NULL;
return new;
}
printf("Invalid command.
");
return NULL;
}
/*---------------------------------------------------------------------------*/
void printCommandList(CmdNode *head){
/**
* This function prints out each command in the linked list one after the
* other. Each command MUST also have a line number printed before it, this
* is what you will be using to modify / delete them. To do this, initialize
* a counter and then increment it for each command.
*
* Depending on whether or not the command needs an additional value
*(like forward, backward and colour), use one of the following statements
* to print out the information for each node:
*[ The format is "linenumber: command value" ]
*
* printf("%3d: %s %d
",...); [With a value]
*
* printf("%3d: %s
",...); [Without a value]
*
* Obviously, you also need to pass in the correct parameters to the print
* function yourself, this is just so you have the correct format.
*/
if(head == NULL){
return;
}
CmdNode *p = head;
for(int i =1; p != NULL; p = p->next, i++){
if(strcmp(p->cmd,"penup")==0|| strcmp(p->cmd,"pendown")==0){
printf("%3d: %s
", i, p->cmd);
}else{
printf("%3d: %s %d
", i, p->cmd, p->val);
}
}
}//TODO
/*---------------------------------------------------------------------------*/
void queryByCommand(CmdNode *head, char cmd[10]){
/**
* This function looks for commands in the linked list that match the input
* query. It prints them out in the same format as the printCommandList()
* function.
*
* Make sure that the line number of the commands that match is the same as
* the line number that would be printed by the printCommandList() function.
*
*--------------------------------------------------------------------------
*
* For instance, if your printCommandList function outputs
*
*0: penup
*1: forward 200
*2: right
*3: forward 50
*
* Then, if this function is called with the same list and cmd = "forward",
* then your output here should be
*
*1: forward 200
*3: forward 50
*
* TODO: Implement this function
*/
return;
}
/*---------------------------------------------------------------------------*/
int coun

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions

Question

2. Does your tone of voice vary with different students?

Answered: 1 week ago