C programming, please explain by adding comments Hey at the moment this is what I have with the code and I would like to

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

C programming, please explain by adding comments Hey at the moment this is what I have with the code and I would like to

Post by answerhappygod »

C programming, please explain by adding comments
Hey at the moment this is what I have with the code and I wouldlike to modify it to add the stuff im missing
C Programming Please Explain By Adding Comments Hey At The Moment This Is What I Have With The Code And I Would Like To 1
C Programming Please Explain By Adding Comments Hey At The Moment This Is What I Have With The Code And I Would Like To 1 (75.34 KiB) Viewed 40 times
---------------------------------------------------------------------------------------------------------------
Code
#include <stdio.h>#include <string.h>#include <stdlib.h>typedef enum { NONE =0, UP =1, DOWN=2, LEFT =4, RIGHT =8}Direction;
typedef struct { int x,y;}Vertex;typedef struct { int numberOfVertices; Direction shiftDirection; Vertex* vertexList;}Polygon;int main(int argc, char** argv)//./main 12{ int max_num_of_polygons = 100;//sscanf(argv[1], "%d",&max_num_of_polygons); Polygon* list = (Polygon*) malloc(max_num_of_polygons* sizeof(Polygon)); char command[1000]; char tokentemp[20]; char firsttoken[20]; char rest[1000]; char *token [3]; int polygonindex = 0; Vertex tempVertexList[100]; //first polygon is at list[0] //second one is at list[1] //strcpy(command, "turn 3 right-up\n"); //strcpy(command, "shift 3 20\n"); strcpy(command, "add 3 4 1 2 3 4 5 6 7 8 91\n"); //strcpy(command, "summary\n"); //strcpy(command, "quit\n"); sscanf(command, "%s %[^\n]\n", firsttoken,rest); if(!strcmp(firsttoken, "quit")){ printf("rest is %s", rest); } if(!strcmp(firsttoken, "summary")){ printf("rest is %s", rest); } if(!strcmp(firsttoken, "shift")){ printf("rest is %s", rest); } if(!strcmp(firsttoken, "turn")){ printf("rest is %s", rest); } if(!strcmp(firsttoken, "add")){ printf("rest is %s\n", rest); int n = strlen(rest), x, y; rest[n] = '\n'; rest[n+1] = '0'; int rv = 0; int counter = 0; while((rv = sscanf(rest, "%d %d%[^\n]\n", &x, &y, rest)) >= 2){ printf("coordinate of thefirst point is (%d, %d), and the rest is %s, rv = %d\n", x, y,rest, rv); tempVertexList[counter].x= x; tempVertexList[counter++].y = y; if(rv == 2) break; } list[polygonindex].numberOfVertices =counter; list[polygonindex].shiftDirection =NONE; list[polygonindex].vertexList =(Vertex*) malloc(counter * sizeof(Vertex)); for(int i = 0; i <counter;i++) list[polygonindex].vertexList.x =tempVertexList.x, list[polygonindex].vertexList.y =tempVertexList.y; polygonindex++; }
return 0;}
----------------------------------------------------------------------------------------------------------
Handlers
Acceptable format of summary
polygon #1 has 4 vertices. It's cetroid is at (1.2,3.4).polygon #2 has 3 vertices. It's cetroid is at (1.2,3.4).polygon #3 has 6 vertices. It's cetroid is at (1.2,3.4).polygon #4 has 4 vertices. It's cetroid is at (1.2,3.4).polygon #5 has 4 vertices. It's cetroid is at (1.2,3.4).
Turn command handler
//command = "turn 4 right-up"//char * token[3];//token[0]: "turn"//token[1]: "4"//token[2]: "right-up"Direction givenDirection = 0;if(strstr(token[2], "right")) givenDirection |= RIGHT;if(strstr(token[2], "left")) givenDirection |= LEFTif(strstr(token[2], "up")) givenDirection |= UP;if(strstr(token[2], "down")) givenDirection |= DOWN;list[atoi(token[1])].shiftDirection = givenDirection;
Shift command handler:
//command: "shift 4 12"//token[0]: "shift"//token[1]: "4"//token[2]: "12"if(list[atoi(token[1])].shiftDirection & RIGHT) //go over all vertices and add their x coord. byatoi(token[2])if(list[atoi(token[1])].shiftDirection & LEFT) //go over all vertices and add their x coord. by-atoi(token[2])if(list[atoi(token[1])].shiftDirection & UP) //go over all vertices and add their y coord. byatoi(token[2])if(list[atoi(token[1])].shiftDirection & DOWN) //go over all vertices and add their y coord. by-atoi(token[2])
----------------------------------------------------------------------------------------
In this assignment, you are asked to write a program that stores and manipulates a list of at-most n polygons where n is given as a command-line argument when running the program. Each polygon must be stored in a structure like this: typedef struct { int numberOfVertices; Direction shift Direction; Vertex* vertexList; }Polygon; where Direction and Vertex are defined like this: typedef enum { NONE = 0, UP = 1, DOWN = 2, LEFT = 4, RIGHT = 8 }Direction; typedef struct { int x, y; }Vertex; 1 Program Input Commands There are five valid input commands: • add X₁ Y₁ 22 Y2 --- In Yn: adds an n-gon with vertices located at coordinates (2₁,91), (2, 2),..., (n. Yn) to the list of all polygons. The newly added polygon must have its shift Direction set to NONE. • summary: prints the program's list of polygons. For each polygon, you need to print the number of vertices and the x-y coordinates of the polygon's centroid. To avoid the complications of calculating the centroid, you can assume that the x-coordinate (y-coordinate) of a polygon's centroid is equal to the average of x-coordinates (y- coordinates) of the polgyon's vertices.
• (15% bonus) turn k x: updates the shift Direction of polygon stored at index k of the program's list of polygons. The shift Direction has to be updated based on the value of that can be equal to one of the following strings: - left - right - up - down - right-up or up-right - left-up or up-left - right-down or down-right -left-down or down-left . (15% bonus) shift ks: updates the x-y coordinates of vertices of polygon stored at index k of the program's list of polygons. To update the vertices' coordinates of a polygon, you need to check its shift Direction. If the direction is NONE, then the shift command has no side effect. If the direction is RIGHT(LEFT), then s has to be added to(subtracted from) the x-coordinate of every vertex of the polygon. If the direction is UP (DOWN), thens has to be added to(subtracted from) the y-coordinate of every vertex of the polygon. If the direction is RIGHT and UP, then s has to be added to both the x- coordinate and y-coordinate of every vertex of the polygon, etc. •quit: ends the program
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply