码迷,mamicode.com
首页 > 其他好文 > 详细

第三次作业

时间:2018-04-22 21:53:42      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:lists   let   creat   错误   方法   语句   turn   for循环   create   

作业要求一

6-1 输出月份英文名

1.设计思路
第一步:定义char,将12个月份存入数组中
第二步:if和else循环语句
2.流程图
技术分享图片

3.实验代码

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

4.问题及解决方法

6-2查找星期

1.设计思路
类似于6-1
2.实验代码

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

3.问题及错误

6-3 计算最长的字符串长度

1.设计思路
第一步:定义数组指针和整型变量
第二步:for循环判断字符串长度
2.实验代码

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

3.问题及错误

6-4 指定位置输出字符串

不会

提交列表

技术分享图片

作业要求二

编程题

不会

作业要求三

6-1 奇数值结点链表

1.设计思路
2.实验代码

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-2 学生成绩链表处理

2.实验代码

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

6-3 链表拼接

2.实验代码

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

第三次作业

标签:lists   let   creat   错误   方法   语句   turn   for循环   create   

原文地址:https://www.cnblogs.com/moxiaoshuai/p/8893220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!