zoukankan      html  css  js  c++  java
  • 第十二周

    这个作业属于那个课程 C语言程序设计II
    这个作业要求在哪里

    二级指针的概念,以及指针数组的应用

    了解指针与函数的关系,掌握指针作为函数返回值

    我在这个课程的目标是 掌握单向链表的概念和操作(建立、增加、删除、修改、遍历)
    这个作业在那个具体方面帮助我实现目标 了解指针与函数的关系
    参考文献 C语言程序设计II
    6-1 计算最长的字符串长度 (15 分)
     

    本题要求实现一个函数,用于计算有n个元素的指针数组s中最长的字符串的长度。

    函数接口定义:

    int max_len( char *s[], int n );
    

    其中n个字符串存储在s[]中,函数max_len应返回其中最长字符串的长度。

    裁判测试程序样例:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAXN 10
    #define MAXS 20
    
    int max_len( char *s[], int n );
    
    int main()
    {
        int i, n;
        char *string[MAXN] = {NULL};
    
        scanf("%d", &n);
        for(i = 0; i < n; i++) {
            string[i] = (char *)malloc(sizeof(char)*MAXS);
            scanf("%s", string[i]);
        }
        printf("%d
    ", max_len(string, n));
    
        return 0;
    }
    
    /* 你的代码将被嵌在这里 */
    

    输入样例:

    4
    blue
    yellow
    red
    green
    

    输出样例:

    6

    1.实验代码
    int max_len( char *s[], int n ){
        int max=0;//假设max为s[0] 
        int i,j;
        
        for(i=0;i<n;i++){
            if(strlen(s[max])<strlen(s[i])){
                max=i;    
            }
        }    
        return strlen(s[max]);
    } 

      2.流程图

    表示不想画

    3.运行结果


    4.遇到的问题
    我没什么遇到问题,就是中间容易弄混


    6-2 统计专业人数 (15 分)
     

    本题要求实现一个函数,统计学生学号链表中专业为计算机的学生人数。链表结点定义如下:

    struct ListNode {
        char code[8];
        struct ListNode *next;
    };
    

    这里学生的学号共7位数字,其中第2、3位是专业编号。计算机专业的编号为02。

    函数接口定义:

    int countcs( struct ListNode *head );
    

    其中head是用户传入的学生学号链表的头指针;函数countcs统计并返回head链表中专业为计算机的学生人数。

    裁判测试程序样例:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct ListNode {
        char code[8];
        struct ListNode *next;
    };
    
    struct ListNode *createlist(); /*裁判实现,细节不表*/
    int countcs( struct ListNode *head );
    
    int main()
    {
        struct ListNode  *head;
    
        head = createlist();
        printf("%d
    ", countcs(head));
    	
        return 0;
    }
    
    /* 你的代码将被嵌在这里 */
    

    输入样例:

    1021202
    2022310
    8102134
    1030912
    3110203
    4021205
    #
    

    输出样例:

    3
    1.实验代码
    int countcs( struct ListNode *head )
    {
        int sum=0;
        while(head!=NULL){
            if(head->code[1]=='0'&&head->code[2]=='2')
                sum++;
            head=head->next;
        }
        return sum;
    }
    
    

    2.流程图

    不想画,太懒了,感觉没用

    3.运行结果



    4.遇到的问题
    经过看书找题型基本不是很难。
    6-3 删除单链表偶数节点 (20 分)
     
    
    

    本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下:

    struct ListNode {
        int data;
        struct ListNode *next;
    };
    

    函数接口定义:

    struct ListNode *createlist();
    struct ListNode *deleteeven( struct ListNode *head );
    

    函数createlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−时表示输入结束,函数应返回指向单链表头结点的指针。

    函数deleteeven将单链表head中偶数值的结点删除,返回结果链表的头指针。

    裁判测试程序样例:

    #include <stdio.h>
    #include <stdlib.h>
    
    struct ListNode {
        int data;
        struct ListNode *next;
    };
    
    struct ListNode *createlist();
    struct ListNode *deleteeven( struct ListNode *head );
    void printlist( struct ListNode *head )
    {
         struct ListNode *p = head;
         while (p) {
               printf("%d ", p->data);
               p = p->next;
         }
         printf("
    ");
    }
    
    int main()
    {
        struct ListNode *head;
    
        head = createlist();
        head = deleteeven(head);
        printlist(head);
    
        return 0;
    }
    
    /* 你的代码将被嵌在这里 */
    

    输入样例:

    1 2 2 3 4 5 6 7 -1
    

    输出样例:

    1 3 5 7 
    1.实验代码
    struct ListNode *createlist()
    {
        struct ListNode *address,*p,*head=NULL;
        int x;
        scanf("%d",&x);
        while(x!=-1){
            address=(struct ListNode *)malloc(sizeof(struct ListNode));       //获得新地址
            if(head==NULL)
                head=address;                                                            
            else
                p->next=address;                  //使得上一个结构体中结构指针指向新的结构体
                
            address->data=x;                      //向新的结构体里的date赋值
            p=address;                            //保留地址,当有下一个新结构体地址时,使得上一个结构体内的next指向新结构体
            scanf("%d",&x);
        }
        address->next=NULL;                       //添加链表的结束标志
        return head;
    } 
    
    struct ListNode *deleteeven( struct ListNode *head )
    {
        struct ListNode *p,*q;
        int x=0;
        p=head;
        while (p!=NULL) {
            if(p->data%2!=0&&x==0){            //确定新链表头
                head=p;
                x++;
            }
            
            if(p->next==NULL)             //如果判断要不要删除的结构体是最后一个,就跳出循环,否则会导致错误
                break;
            
            if(x==1&&p->next->data%2==0){           //如果有头指针了,判断下一个链表是否是偶数节点
                if(p->next->next==NULL){            //如果下一个节点是偶数节点并且是尾指针的话,直接使得判断结构体内的next=NULL
                    p->next=NULL;
                    break; 
                }
                if(p->next->next->data%2==0){       //如果偶数节点是连续的
                    q=p->next->next;                //用新指针来判断,从p后面的第二个连续的偶数节点开始
                    while(q!=NULL){
                        if(q->data%2!=0)           //判断是否为奇数节点
                           break;
                        q=q->next;                 //不是就判断下一个
                    }
                    p->next=q;                   //找到后面的第一个奇数节点后,赋值给p->next,使得下一个链表一定是奇数节点
                }
                else
                    p->next=p->next->next;       //如果偶数节点不是两个或两个以上连接在一起的话,直接赋值,使得下一个指向奇数节点
            }
            
            p=p->next;
        }
        if(x==0)
            head=NULL;                          //全为偶数节点,就什么都不输出
        
        return head;
    }
    
    
    
    这个题不会做
    表示不会做,这是抄的,但看了以后收益匪浅


    4.总结
    我不知道我为啥这么低分,但可能我真的做的不如别好吧,也可能没达到老师布置作业的要求吧
    第二周代码数量
    我这周敲代码的数量600
    第三周代码数量 我这周敲代码的数量800
    第四周代码数量 我这周敲代码的数量900
    第五周代码数量 我这周敲代码的数量750
    第六周代码数量 我这周敲代码的数量1000
    第七周代码数量 我这周敲代码的数量950()
    第八周代码数量 我这周敲代码的数量800(复习)
    第九周代码数量 我这周敲代码的数量1000(在打个脚本)
    第十周代码数量 我这周敲代码的数量1100(预习吧,加上老师的任务)
    第十一周代码数量 我这周敲代码的数量1100(有点不会再敲了会前面代码)
    第十二周代码数量 我这周敲代码的数量1200(中间出了很多小错误)
     
  • 相关阅读:
    ES中对应的SQL的count(distinct 列名) java实现
    maven使用
    自旋锁
    Java手写死锁并用jps和jstack查看
    已知二叉树前序和中序,算法写出后续遍历的结果
    Idea里搭建SpringMVC项目,部署的包下没有lib
    Spring配置文件中关于bean标签的id属性和name属性的说明
    ORA-12519: TNS:no appropriate service handler found 解决
    springmvc常用注解标签详解
    Spring/SpringMvc 配置文件常用标签解释
  • 原文地址:https://www.cnblogs.com/Allen15773771785/p/10875945.html
Copyright © 2011-2022 走看看