复习 C语言
今天把开课为止,所有的作业做了一遍
1 #include <stdio.h> 2 3 4 int my_strlen(char *str); 5 char *my_strcpy(char *dest, char *src); 6 char *my_strncpy(char *dest, char *src, int n); 7 char *reverse(char *str); 8 void bubble_sort(int *str, int n); 9 int fibe_frac(int n); 10 void fibe_iteration(int n); 11 int *fibswap(int *a, int *b, int *c); 12 int bin_search(int *array,int start, int stop, int target); 13 int bin_search_recursion(int *array,int start, int stop, int target); 14 int fractorial_iteration(int n); 15 int fractorial(int n); 16 17 void test_sort(); 18 void test_strlen(); 19 void test_my_strcpy(); 20 21 int input_num(); 22 void print_string(char *str); 23 void printf_array(int *a, int n); 24 25 26 int main() 27 { 28 int i = 0; 29 int n = 0; 30 test_strlen(); 31 test_my_strcpy(); 32 test_sort(); 33 n = input_num(); 34 for (i = 0; i < n; ++i) 35 { 36 printf("%d ",fibe_frac(i)); 37 } 38 printf("\n"); 39 40 printf("The fractorial is %d.\n",fractorial_iteration(input_num())); 41 42 return 0; 43 } 44 45 /* 46 ***************************************************************** 47 */ 48 49 50 /* 51 * calculate the length of the string 52 */ 53 int my_strlen(char *str) 54 { 55 int len = 0; 56 57 while(*str != '\0') 58 { 59 str ++; 60 len ++; 61 } 62 63 return len; 64 } 65 66 /* 67 * reverse the string 68 */ 69 char *reverse(char *str) 70 { 71 int i = 0; 72 int len = my_strlen(str); 73 char tmp = 0; 74 for (i = 0; i < len /2; i ++) 75 { 76 tmp = *(str + i); 77 *(str + i) = *(str + len - i - 1); 78 *(str + len - i -1) = tmp; 79 } 80 return str; 81 } 82 83 /* 84 ******************************************************************** 85 */ 86 87 /* 88 * Make sure the length of dest is bigger than or equals to src. 89 */ 90 char *my_strcpy(char *dest, char *src) 91 { 92 char *tmp = dest; 93 while (*src != '\0') 94 { 95 *dest = *src; 96 dest ++; 97 src ++; 98 } 99 *dest = '\0'; 100 101 return tmp; 102 } 103 104 105 /* 106 * Copy string to n 107 */ 108 char *my_strncpy(char *dest, char *src, int n) 109 { 110 char *tmp = dest; 111 int i = 0; 112 for (i = 0; i < n; i++) 113 { 114 if(*src == '\0') 115 { 116 break; 117 } 118 119 *dest = *src; 120 dest ++; 121 src ++; 122 } 123 124 *dest = '\0'; 125 return tmp; 126 } 127 128 /* 129 **************************************************************** 130 */ 131 132 /* 133 * bubble sort 134 */ 135 void bubble_sort(int *str, int n) 136 { 137 int i, j; 138 char tmp; 139 for (i = 0; i < n; ++i) 140 { 141 for (j = 0; j < n - i - 1; ++j) 142 { 143 if (str[j] > str[j + 1]) 144 { 145 tmp = str[j]; 146 str[j] = str[j + 1]; 147 str[j + 1] = tmp; 148 } 149 } 150 } 151 } 152 153 /* 154 * BinSearch 155 */ 156 int bin_search(int *array,int start, int stop, int target) 157 { 158 int mid; 159 while (start <= stop) 160 { 161 mid = (start + stop) / 2; 162 if (target == array[mid]) 163 { 164 return mid; 165 } 166 else if (target > array[mid]) 167 { 168 start = mid +1; 169 170 } 171 else 172 { 173 stop = mid -1; 174 } 175 } 176 return -1; 177 } 178 179 180 int bin_search_recursion(int *array,int start, int stop, int target) 181 { 182 int mid = (start + stop) / 2; 183 if (start > stop ) 184 { 185 return -1; 186 } 187 if (array[mid] == target) 188 { 189 return mid; 190 } 191 else if (array[mid] > target) 192 { 193 stop = mid -1; 194 } 195 else 196 { 197 start = mid +1; 198 } 199 return bin_search_recursion(array,start,stop,target); 200 } 201 202 /* 203 ********************************************************************** 204 */ 205 206 /* 207 * fib 208 */ 209 210 int fibe_frac(int n) 211 { 212 213 if ((n == 0) || (n == 1)) 214 { 215 return n; 216 } 217 else 218 { 219 //printf("%d ", n); 220 return (fibe_frac(n - 1) + fibe_frac(n - 2)); 221 } 222 } 223 224 void fibe_iteration(int n) 225 { 226 int i ; 227 int a[3] = { 0, 0, 0}; 228 for ( i = 0; i < n; ++i) 229 { 230 if (i < 2) 231 { 232 a[2] = i; 233 printf("%d ", a[2]); 234 fibswap( &a[0], &a[1], &a[2]); 235 } 236 else 237 { 238 printf("%d ", a[2]); 239 fibswap( &a[0], &a[1], &a[2]); 240 } 241 } 242 printf("\n"); 243 } 244 245 int *fibswap(int *a, int *b, int *c) 246 { 247 248 int temp = *b + *c; 249 temp = *b + *c; 250 *a = *b; 251 *b = *c; 252 *c = temp; 253 return a; 254 } 255 256 /* 257 *********************************************************** 258 */ 259 260 /* 261 * fracorial 262 */ 263 264 int fractorial(int n) 265 { 266 if ((n == 1) || (n == 0)) 267 { 268 return 1; 269 } 270 else 271 { 272 return n*fractorial(n - 1); 273 } 274 275 } 276 277 int fractorial_iteration(int n) 278 { 279 int i = 0; 280 int frac = 1; 281 for (i = 1; i <= n; ++i) 282 { 283 frac *= i; 284 } 285 return frac; 286 } 287 288 289 /* 290 *************************************************************** 291 */ 292 293 void test_sort() 294 { 295 // sort 296 int target = 0; 297 int index; 298 int len = 0; 299 int a[15] = { 13, 25, 45, 57, 89, 300 70, 97, 55, 43, 21, 301 32, 65, 75, 37, 65}; 302 303 len = sizeof(a)/sizeof(a[0]); 304 bubble_sort(a,len); 305 printf_array(a,len); 306 307 //input 308 target = input_num(); 309 310 // search 311 index = bin_search_recursion(a,0,len,target); 312 if (index < 0) 313 { 314 printf("No target find, your target is not in the range.\n"); 315 } 316 else 317 { 318 printf("Your target is %d.\n",index); 319 } 320 321 } 322 323 void test_strlen() 324 { 325 char *p = "Hello world!"; 326 printf("%s\n",p); 327 printf("%d\n",my_strlen(p)); 328 } 329 330 331 332 333 void test_my_strcpy() 334 { 335 char *p = "Hello world! hello"; 336 char dest[20] = {}; 337 338 p = my_strncpy(dest,p,13); 339 340 print_string(p); 341 p = reverse(dest); 342 print_string(p); 343 } 344 345 346 /* 347 ******************************************************************* 348 */ 349 350 void print_string(char *str) 351 { 352 int n = my_strlen(str); 353 int i = 0; 354 355 for(i = 0; i < n; i ++) 356 { 357 printf("%c",str[i]); 358 } 359 360 printf("\n"); 361 } 362 363 void printf_array(int *a, int n) 364 { 365 int i = 0; 366 367 for (i = 0; i < n; ++i) 368 { 369 printf("%d ",a[i]); 370 } 371 printf("\n"); 372 } 373 374 375 int input_num() 376 { 377 int num = 0; 378 printf("Please input a number\n"); 379 scanf("%d", &num ); 380 return num; 381 }
包括 冒泡排序 二分法排序 阶乘 求字符串长度 复制字符串 字符串反转 部分有递归和迭代的方式
posted on 2016-04-26 18:06 wangzefeng 阅读(302) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步