2018上C语言程序设计(高级)作业-第3次作业

6-1 输出月份英文名

设计思路

1、算法

第一步:看函数,看函数声明
第二步:理解分析

2、流程图如下

代码如下

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

错误

6-2 查找星期

设计思路:

1、算法

2、流程图

代码

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

错误

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

设计思路

算法

2、流程图

实验代码

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

错误

学生成绩链表处理

设计思路

1、算法

2、流程图

实验代码

#include<string.h>
struct stud_node *createlist()
{
    struct stud_node *p, *ptr, *head=NULL;
    int    num;
    char   name[20];
    int    score;
    scanf("%d",&num);
    while (num != 0)
    {
        scanf("%s %d",name,&score);
        p = (struct stud_node *)malloc(sizeof(struct stud_node));
        p->num = num;
        strcpy(p->name, name);
        p->score = score;
        p->next = NULL;
        if (head == NULL)
        {
            head = p;
        }
        else
        {
            ptr->next = p;
        }
        ptr = p;
        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;
}

错误

奇数值结点链表

设计思路

1、算法

2、流程图

实验代码

struct ListNode *readlist()
{
  struct ListNode *head=NULL,*p=NULL,*tail=NULL;
  int data;
  scanf("%d",&data);
  while(data!=-1)
  {
    p=(struct ListNode *)malloc(sizeof(struct ListNode));
    p->data=data;
    p->next=NULL;
    if(head==NULL)
    {
      head=p;
    }else
    {
      tail->next=p;
    }
    tail=p;
    scanf("%d",&data);
  }
  return head;
}
struct ListNode *getodd( struct ListNode **L )
{ 
   struct ListNode *p=*L,*a,*b,*head1,*head2,*p1=NULL,*p2=NULL;
    head1=(struct ListNode*)malloc(sizeof(struct ListNode));
    head2=(struct ListNode*)malloc(sizeof(struct ListNode));
    head1->next=NULL;
    head2->next=NULL;
    a=head1;
    b=head2;
    for(;p!=NULL;p=p->next)
    {
        if(p->data%2!=0)
        {
          if(p1==NULL)
          p1=p;
          else
            a->next=p;
            a=p;
        }
        else
        {
          if(p2==NULL)
          p2=p;
          else
            b->next=p;
            b=p;
        }
    }
    a->next=NULL;
    b->next=NULL;
    *L=p2;
    return p1;
} 

错误

总结

学习了二级指针,但不太懂,很多流程图还不会弄。

posted @ 2018-04-22 22:17  Kangf  阅读(328)  评论(1编辑  收藏  举报