class.cpp ¶
~cpp
#include "class.h"
void human_data::str_copy(char** target, char* original)
{
register int i=0;
while (0!=original[i])
++i;
*target=new char[i+1];
for (register int j=0; j<=i; ++j)
(*target)[j]=original[j];
}
human_data::human_data(char* input_names)
{
str_copy(&names,input_names);
score=0;
tuched=0;
number_line=0;
line_numbers=NULL;
}
human_data::~human_data()
{
if (NULL!=names)
delete names;
if (line_numbers!=NULL)
delete line_numbers;
}
void human_data::input_line(int input_line_number)
{
int* temp_numbers=new int[number_line+1];
for (register int i=0; i<number_line; ++i)
temp_numbers[i]=line_numbers[i];
temp_numbers[number_line]=input_line_number;
if (NULL!=line_numbers)
delete line_numbers;
line_numbers=temp_numbers;
++number_line;
}
void human_data::change_score(int new_score)
{
score=new_score;
if (0==tuched)
tuched=1;
}
void human_data::get_passive()
{
tuched=-1;
}
int human_data::get_score()
{
return score;
}
char* human_data::get_name()
{
return names;
}
int human_data::get_number_line()
{
return number_line;
}
int human_data::get_line_number(int where)
{
if (0<=where && where<number_line)
return line_numbers[where];
return -1;
}
int human_data::get_tuched()
{
return tuched;
}
line_data::line_data(human_data* input_data, line_data* input_prv)
{
include_data=input_data;
prv=input_prv;
if (NULL!=prv)
prv->link(this);
next=NULL;
}
line_data::~line_data()
{
if (NULL!=next)
delete next;
if (NULL!=prv)
delete prv;
}
void line_data::link(line_data* input_next)
{
next=input_next;
}
human_data* line_data::get_data()
{
return include_data;
}
line_data* line_data::get_next()
{
return next;
}
void data_manager::creat_book()
{
line_data** temp_books=new line_data*[number_books+1];
for (register int i=0; i<number_books; ++i)
temp_books[i]=books[i];
temp_books[number_books]=NULL;
if (NULL!=books)
delete books;
books=temp_books;
++number_books;
}
void data_manager::creat_man(char* input_name)
{
human_data** temp_mans=new human_data*[number_mans+1];
for (register int i=0; i<number_mans; ++i)
temp_mans[i]=mans[i];
temp_mans[number_mans]=new human_data(input_name);
if (NULL!=mans)
delete mans;
mans=temp_mans;
++number_mans;
}
int data_manager::str_cmp(char* target_a, char* target_b)
{
for (register int i=0; 0!=target_b[i] ; ++i)
{
if (0==target_a[i])
return -1;
if (target_a[i]!=target_b[i])
return -1;
}
return 0;
}
int data_manager::such_man(char* target_name)
{
for (register int i=0; i<number_mans; ++i)
{
if (0==str_cmp(target_name,mans[i]->get_name()))
return i;
}
return -1;
}
void data_manager::str_swallow(char* tagert_line)
{
human_data* temp_human;
int temp_man_number=such_man(tagert_line);
if (-1==temp_man_number)
{
creat_man(tagert_line);
temp_human=mans[number_mans-1];
}
else
temp_human=mans[temp_man_number];
temp_human->input_line(number_books-1);
line_data* temp_book=books[number_books-1];
if (NULL==temp_book)
{
books[number_books-1]=new line_data(temp_human,NULL);
}
else
{
while (NULL!=temp_book->get_next())
temp_book=temp_book->get_next();
new line_data(temp_human,temp_book);
}
}
data_manager::data_manager()
{
number_mans=0;
number_books=0;
mans=NULL;
books=NULL;
}
data_manager::~data_manager()
{
for (register int i=0; i<number_books; ++i)
delete books[i];
if (NULL!=books)
delete books;
for (register int i=0; i<number_mans; ++i)
delete mans[i];
if (NULL!=mans)
delete mans;
}
void data_manager::swallow(char* input_line)
{
creat_book();
int copy_start=-1,suched=0,such_point=0;
while (0!=input_line[such_point])
{
if (0==suched && ' '!=input_line[such_point])
{
suched=1;
copy_start=such_point;
}
else if ('.'==input_line[such_point] && (','==input_line[such_point+1] || ':'==input_line[such_point+1]))
{
suched=0;
++such_point;
input_line[such_point]=0;
str_swallow(&input_line[copy_start]);
}
++such_point;
}
}
void data_manager::process_erdos_number(human_data* target_man)
{
int target_number_line=target_man->get_number_line();
for (register int i=0; i<target_number_line; ++i)
{
line_data* temp_book=books[target_man->get_line_number(i)];
while (NULL!=temp_book)
{
if (temp_book->get_data()->get_score()>target_man->get_score()+1 || 0==temp_book->get_data()->get_tuched())
{
temp_book->get_data()->change_score(target_man->get_score()+1);
process_erdos_number(temp_book->get_data());
}
temp_book=temp_book->get_next();
}
}
}
void data_manager::process_erdos_number(char* target_name)
{
int target_man_number=such_man(target_name);
if (-1!=target_man_number)
{
mans[target_man_number]->get_passive();
process_erdos_number(mans[target_man_number]);
}
}
int data_manager::get_score(char* target_name)
{
int temp_target_number=such_man(target_name);
if (-1==temp_target_number)
return -1;
return mans[temp_target_number]->get_score();
}