For my computer programming final, I have to write a program to simulate an organizational chart using linked lists. In the end, the output should look something like this:
Code:
1. President (George)
2. Vice President (Dick)
3. VP's subordinate (Jim)
4. Executive (Mark)
The president is at the top level, vice president and executive are on the same level, and VP's subordinate (obviously) reports to the VP.
The user has to be able to insert people, delete people, change data, blah blah blah. I think I'll be able to handle the actual functions pretty well, but I'm not really sure how to start. Our teacher showed us what linked lists do and how do manually do what STL already does. However, he didn't really show us how to implement a LL. This is where I'm stuck.
So far, I've got two classes, Orgnode and Orgchart. Orgnode is the personal information (name, title, level). Orgchart is all the functions, plus a LL of orgnodes.
Orgnode.h:
Code:
// orgnode.h: interface for the orgnode class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_ORGNODE_H__990674FB_EED8_4ABB_9F06_FAE80C28AED3__INCLUDED_)
#define AFX_ORGNODE_H__990674FB_EED8_4ABB_9F06_FAE80C28AED3__INCLUDED_
#include <list>
#include <iostream>
using namespace std;
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class orgnode
{
friend ostream &operator<<(ostream &, const orgnode &);
public:
orgnode(string, string, int);
string title;
string name;
int level;
};
#endif
orgnode.cpp
Code:
#include "orgnode.h"
#include <string>
orgnode::orgnode(string username, string usertitle, int userlevel)
{
name = username;
level = userlevel;
title = usertitle;
}
ostream &operator<<(ostream &output, const orgnode &node)
{
output << node.level << ": " << node.title << " (" << node.name << ")";
return output;
}
I'm pretty sure orgnode is alright. I don't really know what to do with orgchart though.
orgchart.h
Code:
#if !defined(AFX_ORGCHART_H__A2A5232E_84BD_4986_B7C9_B929B63F7A0B__INCLUDED_)
#define AFX_ORGCHART_H__A2A5232E_84BD_4986_B7C9_B929B63F7A0B__INCLUDED_
#include <iostream>
#include <list>
#include <string>
#include "orgnode.h"
using namespace std;
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class orgchart
{
friend ostream &list(ostream &, orgchart &org);
public:
orgchart();
int insert(int);
private:
list<orgnode> chart;
list<orgnode>::iterator current;
int counter;
void locate(int);
};
#endif
orgchart.cpp
Code:
#include "orgchart.h"
#include "orgnode.h"
#include <list>
orgchart::orgchart()
{
//no clue.
}
int orgchart::insert(int element)
{
string name, title;
cout<<"Enter a name."<<endl;;
cin>>name;
cout<<"enter a title."<<endl;
cin>>title;
orgnode newnode(name, title, element);
//add this node to list at one below "element"th positition.
//adjust pointers of "element" and of newnode.
}
As you can see, I suck at orgchart. I don't even know what to do with the constructor, because it has to be called before any information is input. At the bottom of insert, you can see a little bit of pseudocode, but I'm not sure how to do this, and I can't do trial and error until I get the rest working.
final.cpp (the driver)
Code:
#include <iostream>
#include <string>
#include "orgnode.h"
#include "orgchart.h"
using namespace std;
void handleinput(string);
void main()
{
string input;
while (input != 'Q') //Q is the quit command.
{
cout<<"Enter a command."<<endl;;
cin>>input;
handleinput(input);
}
}
void handleinput(string input)
{
int command;
command = input.find_first_of('I', 0);
if (command ==0)
chart.insert(input.find_first_not_of('I',0));
//^^^this will only work as long as the index is single digit. I23 doesn't work.
}
My main is pretty pitiful as well. Handleinput is only looking for an I# now (meaning insert under #. A command of I2 would be insert a position under line 2), but that will change later.
I'm not asking for anyone to write the program for me, but a shove in the right direct would really help me. Thanks.