Here is another interesting application. I don't have Visual Studio installed to run and take a screenshot. The exe file I probably deleted back than. Should have kept it.
Anyways it was written with Visual Studio 5 if I remember correctly back in the good old University days. I never got the hang of mfc based applications, but somehow I wrapped my head around Dialog based applications, so this is written as a Dialog based app. The application was simply a multi-node tree, you could save it to disk and load it back from disk ( you don't really want to enter your family tree each time), and from what else I see, you could search the tree, create children nodes and sibling nodes. This was definitely semester 4. The main crux of business code is ofcourse the Tree.cpp. I hand the whole project as a zip file towards the end of this article. The tree.h file can be found in the zip file, the Tree.cpp I publish for young budding programmers. Feel free to use whatever you want to with permission or without permission, with attribution or without attribution. If you learn something thats good enough.
// Tree.cpp: implementation of the Tree class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Family tree.h" #include "Tree.h" #include <fstream.h> #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// Tree::Tree() { root = 0 ; } Tree::~Tree() { } Tree::Tree(CString myName,CString fname,CString mname) { root->name = myName; root->mothersName = mname; root->fathersName = fname; } void Tree::saveTree(Tree::TreeNodePtr root) { ofstream outTreeFile("tree.txt",ios::out); //write error msg if(!outTreeFile) { } for(int i=0;i<root->noOfChildrens;i++) { writeNode(root,outTreeFile); saveTree(root->childArray[i]); } outTreeFile.close(); } void Tree::writeNode(Tree::TreeNodePtr root, ofstream & outTreeFile) { outTreeFile << root->noOfChildrens << ' ' ; outTreeFile.write(root->name,(root->name).GetLength()); outTreeFile.put('\n'); outTreeFile.write(root->fathersName,(root->fathersName).GetLength()); outTreeFile.put('\n'); outTreeFile.write(root->mothersName,(root->mothersName).GetLength()); outTreeFile.put('\n'); outTreeFile.write(root->DOB,(root->DOB).GetLength()); outTreeFile.put('\n'); outTreeFile.write(root->gender,(root->gender).GetLength()); outTreeFile.put('\n'); } void Tree::readTree(Tree::TreeNodePtr root) { ifstream inTreeFile("tree.txt",ios::in); //write error msg if(!inTreeFile) { } for(int i=0; inTreeFile.eof() || i<root->noOfChildrens; i++) { readNode(root,inTreeFile); readTree(root->childArray[i]); } } void Tree::readNode(Tree::TreeNodePtr root, ifstream & inTreeFile) { char * myname, * fname, * mname, * dob, * sex; int childrens; fname = dob ; inTreeFile >> childrens; inTreeFile.getline(myname,100); inTreeFile.getline(fname,100); inTreeFile.getline(mname,100); inTreeFile.getline(dob,100); inTreeFile.getline(sex,100); root = new Tree::TreeNode(myname,fname,mname,dob,sex,childrens); } Tree::TreeNodePtr Tree::search(Tree::TreeNode* parent,const CString & key) { return searchAux(parent,root,key); } void Tree::editNode(Tree::TreeNodePtr currentptr,CString myname,CString fname,CString mname,CString DOB) { } Tree::TreeNodePtr Tree::displayParent(const CString key) { Tree::TreeNodePtr parent; search(parent,key); return parent; } void Tree::displaySpouse() { } void Tree::displayChildren(const Tree::TreeNode parent) { for(int i = 0; i <= parent.noOfChildrens ; i++) { //process nodes. } } void Tree::createChild(CString myName,CString fname, CString mname, CString dob, CString sex) { Tree::TreeNodePtr temp1; Tree::TreeNodePtr temp2; Tree::TreeNodePtr parent; temp1 = new Tree::TreeNode(currentptr,((currentptr->noOfChildrens)+1)); int i=0; for(;i<currentptr->noOfChildrens;i++) { currentptr->childArray[i] = currentptr->childArray[i]; currentptr->childArray[i] = NULL; } //Change temp1->childArray[i] = new Tree::TreeNode(myName,fname,mname,dob,sex); //Here values of text boxes to be entered search(parent,currentptr->name); temp2 = parent; parent = temp1; delete temp2; } void Tree::createSibling(CString myName,CString fname,CString mname,CString dob,CString sex) { Tree::TreeNodePtr parent, temp, current; search(parent,currentptr->name); temp = current; current = parent; createChild(myName,fname,mname,dob,sex); current = temp; } void Tree::createParent(CString myName,CString fname,CString mname,CString dob,CString sex) { createSibling(myName,fname,mname,dob,sex); } Tree::TreeNodePtr Tree::searchAux(Tree::TreeNodePtr parent,Tree::TreeNodePtr locptr, const CString key) { if(locptr == 0) { return NULL; } else { parent = locptr; for(int i = 0 ; i<= locptr->noOfChildrens ; i++) { if(locptr->name == key) break; searchAux(parent,locptr->childArray[i],key); } return locptr; } } </currentptr-></root-></root-></fstream.h>
Add new comment