第三次作业

本文提供了多个C语言编程实例,包括字符串操作、链表管理和学生信息处理等,通过具体实验代码展示了如何实现月份英文名输出、查找星期、计算最长字符串长度等功能,并介绍了链表的基本操作。

作业要求一

作业要求二

题目1 输出月份英文名

1. 设计思路

(1). 算法

第一步:定义月份的字符串组

第二步:判断月份

(2). 流程图

2. 实验代码

char *getmonth( int n )
{
    char *s[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};
    if((n<=12)&&(n>=1))
    {
        return s[n-1];
    }
    else return NULL;
} 

3. 本题调试过程碰到问题及解决办法

题目2 查找星期

1. 设计思路

(1). 算法

第一步:定义星期的字符串组

第二步:判断给定的字符串是星期几

(2). 流程图

2. 实验代码

int getindex( char *s )
{
    int i;
    char *str[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    for(i=0;i<7;i++)
    {
        if(strcmp(s,str[i])==0)
        {
            return i;
        }
    }
    return -1;
}

3. 本题调试过程碰到问题及解决办法

题目3 计算最长的字符串长度

1. 设计思路

(1). 算法

第一步:统计第一个字符串的长度给max

第二步:用找最大值的方法找出最长字符串,返回max

(2). 流程图

2. 实验代码

int max_len( char *s[], int n )
{
    int i;
    int max=strlen(s[0]);
    for(i=0;i<n;i++)
    {
        if(max<strlen(s[i]))
        {
            max=strlen(s[i]);
        }
    }
    return max;
}

3. 本题调试过程碰到问题及解决办法

题目4 指定位置输出字符串

1. 设计思路

(1). 算法

第一步:找到给定的第一个字符

第二步:循环输出直到遇到第二个字符

(2). 流程图

2. 实验代码

char *match( char *s, char ch1, char ch2 )
{
    int i;
    char *ch=NULL;
    for(i=0;s[i]!='\0';i++)
    {
        if(s[i]==ch1)
        {
            ch=s+i;
            for(;(s[i]!=ch2)&&(s[i]!='\0');i++)
            {
                printf("%c",s[i]);
            }
            if(s[i]!='\0') printf("%c",ch2);
            printf("\n");
            return ch;
        }
    }
    ch=&s[i];
    printf("\n");
    return ch;
}

3. 本题调试过程碰到问题及解决办法

在返回ch1的地址时出现了很多错误,没有加ch=&s[i]

题目5

实验代码

struct ListNode *readlist()
{
    int data;
    struct ListNode *head=NULL;
    struct ListNode *p;
    while(scanf("%d",&data)&&data!=-1)
    {
        struct ListNode *q=(struct ListNode*)malloc(sizeof(struct ListNode));
        if(q!=NULL)
        {
            q->data=data;
            q->next=NULL;
        }
        else exit(1);
        if(head!=NULL)
        {
             p->next=q;
        }
        else head=q;
        p=q;
    }
    return head;
}
struct ListNode *getodd( struct ListNode **L )
{
    struct ListNode *head0=NULL,*head1=NULL,*p0,*p1;
    while((*L)!=NULL)
    {
        int data=(*L)->data;
        struct ListNode *q=(struct ListNode*)malloc(sizeof(struct ListNode));
        if(data%2)
        {
            if(q!=NULL)
            {
                q->data=data;
                q->next=NULL;
            }
            else exit(1);
            if(head1!=NULL)
            {
                p1->next=q;
            }
            else head1=q;
            p1=q;
        }
        else
        {
            if(q!=NULL)
            {
                q->data=data;
                q->next=NULL;
            }
            else exit(1);
            if(head0!=NULL)
            {
             p0->next=q;
            }
            else head0=q;
            p0=q;
        }
        *L=(*L)->next;
    }
    *L=head0;
    return head1;
}

题目6

实验代码

struct stud_node *createlist()
{
    struct stud_node *head, *tail, *q;
    head = tail = NULL;
    int num;
    scanf ("%d", &num);
    while (num != 0)
    {
        q = (struct stud_node *)malloc (sizeof (struct stud_node));
        scanf ("%s %d", q->name, &q->score);
        q->num = num;
        q->next = NULL;
        if (head == NULL)
            head = q;
        else
            tail->next = q;
        tail = q;
        scanf ("%d", &num);
    }
    return head;
}
struct stud_node *deletelist( struct stud_node *head, int min_score )
{
    struct stud_node *ptr1, *ptr2;
    while (head != NULL && head->score < min_score)
    {
        ptr2 = head;
        head = head->next;
        free(ptr2);
    }
    if (head == NULL)
        return NULL;
    ptr1 = head;
    ptr2 = head->next;
    while (ptr2 != NULL)
    {
        if (ptr2->score < min_score) {
            ptr1->next = ptr2->next;
            free(ptr2);
        }
        else
            ptr1 = ptr2;
        ptr2 = ptr1->next;
    }
    return head;
}

题目7

实验代码

struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2)
{
    int num = 0;
    int temp[100];
    struct ListNode  *p = list1;
    while(p)
    {
        temp[num] = p->data;
        num++;
        p = p->next;
    }
    p = list2;
    while(p)
    {
        temp[num] = p->data;
        num++;
        p = p->next;
    }
    int i,j;
    for(i = 1; i < num; i++)
        for(j = 0; j < num-i; j++)
        {
            if(temp[j] > temp[j+1])
            {
                int t;
                t = temp[j];
                temp[j] = temp[j+1];
                temp[j+1] = t;
            }
        }
      struct ListNode  *head, *tail, *q;
      head = tail = NULL;
      for(i = 0; i < num; i++)
      {
          q = (struct ListNode  *)malloc(sizeof(struct ListNode));
          q->data = temp[i];
          q->next = NULL;
          if (head == NULL)
            head = q;
          else
            tail->next = q;
          tail = q;
      }
      return head;
}

第二次作业的题目参考了网上很多,也有点似懂非懂,会抽时间好好研究一下

作业要求三

1. 知识的总结

讲了些什么东西啊,二级指针我没太听懂,链表那里我没怎么听,不知道怎么总结。

2. 代码管理

Git地址

3. 点评

江北宋天霸

袁中

李春阳

4.图表

转载于:https://www.cnblogs.com/wwb986187/p/8906109.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值