-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eff3ed5
commit ab37f06
Showing
94 changed files
with
3,449 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
#include "13_17union.h" | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
typedef struct pair_proto | ||
{ | ||
tree_node * father; | ||
tree_node * son; | ||
}pair; | ||
|
||
static tree_node * make_node (const item * pt_item); | ||
static bool if_to_left (const item * pt_item, const item * pt_item1); | ||
static bool if_to_right (const item * pt_item, const item * pt_item1); | ||
static void add_one_node (tree_node * new_node, tree_node * root); | ||
static void order (const tree_node * root, void (* pfun)(item one_item)); | ||
static pair seek_one_item (const item * pt_item, const tree * pt_tree); | ||
static void del_one_node (tree_node ** pt_2_tree_node); | ||
static void del_all_nodes (tree_node * root); | ||
|
||
void init_tree (tree * pt_tree) | ||
{ | ||
pt_tree -> root = NULL; | ||
pt_tree -> size = 0; | ||
} | ||
|
||
bool if_tree_empty (const tree * pt_tree) | ||
{ | ||
return (!(pt_tree -> root)); | ||
} | ||
|
||
bool if_tree_full (const tree * pt_tree) | ||
{ | ||
return (pt_tree -> size == MAXITEMS); | ||
} | ||
|
||
int count_tree_item (const tree * pt_tree) | ||
{ | ||
return (pt_tree -> size); | ||
} | ||
|
||
bool add_to_tree (const item * pt_item, tree * pt_tree) | ||
{ | ||
tree_node * new_node; | ||
|
||
if (if_tree_full (pt_tree)) | ||
{ | ||
fprintf (stderr, "Tree is full. \n"); | ||
return false; | ||
} | ||
|
||
if (seek_one_item (pt_item, pt_tree).son) | ||
{ | ||
seek_one_item (pt_item, pt_tree).father -> item.word_count ++; | ||
return true; | ||
} | ||
|
||
new_node = make_node (pt_item); | ||
|
||
if (!new_node) | ||
{ | ||
fprintf (stderr, "Couldn't create node. \n"); | ||
return false; | ||
} | ||
|
||
pt_tree -> size ++; | ||
|
||
(pt_tree -> root) ? add_one_node (new_node, pt_tree -> root) : ((pt_tree -> root) = new_node); | ||
|
||
return true; | ||
} | ||
|
||
int sel_from_tree (const item * pt_item, const tree * pt_tree) | ||
{ | ||
return (seek_one_item (pt_item, pt_tree).son -> item.word_count); | ||
} | ||
|
||
void trav_tree (const tree * pt_tree, void (* pt_fun)(item one_item)) | ||
{ | ||
(pt_tree) ? order (pt_tree -> root, pt_fun) : 0; | ||
} | ||
|
||
void clear_tree (tree * pt_tree) | ||
{ | ||
if (pt_tree -> root) | ||
{ | ||
del_all_nodes (pt_tree -> root); | ||
} | ||
pt_tree -> root = NULL; | ||
pt_tree -> size = 0; | ||
} | ||
|
||
static tree_node * make_node (const item * pt_item) | ||
{ | ||
tree_node * new_node = (tree_node *)malloc(sizeof (tree_node)); | ||
|
||
if (new_node) | ||
{ | ||
new_node -> item = *pt_item; | ||
new_node -> left = NULL; | ||
new_node -> right = NULL; | ||
} | ||
|
||
return new_node; | ||
} | ||
|
||
static bool if_to_left (const item * pt_item, const item * pt_item1) | ||
{ | ||
int cop = strcmp (pt_item -> word, pt_item1 -> word); | ||
|
||
if (cop < 0) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
static bool if_to_right (const item * pt_item, const item * pt_item1) | ||
{ | ||
int cop = strcmp (pt_item -> word, pt_item1 -> word); | ||
|
||
if (cop > 0) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
static void add_one_node (tree_node * new_node, tree_node * root) | ||
{ | ||
if (if_to_left (&new_node -> item, &root -> item)) | ||
{ | ||
(root -> left) ? | ||
({ | ||
add_one_node (new_node, root -> left); | ||
}) : | ||
({ | ||
root -> left = new_node; | ||
}); | ||
}else if (if_to_right (&new_node -> item, &root -> item)) | ||
{ | ||
(root -> right) ? | ||
({ | ||
add_one_node (new_node, root -> right); | ||
}) : | ||
({ | ||
root -> right = new_node; | ||
}); | ||
}else | ||
{ | ||
fprintf (stderr, "Location error in add_one_node. \n"); | ||
exit (1); | ||
} | ||
} | ||
|
||
static void order (const tree_node * root, void (* pfun)(item one_item)) | ||
{ | ||
if (root) | ||
{ | ||
order (root -> left, pfun); | ||
(*pfun)(root -> item); | ||
order (root -> right, pfun); | ||
} | ||
} | ||
|
||
static pair seek_one_item (const item * pt_item, const tree * pt_tree) | ||
{ | ||
pair look; | ||
look.father = NULL; | ||
look.son = pt_tree -> root; | ||
|
||
if (!(look.son)) | ||
{ | ||
return look; | ||
} | ||
|
||
while (look.son) | ||
{ | ||
if (if_to_left (pt_item, &(look.son -> item))) | ||
{ | ||
look.father = look.son; | ||
look.son = (look.son -> left); | ||
}else if (if_to_right (pt_item, &(look.son -> item))) | ||
{ | ||
look.father = look.son; | ||
look.son = (look.son -> right); | ||
}else | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
return look; | ||
} | ||
|
||
static void del_all_nodes (tree_node * root) | ||
{ | ||
tree_node * pt_right; | ||
|
||
if (root) | ||
{ | ||
pt_right = root -> right; | ||
del_all_nodes (root -> left); | ||
free (root); | ||
del_all_nodes (pt_right); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#ifndef _TREE_H | ||
#define _TREE_H | ||
#include <stdbool.h> | ||
|
||
#define SLEN 20 | ||
|
||
typedef struct item_proto | ||
{ | ||
char word[SLEN]; | ||
int word_count; | ||
}item; | ||
|
||
#define MAXITEMS 10 | ||
|
||
typedef struct tree_node_proto | ||
{ | ||
struct tree_node_proto * left; | ||
item item; | ||
struct tree_node_proto * right; | ||
}tree_node; | ||
|
||
typedef struct tree_proto | ||
{ | ||
tree_node * root; | ||
int size; | ||
}tree; | ||
|
||
void init_tree (tree * pt_tree); | ||
bool if_tree_empty (const tree * pt_tree); | ||
bool if_tree_full (const tree * pt_tree); | ||
int count_tree_item (const tree * pt_tree); | ||
bool add_to_tree (const item * pt_item, tree * pt_tree); | ||
int sel_from_tree (const item * pt_item, const tree * pt_tree); | ||
void trav_tree (const tree * pt_tree, void (* pt_fun)(item one_item)); | ||
void clear_tree (tree * pt_tree); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include "../s_gets.h" | ||
|
||
#define TSIZE 45 | ||
#define FMAX 5 | ||
|
||
struct film | ||
{ | ||
char title[TSIZE]; | ||
int rating; | ||
}; | ||
|
||
inline static void eatline (void) | ||
{ | ||
while (getchar () != '\n'); | ||
} | ||
|
||
int main (void) | ||
{ | ||
struct film movies[FMAX]; | ||
int j = 0; | ||
int p; | ||
|
||
puts ("Enter first movie title: "); | ||
while (j < FMAX && s_gets (movies[j].title, TSIZE) && movies[j].title[0]) | ||
{ | ||
puts ("Enter your rating (0 - 10): "); | ||
while (1) | ||
{ | ||
if (scanf ("%d", &movies[j].rating)) | ||
{ | ||
eatline (); | ||
if (movies[j].rating <= 10 && movies[j].rating >= 0) | ||
{ | ||
break; | ||
} | ||
} | ||
printf ("Please enter a correct rate. \n"); | ||
} | ||
j ++; | ||
puts ("Enter next movie title (empty line to stop): "); | ||
} | ||
|
||
if (!j) | ||
{ | ||
printf ("No data entered. \n"); | ||
return 0; | ||
} | ||
|
||
printf ("Here is the movie list: \n"); | ||
for (p = 0; p < j; p ++) | ||
{ | ||
printf ("Movie: %s Rating: %d\n", movies[p].title, movies[p].rating); | ||
} | ||
|
||
printf ("Thank you for using. \n"); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.