美文网首页简友广场散文哲思
C语言第15周作业(枚举enum,函数指针,链表,宏)

C语言第15周作业(枚举enum,函数指针,链表,宏)

作者: Cache_wood | 来源:发表于2020-12-20 00:07 被阅读0次

    1.

    16.

    (c)(e)正确。
    (a)数字太大时会出问题。
    (b)错误的主要原因是枚举enum和用#define指令创建的常量相比,枚举遵循C语言的作用域规则:如果枚举声明在函数体内,那么它的常量对外部函数来说是不可见的。
    (d)没有必然要求,可以相同。

    21.

    (a)enum {NUL,SOH,STX,ETX};
    NUL = 0,SOH = 1,STX = 2,ETX = 3.
    (b)enum {VT = 11,FF,CR}
    VT = 11, FF = 12, CR = 13
    (c)enum {SO = 14,SI,DLE,CAN = 24,EM}
    SO = 14,SI = 15,DLE = 16,CAN = 24,EM = 25
    (d)enum {ENQ = 45,ACK,BEL,LF = 37,ETB,ESC}
    ENQ = 45,ACK = 46,BEL = 47,LF = 37,ETB = 38, ESC = 39

    2.

    /*有序插入建立链表 C语言实现*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define N 100000
    /*定义一个linklist结构体类型*/
    typedef struct linklist
    {
        char name;
        int student_id;
        int grade;
        struct linklist *next;
    }list, *plist;
    
    /*按从小到大顺序插入*/
    void order_insert_list(plist *head)
    {
        plist p_new = (plist)malloc(sizeof(list));
        plist pre = NULL;
        p_new->name = 'a'+rand()%26;
        p_new->student_id = rand();           /* pre指针作为temp指向next前的备份 */
        p_new->grade = rand();       /* 赋予新值给新结点 */
        p_new->next = NULL;         
    
        plist temp = (plist)malloc(sizeof(list));
        temp = *head;  /*每次插入前从链表头开始的位置*/
    
        /*首位空结点赋初值*/
        if (NULL == *head)
        {
            *head = p_new;
            return;
        }
    
        /*若新data比头结点小,头结点前插入*/
        if (p_new->grade < temp->grade)
        {
            p_new->next = temp; 
            *head = p_new;
            return;
        }
        else
        {
            while (NULL != temp)
            {   
                if (p_new->grade >= temp->grade)/* 新结点对当前结点data比较 */
                {
                    pre = temp;     
                    temp= temp->next;/*当前结点后移,直至5(比如1 2 3 5 插入4)的位置*/
                    continue;
                }
                else
                {
                    p_new->next = temp;/* 插入新结点 */
                    pre->next = p_new; /* temp的前一个的位置的next指向新结点p_new */
                    break;
                }           
            }
            if (NULL == temp)/* 当temp最终为尾结点时,说明新元素data最大,将结点作为尾结点 */
            {
                p_new->next = NULL;
                pre->next = p_new;   /* temp的前一个的位置的next指向新结点p_new */
            }
        }
    }
    
    void print_list(plist head)
    {
        plist elem = head; 
        while (elem != NULL)
        {
            printf("%d %d %c\n", elem->grade,elem->student_id,elem->name);
            elem = elem->next;
        }
        printf("\n");
    }
    
    int main()
    {
        int start = time(NULL);
        srand(time(NULL));
        plist head;     /*定义表头*/
        head = NULL;
    
        //printf("input some numbers:\n");
        for(int i=0;i<N;i++){
            order_insert_list(&head);
        }
    
        //print_list(head);/*打印输出*/
        int end =time(NULL)-start;
        printf("Program execution time is %d",end);
    
        return 0;
    }
    PS E:\vscode-c\build> cd "e:\vscode-c\build\" ; if ($?) { gcc link_list_order1.c -o link_list_order1 } ; if ($?) { .\link_list_order1 }
    when N = 10000, Program execution time is 0
    PS E:\vscode-c\build> cd "e:\vscode-c\build\" ; if ($?) { gcc link_list_order1.c -o link_list_order1 } ; if ($?) { .\link_list_order1 }
    when N = 30000, Program execution time is 3
    PS E:\vscode-c\build> cd "e:\vscode-c\build\" ; if ($?) { gcc link_list_order1.c -o link_list_order1 } ; if ($?) { .\link_list_order1 }
    when N = 50000, Program execution time is 8
    PS E:\vscode-c\build> cd "e:\vscode-c\build\" ; if ($?) { gcc link_list_order1.c -o link_list_order1 } ; if ($?) { .\link_list_order1 }
    when N = 80000, Program execution time is 31
    PS E:\vscode-c\build> cd "e:\vscode-c\build\" ; if ($?) { gcc link_list_order1.c -o link_list_order1 } ; if ($?) { .\link_list_order1 }
    when N = 100000, Program execution time is 98
    

    可以看出随着N的增大,程序的执行时间不断增大而且幅度很大,所以构建有序列表会耗费大量时间,但反而有序列表会使得检索比较方便,从而在检索时节省大量时间。

    3.

    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #define N 30000
    
    struct node{
        int value;
        struct node *next;
    };
    int get_list_node_len(struct node *list){
        int len = 0;
        while(list!=NULL){
            list = list->next;
            len++;
        }
        return len;
    }
    struct node *search_list(struct node *list,int len,int L){
        if(len<L){
            printf("len < L !");
            return list;
        }else{
            for(int i=0;i<len-L;i++){
                list = list->next;
            }
            return list;
        }
    };
    
    struct node *add_to_list(struct node *list,int n)
    {
        struct node *new_node;
    
        new_node = malloc(sizeof(struct node));
        if(new_node == NULL){
            printf("Error:malloc failed in add_to list\n");
            exit(1);
        }
        new_node->value = n;
        new_node->next = list;
    
        return new_node;
    };
    
    struct node *initial(struct node *list){
        int n;
        //struct node *first = NULL;
        for(int i=0;i<N+rand()%1000;i++){
            n = rand();
            //printf("%d\n",n);
            if(n==0) break;
            list = add_to_list(list,n);
            //printf("the node of list is %d\n",get_list_node_len(list));        
        }
        return list;
    }
    void print_list(struct node *list){
        while (list!= NULL){
            printf("%d\n", list->value);
            list = list->next;
        }
        printf("\n");
    }
    int main(){
        int L;
        struct node *first = NULL;
        srand(time(NULL));
    
        first = initial(first);
        int length = get_list_node_len(first);
        printf("enter the L:");
        scanf("%d",&L);
    
        printf("the length is %d,the L is %d\n",length,L);
        struct node *list = search_list(first,length,L);
        printf("the penultimate %d node is %d\n",L,list->value);
    
        //print_list(first);
        //print_list(list);
        return 0;
    }
    

    4.

    16.
    #include <stdio.h>
    
    int g(int);
    int sum(int (*f)(int),int start, int end);
    
    int main(){
        int i,j;
        printf("enter two numbers:");
        scanf("%d %d",&i,&j);
        printf("the result is %d",sum(g,i,j));
    
        return 0;
    }
    int g(int x){
        return x*x;
    }
    int sum(int (*g)(int),int start, int end){
        int sum;
        for(int i=start;i<=end;i++){
            sum += g(i);
        }
        return sum;
    }
    

    5.

    14.
    #define N = 10
    #define INC(x) x+1
    #define SUB(x,y) x - y
    #define SQR(x) ((x)*(x))
    #define CUBE(x) (SQR(x)*(x))
    #define M1(x,y) x##y
    #define M2(x,y) #x #y
    
    int main(){
        int a[N],i,j,k,m;
    #ifdef N
        i = j;
    #else
        j = i;
    #endif
        i = 10 *INC(j);
        i = SUB(j,k);
        i = SQR(SQR(j));
        i = CUBE(j);
        i = M1(j,k);
        puts(M2(i,j));
    #undef SQR
        i = SQR(j);
    #define SQR
        i = SQR(j);
    
        return 0;
    }
    

    有三处错误。
    第1行有错,应删去 “=”。
    第23行有错, i = M1(j,k);将jk连接在一起导致未定义的行为。
    第26行有错,#undef 取消了对于SQR的定义,之后又使用了宏SQR导致错误。

    Blank line
    Blank line
    Blank line
    Blank line
    Blank line
    Blank line
    Blank line
    int main(void)
    {
     int a[= 10], i, j, k, m;
    Blank line
     i = j;
    Blank line
    Blank line
    Blank line
     i = 10 * j+1;
     i = (x,y) x-y(j, k);
     i = ((((j)*(j)))*(((j)*(j))));
     i = (((j)*(j))*(j));
     i = jk;
     puts("i" "j");
    Blank line
     i = SQR(j);
    Blank line
     i = (j);
     return 0; 
    

    gcc编译器可以使用-E来查看预处理之后的输出。
    在cmd命令行环境下输入gcc -E macro_error.c -o macro,会产生macro文件,使用记事本打开之后就会看到预处理器的输出。

    15.
    #include <stdio.h>
    #define ENGLISH 
    #define FRENCH 
    #define SPANISH 
    
    int main(){
        #ifdef ENGLISH
            printf("Insert Disk 1\n");
        #endif
        #ifdef FRENCH
            printf("Insert LeDisq 1\n");
        #endif
        #ifdef SPANISH
            printf("Insert El Disco\n");
        #endif 
        return 0;
    }
    

    相关文章

      网友评论

        本文标题:C语言第15周作业(枚举enum,函数指针,链表,宏)

        本文链接:https://www.haomeiwen.com/subject/thzcnktx.html