1. 程式人生 > >2018上C語言程序設計(高級)作業-第3次作業

2018上C語言程序設計(高級)作業-第3次作業

鏈表 can 指針 turn 算法 rcp HA cto 月份

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

錯誤

總結

學習了二級指針,但不太懂,很多流程圖還不會弄。

2018上C語言程序設計(高級)作業-第3次作業