The third assignment (second semester)

Homework requirement one (20 points)

Complete the following programming topics, each class will add 2-3 topics, and record the programming process in the blog, choose one topic for a PTA assignment to give design ideas, flowcharts, source code and error records, other topics Only design ideas, source code and error records can be given. In addition, the submission list of each PTA assignment will be posted on the blog, 5 points each time.

1) C Advanced Third PTA Assignment (1)


2) C Advanced Third PTA Assignment (2)


3) A programming question:

Homework Requirements II (65 points)

Problem 6-1 Output the English name of the month

1. Design ideas (6 points)
(1) Mainly describe the topic algorithm (2 points).
Step 1: Simply output the corresponding month in English according to the given number, so you must first judge whether the month is valid.
Step 2: Enter the switch statement for the valid month, and return the corresponding string according to the value.

(2) Flowchart (4 points)
2. Experiment code (2 points)

char *getmonth( int n )
{   
    
    if(n>12||n<1)
    {return 0;
    }
    else
    {
        switch (n)
        {case 1:return "January" ;
         case 2:return "February"  ;
         case 3:return "March";
         case 4:return "April" ;
         case 5:return "May" ;
         case 6:return "June" ;
         case 7:return "July";
         case 8:return "August";
         case 9:return "September";
         case 10:return "October";
         case 11:return "November";
         case 12:return "December" ;
        }
    }
 } 

3. Problems encountered in the debugging process of this question and solutions (4 points)
None

Topic 6-2 Finding the day of the week

1. Design ideas (6 points)
(1) Mainly describe the topic algorithm (2 points).
The first step: define a pointer array to store the English name of each week.
Step 2: Compare the strings sequentially from Sunday, and return the subscript if the same string is found.
Step 3: If the same string is not found, return -1 after the loop ends.

(2) Flowchart (4 points)

2. Experiment code (2 points)

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

3. Problems encountered in the debugging process of this question and solutions (4 points)
None

Problem 6-3 Calculating the longest string length

1. Design ideas (6 points)
(1) Mainly describe the topic algorithm (2 points).
Step 1: Compare the length of the first string as the value of max with the length of subsequent strings.
Step 2: If max is less than a string length, assign the string length to max.
Step 3: If the max value is greater than the length of the string, continue to compare with the next string.
Step 4: End the loop and return to max.

(2) Flowchart (4 points)
2. Experiment code (2 points)

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

3. Problems encountered in the debugging process of this question and solutions (4 points)
None

Problem 6-4 Specifying the position to output a string

1. Design ideas (6 points)
(1) Mainly describe the topic algorithm (2 points).
Step 1: First identify the 2 characters given by the title, mark whether they appear, and record their subscripts.
Step 2: Judge the statement according to the appearance of its characters.
Step 3: If the start character is not recognized, output a newline directly and return empty.
Step 4: If only the start character is recognized, output from the start character to the end, and return the output characters.
Step 5: If the start and end characters are recognized, output the characters including them, and return all characters from the start character to the end.

(2) Flowchart (4 points)
2. Experiment code (2 points)

char *match( char *s, char ch1, char ch2 )
{   int i,m=0;
    char *q;
    int mark=-1,flag=-1;
    int biaoji1=0,biaoji2=0;
   for(i=0;;i++)
   {if(*(s+i)=='\0')
   {break;
   }
   if(*(s+i)==ch1&&mark==-1)
   {mark=i;
   }
   if(*(s+i)==ch2)
   {if(biaoji2!=1||i<biaoji1)
   { flag=i;
   biaoji2=1; 
   biaoji1=flag;
   }
  }
   }
   if(mark==-1)
   {
    printf("\n");
    *s='\0';
   return s;
   }
   if(mark!=-1&&flag!=-1)
   {
    for(i=mark;i<=flag;i++)
    {
           if(i==flag)
           {printf("%c\n",*(s+i));
           }
      else
      {   printf("%c",*(s+i));
      } 
       }
    return (s+mark);
   }
   if(mark!=-1&&flag==-1)
   {
    printf("%s\n",(s+mark));
    return (s+mark);
   }
}

3. Problems encountered in the debugging process of this question and solutions (4 points)
I think the PTA code review standards are not perfect. For example, ch1 is equal to ch2, and ch2 appears twice before and after.

6-1 Odd-valued node linked list

1. Design ideas (6 points)
(1) Mainly describe the topic algorithm (2 points).
Step 1: Create two new linked lists respectively.
Step 2: By identifying the parity of each value in the given linked list, assign it to different linked lists.
Step 3: Return to the head node of the two linked lists.

(2) Flowchart (4 points)
2. Experiment code (2 points)

struct ListNode *readlist()
{  int i;
   struct ListNode *head=NULL,*t,*end;
   
   for(i=0;;i++)
  {t=(struct ListNode*)malloc(sizeof(struct ListNode)); 
  scanf("%d",&t->data);
    if(t->data==-1)
    {end->next=NULL;
    
    break;
    }
    if(head==NULL)
  {
  head=t;
 end=t;
  }
else
{end->next=t;
end=t;
}

    } 
    return head; 
    }
struct ListNode *getodd( struct ListNode **L )
{  struct ListNode *start1,*a,*b,*c,*start2;
    a=*L;
    start1=(struct ListNode*)malloc(sizeof(struct ListNode));
    start2=(struct ListNode*)malloc(sizeof(struct ListNode));
    start1->next=NULL;
    start2->next=NULL;
    b=start1;
    c=start2;
    for(;;)
    {
    if(a==NULL)
    {
    break;
    }
    if(a->data%2!=0)
    {
    b->next=a;
    b=a;
    }
    else
    {
    c->next=a;
    c=a;
    }
    a=a->next;
    
    }
    c->next=NULL;
     b->next=NULL;
    *L=start2->next;
    return start1->next;
}

3. Problems encountered in the debugging process of this question and solutions (4 points)
Problem: My first idea is to just create a new linked list and strip out the even numbers. And let the odd numbers left as a linked list. Encountered a segfault problem.
Solution: Create 2 new linked lists.

6-2 Processing of Student Score List

1. Design ideas (6 points)
(1) Mainly describe the topic algorithm (2 points).
Step 1: Create a linked list and assign values ​​through a loop. When the sequence number is 0, make the last node of the linked list point to null.
Step 2: Enter the condition value.
Step 3: Traverse the linked list and delete each node that is lower than the condition value.
Step 4: Return to the head node of the linked list after the delete operation.

(2) Flowchart (4 points)
2. Experiment code (2 points)

struct stud_node *createlist()
{
    struct stud_node *head=NULL,*t,*end;
    head=(struct stud_node*)malloc(sizeof(struct stud_node));
    end=head;
    for(;;)
    { t=(struct stud_node*)malloc(sizeof(struct stud_node));
        scanf("%d",&t->num);    
        if(t->num==0)
        {
        break;
        }
        scanf("%s",t->name);
        scanf("%d",&t->score);
    
        if(head==NULL)
        {head=t;
        end=t;
        }
        else
        {end->next=t;
        end=t;
        }   
    }
    end->next=NULL;
return head;
 } 
 struct stud_node *deletelist( struct stud_node *head, int min_score )
 {
    struct stud_node *a=head,*b=head;
    for(;;a=a->next)
    {   if(a==NULL)
        {break;
         }
        if(a->score<min_score)
        {if(a==head)
        {head=a->next;
        free(a);
        }
         else
         {
         b->next=a->next;
          free(a);
         }
         }
         else
         {b=a;
         }
        
     }
     return head;
 }

3. Problems encountered in the debugging process of this question and solutions (4 points)
A certain knowledge point identifies a segmentation fault and solves it by manually allocating memory.

6-3 Linked list splicing

1. Design ideas (6 points)
(1) Mainly describe the topic algorithm (2 points).
Step 1: Assign each value of linked list 1 and 2 to an array respectively.
Step 2: Get an ordered array by sorting the array.
Step 3: Assign each element of the array back to a new linked list.
Step 4: Return to the head node of the linked list.

(2) Flowchart (4 points)
2. Experiment code (2 points)

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

 

3. Problems encountered in the debugging process of this question and solutions (4 points)
Problem: My first idea was not to use an array, but to compare the values ​​of the second linked list with linked list 1 and perform an insertion operation. The modified 3 or 4 versions cannot get all correct. I have encountered wrong answers, runtime timeouts and segfaults. The reason is unknown. From debugging it looks like I'm not thinking well about insert operations for different situations.
Solution: Choose a method with strong versatility, clear thinking, simple and more familiar. That is, sorting by turning into an array.


Requirement 3. Learning summary and progress (15 points)

1. Summarize the knowledge points learned in the past two weeks and answer the following questions? (Express your understanding in your own words, no points for copying and pasting online) (5 points)

 (1)如何理解指针数组,它与指针、数组有何关系?为何可以用二级指针对指针数组进行操作?

 (2)将C高级第三次PTA作业(1)任何一个题目改为使用二级指针对指针数组进行操作。

 (3)用指针数组处理多个字符串有何优势?可以直接输入多个字符串给未初始化的指针数组吗?为什么?

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.
Address: https://git.coding.net/Donahue_Xu/The-Third-Homework2.git
Screenshot:

3. Comment on 3 classmates' homework this week
Liu Weiqi:
Zhao Yinsheng:

4. Please use tables and line charts to present the number of lines of code and time spent this week (4/9 8:00~4/23 8:00), the number of blog words and the time spent (3 points)
:

line chart:

Guess you like

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