|
[x]
Posted via EE Mobile
|
||
Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again. |
||
| 11/01/2009 at 11:25PM PST, ID: 24862961 |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: |
#include <stdio.h>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
struct TreeNode
{
std::string key;
std::vector<TreeNode> children;
};
class Tree
{
// the one and only root node
TreeNode root;
ifstream fin;
char holder[100];
string beforePreviousCurrentTag;// help to solve tag case: /branch ->branch->attribute
public:
bool ignoreStartTags();
bool parseXML();
void draw();
private:
void parseXMLNode(TreeNode &parentNode);
string readCurrentXmlTag();
string readCurrentXmlKey(string tag);
void drawNode(TreeNode & parentNode, int indent);
};
string Tree:: readCurrentXmlKey(string tag)
{
string key;
char array[100];
int counter = 0;
if( tag == "branch" || tag == "leaf")
{
counter = 3;
}
else if(tag =="attribute") //recusive function : tag is attribute... case
{
counter = 2;
}
if( !fin.eof())
{
for(int i = 0; i < counter; i ++)
{
if(tag == "attribute" && beforePreviousCurrentTag == "/branch")
{
printf("holder is :%s\n",holder);
strcpy(array,holder);
strcpy(holder,"\0"); //reset content
beforePreviousCurrentTag ="";// reset
cout<<"***special case ***"<<endl;
}
else
{
fin.getline(array,100);
}
cout<<"XmlKey-> read line : "<<array<<endl;
if( i == 0)
{
char * pch;
char delims[]= "=\"" ; //"<=\">"; //get " must use \" to distingusih between newline and "
pch = strtok(array,delims);
// printf("1st~~~~~%s\n",pch);
while(pch != NULL)
{
if( strcmp(pch,"latin_name") ==0 )
{
pch = strtok(NULL,delims);
// printf("2nd~~~~~%s\n",pch);
pch = strtok(NULL,delims);
// printf("3rd~~~~~%s\n",pch);
key = pch;
//cout<<Value<<endl;
break;
}
pch = strtok(NULL,delims);
}//end of strtok while
}// end of if counter == 0
}//end of for
cout<<"key is :"<<key<<endl;
}// end of file
return key;
}
bool Tree::ignoreStartTags()
{
char array[1000];
fin.open("Treeinput.txt",ios_base::in);
if (!fin)
{
cout << "Fail to open file!!!."<< endl;
return false;
}
else
{
// just ignore the first 8 lines
for( int i = 0; i< 8; i++)
{
fin.getline(array,100);
}
return true;
}
}
void Tree::drawNode ( TreeNode &parentNode, int indent)
{
int i=0,j=0;
indent ++;
for( j =0; j <indent; j++)
{
cout<<"+";
}
cout<<"\""<<parentNode.key<<"\""<<" "<<endl;
if( parentNode.children.size()== 0)
{
return;
}
else
{
for(int i = 0; i <parentNode.children.size();i++)
{
drawNode(parentNode.children[i],indent);
}
}
}
void Tree::draw()
{
int indent = 0;
cout<<endl<<"start display tree"<<endl;
drawNode(root,indent);
}
bool Tree::parseXML()
{
string tag;
//here in that function we put the loop you now have at begin of parseXMLNode
//in case of read errors or missin g tags it return false
if( !ignoreStartTags()) return false;
tag = readCurrentXmlTag();
// read root key
string key = readCurrentXmlKey(tag);
if( key.empty()) return false;
root.key =key;
// now the root node is complete like any other parent node...
parseXMLNode(root);
return true;
}
string Tree::readCurrentXmlTag()
{
string Tag;
char array[100];
if( !fin.eof())
{
fin.getline(array,100);
cout<<endl<<"XmlTag-> read line : "<<array<<endl;
char tmp[100];
strcpy(tmp,array);
char * pch;
char delims[]= "< >" ;
pch = strtok(array,delims);
while(pch != NULL)
{
if( strcmp(pch,"branch")==0 || strcmp (pch,"/branch")== 0||
strcmp(pch,"leaf")== 0 || strcmp (pch,"/leaf")== 0 || strcmp(pch,"attribute") == 0)
{
/*
if(strcmp(pch,"attribute")== 0 && beforePreviousCurrentTag == "/branch" )
{
strcpy(holder,tmp);
printf("holder is %s\n",holder);
}*/
Tag = pch;
break;
}
pch = strtok(NULL,delims);
}//end of strtok while
if(Tag =="/branch")
{
// beforePreviousCurrentTag = Tag;
cout<<"--->Tag is /branch"<<endl;
}
cout<<"before return, Tag : "<<Tag<<endl;
return Tag;
}
else
{
fin.close();
exit(1);
}
}
void Tree::parseXMLNode( TreeNode &parentNode)
{
cout<<"Beginning of parseXMLNode"<<endl;
string tag;
string key;
TreeNode child;
tag = readCurrentXmlTag();
cout<<"tag is : "<<endl; //??????????????
while( tag != "/branch")
{
key = readCurrentXmlKey(tag);
child.key = key;
parentNode.children.push_back(child);
if(tag == "branch")
{
TreeNode &childRef = parentNode.children.back();
draw();
cout<<"childRef key is "<<childRef.key<<endl;
parseXMLNode(childRef);
}
else if( tag =="leaf")
{
draw();
parseXMLNode(parentNode);
tag == readCurrentXmlTag();
if(tag == "/leaf")
{
continue;
}
}
tag == readCurrentXmlTag();
}
}
int main()
{
Tree tree;
tree.parseXML();
tree.draw();
return 1;
}
|
Advertisement