第十二周作业

这个作业属于那个课程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\n", max_len(string, n));

    return 0;
}

/* 你的代码将被嵌在这里 */
输入样例:
4
blue
yellow
red
green
输出样例:
6

实验代码

int max_len( char *s[], int n )
{   int max1=0,i;
    for(i=0;i<n;i++){
        if(strlen(s[i])>max1)          //还可以用三元运算符( ?:),如后: max1=strlen(s[i])>max1?strlen(s[i]):max1;
        max1=strlen(s[i]);
    }
    return max1;
}

实验思路

1581432-20190517193202449-514542521.png

实验截图

1581432-20190517193220637-827697513.png

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\n", countcs(head));
    
    return 0;
}

/* 你的代码将被嵌在这里 */
输入样例:
1021202
2022310
8102134
1030912
3110203
4021205

输出样例:
3

实验代码

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;
}

实验思路

1581432-20190517193459418-1581415163.png

实验截图

1581432-20190517193522883-1198508274.png

6-3 删除单链表偶数节点 (20 分)

本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中偶数值的结点删除。链表结点定义如下:
struct ListNode {
int data;
struct ListNode next;
};
函数接口定义:
struct ListNode
createlist();
struct ListNode deleteeven( struct ListNode head );
函数createlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。
函数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("\n");
}

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

实验代码

struct ListNode *deleteeven( struct ListNode *head )
{
    struct ListNode *p,*q,*s;
    while(head!=NULL){                //判断头指针位置,即第一个奇数结点 
        if(head->data%2!=0)
            break;
        s=head;                       //记录偶数结点 
        head=head->next;
        free(s);                      //指向下一个结点后,释放偶数结点内存 
    }
    
    p=head;                           //p是新链表的结点 ,即全为奇数结点 
    if(head==NULL)                    //为空指针或全为偶数结点,返回head的值到主函数 
        return head;
     
    q=p->next;                    
    while(q!=NULL){                    //判断头指针后的结点 
        if(q->data%2==0){              //偶数结点,s记录 ,q指向下一个结点后释放内存 
            s=q;
            q=q->next;
            free(s);
        }
        else{                          //奇数结点,赋值p的下一个结点为奇数结点,然后p的地址改变为这个新的奇数结点 
            p->next=q;
            p=p->next;
            q=q->next;
        }
        if(q==NULL)                    //q已经是最后一个结点,p没有下一个奇数结点可以指向,赋值为NULL 
            p->next=NULL;
    }
    return head;
}

实验思路

1581432-20190517195851095-1005010942.png

实验截图

1581432-20190517195906389-1716280152.png

学习进度条

周/日期这周所花的时间代码行学习的知识点目前比较迷惑的问题
3/2-3/8两天30文件的处理文件指针的用法
3/9-3/15三天45数组的使用数组的下标
3/16-3/22两天1101.二维数组与矩阵 2.选择排序法不是很懂二分查找法
3/23-3/29两天78判断回文、一维字符数组的用法、使用字符串编程使用字符串编程
****3/30-4/5****三天1501、指针的含义,变量、地址、指针变量等间的关系;2、指针变量的初始化,运用指针做一些简单运算;3、指针与数组之间的关系在用指针处理字符串时,应怎样定义指针变量
4/6-4/12两天200学到了scanf的自定输入暂无
4/13-4/19两天180字符串和字符指针字符数组
4/20-4/26一天100定义结构,能够使用结构变量与结构数组进行熟练编程,掌握结构指针的操作命令行参数怎么使用
4/27-5/2十小时01、如何有效地记忆与学习2、如何提问能够使用递归函数进行编程
5/3-5/10一天22递归思想命令行参数怎么定义和使用
5/11-5/179小时200链表的基本使用方法怎么做出游戏

转载于:https://www.cnblogs.com/tanghongsheng/p/10883396.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值