3rd homework

homework requirement one

1) C Advanced Third PTA Assignment (1)




2) C Advanced Third PTA Assignment (2)

homework requirement two

1) C Advanced Third PTA Assignment (1)

6-1 Output the English name of the month

1. Design ideas

2. Experimental code

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

3. Problems encountered in the debugging process of this question and solutions

6-2 Find the day of the week

1. Design ideas

2. Experimental code

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

6-3 Calculate the longest string length

1. Design ideas

2. Experimental code

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

6-4 Specify the position to output the character string

1. Design ideas

2. Experimental code

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

3. Problems encountered in the debugging process of this question and solutions

2) A programming question

1. Design ideas

2. Experimental code

3) C Advanced Third PTA Assignment (2)

6-1 Odd-valued node linked list

1. Design ideas

2. Experimental code

'struct ListNode *readlist()
    {
        int data;
        struct ListNode *head=NULL,*p=NULL,*tail=NULL;
        scanf("%d",&data);
        while(data != -1){
            p = (struct ListNode *)malloc(sizeof(struct ListNode));
            p->data = data;
            p->next = NULL;
            if(head == NULL){
                head = p;
                tail = p;
            }else{
                tail->next = p;
                tail = p;
            }
            scanf("%d",&data);
    }
        return head; 

    } 

struct ListNode *getodd( struct ListNode **L )
{
     struct ListNode *p = *L,*m=NULL,*n=NULL,*head1=NULL,*head2=NULL;
     head1=(struct ListNode*)malloc(sizeof(struct ListNode));
     head2=(struct ListNode*)malloc(sizeof(struct ListNode));
    head1->next=NULL;
    head2->next=NULL;
    m=head1;
    n=head2;
     while (p) {
           if((p->data)%2 == 1){
                n->next=p;
                n=p;
             }else{
                m->next=p;
                m=p;
                 }
               p = p->next;
     }
     m->next=NULL;
    n->next=NULL;
    *L = head1->next;   
    return head2->next;
}'

3. Problems encountered in the debugging process of this question and solutions

6-2 Processing of Student Score List

1. Design ideas

2. Experimental code

'struct stud_node *createlist()
{
    struct stud_node *tail=NULL,*head=NULL,*p=NULL;
    int num=0,score=0;
    char name[20];
    scanf("%d",&num);
    while(num!=0)
    {   
        p=(struct stud_node*)malloc(sizeof(struct stud_node));
        p->num=num; 
        scanf("%s %d",p->name,&p->score);
        if(head==NULL)
        {
            head=p;
        }else
        {
            tail->next=p;
        }
        tail=p;
        scanf("%d",&num);
        p->next=NULL;
    }
    return head;
}
struct stud_node *deletelist( struct stud_node *head, int min_score )
{
    struct stud_node *ptr1=NULL,*ptr2=NULL;
    for(;head!=NULL;head=head->next)
    {
        if(head->score>=min_score)
        {
            if(ptr1==NULL)
            {
                    ptr1=head;
            }else
            {
                ptr2->next=head;
            }
            ptr2=head;
        }
    }
    if(ptr1==NULL)
    {
        return NULL;
    }else
    {
        ptr2->next=NULL;
    }
    return ptr1;
}'

3. Problems encountered in the debugging process of this question and solutions

6-3 Linked list splicing

1. Design ideas

2. Experimental code

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

3. Problems encountered in the debugging process of this question and solutions

Requirement 3. Learning summary and progress

1. Summarize the knowledge points learned in the two weeks and answer the following questions

(1) How to understand an array of pointers, and how does it relate to pointers and arrays? Why can you operate on arrays of pointers with secondary pointers?

(2) Change any topic of the third PTA assignment (1) of C Advanced to use the second-level pointer to operate on the pointer array.

(3) What are the advantages of handling multiple strings with an array of pointers? Is it possible to directly input multiple strings to an uninitialized array of pointers? Why?

2. Submit the source code of the PTA job to the hosting platform using git, and ask for a screenshot of the successful upload and your git address.

3. Comment on 3 classmates' homework this week

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324694958&siteId=291194637