第三次作业

Posted on 2018-04-22 22:22  杨雨鑫1  阅读(163)  评论(1编辑  收藏  举报

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

6-1 输出月份英文名
1.设计思路
(1)主要描述题目算法
第一步:将十二个月的名称分别赋值给一维数组指针,定义用于返回的数据类型。
第二步:遍历数组,满足若n在1-12范围则将month第n-1行的首元素的地址赋给一开始定义的数据。
第三步:返回变量的地址值。
2.实验代码

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

3.本题调试过程碰到问题及解决办法

6-2 查找星期
1.设计思路
(1)主要描述题目算法
第一步:将一个星期每天的英文分别赋值给一维数组指针,定义一个整型变量,赋初值为-1。
第二步:遍历一维数组,用strmcp函数比较数组中每行元素与输入的字符串是否相同,若相同,则令此时的行数等于一开始定义的整型变量,且跳出循环。
2.实验代码

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

3.本题调试过程碰到问题及解决办法

6-3 计算最长的字符串长度
1.设计思路
(1)主要描述题目算法
第一步:运用strlen计算出每行元素的长度。
第二步:for循环定义int型max,赋初值为0,当行元素的长度大于max,则将长度的值赋给max。
第三步:返回max。
2.实验代码

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

3.本题调试过程碰到问题及解决办法

6-4 指定位置输出字符串
1.设计思路
(1)主要描述题目算法
第一步:先遍历s字符数组,找到与ch1相同时s的下标并将下标值赋给一个整型变量,定义为j。
第二步:并将下标值赋给一个整型变量,定义为j。
第三步:循环字符数组s,在循环中分情况,若s[i] != ch2,则输出s[i],反之则输出s[i]加换行,且返回temp。
第四步:循环结束后,输出换行及返回temp。
2.实验代码

char *match( char *s, char ch1, char ch2 )
{
  int i=0,j=0,t=1,g=1,mark,flag;
  for(i=0;;i++)
  {	if(*(s+i)=='\0')
  {break;
  }
    if(*(s+i)==ch1&&t!=0)
    {t=0;
	mark=i;} 
	if(*(s+i)==ch2)	
	{g=0;
	flag=i;
	}
		}	
	if(t==1)
  {
  	printf("\n");
  	*s='\0';
  	return s;
  }
  if(t!=1&&g==0)
  {
  for(j=mark;;j++)
      {
      	if(*(s+j)=='\0')
      	{break;
		  }
        printf("%c",*(s+j));
        if(*(s+j)==ch2)
        {   printf("\n");
             return (s+mark); 
        }
      }
   } 
    if(t!=1&&g==1)
  {
    printf("%s\n",(s+mark));
    return (s+mark);
    
   } 
}

3.本题调试过程碰到问题及解决办法

6-1 奇数值结点链表
1.设计思路
(1)主要描述题目算法
第一步:在readlist函数中,将输入的值存储在链表里,且用while循环while循环内对p进行动态分配内存,
第二步:在getodd函数中,根据题目要求的条件对链表结点中date的值进行分类判断
第三步:最后返回要求链表的头结点。
2.实验代码

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

3.本题调试过程碰到问题及解决办法

6-2 学生成绩链表处理
1.设计思路
(1)主要描述题目算法
第一步:在createlist函数中将输入的学号,姓名和分数存储到链表中,while循环
第二步:在deletelist函数中遍历链表的结点
第三步:将判断后链表的头结点返回主函数。
2.实验代码

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

3.本题调试过程碰到问题及解决办法

6-3 链表拼接
1.设计思路
(1)主要描述题目算法
2.实验代码

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.本题调试过程碰到问题及解决办法

Copyright © 2024 杨雨鑫1
Powered by .NET 8.0 on Kubernetes