#include "stdio.h"
#include <math.h>
#include <string.h>
#pragma warning(disable:4996)
struct Person {
char name[100];
int birth_year;
int birth_month;
int birth_day;
char address[100];
char phone[100];
};
struct BTNode
{BTNode *left;
BTNode *right;
Person person;
};
void print_person_data( struct BTNode *root )
{if ( root->left != NULL ) {
print_person_data( root->left );
}
printf( "%s, \t%d/%d/%d, \t%s, \t%s\n", root->person.name, root->person.birth_year, root->person.birth_month, root->person.birth_day, root->person.address, root->person.phone );
if ( root->right != NULL ) {
print_person_data( root->right );
}
}
struct BTNode *new_person_node(Person *p, struct BTNode *y, struct BTNode *z)
{struct BTNode *w = new BTNode();
strcpy_s( w->person.name, p->name);
w->person.birth_year = p->birth_year;
w->person.birth_month = p->birth_month;
w->person.birth_day = p->birth_day;
strcpy_s( w->person.address, p->address );
strcpy_s( w->person.phone, p->phone );
w->left = y;
w->right = z;
return w;
}
struct BTNode *insert_person_node(struct BTNode *node, Person *p)
{if ( node == NULL ) {
return new_person_node(p, NULL, NULL);
}
else if ( strcmp( p->name, node->person.name ) < 0 ) {
node->left = insert_person_node(node->left, p);
return node;
}
else if ( strcmp( p->name, node->person.name ) > 0) {
node->right = insert_person_node(node->right, p);
return node;
}
else {
return node;
}
}
BTNode* read_file_and_create_tree( char* file_name )
{FILE *in_file;
char line[sizeof(Person)];
Person p;
BTNode *root;
in_file = fopen(file_name, "r");
if ( in_file == NULL ) {
return 0;
}
root = NULL;
while( fgets( line, sizeof(Person), in_file ) != NULL ) {
sscanf_s( line, "%s %d/%d/%d %s %s", &(p.name), &(p.birth_year), &(p.birth_month), &(p.birth_day), &(p.address), &(p.phone) );
if ( root == NULL ) {
root = new_person_node( &p, NULL, NULL );
}
else {
insert_person_node( root, &p );
}
}
fclose(in_file);
return root;
}
int main()
{BTNode *root;
int ch;
root = read_file_and_create_tree( "z:\\Address.txt" );
print_person_data( root );
printf( "Enter キーを1,2回押してください. プログラムを終了します\n");
ch = getchar();
ch = getchar();
return 0;
}•6
print_person_data 関数
new_person_node 関数
insert_person_node 関数
read_file_and_create_tree 関数
構造体 Person の定義(説明は後述)
構造体 BTNode の定義(説明は後述)