rendered paste body#include <iostream>
#include <stack>
using namespace std;
struct mytag {
char tag[10];
};
bool equal(char a[10], char b[10]) {
for (int i=0; i<10; i++) {
if (a[i]!=b[i])
return false;
}
return true;
}
int main () {
int cases = 0;
int numlines, index, linenum, i;
bool error = false;
bool starting;
mytag tag;
char thischar;
scanf("%d%c",&numlines, &thischar);
stack<mytag> tags;
while (numlines) {
while (!tags.empty()) tags.pop();
error = false;
cases++;
cout << "Test Case " << cases << endl;
linenum=1;
while (linenum<=numlines) {
scanf("%c",&thischar);
if (thischar == '\n') {
linenum++;
}
if (!error && thischar == '<') {
for (i=0; i<10; i++) tag.tag[i]='\0';
index=0;
scanf("%c",&thischar);
if (thischar == '\n') {
linenum++;
}
if(thischar == '/'){
scanf("%c",&thischar);
if (thischar == '\n') {
linenum++;
}
starting = false;
}
else starting = true;
while (!error && thischar != '>') {
if (thischar<'A' || thischar>'Z') {
cout << "line " << linenum << ": bad character in tag name." << endl;
error=true;
}
else {
tag.tag[index]=thischar;
index++;
if (index>10) {
cout << "line " << linenum << ": too many/few characters in tag name." << endl;
error=true;
}
}
scanf("%c",&thischar);
if (thischar == '\n') {
linenum++;
}
}
if (!error && index==0) {
cout << "line " << linenum << ": too many/few characters in tag name." << endl;
error=true;
}
if (!error && starting)
tags.push(tag);
else {
if (!error && tags.empty()){
cout << "line " << linenum << ": no matching begin tag." << endl;
error = true;
}
else if (!error && !equal(tags.top().tag,tag.tag)){
cout << "line " << linenum << ": expected </" << tags.top().tag
<< ">" << endl;
error = true;
}
else{
if (!error) tags.pop();
}
}
} //end if char is <
} //end while
if (!error && !tags.empty()){
cout << "line " << linenum-1 << ": expected </" << tags.top().tag
<< ">" << endl;
error = true;
}
if (!error)
cout << "OK" << endl;
scanf("%d%c",&numlines,&thischar);
} // end for each case
return 0;
}