C语言- 二分查找法实现(综合应用)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdarg.h>
 
#define NOT_IN_MESSAGE "search %d not in %s\n "
#define IN_MESSAGE "search %d in %s, that Index of array eq %d\n "
#define ERROR -1
 
#define MAX_INT_DIGIT 10
 
int * getSizeTable();
int str2int(char*);
char * int2str(int);
 
int binsearch(int , int * ,int);
int * initArrays(int );
void printfArrays(char * message,int * ,int n);
int power(int, int);
int getLength(int *, int n);
 
main()
{
     
    int n = 100;
    int search = 45;
 
    int *pInt = initArrays( n );
    char buffer[getLength(pInt, n)];
    printfArrays(buffer,pInt, n);
 
    int status = binsearch(search, pInt, n);
    if(status == -1)
    {   
        printf(NOT_IN_MESSAGE, search, buffer);
    }
    else if(status >= 0 )
    {
        printf(IN_MESSAGE,search, buffer, status);
    }
 
 
}
 
 
void nstrcat(char *dest, ...){ 
    va_list  ap;  
    char     *p, *q;  
  
    va_start(ap, dest); 
    while( (p = va_arg(ap, char *)) != NULL ){ 
        strcat(dest, p);  
        dest += strlen(p); 
    }    
           
    va_end(ap); 
}
 
/*
* God Domn segmentation fault
*/
void printfArrays(char * buffer, int * v,  int n)
{
    if(v == NULL)
    {
        return ;
    }
 
    buffer[0] = ‘[‘;
    int index;
    for(index = 0 ; index <n ; index++)
    {
        int element = *(v+index);
        char * s = int2str(element);
        if(index < n -1)
        {
            nstrcat(buffer, s, ", ");   
        }
        else
        {
            nstrcat(buffer, s, "]");
        }
         
    }
 
}
 
 
int binsearch(int x, int* pInt,  int n)
{
    int low, high, mid;
    low = 0;
    high = n -1;
 
    while(low <= high)
    {
        mid = (low + high) / 2;
        printf( "search = %d, low = %d, high = %d, mid = %d, v[%d] = %d\n", x, low, high, mid, mid, *(pInt+mid) );
        if(x < *(pInt + mid))
        {
            high = mid - 1;
        }
        else if(x > *(pInt + mid))
        {
            low = mid + 1;
        }
        else if(x == *(pInt + mid))
        {
            return mid;
        }
 
    }
 
    return -1;
}
 
 
int * initArrays(  int n )
{
    int * pInt  = (int *) malloc (n * sizeof(int));
    int index = 0 ;
    for(; index < n ; index++)
    {
        *(pInt+index) = index + 1;
    }
 
    return pInt;
 
}
 
 
int getLength(int * v, int n)
{
 
    int size = 2 ;
    int index;
    for(index = 0; index < n ; index++ )   
    {
        int element = *(v+index);
        char * s = int2str(element);
        if(index < (n -1) )
        {
            size += strlen(s) + 2;           
        }else
        {
            size += strlen(s);
        }
    }
 
    return size + n/2;   
}
 
 
/**
 * str2int 
 */
int str2int( char* string)
{
    int result = 0;
 
    int size = (int)strlen(string);
    int index = 0;
    int base = 10;
    int convert= 0;
    int n = 0;
 
    printf("%s size = %d\n", string ,size );
 
    for(index = 0 ; index<= size -1 ; index++)
    {
        int c = string[index];
        if(c < ‘0‘ || c > ‘9‘)
        {           
            convert  = ERROR;           
            break;
        }
        int n = power(10, size -1 - index);
        if(n == ERROR)
        {
            convert = ERROR;
            break;
        }
 
        n = n * (string[index] - 48);
 
        /*
        n = n * (string[index] - ‘0‘);
        */
 
        convert += n;
 
        printf("%c * pow(10, %d) = %d\n", string[index], (size - 1 - index), n );
    }
 
    result = convert;
 
    return result;
 
}
 
 
int power(int base, int n)
{
    int result = ERROR;
    int m = 0 ;
 
    if(base <= 0)
    {
        return result;
    }
 
    if(n < 0)
    {
        return result;
    }
 
    result = 1 ;
 
    for(; m < n; m++)
    {
        result *= base;
    }
 
    return result;
}
 
 
 
char* int2str( int n)
{
    int absn = abs(n);
 
    int digit = getDigit(absn) + 1 ;
 
    char *number = (char *) malloc (digit * sizeof(char));
 
    int index = 0, difference = 0, realDigit = digit;
 
    for(; index < digit ; index++ )
    {
        realDigit = realDigit - 1;
        int iDigit = 0;
        if(index == 0)
        {
            iDigit = (absn/ power(10, realDigit ));   
            difference = absn % (power(10, realDigit));
        }
        else
        {
            iDigit = (difference/ power(10, realDigit ));   
            difference = difference % (power(10, realDigit));
        }
         
        char c  = {iDigit + ‘0‘};
        char cc[2];
        cc[0] = c;
        cc[1] = ‘\0‘;
        number = strcat(number, cc);
 
    }
 
    return number;
}
 
 
int * getSizeTable()
{
      int * sizeTable  = sizeTable =  (int *) malloc (MAX_INT_DIGIT * sizeof(int));
 
      int index = 0, digit = 10, sum = 0;
      for( ; index < MAX_INT_DIGIT - 1 ; index++ )
      {
          sum += power(digit, index) * 9;
          *( sizeTable + index ) = sum;        
      }
 
      *(sizeTable + MAX_INT_DIGIT -1 ) = INT_MAX;
 
      return sizeTable;
 
}
 
int getDigit(int nInt)
{   
    int result = 0,min = 0, index = 0 ;
    int * sizeTable = getSizeTable();
    for(; index < MAX_INT_DIGIT; index++)
    {
        if(nInt <= *(sizeTable + index))
        {
            result = index;
            break;
        }
    }
 
    return result;
 
}

 本示例存在缺陷:当int2str函数传递的整数是负数的时候,或转成它的绝对值,运行结果如下:

C语言- 二分查找法实现(综合应用)

C语言- 二分查找法实现(综合应用)

上一篇:4.1 Python中的序列操作


下一篇:4.3 Python中的字符串格式化