here is a solution to the problem.
its very messy, i did not plan, and it was written in haste. please look over bugs, i didnt read over, just ran it and it worked.
info.h
Code: Select all
#ifndef _info_h
#define _info_h
struct info
{
char *dest;
char *next;
int cost;
};
info *newInfo(char *dest, char *next, int cost);
#endif
info.cpp
Code: Select all
#include <stdlib.h>
#include "info.h"
info *newInfo(char *dest, char *next, int cost)
{
info *temp = (info*)malloc(sizeof(info));
temp->dest = dest;
temp->next = next;
temp->cost = cost;
return temp;
}
person.h
Code: Select all
#include "info.h"
#ifndef _person_h
#define _person_h
class person
{
public:
person(char *name);
void addFriend(person *p);
void printFriends();
void tellFriends();
void newInf(char *dest, char *next, int cost);
bool message;
bool told;
info *dat;
bool semaphore;
char *name;
int count;
person *friends[20];
private:
};
#endif
person.cpp
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "person.h"
person::person(char *name)
{
this->name = (char*)malloc((strlen(name))*sizeof(char));
this->name = name;
this->count = 0;
this->dat = NULL;
this->message = false;
this->semaphore = false;
this->told = false;
}
void person::addFriend(person *p)
{
this->friends[this->count] = p;
this->count++;
}
void person::printFriends()
{
for(int a = 0;a < this->count;a++)
{
printf("%s\n", this->friends[a]->name);
}
}
void person::newInf(char *dest, char *next, int cost)
{
if(this->dat != NULL)
{
if(this->dat->cost > cost )
this->dat = newInfo(dest, next, cost);
}else{
this->dat = newInfo(dest, next, cost);
}
}
void person::tellFriends()
{
if(this->told == false);
{
this->message = false;
this->semaphore = true;
this->told = true;
for(int a = 0;a < this->count;a++)
{
if(this->friends[a]->message == false && !this->friends[a]->told)
{
printf("%s is telling %s\n",this->name, this->friends[a]->name);
this->friends[a]->message = true;
this->friends[a]->semaphore = true;
this->friends[a]->newInf(this->dat->dest, this->name, this->dat->cost+1);
}
}
}
}
network.h
Code: Select all
#include "person.h"
#include "info.h"
#ifndef _network_h
#define _network_h
class network
{
public:
network();
int count;
void sparkMessage(char *name);
void addPerson(char *name);
void beFriend(char *A, char *B);
bool valid(char *name);
int find(char *name);
void printList();
void updateNetworkMessages();
person *com[20];
bool semaphore;
char *semWho;
private:
};
#endif
network.cpp
Code: Select all
#include "stdio.h"
#include "network.h"
network::network()
{
printf("Network Created.\n");
this->count = 0;
this->semaphore = false;
this->semWho = NULL;
}
void network::addPerson(char *name)
{
this->com[this->count] = new person(name);
this->count++;
}
void network::printList()
{
for(int a = 0;a < this->count;a++)
{
printf("name : %s\n", this->com[a]->name);
}
}
bool network::valid(char *name)
{
for(int a = 0;a <this->count;a++)
{
if(this->com[a]->name == name)
return true;
}
return false;
}
int network::find(char *name)
{
if(this->valid(name))
{
for(int a = 0;a < this->count;a++)
{
if(this->com[a]->name == name)
return a;
}
}
return -1;
}
void network::beFriend(char *A, char *B)
{
if(!this->valid(A) || !this->valid(B))
{
printf("ERROR WITH INPUT\n");
return;
}
int a = this->find(A);
int b = this->find(B);
this->com[a]->addFriend(this->com[b]);
this->com[b]->addFriend(this->com[a]);
}
void network::sparkMessage(char *name)
{
if(!this->valid(name))
return;
int a = this->find(name);
this->com[a]->message = true;
this->com[a]->semaphore = false;
this->com[a]->dat = newInfo(name, NULL, 0);
}
void network::updateNetworkMessages()
{
for(int a = 0;a < this->count;a++)
{
if(this->com[a]->message && !this->com[a]->told)
{
this->com[a]->tellFriends();
}
}
for(int a = 0;a < this->count;a++)
{
this->com[a]->semaphore = false;
}
}
main.cpp
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include "network.h"
#include "person.h"
int main(void)
{
network *net = new network();
net->addPerson("A");
net->addPerson("B");
net->addPerson("C");
net->addPerson("D");
net->addPerson("E");
net->addPerson("F");
net->addPerson("G");
net->addPerson("H");
net->addPerson("I");
net->addPerson("X");
net->printList();
net->beFriend("A", "B");
net->beFriend("A", "C");
net->beFriend("A", "D");
net->beFriend("B", "E");
net->beFriend("E", "F");
net->beFriend("E", "G");
net->beFriend("E", "X");
net->beFriend("G", "H");
net->beFriend("H", "I");
net->sparkMessage("I");
for(int a = 0;a < 20;a++)
{
if(net->com[0]->dat)
{
printf("%s , %s , %d\n", net->com[0]->dat->dest, net->com[0]->dat->next, net->com[0]->dat->cost);
break;
}else
{
printf("nothing yet.\n");
}
net->updateNetworkMessages();
}
printf("\n");
int f = 0;
for(int a = 0;a < net->com[0]->dat->cost+1;a++)
{
//printf("%s , %s , %d\n", net->com[f]->dat->dest, net->com[f]->dat->next, net->com[f]->dat->cost);
printf("-> %s ",net->com[f]->name);
f = net->find(net->com[f]->dat->next);
}
getchar();
return 0;
}