
What's the size of 'a' line ??
is it ok with gets??
does the input accept only UPPERCASES tag ???
if it does not accept mixed cases tag then whats the error ??
Code: Select all
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define M 1000
#define N 20
//char stack[M][N];
//int top;
struct ListNode
{
char item[N];
struct ListNode *next;
};
ListNode *top;
void push(char s[N])
{
//strcpy(stack[top],s);
//top++;
ListNode *temp = new ListNode;
strcpy(temp->item,s);
temp->next=top;
top=temp;
}
char *pop()
{
char res[N];
//top--;
//strcpy(res,stack[top]);
//top--;
strcpy(res,top->item);
top = top->next;
return res;
}
int isEmpty()
{
return top==NULL;
}
int main()
{
int cases;
int n,line,idx;
int i,error,tag;
char str[M],store[N],word[N],check[N];
char ch;
top = NULL;
cases=0;
while(1)
{
scanf("%d",&n);
if(n==0)
break;
gets(str);
error = 0;
line = 0;
idx = 0;
strcpy(word,"");
strcpy(check,"");
tag = 0;
top=NULL;
//for(i=0;i<M;i++)
//memset(stack[i],'\0',sizeof(stack[i]));
while(line<n)
{
gets(str);
//tag = 0;
for(i=0;i<strlen(str);i++)
{
ch = str[i];
if( (ch!='>' && ch!='/') && tag)
{
word[idx++]=ch;
}
else if(ch=='<' && !tag)
{
tag=1;
}
else if(ch=='>' && tag==1)
{
if(!idx || idx>10)
error = 2;
else
{
word[idx]='\0';
push(word);
idx=0;
}
tag = 0;
}
else if(ch=='/' && tag==1)
{
tag=2;
}
else if(ch=='>' && tag==2)
{
word[idx]='\0';
if(!isEmpty())
{
strcpy(store,pop());
if(strcmp(store,word)!=0)
error = 3;
}
else
error = 4;
idx=0;
tag = 0;
}
//if(tag)
//error = 1;
if(error)
break;
}
if(tag) error = 1;
if(error)
break;
line++;
}
i = line+1 ;
if(!isEmpty() && !error)
{
error = 3;
strcpy(store,pop());
line--;
}
if(error)
while( i<n)
{
gets(str);
i++;
}
printf("Test Case %d\n",cases+1);
cases++;
if(!error)
printf("OK\n");
else if(error==1)
{
printf("line %d: bad characters in tag name.\n",line+1);
}
else if(error==2)
{
printf("line %d: too many/few characters in tag name.\n",line+1);
}
else if(error==3)
{
printf("line %d: expected </%s>.\n",line+1,store);
}
else if(error==4)
{
printf("line %d: no matching begin tag.\n",line+1);
}
}
return 0;
}