// Steve Jun, z3216638. // 25th May 2007 //Assignment 2 task 4 #include #include #include #define Pi 3.141592654 #define MAXARRAY 2000 #include // Node of a list typedef struct NodeT { int xStore; int yStore; float redStore; float blueStore; float greenStore; double directionStore; struct NodeT * next; } Node; // List with a pointer to the first (head) and the last (foot) // element of the list typedef struct ListT { Node * head; Node * foot; } List; List * makeNewList (){ // allocate space for the List struct List * l = (List*) malloc (sizeof(List)); // check if assert was succesfull assert (l); // list is empty, so head and foot don't point to a node yet l->head = NULL; l->foot = NULL; // return the list pointer return (l); } // insert a new item at the foot of the list void InsertAtHead(List *listp,int xStore ,int yStore ,float redStore ,float blueStore ,float greenStore ,double directionStore); void deleteFoot(List *listp); void deleteHead(List *listp); // create a new list and return the pointer to the list //List * makeNewList (); int main (int argc, char ** argv) { int lineLength=0; double direction[MAXARRAY]; double ddirection=0; char grammar; double aangle; int k=0; int h=0; int xStart[MAXARRAY]; int yStart[MAXARRAY]; int xEnd[MAXARRAY]; int yEnd[MAXARRAY]; float red=1; //red float green=0; //green float blue=0; //blue // float redStore[MAXARRAY]; // float greenStore[MAXARRAY]; // float blueStore[MAXARRAY]; // int xStore[MAXARRAY]; // int yStore[MAXARRAY]; // double directionStore[MAXARRAY]; // int x=0; //store counter List *listp = makeNewList(); scanf("%d", &lineLength); scanf("%lf", &ddirection); scanf("%lf", &aangle); direction[k]=ddirection; xStart[k]= 0; yStart[k]= 0; while ((grammar = getchar()) !=EOF){ if (grammar!='\n'){ if (grammar=='G'){ green=green+0.1; printf("greenis : %f\n",green); } if (grammar=='B'){ blue=blue+0.1; } if (grammar=='R'){ red=red+0.1; } if (grammar=='g'){ green=green-0.1; } if (grammar=='b'){ blue=blue-0.1; } if (grammar=='r'){ red=red-0.1; } if (green<0 || green>1){ printf("Error, your green is out of range\n"); } if (blue<0 || blue>1){ printf("Error, your blue is out of range\n"); } if (red<0 || red>1){ printf("Error, your red is out of range\n"); } if (grammar=='+'){ ddirection=ddirection+aangle; direction[k]=ddirection; if (direction[k]<0){ direction[k]=direction[k]+2*Pi; } xEnd[k] = lineLength*sin(direction[k]); yEnd[k] = lineLength*cos(direction[k]); } if (grammar=='-'){ ddirection=ddirection-aangle; direction[k]=ddirection; if (direction[k]<0){ direction[k]=direction[k]+2*Pi; } xEnd[k] = lineLength*sin(direction[k]); yEnd[k] = lineLength*cos(direction[k]); } if (grammar=='F'){ h++; direction[k]=ddirection; if (direction[k]<0){ direction[k]=direction[k]+2*Pi; } xEnd[k] = lineLength*sin(direction[k]) + xStart[k]; yEnd[k] = lineLength*cos(direction[k]) + yStart[k]; printf("%d ",xStart[k]); printf("%d ",yStart[k]); printf("%d ",xEnd[k]); printf("%d ",yEnd[k]); printf("%f ",red); printf("%f ",green); printf("%f \n",blue); k++; } if(grammar=='f'){ h++; direction[k]=ddirection; if (direction[k]<0){ direction[k]=direction[k]+2*Pi; } xEnd[k] = lineLength*sin(direction[k]) + xStart[k]; yEnd[k] = lineLength*cos(direction[k]) + yStart[k]; k++; } if(grammar=='['){ InsertAtHead(listp, xStart[k], yStart[k], red, blue, green, ddirection); /* xStore[x]=xStart[k]; yStore[x]=yStart[k]; redStore[x]=red; blueStore[x]=blue; greenStore[x]=green; directionStore[x]=ddirection; */ // x++; } if(grammar==']'){ k--; deleteHead(listp); xStart[k]=listp->head->xStore; yStart[k]=listp->head->yStore; red=listp->head->redStore; blue=listp->head->blueStore; green=listp->head->greenStore; ddirection=listp->head->directionStore; } if(grammar!=']'){ yStart[k] = yEnd[k-1]; xStart[k] = xEnd[k-1]; } } } return (0); } void InsertAtHead(List *listp,int xStore ,int yStore ,float redStore ,float blueStore ,float greenStore ,double directionStore) { // allocate space for the new node struct Node * node = (Node *) malloc (sizeof(Node)); // check if assert was succesfull assert (node); // initialise data field of the node node->xStore = xStore; node->yStore = yStore; node->redStore = redStore; node->blueStore= blueStore; node->greenStore= greenStore; node->directionStore= directionStore; // make sure the list pointer is not NULL assert (listp); // let node point to first node in list node->next = listp->head; // the new head of the list is the new node listp->head = node; // if list was empty, foot is still NULL, and // head and foot should point to the same node // since the list has only one element now if (listp->foot == NULL) { listp->foot = node; } } void deleteHead(List *listp) { Node *node; assert(listp != NULL); // if head and foot are the same then // the operation is identical to deleteFoot if (listp->head == listp->foot) { deleteFoot(listp); } else { // delete and update head node = listp->head; listp->head = node->next; free(node); } } void deleteFoot(List *listp) { Node *node; // check pointers assert(listp); if (listp->head != NULL) { if (listp->head == listp->foot) { free(listp->foot); //initList(listp); } else { // find second last node node = listp->head; while (node->next != listp->foot) { node = node->next; } // set its next pointer to NULL node->next = NULL; // free foot free(listp->foot); // update foot listp->foot = node; } } }