Chinaunix首页 | 论坛 | 博客
  • 博客访问: 474973
  • 博文数量: 112
  • 博客积分: 2436
  • 博客等级: 大尉
  • 技术积分: 2769
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-04 19:48
文章分类

全部博文(112)

文章存档

2013年(7)

2012年(105)

分类: C/C++

2012-03-22 14:26:21


点击(此处)折叠或打开

  1. #练习使用Makefile

  2. src = $(wildcard *.c)
  3. targets = $(patsubst %.c, %, $(src))

  4. CC = gcc
  5. CFLAGS = -Wall -g

  6. all:$(targets)
  7. $(targets):%:%.c
  8.     $(CC) $(CFLAGS) $< -o $@
  9. clean:
  10.     rm $(targets)
  11. .PHONY:clean

点击(此处)折叠或打开

  1. //main.c
  2. //1.#反汇编查看变参函数的固参和变参压栈和使用的过程

  3. #include <stdio.h>

  4. int add(int a, int b)
  5. {
  6.     return a + b;
  7. }

  8. int main(int argc, const char *argv[])
  9. {
  10.     //printf("add: %d\n", add(4, 5));
  11.     printf("%d + %d = %d %d %d %d\n", 1, 2, 1 + 2, 4, 5, 6);
  12.     return 0;
  13. }

点击(此处)折叠或打开

  1. /*
  2. 2.#自定义一个求几个数的最大值的变参函数
  3.   #变参函数两种办法确定获取几个参数
  4.     #1:在固参中提供信息 如printf格式控制符中的个数
  5.     #2:变参以NULL结尾
  6. */

  7. //max.c

  8. #include <stdio.h>
  9. #include <stdarg.h>

  10. int max(const int n, ...)
  11. {
  12.     int i, tmp, max_n;
  13.     va_list ap;

  14.     va_start(ap, n);

  15.     max_n = va_arg(ap, int);
  16.     for (i = 1; i < n; i++)
  17.     {
  18.         tmp = va_arg(ap, int);
  19.         if(max_n < tmp)
  20.             max_n = tmp;
  21.     }
  22.     va_end(ap);

  23.     return max_n;
  24. }

  25. int main(int argc, const char *argv[])
  26. {
  27.     printf("%d\n", max(5, 2, 0, 1, 6, 10));
  28.     return 0;
  29. }

点击(此处)折叠或打开

  1. //3.#printf变参函数的简单实现 c和s

  2. //myprintf.c

  3. #include <stdio.h>
  4. #include <stdarg.h>

  5. void myprintf(const char *format,...)
  6. {
  7.     va_list ap;
  8.     char ch;

  9.     va_start(ap, format);
  10.     while((ch = *format++))
  11.     {
  12.         switch(ch)
  13.         {
  14.             case 'c':
  15.             {
  16.                 char c = va_arg(ap, int);
  17.                 putchar(c);
  18.                 break;
  19.             }
  20.             case 's':
  21.             {
  22.                 char *p=va_arg(ap, char *);
  23.                 fputs(p, stdout);
  24.                 break;
  25.             }
  26.             default:
  27.                 putchar(ch);
  28.         }
  29.     }
  30.     va_end(ap);
  31. }

  32. int main(int argc, const char *argv[])
  33. {
  34.     myprintf("c\ts\n",'1',"hello");
  35.     return 0;
  36. }

