root/libbst/test.c

Revision 68, 2.6 KB (checked in by aqua, 4 years ago)

removing funcion prototype related with test stubs from libbst.h

Line 
1#include <stdio.h>
2
3#include "libbst.h"
4#include "libbst_test.h"
5
6int start( bst_ptr* root, FILE* out, FILE* in );
7void print_menu();
8
9int main( int argc, char** argv ){
10
11        bst_ptr root=NULL;
12       
13        FILE* out;
14        FILE* in;
15       
16        if( argc == 1 ){
17               
18                in = stdin;
19                out = stdout;
20
21        }
22        else if( !strcmp( argv[1] , "-f" ) && argc >= 3){
23
24                if( in = fopen( argv[2], "r" ) ){
25                               
26                        if( ( out = fopen( "test.out", "w" ) ) == NULL ){
27                                       
28                                printf("Error: failed open the file for output..\n");
29                                return -1;
30
31                        }
32
33                }
34                else{
35
36                        printf("Error: failed open the file for input..\n");
37                        return -1;
38
39                }
40
41        }
42        else
43                printf("Error: incorrect argument!!\n");
44
45        start( &root, out, in );
46        return 0;
47}
48
49int start( bst_ptr* root, FILE* out, FILE* in ){
50
51        char menu;
52        int var;
53
54        int status;
55        bst_ptr tmp;
56
57        while( 1 ){
58
59                printf(">");
60                status = fscanf( in, " %c", &menu );
61
62                if( status == -1 )
63                        break;
64
65                else {
66                               
67                        switch( menu ){
68
69                                case 'a':
70                                case 'A':
71
72                                        if( fscanf( in, " %d", &var ) ){
73                                                       
74                                                if( bst_add( root, var ) != ERROR )
75                                                        fprintf( out, "Add node %d\n", var );
76                                               
77                                                else
78                                                        fprintf( out, "Add node : %d error\n", var );
79
80                                        }
81                                        else
82                                                fprintf( out, "Add node : Wrong parameter..\n" );
83
84                                        break;
85       
86                                case 'd':
87                                case 'D':
88
89                                        if( fscanf( in, " %d", &var ) ){
90                                                       
91                                                if( bst_del( root, var ) == ERROR )
92                                                        fprintf( out, "Can't find node %d\n", var );
93
94                                                else
95                                                        fprintf( out, "Del node %d\n", var );
96
97                                        }
98                                        else
99                                                fprintf( out, "Del node : wring parameter..\n" );
100                                       
101                                        break;
102
103                                case 's':
104                                case 'S':
105                                        if( fscanf( in, " %d", &var ) ){
106                                                       
107                                                if( ( tmp = bst_search( (*root), var ) ) == NULL )
108                                                        fprintf( out, "Can't find node %d\n", var );
109
110                                                else
111                                                        fprintf( out, "Search node %d\n", tmp->var );
112
113                                        }
114                                        else
115                                                fprintf( out, "Search node : wrong parameter..\n" );
116
117                                        break;
118
119                                case 'm':
120                                case 'M':
121                                        print_menu( out );
122                                        break;
123
124                                case 'q':
125                                case 'Q':
126                                        goto for_break;
127                                        break;
128
129                                case 'P':
130                                case 'p':
131                                        bst_print( *root, out );
132                                        break;
133                                       
134                                default:
135                                        fprintf( out, "Incorrect menu.. skipping\n");
136                                        break;
137
138                        }
139
140                }
141
142        }                               
143        for_break:
144                ;
145}
146
147void print_menu( FILE* out ){
148
149        fprintf( out, "--------------------------\n");
150        fprintf( out, " ADD node      : A(a)     \n");
151        fprintf( out, " DELETE node   : D(d)     \n");
152        fprintf( out, " SEARCH tree   : S(s)     \n");
153        fprintf( out, " PRINT tree    : P(p)     \n");
154        fprintf( out, " MENU          : M(m)     \n");
155        fprintf( out, " Quit program  : Q(q)     \n");
156        fprintf( out, "--------------------------\n");
157
158}
Note: See TracBrowser for help on using the browser.