第三次作业

一.代码:
6-1 输出月份英文名
1.截图:

2.设计思路:
第一步:定义包含月份名的数组
第二步:运用循环语句遍历数组找出对应月份,未找到对应月份时输出“wrong input”
第三步:输出结果
3.代码:

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

4错误:定义数组出现了错误,导致结果总是不正确。
将数组元素数量由12改为13.

6-2 查找星期
1.截图:

2.设计思路:
第一步:定义一个数组
第二步:运用循环遍历数组,查找到对应星期后返还对应循环变量

3.代码:

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

4错误:
编译过程中遇到很多小错误
用devC++错误提示解决;

6-3 计算最长的字符串长度
1.截图:

2.设计思路:
第一步:输入字符串,用循环进行输入,求出最长字符串长度然后输出
第二步:运用循环遍历字符串,if语句判断字符串长短,将值赋给最长长度,返回对应值;
3.代码:

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

4错误:
运用devC++已解决;
6-4 指定位置输出字符串
本题没有思路,没有完成

编程题
设计思路:
第一步:定义连个变量储存值,进行动态分配空间;
第二步:定义数组,用循环遍历数组赋值;
第三步:运用判断语句查找所要输出的值并输出;
代码:

#include<stdio.h>
int main()
{
int m=0,n=0,i=0,j=0,flag=0;
scanf("%d %d",&m,&n);
flag=m*n;
int *p = (int *)malloc((m*n) *sizeof(int));
int *q = (int *)malloc((m*n) *sizeof(int));
for(i=0;i<flag;i++) 
{
    p[i] = i+1;
}
for(i=0;i<flag;i++)
 {
    for(j = i+1;j<=flag;j++)
{
        if(p[i] !=1&&p[j] != 1) {

            if(p[j]%p[i] ==0) 
{
                p[j] = 1;
            }
   }
}
}
j=0;
for(i=0;i<flag;i++)
 {
    if(p[i] != 1)
 {
        printf(" %d",p[i]);
        j++;
    } 
    if(j == 5) 
{
        printf("\n");
        j=0;
    }
}
}

*链表这部分题不太会 是照着同学的代码敲下来的
6-1 奇数值结点链表
1.截图:

2.代码:

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

6-2 学生成绩链表处理
1.截图:

2.代码:

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

6-3 链表拼接
1.截图:

2.代码:

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

二.学习进度总结
1近期所学知识点与问题:
1)如何理解指针数组,它与指针、数组有何关系?为何可以用二级指针对指针数组进行操作?
指针数组就是将指针和数组相结合,其元素都为指针。二级指针与指针数组指向的地址相同。

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

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

3)用指针数组处理多个字符串有何优势?可以直接输入多个字符串给未初始化的指针数组吗?为什么?
可以节省空间。不可以,地址随机。

2.上传至git:
网址:https://coding.net/u/AssassinCreed/p/Devil-May-Cry/git/tree/master/?public=true
截图:

3.学习进度:
表格:

折线图:

posted @ 2018-04-22 23:39  DevilDante  阅读(129)  评论(1编辑  收藏  举报