• 第三次作业


    要求二

    6-1 输出月份英文名

    本题要求实现函数,可以返回一个给定月份的英文名称。

    函数接口定义:

    char *getmonth( int n );
    函数getmonth应返回存储了n对应的月份英文名称的字符串头指针。如果传入的参数n不是一个代表月份的数字,则返回空指针NULL。

    裁判测试程序样例:

    #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;
    }
    
    /* 你的代码将被嵌在这里 */
    

    输入样例1:

    5

    输出样例1:

    May

    输入样例2:

    15

    输出样例2:

    wrong input!

    设计思路

    1.定义指针数组*month[12]来存放月份的对应英文
    2.通过形参n的值来判断是否为正确月份,若为正确月份,则返回对应地址,否则返回NULL
    

    实验代码

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

    错误信息:虽然能通过编译并且能通过PTA,但实际上在内存分配上是由一定问题的,在子函数运行结束后,该指针数组的空间会被释放
    错误改正:原题中无改正,但实际应申请动态空间

    6-2 查找星期

    本题要求实现函数,可以根据下表查找到星期,返回对应的序号。
    序号 星期
    0 Sunday
    1 Monday
    2 Tuesday
    3 Wednesday
    4 Thursday
    5 Friday
    6 Saturday

    函数接口定义:

    int getindex( char *s );
    函数getindex应返回字符串s序号。如果传入的参数s不是一个代表星期的字符串,则返回-1。
    裁判测试程序样例:

    #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;
    }
    
    /* 你的代码将被嵌在这里 */
    

    输入样例1:

    Tuesday

    输出样例1:

    2

    输入样例2:

    today

    输出样例2:

    wrong input!

    设计思路

    1.建立字符串指针数组day[7]储存对应星期英文
    2.遍历day,若字符串匹配则返回对应下标
    3.若未找到相匹配字符串则返回-1
    

    实验代码

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

    错误信息:字符串匹配过程中出错
    错误解决:使用strcmp函数来实现字符串的匹配

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

    本题要求实现一个函数,用于计算有n个元素的指针数组s中最长的字符串的长度。
    函数接口定义:
    int max_len( char *s[], int n );
    其中n个字符串存储在s[]中,函数max_len应返回其中最长字符串的长度。

    裁判测试程序样例:

    #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;
    }
    
    /* 你的代码将被嵌在这里 */
    

    输入样例:

    4
    blue
    yellow
    red
    green
    

    输出样例:

    6

    设计思路

    1.定义一个整型变量length来存放当前字符串的长度
    2.遍历该指针数组,若有长度大于length的字符串,则将当前长度赋值给length
    3.返回length
    

    实验代码

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

    错误信息:暂无错误

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

    本题要求实现一个函数,对给定的一个字符串和两个字符,打印出给定字符串中从与第一个字符匹配的位置开始到与第二个字符匹配的位置之间的所有字符。

    函数接口定义:

    char *match( char *s, char ch1, char ch2 );
    函数match应打印s中从ch1ch2之间的所有字符,并且返回ch1的地址。

    裁判测试程序样例:

    #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;
    }
    
    /* 你的代码将被嵌在这里 */
    

    输入样例1:

    program
    r g
    

    输出样例1:

    rog
    rogram
    

    输入样例2:

    program
    z o
    

    输出样例2:

    (空行)
    (空行)
    

    输入样例3:

    program
    g z
    

    输出样例3:

    gram
    gram
    

    设计思路

    1.遍历该数组,尝试查找ch1,若找到ch1,则执行2.,否则返回该字符串最后的''
    2.记录ch1的位置,遍历剩下元素,尝试查找ch2,找到ch2之前,输出遍历过的每一个字符;当找到ch2时,按格式输出,并返回ch1的位置
    3.若没找到ch2,则按格式输出,并返回ch1的位置
    4.
    

    实验代码

    char *match( char *s, char ch1, char ch2 ){  
      
        int i=0,j=0,k=0,x=0;  
        char *p=NULL;  
        x = strlen(s);  
        for(i=0;i<x;i++)
        {  
            if(s[i]==ch1)
            {  
                p=&s[i];  
                for(j=i;j<x;j++)
                {  
                    if(s[j]!=ch2)
                    {  
                        printf("%c", s[j]);  
                    }  
                    if(s[j]==ch2)
                    {  
                        printf("%c
    ", s[j]);  
                        return p;  
                    }     
                }  
                printf("
    ");  
                return p;  
            }  
        }  
        p=&s[x];
        printf("
    ");  
        return p;  
    } 
    

    错误信息1:输出出现遗漏第一个相匹配的字符
    错误改正:指针的初值赋值为NULL
    错误信息2:答案不符
    错误分析:遍历数组都未找到ch1时,返回主函数的指针为空;但主函数中的输出是以字符串的形式输出,即是以''为结尾的数组;当返回值为空时,主函数无法正常输出字符串
    错误改正:最后将该指针指向的位置的值为''即可

    6-1 奇数值结点链表

    本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中奇数值的结点重新组成一个新的链表。链表结点定义如下:

    struct ListNode {
        int data;
        ListNode *next;
    };
    

    函数接口定义:

    struct ListNode *readlist();
    struct ListNode *getodd( struct ListNode **L );
    

    函数readlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。
    函数getodd将单链表L中奇数值的结点分离出来,重新组成一个新的链表。返回指向新链表头结点的指针,同时将L中存储的地址改为删除了奇数值结点后的链表的头结点地址(所以要传入L的指针)。

    裁判测试程序样例:

    #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;
    }
    
    /* 你的代码将被嵌在这里 */
    

    输入样例:

    1 2 2 3 4 5 6 7 -1
    

    输出样例:

    1 3 5 7 
    2 2 4 6
    

    设计思路

    1.创建链表
    2.定义结构体指针`*oddhead``*oddtail``*newhead``*newtail``*p`,令p=*L
    3.遍历链表,若为第一个奇数,则赋值给oddhead,并向下建立链表;同理建立偶数链表newhead
    4.将两链表的结尾处指向NULL
    5.新的偶数链表头newhead赋值给*L,返回oddhead
    

    实验代码

    struct ListNode *readlist()
    {
    	struct ListNode *p=NULL,*head=NULL,*tail=NULL;
    	int date;
    	scanf("%d",&date);
    	while(date!=-1)
    	{	
    		p=(struct ListNode *)malloc(sizeof(struct ListNode));
    		p->data=date,p->next=NULL;
    		if(head==NULL)
    		{
    			head=p,tail=p;
    		}
    		else
    		{
    			tail->next=p;
    			tail=p;
    		}
    		scanf("%d",&date);
    	}
    	return head;
    }
    struct ListNode *getodd( struct ListNode **L )
    {
    	struct ListNode *oddhead=NULL,*oddtail=NULL,*newhead=NULL,*newtail=NULL,*p=*L;
    	while(p!=NULL)
    	{
    		if((p->data)%2!=0)
    		{
    			if(oddhead==NULL)
    			{
    				oddhead=p;
    				oddtail=p;
    			}
    			else
    			{
    				oddtail->next=p;
    				oddtail=p;
    			}
    		}
    		if((p->data)%2==0)
    		{
    			if(newhead==NULL)
    			{
    				newhead=p;
    				newtail=p;
    			}
    			else
    			{
    				newtail->next=p;
    				newtail=p;
    			}
    		}
    		p=p->next;
    	}
    	if(oddtail->next!=NULL)
    	oddtail->next=NULL;
    	if(newtail->next!=NULL)
    	newtail->next=NULL;
    	*L=newhead;
    	return oddhead;
    }
    

    错误信息:位置段错误
    错误改正:暂无改正

    总结分析

    数组指针的使用方便了关于字符串的调用和传递;链表类知识点尚未能完全理解

  • 相关阅读:
    Luogu P6623 [省选联考 2020 A 卷] 树|Trie
    Luogu P4683【IOI2008】Type Printer 打印机|trie
    Luogu P5658 括号树|搜索+递推
    Luogu P4514 上帝造题的七分钟|二维树状数组
    Luogu P1314 【NOIP2011】聪明的质检员|前缀和+二分
    Html5表单元素
    HTML5视频音频
    HTML5语义化标签
    斗地主案例
    Collection集合
  • 原文地址:https://www.cnblogs.com/cty-1/p/8926064.html
Copyright © 2020-2023  润新知