免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1383 | 回复: 0
打印 上一主题 下一主题

第四周:C语言 变参函数,宏定义, 调试,标准库等 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-21 08:41 |只看该作者 |倒序浏览
1.作业:自己编写printf()函数

::::::::::::::
main.c
::::::::::::::
#include "myprintf.c"

void main(void)
{
    char ch='T';
    char name[]="YEP";
    int M = 12345;
    double pi = 314.15926;
    myprintf("\t:::%c:::%d:::%f:::%s \nhello,world!!!\n", ch, M, pi, name);
}
::::::::::::::
myprintf.c
::::::::::::::
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#define MAX    30

static int count=0;

void shownumber(int m)
{
    int M[MAX];
    int j=0;
    while(m>0)
    {
    M[j++]=m %10;
    m /= 10;
    }
    for(j=j-1;j>=0;j--)
    {
        putchar(M[j]+'0');
        count++;
    }
}

void myprintf(const char *format,...)
{
    va_list ap;
    int i=0,j;
    long int M; //for %d
    int N;
    char *K;
    double L; //for %f
    long int LM; //for %f;

    va_start(ap, format);

    while(*(format+i) != '\0')
    {
        if(*(format+i) == '\\')
        {
            i++;
            switch(*(format+i))
            {
            case 'n':
                for(j=100-count;j>=0;j--)
                    putchar(' ');
                count=0;
                break;
            case 't':
                for(j=0;j<8;j++)
                    putchar(' ');
                count += 8;
                break;
            }
        }

            
        if(*(format+i) != '%')
        {
            putchar(*(format+i));
            count++;
        }
        else
        {
            i++;
            switch(*(format+i)){
                case 'd':
                    M = va_arg(ap, int);
                    shownumber(M);
                    //printf("%d",M);
                    break;
                case 'f':
                    L = va_arg(ap, double);
                    LM=(int)L;
                    shownumber(LM);
                    putchar('.');
                    count++;

                    L = L - LM*1.0;
                    LM = L*100000000;
                    while(LM %10 == 0)
                        LM /= 10;
                    shownumber(LM);            
                    break;
                case 's':
                    K = va_arg(ap, char *);
                    j=0;
                    while(K[j]!='\0')
                    {
                        putchar(K[j++]);
                        count++;
                    }
                    break;
                case 'c':
                    N = va_arg(ap, int);
                    putchar(N);
                    count++;
                    break;
                    
                }
        }
        i++;
    }

}

2.面试题:求结果:
#include <stdio.h>

union{
    struct{
        char a:1;
        char b:2;
        char c:1;
    } s;
    char d;
} u;

main(void)
{    
    u.d = 3;
    printf("%d\n", u.s.a);
}

3.宏定义: # 和 ##

4.调试:
1)
#ifdef DEBUG
    printf("...");
#endif

编译的时候加: -DDEBUG
P.S. gcc llist.c -DDEBUG=1

2)保护一个头文件,防止被重复编译的方法:在头文件里加如ifndef语句:
#ifndef HS_DEBUG_H
 #define HS_DEBUG_H
...
...
#endif

老师代码:

#ifndef HS_DEBUG_H
#define HS_DEBUG_H

#ifdef DEBUG3
#ifndef DEBUG2
#define DEBUG2
#endif
#endif

#ifdef DEBUG2
#ifndef DEBUG1
#define DEBUG1
#endif
#endif

#ifdef DEBUG1
#ifndef DEBUG0
#define DEBUG0
#endif
#endif

#ifdef DEBUG0
#ifndef DEBUG
#define DEBUG
#endif
#endif

#ifdef DEBUG
#ifndef DEBUG0
#define DEBUG0
#endif
#endif

#ifdef DEBUG3
#define debug3_print(...) printf(__VA_ARGS__)
#else
#define debug3_print(...)
#endif

#ifdef DEBUG2
#define debug2_print(...) printf(__VA_ARGS__)
#else
#define debug2_print(...)
#endif

#ifdef DEBUG1
#define debug1_print(...) printf(__VA_ARGS__)
#else
#define debug1_print(...)
#endif

#ifdef DEBUG0
#define debug0_print(...) printf(__VA_ARGS__)
#else
#define debug0_print(...)
#endif

#ifdef DEBUG
#define debug_print(...) printf(__VA_ARGS__)
#else
#define debug_print(...)
#endif

#endif

5.标准库:

6.fopen(),fseek(),ftell()

7.老师其他代码:

::::::::::::::
llist.c
::::::::::::::
#include <stdio.h>
#include <stdlib.h>

#include "debug.h"

#define NAMESIZE 32
struct score {
      int id;
      char name[NAMESIZE];
      int math;
};

struct node_st {
      struct score data;
      struct node_st *next;
};

int insert(struct node_st **list, struct score *data)
{
      struct node_st *new;

      new = malloc(sizeof(*new));
      if (new == NULL) {
        return -1;
      }
      debug3_print("debug %d\n", 3);

      new->data = *data;
      new->next = *list;
      *list = new;

      return 0;
}

