• 复习 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("
    ");
     39 
     40     printf("The fractorial is %d.
    ",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 != '')
     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 != '')
     94     {
     95         *dest = *src;
     96         dest ++;
     97         src ++;
     98     }
     99     *dest = '';
    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 == '')
    115         {
    116             break;
    117         }
    118 
    119         *dest = *src;
    120         dest ++;
    121         src ++;
    122     }
    123 
    124     *dest = '';
    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("
    ");
    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.
    ");
    315     }
    316     else
    317     {
    318         printf("Your target is %d.
    ",index);
    319     }
    320 
    321 }
    322 
    323 void test_strlen()
    324 {
    325     char *p = "Hello world!";
    326     printf("%s
    ",p);
    327     printf("%d
    ",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("
    ");
    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("
    ");
    372 }
    373 
    374 
    375 int input_num()
    376 {
    377     int num = 0;
    378     printf("Please input a number
    ");
    379     scanf("%d", &num );
    380     return num;
    381 }

    包括 冒泡排序 二分法排序 阶乘 求字符串长度 复制字符串 字符串反转  部分有递归和迭代的方式

  • 相关阅读:
    Android 生命周期
    adb 的相关操作及遇到的问题
    各种内部类
    Android 的系统架构
    Hibernate的各种关联关系
    Eclipse常用的快捷键
    Android 开发前的基本的配置及第一个Android 程序
    spring MVC Validation(表单验证)
    创建 spring MVC项目的基本步骤
    Android_demo之生成二维码
  • 原文地址:https://www.cnblogs.com/wangzefeng/p/5436059.html
Copyright © 2020-2023  润新知