PLEASE ANSWER 2 AND 3 AND SHOW YOUR WORK PLEASE CODE IT IN C Name 2. (15pt) XHTM
ID: 3721722 • Letter: P
Question
PLEASE ANSWER 2 AND 3 AND SHOW YOUR WORK PLEASE CODE IT IN C
Name 2. (15pt) XHTML files consists of regular text and tags enclosed in angle brackets, symbols. Tags are used to identify the structure of a document. Most tags come in pairs: a beginning tag and a closing tag. For example, the tags and are the beginning and closing tags. There are several such tags including First header Some other text text Then YES, all the tags are nested correctly. But the tags in the following file are not correctly nested THIS TITLE IS NOT NESTED CORRECTLY. some text is not nested correctly Now, you are asked to implement a driver.c program, which takes input file name from the command line (driver input.htm1) and then prints the nesting structure of the tags on the screen (print one tag per line). You can stop the program when you detect the first incorrectly nested tag and print an error msg. Make sure there will be no memory leakage For the above two files, Assume that the following three functions are already implemented for you your program should generate thefolowingtext file pointed by fp into a dynamically allocated string and returns the pointer to this outputs on the screen char * read a tag (FILE *fp) /* reads the next XHTML tag from an opened string. If there is no more tag in the file, it returns NULL */ int tag match (char *b, char *compares if two tags are the matching beginning and closing tags. If so, it returns I; otherwise it returns 0. * void print a_tag (char *tag, int nesting_level) ; /*prints 3?nesting level many space characters and then tag followed by " " In case you need, here is the stack ADT interface, exporting void * as the element type.. #ifndef stack h #de fine stack h #include "genl ib . h" typedef void *stackElementT typedef struct stackCDT stackADT stackADT void void stackElementT bool bool int stackElementT #endif /ba Sp> ; StackIsFull (stackADT stack) StackDepth (stackADT stack) GetstackElement (stackADT stack, int index);Explanation / Answer
Answer for 2)
int main(int argc,char *agrv[])
{
//take a stack to push all tages from file
STACK *head = NULL;
STACK *tail = NULL;
char *filename;
strcpy(filename,agrv[1]);
//take a temperary stack to collect beganing tags.
STACK *head_1 = NULL;
STACK *tail_1 = NULL;
char temp[20]={};
FILE *fp=fopen(filename,"r");//open the file;
while(temp!=NULL)
{
strcpy(temp,read_a_tag(fp));
push(&head,&tail,temp);
}
fclose(fp);
//reverse the elements of the stack heaving all tags.....e.g. STACK:/title /h1 /i i h1 title becomes STACK:title h1 i /i /h1 /title
char tag[20]="";
int level=-1;//set tag_level to -1 initially;
while(tag!=NULL)//till no tag remains in first stack
{
strcpy(tag,pop(&head,tail));//get tag from stack in "tag"
if(tag[0]=='/')//if tag is a clossing tag
{
//find its expected beganing tag from temperary stack
if(!(strcmp(pop(&head_1,tail_1),strstr(tag,1,strgel(tag)))))//get length of tag(using strlen()) then get the string(tag) from index 1 to end (using strstr()) and compair the string with recent pushed begainig tag
{
//if string matches or expected beganing tag found then print it with expected tag level
print_a_tag(tag,level);
level--;
}
else
{
printf("Voilates Nesting Structure ");
}
}
else
{
//if tag is an opening tag then
level++;//increment the tag level
push(&head_1,tag);//push it in temperary stack
print_a_tag(tag,level);//print it with expected tag level
}
}
if(push(&head,tail)||push(&head_1,tail_1))//check if any beganing or clossing tag remains or not...push should return NULL if stack do not have any element
{
printf("Voilates Nesting Structure ");
}
return 0;
}
Answer For 3)
void poly_sort(polyT *p)
{
polyT *traverse=p;
int temp_coeff;
int temp_degree;
while(traverse!=NULL&&traverse->next!=NULL)
{
if(traverse->degree<traverse->next->degree)
{
temp_coeff=traverse->coeff;
temp_degree=traverse->degree;
traverse->coeff=traverse->next->coeff;
traverse->degree=traverse->next->degree;
traverse->next->coeff=temp_coeff;
traverse->next->degree=temp_degree;
}
traverse=traverse->next;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.