void travel(struct node_st *list)
{
      struct node_st *cur;

      for (cur = list; cur != NULL; cur = cur->next) {
        printf("%d %s %d\n", cur->data.id, cur->data.name, cur->data.math);
      }
}

struct score *find(struct node_st *list, int id)
{
      struct node_st *cur;

      for (cur = list; cur != NULL; cur = cur->next) {
        if (id == cur->data.id) {
          return &cur->data;
        }
      }
      return NULL;
}

int main(void)
{
      struct score tmp, *datap;
      struct node_st *list = NULL;
      int i;

      for (i = 0; i < 7; i++) {
        tmp.id = i;
        tmp.math = 100 - i;
        snprintf(tmp.name, NAMESIZE, "stu%d", i);

        insert(&list, &tmp);
      }
      travel(list);
      debug_print("in main() debug\n");

      datap = find(list, 9);
      if (datap == NULL) {
        printf("Can not find.\n");
      } else {
        printf("%d %s %d\n", datap->id, datap->name, datap->math);
      }

      return 0;
}
::::::::::::::
macro.c
::::::::::::::
//#include <stdio.h>

#define PI 3.14

#define add(A, B) ((A) + (B))
#define mul(A, B) ((A) * (B))
#define max(A, B) \
      ((A) > (B) ? (A) : (B))

#define bar(abc) #abc
#define link(a, b) a ## b
int link(stu, 1)()
{

}

#if 0
int foo(void)
{
      int a;
      return a + 32167;
}
#else
int foo(void)
{
      int a;
#ifdef X86
      a = 6;
#elif defined ARM
      a = ARM;
#endif
      return a + 32167;
}
#endif


int main(void)
{
      //s = PI * r * r;
      int a, b, c;

      bar(kkk);
      link(abc, 123);
#if 0
      a = 3; b = 6;
#else
      a = 4; b = 8;
#endif
      c = max(a++, b++);
      printf("%d, %d, %d\n", a, b, c);

      return 0;
}

#include "abc.h"
#include "abc.h"
#include "abc.h"
#include "abc.h"
::::::::::::::
myarg.h
::::::::::::::
#ifndef HS_MYARG_H
#define HS_MYARG_H

typedef char *myva_list;

#define ALIGN 4
#define align_size(type) \
      ((sizeof(type) + ALIGN - 1) & ~(ALIGN - 1))

#define myva_start(ap, last) \
      ((ap) = (myva_list)&(last) + align_size(last))

#define myva_arg(ap, type) \
      ((ap) += align_size(type), *(type *)((ap) - align_size(type)))

#define myva_end(ap)

#endif
::::::::::::::
vararg.c
::::::::::::::
#include <stdio.h>
#include <stdarg.h>

#include "myarg.h"

int add(int a, ...)
{
      int *p = &a;
      int sum = 0;

      while (*p != 0) {
        sum += *p;
        p++;
      }

      return sum;
}

#if 0
void myprintf(const char *format, ...)
{
      char *ap;

      ap = (char *)(&format + 1);
      while (*format) {
        switch (*format) {
        case 'd':
          printf("%d ", *(int *)ap);
          ap += sizeof(int);
          break;
        case 'f':
          printf("%f ", *(double *)ap);
          ap += sizeof(double);
          break;
        case 's':
          printf("%s ", *(char **)ap);
          ap += sizeof(char *);
          break;
        case 'c':
          printf("%c ", *(char *)ap);
          ap += sizeof(int);
          break;
        }
        format++;
      }
      printf("\n");
}
#elif 0
void myprintf(const char *format, ...)
{
      va_list ap;

      va_start(ap, format);
      while (*format) {
        switch (*format) {
        case 'd':
          //d = va_arg(ap, int);
          //printf("%d ", d);
          printf("%d ", va_arg(ap, int));
          break;
        case 'f':
          printf("%f ", va_arg(ap, double));
          break;
        case 's':
          printf("%s ", va_arg(ap, char *));
          break;
        case 'c':
          printf("%c ", va_arg(ap, int));
          break;
        }
        format++;
      }

      va_end(ap);
      printf("\n");
}
#else
void myprintf(const char *format, ...)
{
      myva_list ap;

      myva_start(ap, format);
      while (*format) {
        switch (*format) {
        case 'd':
          //d = myva_arg(ap, int);
          //printf("%d ", d);
          printf("%d ", myva_arg(ap, int));
          break;
        case 'f':
          printf("%f ", myva_arg(ap, double));
          break;
        case 's':
          printf("%s ", myva_arg(ap, char *));
          break;
        case 'c':
          printf("%c ", myva_arg(ap, char));
          break;
        }
        format++;
      }

      myva_end(ap);
      printf("\n");
}
#endif

int main(void)
{
      char c = 'A';

      //c = add(1, 2, 3, 4, 5, 6, 0);
      //printf("c = %d\n", c);
      myprintf("dfdcdsc", 3, 3.14, 5, c, 6, "hello", c);

      return 0;
}

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP