• 2018第三次作业


    作业要求一 (20分)
    1)C高级第三次PTA作业(1)

    6-1 输出月份英文名

    1 设计思路(6分)
    (1)主要描述题目算法
    第一步:设出来一个二维数组m来储存英文月份。
    第二步:用来if语句来判断输入的数字是否在1~12之间,如果是则找到对应的英文月份,如果不是则让m=NULL.
    第三步:最后返回m。

    2.实验代码(2分)

    #include <stdio.h>
    char *getmonth( int n );
    int main()
    {
        int n;
        char *s;
        scanf("%d", &n);
        s = getmonth(n);
        if ( s==NULL ) printf("wrong input!
    ");
        else printf("%s
    ", s);
    
        return 0;
    }
    char p[12][100]={"January","February","March","April","May","June","July","August","September","October","November","December" };
    char *getmonth( int n )
    {
    	char *m;
    	if(n<=0||n>12)
    	{
    	  	m=NULL;
    	  return m;
        }
    	m=p[n-1];
    	return m;
    	
    }
    

    3.本题调试过程碰到问题及解决办法(12分)
    错误信息1:

    错误原因:输入1的时候会出现二月的英文,数组中的一月在的位置是p[0]。
    改正方法:让m=p[n-1]让m等于它的下一位。

    6-2 查找星期

    1 设计思路(6分)
    (1)主要描述题目算法
    第一步:设出来一个二维数组m来储存英文星期。
    第二步:用来strcmp语句for循环来判断输入的星期数是否与设出来的相等,如果是则找让m=i,如果不是则让m=-1.
    第三步:最后返回m。

    2.实验代码(2分)

    #include <stdio.h>
    #include <string.h>
    
    #define MAXS 80
    
    int getindex( char *s );
    
    int main()
    {
        int n;
        char s[MAXS];
    
        scanf("%s", s);
        n = getindex(s);
        if ( n==-1 ) printf("wrong input!
    ");
        else printf("%d
    ", n);
    
        return 0;
    }
    char p[7][20]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    int getindex( char *s )
    {
    	int m,i;
       for(i=0;i<=6;i++)
       {
       if(strcmp(s,p[i])==0) 
       break;
    	   }
       
       if(i==7)
       {
       	m=-1;
       	return m;
       }
       else
       {   
       m=i;
       return m;
    }
    }
    

    3.本题调试过程碰到问题及解决办法(12分)
    错误信息1:

    错误原因:一开始没有想到用strcmp去解决,绕了很大的弯子。
    改正方法:用strcmp直接比较两个数是否相等。

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

    1 设计思路(6分)
    (1)主要描述题目算法
    第一步:用循环的方法和strlen来将每一个颜色的字数数出,赋给max。
    第二步:再用if来比较,把最大的赋给m。
    第三步:返回m。

    2.实验代码(2分)

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAXN 10
    #define MAXS 20
    
    int max_len( char *s[], int n );
    
    int main()
    {
        int i, n;
        char *string[MAXN] = {NULL};
    
        scanf("%d", &n);
        for(i = 0; i < n; i++) {
            string[i] = (char *)malloc(sizeof(char)*MAXS);
            scanf("%s", string[i]);
        }
        printf("%d
    ", max_len(string, n));
    
        return 0;
    }
    int max_len( char *s[], int n )
    {
    	int max,i,m=0;
    	for(i=0;i<n;i++)
    	{
    		max=strlen(s[i]);
    		if(max>m)
    		{
    			m=max;
    		}
    	}
    	return m;
     }  
    

    3.本题调试过程碰到问题及解决办法(12分)
    错误信息1:无。

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

    1 设计思路(6分)
    (1)主要描述题目算法
    第一步:利用循环语句来找出第一个字母的位置,并且开始复制。
    第二步:在找到最后一个字母并停止,输出复制的字母。
    第三步:返回把首字母的地址,输出从它开始,剩下所以的字母。
    2.实验代码(2分)

    #include <stdio.h>
    
    #define MAXS 10
    
    char *match( char *s, char ch1, char ch2 );
    
    int main()
    {
        char str[MAXS], ch_start, ch_end, *p;
    
        scanf("%s
    ", str);
        scanf("%c %c", &ch_start, &ch_end);
        p = match(str, ch_start, ch_end);
        printf("%s
    ", p);
    
        return 0;
    }
    char *match( char *s, char ch1, char ch2 )
    {
        int m=0;
    
        while(*s!=ch1&&*s!=0)
        s++;
        while(*s!=ch2&&*s!=0)
        {
            printf("%c",*s);
            s++;
            m++;
        }
        if(*s==ch2)
        printf("%c
    ",*s);
        else
        printf("
    ");
        s=s-m;
        return s; 
    }
    
    

    3.本题调试过程碰到问题及解决办法(12分)
    错误信息1:无。

    6-1 奇数值结点链表

    2.实验代码(2分)

    #include <stdio.h>
    #include <stdlib.h>
    
    struct ListNode {
        int data;
        struct ListNode *next;
    };
    
    struct ListNode *readlist();
    struct ListNode *getodd( struct ListNode **L );
    void printlist( struct ListNode *L )
    {
         struct ListNode *p = L;
         while (p) {
               printf("%d ", p->data);
               p = p->next;
         }
         printf("
    ");
    }
    
    int main()
    {
        struct ListNode *L, *Odd;
        L = readlist();
        Odd = getodd(&L);
        printlist(Odd);
        printlist(L);
    
        return 0;
    }
    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.实验代码(2分)

    #include <stdio.h>
    #include <stdlib.h>
    
    struct stud_node {
         int    num;
         char   name[20];
         int    score;
         struct stud_node *next;
    };
    
    struct stud_node *createlist();
    struct stud_node *deletelist( struct stud_node *head, int min_score );
    
    int main()
    {
        int min_score;
        struct stud_node *p, *head = NULL;
    
        head = createlist();
        scanf("%d", &min_score);
        head = deletelist(head, min_score);
        for ( p = head; p != NULL; p = p->next )
            printf("%d %s %d
    ", p->num, p->name, p->score);
    
        return 0;
    }
    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.实验代码(2分)

    #include <stdio.h>
    #include <stdlib.h>
    
    struct ListNode {
        int data;
        struct ListNode *next;
    };
    
    struct ListNode *createlist(); /*裁判实现,细节不表*/
    struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2);
    void printlist( struct ListNode *head )
    {
         struct ListNode *p = head;
         while (p) {
               printf("%d ", p->data);
               p = p->next;
         }
         printf("
    ");
    }
    
    int main()
    {
        struct ListNode  *list1, *list2;
    
        list1 = createlist();
        list2 = createlist();
        list1 = mergelists(list1, list2);
        printlist(list1);
    	
        return 0;
    }
    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;
    }
    
    

    这周遇到一些私人的事情,完成的博客很糟糕,希望老师看了不要生气,因为我的状态无法完成这次作业,

  • 相关阅读:
    Express 框架中 使用ejs
    Nodejs操作MongoDB数据库
    MongoDB基础操作
    node中的包、npm和模块
    background
    animation
    transition
    transform
    【SpringCloud】各种组件的更新情况
    【SpringCloud】版本选择
  • 原文地址:https://www.cnblogs.com/yaoshunyux/p/8905879.html
Copyright © 2020-2023  润新知