点击(此处)折叠或打开

  1. /*
  2. 4.#实现一个功能比较完整的printf,能够处理%c、%s、%d(对应参数是int型,以十进制打印)%o(对应参数是unsigned int型,以八进制打印)%x(对应参数是unsigned int型,以十六进制打印)%f(对应参数是double型,打印到小数点后6位)%%(打印一个%)等转换说明,在实现中不能调用printf一族的函数。(可使用putchar和fputs函数)
  3. */
  4. //my_printf.c

  5. #include <stdio.h>
  6. #include <stdarg.h>

  7. char str[32];

  8. char *itoa(int num, int cardinal, char str[])
  9. {
  10.     int i = 0, tmp, number = 0;

  11.     tmp = num;
  12.     while(tmp)
  13.     {
  14.         number++;
  15.         tmp /= cardinal;
  16.     }

  17.     str[number] = '\0';

  18.     for (i = number-1; i >= 0; i--)
  19.     {
  20.         if(((num % cardinal) > 9) &&((num % cardinal) <= 15))
  21.             str[i] = num % cardinal + 87;
  22.         else
  23.             str[i] = num % cardinal + '0';
  24.         num /= cardinal;
  25.     }

  26.     return str;
  27. }


  28. void my_printf(const char *format, ...)
  29. {
  30.     va_list ap;
  31.     char ch;

  32.     va_start(ap, format);
  33.     while((ch = *format++))
  34.     {
  35.         char nextc = *format;
  36.         if((ch == '%') && nextc)
  37.         {
  38.             format++;
  39.             switch(nextc)
  40.             {
  41.                 case 'c':
  42.                 {
  43.                     char c = va_arg(ap, int);
  44.                     putchar(c);
  45.                     break;
  46.                 }
  47.                 case 's':
  48.                 {
  49.                     char *s = va_arg(ap, char *);
  50.                     fputs(s, stdout);
  51.                     break;
  52.                 }
  53.                 case 'd':
  54.                 {
  55.                     char *d = itoa(va_arg(ap, int), 10, str);
  56.                     fputs(d, stdout);
  57.                     break;
  58.                 }
  59.                 case 'o':
  60.                 {
  61.                     char *o = itoa(va_arg(ap, int), 8, str);
  62.                     fputs(o, stdout);
  63.                     break;
  64.                 }
  65.                 case 'x':
  66.                 {
  67.                     char *x = itoa(va_arg(ap, int), 16, str);
  68.                     fputs(x, stdout);
  69.                     break;
  70.                 }
  71.                 case 'f':
  72.                 {
  73.                     double f = va_arg(ap, double);
  74.                     char *fint, *ffloat;
  75.                     int n = f;

  76.                     fint = itoa(n, 10, str);
  77.                     fputs(fint, stdout);
  78.                     putchar('.');

  79.                     n = (f - n)*1000000;
  80.                     ffloat = itoa(n, 10, str);
  81.                     fputs(ffloat, stdout);
  82.                     
  83.                     break;
  84.                 }
  85.                 case '%':
  86.                 {
  87.                     putchar('%');
  88.                     break;
  89.                 }
  90.                 case ' ':
  91.                 {
  92.                     fputs("% ", stdout);
  93.                     break;
  94.                 }
  95.                 default:
  96.                 {
  97.                     fputs("error format!", stdout);
  98.                     putchar('\n');
  99.                 }
  100.             }
  101.         }
  102.         else
  103.         {
  104.             if(nextc != '\0')
  105.                 putchar(ch);
  106.         }
  107.     }

  108. }

  109. int main(int argc, const char *argv[])
  110. {
  111.     my_printf("%% %c %s %d %o %x %f\n%", '1', "hello", 12, 12, 12, 12.3456);
  112.     my_printf("%a\n");
  113.     return 0;
  114. }

点击(此处)折叠或打开

  1. /*
  2. 5.#自定义一个变参函数:实现将输出变参列表
  3.   #2:变参以NULL结尾
  4. */
  5. //printlist.c

  6. #include <stdio.h>
  7. #include <stdarg.h>

  8. void printlist(int begin, ...)
  9. {
  10.     va_list ap;
  11.     char *p;

  12.     va_start(ap, begin);

  13.     p = va_arg(ap, char *);
  14.     while(p != NULL)
  15.     {
  16.         fputs(p, stdout);
  17.         putchar('\n');
  18.         p = va_arg(ap, char *);
  19.     }
  20.     va_end(ap);

  21. }

  22. int main(int argc, const char *argv[])
  23. {
  24.     printlist(0, "hello", "world", "foo", "bar", NULL);
  25.     return 0;
  26. }

阅读(1284) | 评论(0) | 转发(3) |
0

上一篇:Makefile_练习 _0228

下一篇:回调函数 _0302

给主人留下些什么吧!~~